Skip to content

Commit 176b75e

Browse files
committed
Add command to show tablespaces attached to a hypertable
Users can now call `show_tablespaces()` to list the tablespaces attached to a particular hypertable.
1 parent 6e92383 commit 176b75e

File tree

7 files changed

+111
-3
lines changed

7 files changed

+111
-3
lines changed

sql/ddl_api.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,9 @@ CREATE OR REPLACE FUNCTION detach_tablespaces(hypertable REGCLASS)
317317
$BODY$
318318
SELECT * FROM _timescaledb_internal.detach_tablespaces(hypertable);
319319
$BODY$;
320+
321+
CREATE OR REPLACE FUNCTION show_tablespaces(hypertable REGCLASS)
322+
RETURNS SETOF NAME LANGUAGE SQL AS
323+
$BODY$
324+
SELECT * FROM _timescaledb_internal.show_tablespaces(hypertable);
325+
$BODY$;

sql/ddl_internal.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@ AS '$libdir/timescaledb', 'tablespace_detach' LANGUAGE C VOLATILE;
627627
CREATE OR REPLACE FUNCTION _timescaledb_internal.detach_tablespaces(hypertable REGCLASS) RETURNS INTEGER
628628
AS '$libdir/timescaledb', 'tablespace_detach_all_from_hypertable' LANGUAGE C VOLATILE;
629629

630+
CREATE OR REPLACE FUNCTION _timescaledb_internal.show_tablespaces(hypertable REGCLASS) RETURNS SETOF NAME
631+
AS '$libdir/timescaledb', 'tablespace_show' LANGUAGE C VOLATILE STRICT;
632+
630633
--documentation of these function located in chunk_index.h
631634
CREATE OR REPLACE FUNCTION _timescaledb_internal.chunk_index_clone(chunk_index_oid OID) RETURNS OID
632635
AS '$libdir/timescaledb', 'chunk_index_clone' LANGUAGE C VOLATILE STRICT;

src/tablespace.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <commands/tablespace.h>
99
#include <access/xact.h>
1010
#include <miscadmin.h>
11+
#include <funcapi.h>
1112

1213
#include "hypertable_cache.h"
1314
#include "errors.h"
@@ -469,3 +470,51 @@ tablespace_detach_all_from_hypertable(PG_FUNCTION_ARGS)
469470

470471
PG_RETURN_INT32(tablespace_detach_all(PG_GETARG_OID(0)));
471472
}
473+
474+
TS_FUNCTION_INFO_V1(tablespace_show);
475+
476+
Datum
477+
tablespace_show(PG_FUNCTION_ARGS)
478+
{
479+
FuncCallContext *funcctx;
480+
Oid hypertable_oid = PG_ARGISNULL(0) ? InvalidOid : PG_GETARG_OID(0);
481+
Cache *hcache;
482+
Hypertable *ht;
483+
484+
if (SRF_IS_FIRSTCALL())
485+
{
486+
MemoryContext oldcontext;
487+
488+
if (!OidIsValid(hypertable_oid))
489+
elog(ERROR, "Invalid argument");
490+
491+
funcctx = SRF_FIRSTCALL_INIT();
492+
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
493+
funcctx->user_fctx = hypertable_cache_pin();
494+
MemoryContextSwitchTo(oldcontext);
495+
}
496+
497+
funcctx = SRF_PERCALL_SETUP();
498+
hcache = funcctx->user_fctx;
499+
ht = hypertable_cache_get_entry(hcache, hypertable_oid);
500+
501+
if (NULL == ht)
502+
ereport(ERROR,
503+
(errcode(ERRCODE_IO_HYPERTABLE_NOT_EXIST),
504+
errmsg("Table \"%s\" is not a hypertable",
505+
get_rel_name(hypertable_oid))));
506+
507+
if (NULL != ht->tablespaces && funcctx->call_cntr < (uint64) ht->tablespaces->num_tablespaces)
508+
{
509+
Tablespaces *tspcs = ht->tablespaces;
510+
Oid tablespace_oid = tspcs->tablespaces[funcctx->call_cntr].tablespace_oid;
511+
const char *tablespace_name = get_tablespace_name(tablespace_oid);
512+
513+
SRF_RETURN_NEXT(funcctx, CStringGetDatum(tablespace_name));
514+
}
515+
else
516+
{
517+
cache_release(hcache);
518+
SRF_RETURN_DONE(funcctx);
519+
}
520+
}

test/expected/extension.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ ORDER BY proname;
2727
indexes_relation_size_pretty
2828
last
2929
set_chunk_time_interval
30+
show_tablespaces
3031
time_bucket
31-
(17 rows)
32+
(18 rows)
3233

test/expected/pg_dump.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ SELECT count(*)
4949
AND refobjid = (SELECT oid FROM pg_extension WHERE extname = 'timescaledb');
5050
count
5151
-------
52-
123
52+
125
5353
(1 row)
5454

5555
SELECT * FROM test.show_columns('public."two_Partitions"');
@@ -235,7 +235,7 @@ SELECT count(*)
235235
AND refobjid = (SELECT oid FROM pg_extension WHERE extname = 'timescaledb');
236236
count
237237
-------
238-
123
238+
125
239239
(1 row)
240240

241241
--main table and chunk schemas should be the same

test/expected/tablespace.out

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ SELECT * FROM _timescaledb_catalog.tablespace;
6868
2 | 1 | tablespace2
6969
(2 rows)
7070

71+
SELECT * FROM show_tablespaces('tspace_2dim');
72+
show_tablespaces
73+
------------------
74+
tablespace1
75+
tablespace2
76+
(2 rows)
77+
7178
--insert into another chunk
7279
INSERT INTO tspace_2dim VALUES ('2017-01-20T09:00:01', 24.3, 'brown');
7380
SELECT relname, spcname FROM pg_class c
@@ -141,6 +148,19 @@ SELECT * FROM _timescaledb_catalog.tablespace;
141148
4 | 2 | tablespace2
142149
(3 rows)
143150

151+
SELECT * FROM show_tablespaces('tspace_1dim');
152+
show_tablespaces
153+
------------------
154+
tablespace2
155+
(1 row)
156+
157+
SELECT * FROM show_tablespaces('tspace_2dim');
158+
show_tablespaces
159+
------------------
160+
tablespace1
161+
tablespace2
162+
(2 rows)
163+
144164
--detach the other tablespace
145165
SELECT detach_tablespace('tablespace2', 'tspace_1dim');
146166
detach_tablespace
@@ -155,6 +175,18 @@ SELECT * FROM _timescaledb_catalog.tablespace;
155175
2 | 1 | tablespace2
156176
(2 rows)
157177

178+
SELECT * FROM show_tablespaces('tspace_1dim');
179+
show_tablespaces
180+
------------------
181+
(0 rows)
182+
183+
SELECT * FROM show_tablespaces('tspace_2dim');
184+
show_tablespaces
185+
------------------
186+
tablespace1
187+
tablespace2
188+
(2 rows)
189+
158190
--detaching a tablespace from table without permissions should fail
159191
SELECT detach_tablespace('tablespace2', 'tspace_2dim');
160192
ERROR: User "default_perm_user_2" lacks permissions on table "tspace_2dim"
@@ -173,6 +205,16 @@ SELECT * FROM _timescaledb_catalog.tablespace;
173205
----+---------------+-----------------
174206
(0 rows)
175207

208+
SELECT * FROM show_tablespaces('tspace_1dim');
209+
show_tablespaces
210+
------------------
211+
(0 rows)
212+
213+
SELECT * FROM show_tablespaces('tspace_2dim');
214+
show_tablespaces
215+
------------------
216+
(0 rows)
217+
176218
--cleanup
177219
DROP TABLE tspace_1dim CASCADE;
178220
DROP TABLE tspace_2dim CASCADE;

test/sql/tablespace.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ GRANT :ROLE_DEFAULT_PERM_USER_2 TO :ROLE_DEFAULT_PERM_USER;
5252
SELECT attach_tablespace('tablespace2', 'tspace_2dim');
5353

5454
SELECT * FROM _timescaledb_catalog.tablespace;
55+
SELECT * FROM show_tablespaces('tspace_2dim');
5556

5657
--insert into another chunk
5758
INSERT INTO tspace_2dim VALUES ('2017-01-20T09:00:01', 24.3, 'brown');
@@ -87,10 +88,14 @@ INNER JOIN _timescaledb_catalog.chunk ch ON (ch.table_name = c.relname);
8788
--should only detach from 'tspace_1dim' (1 tablespace)
8889
SELECT detach_tablespace('tablespace1');
8990
SELECT * FROM _timescaledb_catalog.tablespace;
91+
SELECT * FROM show_tablespaces('tspace_1dim');
92+
SELECT * FROM show_tablespaces('tspace_2dim');
9093

9194
--detach the other tablespace
9295
SELECT detach_tablespace('tablespace2', 'tspace_1dim');
9396
SELECT * FROM _timescaledb_catalog.tablespace;
97+
SELECT * FROM show_tablespaces('tspace_1dim');
98+
SELECT * FROM show_tablespaces('tspace_2dim');
9499

95100
--detaching a tablespace from table without permissions should fail
96101
SELECT detach_tablespace('tablespace2', 'tspace_2dim');
@@ -100,6 +105,8 @@ SELECT detach_tablespaces('tspace_2dim');
100105
SET ROLE :ROLE_DEFAULT_PERM_USER;
101106
SELECT detach_tablespaces('tspace_2dim');
102107
SELECT * FROM _timescaledb_catalog.tablespace;
108+
SELECT * FROM show_tablespaces('tspace_1dim');
109+
SELECT * FROM show_tablespaces('tspace_2dim');
103110

104111
--cleanup
105112
DROP TABLE tspace_1dim CASCADE;

0 commit comments

Comments
 (0)