Skip to content

Commit

Permalink
Move get_create_command into _timescaledb_functions schema
Browse files Browse the repository at this point in the history
To increase schema security we do not want to mix our own internal
objects with user objects. Since chunks are created in the
_timescaledb_internal schema our internal functions should live in
a different dedicated schema. This patch make the necessary
adjustments for the get_create_command function.
  • Loading branch information
svenklemm committed Aug 21, 2023
1 parent 56ea8b4 commit 0dd06e9
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion scripts/ts_dump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pg_dump "$@" --schema-only -t $HYPERTABLE -f $PREFIX-schema.sql
echo >> $PREFIX-schema.sql "--
-- Restore to hypertable
--"
psql "$@" -qAtX -c "SELECT _timescaledb_internal.get_create_command('$HYPERTABLE');" >> $PREFIX-schema.sql
psql "$@" -qAtX -c "SELECT _timescaledb_functions.get_create_command('$HYPERTABLE');" >> $PREFIX-schema.sql

echo "Backing up data as $PREFIX-data.csv..."
psql "$@" -c "\COPY (SELECT * FROM $HYPERTABLE) TO $PREFIX-data.csv DELIMITER ',' CSV"
Expand Down
1 change: 1 addition & 0 deletions sql/updates/latest-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ $$;
ALTER FUNCTION _timescaledb_internal.insert_blocker() SET SCHEMA _timescaledb_functions;
ALTER FUNCTION _timescaledb_internal.continuous_agg_invalidation_trigger() SET SCHEMA _timescaledb_functions;

ALTER FUNCTION _timescaledb_internal.get_create_command(name) SET SCHEMA _timescaledb_functions;
1 change: 1 addition & 0 deletions sql/updates/reverse-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ ALTER FUNCTION _timescaledb_functions.insert_blocker() SET SCHEMA _timescaledb_i
ALTER FUNCTION _timescaledb_functions.continuous_agg_invalidation_trigger() SET SCHEMA _timescaledb_internal;
ALTER FUNCTION _timescaledb_functions.drop_dist_ht_invalidation_trigger(integer) SET SCHEMA _timescaledb_internal;

ALTER FUNCTION _timescaledb_functions.get_create_command(name) SET SCHEMA _timescaledb_internal;
2 changes: 1 addition & 1 deletion sql/util_internal_table_ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
-- so that it knows how to restore the hypertable without user intervention.
--
-- It only works for hypertables with up to 2 dimensions.
CREATE OR REPLACE FUNCTION _timescaledb_internal.get_create_command(
CREATE OR REPLACE FUNCTION _timescaledb_functions.get_create_command(
table_name NAME
)
RETURNS TEXT LANGUAGE PLPGSQL VOLATILE AS
Expand Down
12 changes: 6 additions & 6 deletions test/expected/create_hypertable.out
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ SET ROLE :ROLE_DEFAULT_PERM_USER;
create table test_schema.test_table(time BIGINT, temp float8, device_id text, device_type text, location text, id int, id2 int);
\set ON_ERROR_STOP 0
-- get_create_command should fail since hypertable isn't made yet
SELECT * FROM _timescaledb_internal.get_create_command('test_table');
SELECT * FROM _timescaledb_functions.get_create_command('test_table');
ERROR: hypertable "test_table" not found
\set ON_ERROR_STOP 1
\dt "test_schema".*
Expand Down Expand Up @@ -72,7 +72,7 @@ SELECT * FROM test.show_triggers('test_schema.test_table');
ts_insert_blocker | 7 | _timescaledb_functions.insert_blocker
(1 row)

SELECT * FROM _timescaledb_internal.get_create_command('test_table');
SELECT * FROM _timescaledb_functions.get_create_command('test_table');
get_create_command
--------------------------------------------------------------------------------------------------------------------------------------------------
SELECT create_hypertable('test_schema.test_table', 'time', 'device_id', 2, chunk_time_interval => 2592000000000, create_default_indexes=>FALSE);
Expand Down Expand Up @@ -137,7 +137,7 @@ ERROR: invalid number of partitions: must be between 1 and 32767
SELECT set_number_partitions('test_schema.test_table', 32768, 'location');
ERROR: invalid number of partitions: must be between 1 and 32767
-- get_create_command only works on tables w/ 1 or 2 dimensions
SELECT * FROM _timescaledb_internal.get_create_command('test_table');
SELECT * FROM _timescaledb_functions.get_create_command('test_table');
ERROR: get_create_command only supports hypertables with up to 2 dimensions
\set ON_ERROR_STOP 1
--test adding one more open dimension
Expand Down Expand Up @@ -416,7 +416,7 @@ NOTICE: adding not-null constraint to column "time"
(7,test_schema,test_1dim,t)
(1 row)

SELECT * FROM _timescaledb_internal.get_create_command('test_1dim');
SELECT * FROM _timescaledb_functions.get_create_command('test_1dim');
get_create_command
--------------------------------------------------------------------------------------------------------------------------------
SELECT create_hypertable('test_schema.test_1dim', 'time', chunk_time_interval => 604800000000, create_default_indexes=>FALSE);
Expand Down Expand Up @@ -713,13 +713,13 @@ NOTICE: adding not-null constraint to column "time"
(15,test_schema,test_sql_cmd,t)
(1 row)

SELECT * FROM _timescaledb_internal.get_create_command('test_sql_cmd');
SELECT * FROM _timescaledb_functions.get_create_command('test_sql_cmd');
get_create_command
-----------------------------------------------------------------------------------------------------------------------------------
SELECT create_hypertable('test_schema.test_sql_cmd', 'time', chunk_time_interval => 604800000000, create_default_indexes=>FALSE);
(1 row)

SELECT _timescaledb_internal.get_create_command('test_sql_cmd') AS create_cmd; \gset
SELECT _timescaledb_functions.get_create_command('test_sql_cmd') AS create_cmd; \gset
create_cmd
-----------------------------------------------------------------------------------------------------------------------------------
SELECT create_hypertable('test_schema.test_sql_cmd', 'time', chunk_time_interval => 604800000000, create_default_indexes=>FALSE);
Expand Down
12 changes: 6 additions & 6 deletions test/sql/create_hypertable.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ create table test_schema.test_table(time BIGINT, temp float8, device_id text, de

\set ON_ERROR_STOP 0
-- get_create_command should fail since hypertable isn't made yet
SELECT * FROM _timescaledb_internal.get_create_command('test_table');
SELECT * FROM _timescaledb_functions.get_create_command('test_table');
\set ON_ERROR_STOP 1

\dt "test_schema".*
Expand Down Expand Up @@ -58,7 +58,7 @@ select * from create_hypertable('test_schema.test_table', 'time', 'device_id', 2
-- Check that the insert block trigger exists
SELECT * FROM test.show_triggers('test_schema.test_table');

SELECT * FROM _timescaledb_internal.get_create_command('test_table');
SELECT * FROM _timescaledb_functions.get_create_command('test_table');

--test adding one more closed dimension
select add_dimension('test_schema.test_table', 'location', 4);
Expand All @@ -79,7 +79,7 @@ SELECT set_number_partitions('test_schema.test_table', 0, 'location');
-- Too many
SELECT set_number_partitions('test_schema.test_table', 32768, 'location');
-- get_create_command only works on tables w/ 1 or 2 dimensions
SELECT * FROM _timescaledb_internal.get_create_command('test_table');
SELECT * FROM _timescaledb_functions.get_create_command('test_table');
\set ON_ERROR_STOP 1

--test adding one more open dimension
Expand Down Expand Up @@ -197,7 +197,7 @@ select add_dimension('test_schema.test_table', 'location', 2, if_not_exists => t
--test partitioning in only time dimension
create table test_schema.test_1dim(time timestamp, temp float);
select create_hypertable('test_schema.test_1dim', 'time');
SELECT * FROM _timescaledb_internal.get_create_command('test_1dim');
SELECT * FROM _timescaledb_functions.get_create_command('test_1dim');

\dt "test_schema".*

Expand Down Expand Up @@ -386,8 +386,8 @@ select add_dimension('test_schema.test_partfunc', 'device', 2, partitioning_func
-- check get_create_command produces valid command
CREATE TABLE test_schema.test_sql_cmd(time TIMESTAMPTZ, temp FLOAT8, device_id TEXT, device_type TEXT, location TEXT, id INT, id2 INT);
SELECT create_hypertable('test_schema.test_sql_cmd','time');
SELECT * FROM _timescaledb_internal.get_create_command('test_sql_cmd');
SELECT _timescaledb_internal.get_create_command('test_sql_cmd') AS create_cmd; \gset
SELECT * FROM _timescaledb_functions.get_create_command('test_sql_cmd');
SELECT _timescaledb_functions.get_create_command('test_sql_cmd') AS create_cmd; \gset
DROP TABLE test_schema.test_sql_cmd CASCADE;
CREATE TABLE test_schema.test_sql_cmd(time TIMESTAMPTZ, temp FLOAT8, device_id TEXT, device_type TEXT, location TEXT, id INT, id2 INT);
SELECT test.execute_sql(:'create_cmd');
Expand Down
2 changes: 1 addition & 1 deletion tsl/test/shared/expected/extension.out
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ORDER BY pronamespace::regnamespace::text COLLATE "C", p.oid::regprocedure::text
_timescaledb_functions.drop_dist_ht_invalidation_trigger(integer)
_timescaledb_functions.first_combinefunc(internal,internal)
_timescaledb_functions.first_sfunc(internal,anyelement,"any")
_timescaledb_functions.get_create_command(name)
_timescaledb_functions.hist_combinefunc(internal,internal)
_timescaledb_functions.hist_deserializefunc(bytea,internal)
_timescaledb_functions.hist_finalfunc(internal,double precision,double precision,double precision,integer)
Expand Down Expand Up @@ -87,7 +88,6 @@ ORDER BY pronamespace::regnamespace::text COLLATE "C", p.oid::regprocedure::text
_timescaledb_internal.get_chunk_colstats(regclass)
_timescaledb_internal.get_chunk_relstats(regclass)
_timescaledb_internal.get_compressed_chunk_index_for_recompression(regclass)
_timescaledb_internal.get_create_command(name)
_timescaledb_internal.get_git_commit()
_timescaledb_internal.get_os_info()
_timescaledb_internal.get_partition_for_key(anyelement)
Expand Down

0 comments on commit 0dd06e9

Please sign in to comment.