-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tablespaces can now be associated with a hypertable using the new user-facing API attach_tablespace(). Alternatively, if the main table, which is to be converted into a hypertable, was created with an associated tablespace, that tablespace will automatically be associated also with the hypertable. Whenever a chunk is created, a tablespace will be chosen from the ones associated with the chunk's hypertable (if any). This is done in a way that ensures consistency in one dimension. I.e., if a hypertable has a closed (space) dimension with a fixed number of slices (ranges), it will ensure that chunks that fall in the same slice will alsp be stored in the same tablespace. If a hypertable has more than one closed dimension, the first one will be used to assign tablespaces to chunks. If the table has no closed dimensions, but one or more open (time) dimensions, then the first time dimension will be used. However, since open dimensions do not have a fixed number of slices, tablespaces will be assigned in a round-robbin fashion as new slices are created. Still, chunks in the same time slice will be stored in the same tablespace.
- Loading branch information
Showing
14 changed files
with
356 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
CREATE OR REPLACE FUNCTION _timescaledb_internal.select_tablespace( | ||
hypertable_id INTEGER, | ||
chunk_id INTEGER | ||
) | ||
RETURNS NAME LANGUAGE PLPGSQL VOLATILE AS | ||
$BODY$ | ||
<<main_block>> | ||
DECLARE | ||
chunk_slice_id INTEGER; | ||
chunk_slice_index INTEGER; | ||
dimension_id INTEGER; | ||
dimension_slice_id INTEGER; | ||
tablespaces NAME[] = ARRAY( | ||
SELECT t.tablespace_name FROM _timescaledb_catalog.tablespace t | ||
WHERE (t.hypertable_id = select_tablespace.hypertable_id) | ||
ORDER BY id DESC | ||
); | ||
dimension_slices INT[]; | ||
partitioning_func TEXT; | ||
BEGIN | ||
|
||
IF array_length(tablespaces, 1) = 0 THEN | ||
RETURN NULL; | ||
END IF; | ||
|
||
-- Try to pick first closed dimension, otherwise first open. | ||
-- The partition_func variable will be valid or NULL depending | ||
-- on the type of dimension found. This can be used to pick | ||
-- different tablespace assignment strategies depending | ||
-- on type of dimension. | ||
SELECT d.id, d.partitioning_func FROM _timescaledb_catalog.dimension d | ||
WHERE (d.hypertable_id = select_tablespace.hypertable_id) | ||
ORDER BY partitioning_func NULLS LAST, id DESC | ||
LIMIT 1 | ||
INTO dimension_id, partitioning_func; | ||
|
||
-- Find all dimension slices for the chosen dimension | ||
dimension_slices := ARRAY( | ||
SELECT s.id FROM _timescaledb_catalog.dimension_slice s | ||
WHERE (s.dimension_id = main_block.dimension_id) | ||
); | ||
|
||
-- Find the chunk's dimension slice for the chosen dimension | ||
SELECT s.id FROM _timescaledb_catalog.dimension_slice s | ||
INNER JOIN _timescaledb_catalog.chunk_constraint cc ON (cc.dimension_slice_id = s.id) | ||
INNER JOIN _timescaledb_catalog.chunk c ON (cc.chunk_id = c.id) | ||
WHERE (s.dimension_id = main_block.dimension_id) | ||
AND (c.id = select_tablespace.chunk_id) | ||
INTO STRICT chunk_slice_id; | ||
|
||
-- Find the array index of the chunk's dimension slice | ||
SELECT i | ||
FROM generate_subscripts(dimension_slices, 1) AS i | ||
WHERE dimension_slices[i] = chunk_slice_id | ||
INTO STRICT chunk_slice_index; | ||
|
||
-- Use the chunk's dimension slice index to pick a tablespace in the tablespaces array | ||
RETURN tablespaces[chunk_slice_index % array_length(tablespaces, 1) + 1]; | ||
END | ||
$BODY$; | ||
|
||
CREATE OR REPLACE FUNCTION _timescaledb_internal.select_tablespace( | ||
chunk_id INTEGER | ||
) | ||
RETURNS NAME LANGUAGE SQL AS | ||
$BODY$ | ||
SELECT _timescaledb_internal.select_tablespace( | ||
(SELECT hypertable_id FROM _timescaledb_catalog.chunk WHERE id = chunk_id), | ||
chunk_id); | ||
$BODY$; | ||
|
||
CREATE OR REPLACE FUNCTION _timescaledb_internal.attach_tablespace( | ||
hypertable_id INTEGER, | ||
tablespace_name NAME | ||
) | ||
RETURNS VOID LANGUAGE PLPGSQL VOLATILE AS | ||
$BODY$ | ||
DECLARE | ||
tablespace_oid OID; | ||
BEGIN | ||
SELECT oid | ||
FROM pg_catalog.pg_tablespace | ||
WHERE spcname = tablespace_name | ||
INTO tablespace_oid; | ||
|
||
IF tablespace_oid IS NULL THEN | ||
RAISE EXCEPTION 'No tablespace "%" exists. A tablespace needs to be created before assigning it to a hypertable dimension', tablespace_name | ||
USING ERRCODE = 'IO101'; | ||
END IF; | ||
|
||
BEGIN | ||
INSERT INTO _timescaledb_catalog.tablespace (hypertable_id, tablespace_name) | ||
VALUES (hypertable_id, tablespace_name); | ||
EXCEPTION | ||
WHEN unique_violation THEN | ||
RAISE EXCEPTION 'Tablespace "%" already assigned to hypertable "%"', | ||
tablespace_name, (SELECT table_name FROM _timescaledb_catalog.hypertable | ||
WHERE id = hypertable_id); | ||
END; | ||
END | ||
$BODY$; |
Oops, something went wrong.