Skip to content

Commit e593876

Browse files
committed
Refactor tablespace handling
Attaching tablespaces to hypertables is now handled in native code, with improved permissions checking and caching of tablespaces in the Hypertable data object.
1 parent c4a46ac commit e593876

22 files changed

+442
-89
lines changed

sql/cache.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ FOR EACH STATEMENT EXECUTE PROCEDURE _timescaledb_cache.invalidate_relcache_trig
3434
DROP TRIGGER IF EXISTS "0_cache_inval" ON _timescaledb_catalog.dimension;
3535
CREATE TRIGGER "0_cache_inval" AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON _timescaledb_catalog.dimension
3636
FOR EACH STATEMENT EXECUTE PROCEDURE _timescaledb_cache.invalidate_relcache_trigger();
37+
38+
DROP TRIGGER IF EXISTS "0_cache_inval" ON _timescaledb_catalog.tablespace;
39+
CREATE TRIGGER "0_cache_inval" AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON _timescaledb_catalog.tablespace
40+
FOR EACH STATEMENT EXECUTE PROCEDURE _timescaledb_cache.invalidate_relcache_trigger();

sql/ddl_api.sql

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -298,33 +298,8 @@ BEGIN
298298
END
299299
$BODY$;
300300

301-
CREATE OR REPLACE FUNCTION attach_tablespace(
302-
hypertable REGCLASS,
303-
tablespace NAME
304-
)
305-
RETURNS VOID LANGUAGE PLPGSQL VOLATILE
306-
SECURITY DEFINER SET search_path = ''
307-
AS
301+
CREATE OR REPLACE FUNCTION attach_tablespace(tablespace NAME, hypertable REGCLASS)
302+
RETURNS VOID LANGUAGE SQL AS
308303
$BODY$
309-
DECLARE
310-
hypertable_id INTEGER;
311-
tablespace_oid OID;
312-
BEGIN
313-
PERFORM _timescaledb_internal.check_role(hypertable);
314-
315-
SELECT id
316-
FROM _timescaledb_catalog.hypertable h, pg_class c, pg_namespace n
317-
WHERE h.schema_name = n.nspname
318-
AND h.table_name = c.relname
319-
AND c.oid = hypertable
320-
AND n.oid = c.relnamespace
321-
INTO hypertable_id;
322-
323-
IF hypertable_id IS NULL THEN
324-
RAISE EXCEPTION 'No hypertable "%" exists', main_table_name
325-
USING ERRCODE = 'IO101';
326-
END IF;
327-
328-
PERFORM _timescaledb_internal.attach_tablespace(hypertable_id, tablespace);
329-
END
304+
SELECT * FROM _timescaledb_internal.attach_tablespace(tablespace, hypertable);
330305
$BODY$;

sql/ddl_internal.sql

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ BEGIN
8080
)
8181
RETURNING * INTO hypertable_row;
8282

83-
--add default tablespace, if any
84-
IF tablespace IS NOT NULL THEN
85-
PERFORM _timescaledb_internal.attach_tablespace(hypertable_row.id, tablespace);
86-
END IF;
87-
8883
--create time dimension
8984
PERFORM _timescaledb_internal.add_dimension(main_table,
9085
hypertable_row,
@@ -112,6 +107,11 @@ BEGIN
112107
PERFORM _timescaledb_internal.create_default_indexes(hypertable_row, main_table, partitioning_column);
113108
END IF;
114109

110+
--add default tablespace, if any
111+
IF tablespace IS NOT NULL THEN
112+
PERFORM _timescaledb_internal.attach_tablespace(tablespace, main_table);
113+
END IF;
114+
115115
RETURN hypertable_row;
116116
END
117117
$BODY$;
@@ -618,6 +618,8 @@ BEGIN
618618
END
619619
$BODY$;
620620

621+
CREATE OR REPLACE FUNCTION _timescaledb_internal.attach_tablespace(tablespace NAME, hypertable REGCLASS) RETURNS VOID
622+
AS '$libdir/timescaledb', 'tablespace_attach' LANGUAGE C VOLATILE;
621623

622624
--documentation of these function located in chunk_index.h
623625
CREATE OR REPLACE FUNCTION _timescaledb_internal.chunk_index_clone(chunk_index_oid OID) RETURNS OID

sql/tablespace.sql

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,34 +88,3 @@ $BODY$
8888
(SELECT hypertable_id FROM _timescaledb_catalog.chunk WHERE id = chunk_id),
8989
chunk_id);
9090
$BODY$;
91-
92-
CREATE OR REPLACE FUNCTION _timescaledb_internal.attach_tablespace(
93-
hypertable_id INTEGER,
94-
tablespace_name NAME
95-
)
96-
RETURNS VOID LANGUAGE PLPGSQL VOLATILE AS
97-
$BODY$
98-
DECLARE
99-
tablespace_oid OID;
100-
BEGIN
101-
SELECT oid
102-
FROM pg_catalog.pg_tablespace
103-
WHERE spcname = tablespace_name
104-
INTO tablespace_oid;
105-
106-
IF tablespace_oid IS NULL THEN
107-
RAISE EXCEPTION 'No tablespace "%" exists. A tablespace needs to be created before assigning it to a hypertable dimension', tablespace_name
108-
USING ERRCODE = 'IO101';
109-
END IF;
110-
111-
BEGIN
112-
INSERT INTO _timescaledb_catalog.tablespace (hypertable_id, tablespace_name)
113-
VALUES (hypertable_id, tablespace_name);
114-
EXCEPTION
115-
WHEN unique_violation THEN
116-
RAISE EXCEPTION 'Tablespace "%" already assigned to hypertable "%"',
117-
tablespace_name, (SELECT table_name FROM _timescaledb_catalog.hypertable
118-
WHERE id = hypertable_id);
119-
END;
120-
END
121-
$BODY$;

sql/updates/pre-0.6.1--0.7.0.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ DROP FUNCTION create_hypertable(regclass,name,name,integer,name,name,anyelement,
3535
DROP FUNCTION add_dimension(regclass,name,integer,bigint);
3636
DROP FUNCTION _timescaledb_internal.create_hypertable_row(regclass,name,name,name,name,integer,name,name,bigint,name);
3737
DROP FUNCTION _timescaledb_internal.add_dimension(regclass,_timescaledb_catalog.hypertable,name,integer,bigint,boolean);
38+
39+
-- Tablespace functions
40+
DROP FUNCTION _timescaledb_internal.attach_tablespace(integer, name);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
DROP FUNCTION _timescaledb_cache.invalidate_relcache(oid);
22

3+
34
DROP FUNCTION set_chunk_time_interval(REGCLASS, BIGINT);
45
DROP FUNCTION add_dimension(REGCLASS, NAME, INTEGER, BIGINT, REGPROC);
56
DROP FUNCTION _timescaledb_internal.add_dimension(REGCLASS, _timescaledb_catalog.hypertable, NAME, INTEGER, BIGINT, REGPROC, BOOLEAN);
67
DROP FUNCTION _timescaledb_internal.time_interval_specification_to_internal(REGTYPE, anyelement, INTERVAL, TEXT);
8+
9+
-- Tablespace changes
10+
DROP FUNCTION _timescaledb_internal.attach_tablespace(integer, name);
11+
DROP FUNCTION attach_tablespace(regclass, name);

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ set(HEADERS
6363
process_utility.h
6464
scanner.h
6565
subspace_store.h
66+
tablespace.h
6667
trigger.h
6768
utils.h)
6869

@@ -105,6 +106,7 @@ set(SOURCES
105106
scanner.c
106107
sort_transform.c
107108
subspace_store.c
109+
tablespace.c
108110
trigger.c
109111
utils.c
110112
version.c)

src/catalog.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static const char *catalog_table_names[_MAX_CATALOG_TABLES + 1] = {
2828
[CHUNK] = CHUNK_TABLE_NAME,
2929
[CHUNK_CONSTRAINT] = CHUNK_CONSTRAINT_TABLE_NAME,
3030
[CHUNK_INDEX] = CHUNK_INDEX_TABLE_NAME,
31+
[TABLESPACE] = TABLESPACE_TABLE_NAME,
3132
[_MAX_CATALOG_TABLES] = "invalid table",
3233
};
3334

@@ -79,6 +80,13 @@ static const TableIndexDef catalog_table_index_definitions[_MAX_CATALOG_TABLES]
7980
[CHUNK_INDEX_CHUNK_ID_INDEX_NAME_IDX] = "chunk_index_chunk_id_index_name_key",
8081
[CHUNK_INDEX_HYPERTABLE_ID_HYPERTABLE_INDEX_NAME_IDX] = "chunk_index_hypertable_id_hypertable_index_name_idx",
8182
}
83+
},
84+
[TABLESPACE] = {
85+
.length = _MAX_TABLESPACE_INDEX,
86+
.names = (char *[]) {
87+
[TABLESPACE_PKEY_IDX] = "tablespace_pkey",
88+
[TABLESPACE_HYPERTABLE_ID_TABLESPACE_NAME_IDX] = "tablespace_hypertable_id_tablespace_name_key",
89+
}
8290
}
8391
};
8492

@@ -88,6 +96,8 @@ static const char *catalog_table_serial_id_names[_MAX_CATALOG_TABLES] = {
8896
[DIMENSION_SLICE] = CATALOG_SCHEMA_NAME ".dimension_slice_id_seq",
8997
[CHUNK] = CATALOG_SCHEMA_NAME ".chunk_id_seq",
9098
[CHUNK_CONSTRAINT] = NULL,
99+
[CHUNK_INDEX] = NULL,
100+
[TABLESPACE] = CATALOG_SCHEMA_NAME ".tablespace_id_seq",
91101
};
92102

93103
typedef struct InternalFunctionDef

src/catalog.h

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ typedef enum CatalogTable
2929
CHUNK,
3030
CHUNK_CONSTRAINT,
3131
CHUNK_INDEX,
32+
TABLESPACE,
3233
_MAX_CATALOG_TABLES,
3334
} CatalogTable;
3435

@@ -375,14 +376,73 @@ enum Anum_chunk_index_chunk_id_index_name_idx
375376
_Anum_chunk_index_chunk_id_index_name_idx_max,
376377
};
377378

378-
379379
enum Anum_chunk_index_hypertable_id_hypertable_index_name_idx
380380
{
381381
Anum_chunk_index_hypertable_id_hypertable_index_name_idx_hypertable_id = 1,
382382
Anum_chunk_index_hypertable_id_hypertable_index_name_idx_hypertable_index_name,
383383
Anum_chunk_index_hypertable_id_hypertable_index_name_idx_max,
384384
};
385385

386+
/************************************
387+
*
388+
* Tablespace table definitions
389+
*
390+
************************************/
391+
392+
#define TABLESPACE_TABLE_NAME "tablespace"
393+
394+
enum Anum_tablespace
395+
{
396+
Anum_tablespace_id = 1,
397+
Anum_tablespace_hypertable_id,
398+
Anum_tablespace_tablespace_name,
399+
_Anum_tablespace_max,
400+
};
401+
402+
#define Natts_tablespace \
403+
(_Anum_tablespace_max - 1)
404+
405+
typedef struct FormData_tablespace
406+
{
407+
int32 id;
408+
int32 hypertable_id;
409+
NameData tablespace_name;
410+
} FormData_tablespace;
411+
412+
typedef FormData_tablespace *Form_tablespace;
413+
414+
enum
415+
{
416+
TABLESPACE_PKEY_IDX = 0,
417+
TABLESPACE_HYPERTABLE_ID_TABLESPACE_NAME_IDX,
418+
_MAX_TABLESPACE_INDEX,
419+
};
420+
421+
enum Anum_tablespace_pkey_idx
422+
{
423+
Anum_tablespace_pkey_idx_tablespace_id = 1,
424+
_Anum_tablespace_pkey_idx_max,
425+
};
426+
427+
typedef struct FormData_tablespace_pkey_idx
428+
{
429+
int32 tablespace_id;
430+
} FormData_tablespace_pkey_idx;
431+
432+
enum Anum_tablespace_hypertable_id_tablespace_name_idx
433+
{
434+
Anum_tablespace_hypertable_id_tablespace_name_idx_hypertable_id = 1,
435+
Anum_tablespace_hypertable_id_tablespace_name_idx_tablespace_name,
436+
_Anum_tablespace_hypertable_id_tablespace_name_idx_max,
437+
};
438+
439+
typedef struct FormData_tablespace_hypertable_id_tablespace_name_idx
440+
{
441+
int32 hypertable_id;
442+
NameData tablespace_name;
443+
} FormData_tablespace_hypertable_id_tablespace_name_idx;
444+
445+
386446
#define MAX(a, b) \
387447
((long)(a) > (long)(b) ? (a) : (b))
388448

@@ -392,7 +452,8 @@ enum Anum_chunk_index_hypertable_id_hypertable_index_name_idx
392452
MAX(_MAX_DIMENSION_SLICE_INDEX, \
393453
MAX(_MAX_CHUNK_CONSTRAINT_INDEX, \
394454
MAX(_MAX_CHUNK_INDEX_INDEX, \
395-
_MAX_CHUNK_INDEX)))))
455+
MAX(_MAX_TABLESPACE_INDEX, \
456+
_MAX_CHUNK_INDEX))))))
396457

397458
typedef enum CacheType
398459
{

src/errors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define ERRCODE_IO_HYPERTABLE_EXISTS MAKE_SQLSTATE('I','O','1','1','0')
2828
#define ERRCODE_IO_NODE_EXISTS MAKE_SQLSTATE('I','O','1','2','0')
2929
#define ERRCODE_IO_USER_EXISTS MAKE_SQLSTATE('I','O','1','3','0')
30+
#define ERRCODE_IO_TABLESPACE_ALREADY_ATTACHED MAKE_SQLSTATE('I','O','1','4','0')
3031

3132
/*
3233
--IO500 - GROUP: internal error

0 commit comments

Comments
 (0)