Skip to content

Commit

Permalink
Move partitioning functions to _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 following functions:

- get_partition_for_key(val anyelement)
- get_partition_hash(val anyelement)
  • Loading branch information
svenklemm committed Aug 29, 2023
1 parent b2a9149 commit a9751cc
Show file tree
Hide file tree
Showing 38 changed files with 370 additions and 361 deletions.
4 changes: 2 additions & 2 deletions sql/partitioning.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
-- LICENSE-APACHE for a copy of the license.

-- Deprecated partition hash function
CREATE OR REPLACE FUNCTION _timescaledb_internal.get_partition_for_key(val anyelement)
CREATE OR REPLACE FUNCTION _timescaledb_functions.get_partition_for_key(val anyelement)
RETURNS int
AS '@MODULE_PATHNAME@', 'ts_get_partition_for_key' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION _timescaledb_internal.get_partition_hash(val anyelement)
CREATE OR REPLACE FUNCTION _timescaledb_functions.get_partition_hash(val anyelement)
RETURNS int
AS '@MODULE_PATHNAME@', 'ts_get_partition_hash' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

4 changes: 4 additions & 0 deletions sql/updates/latest-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,8 @@ ALTER FUNCTION _timescaledb_internal.restart_background_workers() SET SCHEMA _ti

ALTER FUNCTION _timescaledb_internal.process_ddl_event() SET SCHEMA _timescaledb_functions;

ALTER FUNCTION _timescaledb_internal.get_partition_for_key(val anyelement) SET SCHEMA _timescaledb_functions;
ALTER FUNCTION _timescaledb_internal.get_partition_hash(val anyelement) SET SCHEMA _timescaledb_functions;

UPDATE _timescaledb_catalog.dimension SET partitioning_func_schema = '_timescaledb_functions' WHERE partitioning_func_schema = '_timescaledb_internal' AND partitioning_func IN ('get_partition_for_key','get_partition_hash');

5 changes: 5 additions & 0 deletions sql/updates/reverse-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,8 @@ ALTER FUNCTION _timescaledb_functions.process_ddl_event() SET SCHEMA _timescaled
ALTER FUNCTION _timescaledb_functions.recompress_chunk_segmentwise(regclass,boolean) SET SCHEMA _timescaledb_internal;
ALTER PROCEDURE _timescaledb_functions.wait_subscription_sync(name,name,integer,numeric) SET SCHEMA _timescaledb_internal;

ALTER FUNCTION _timescaledb_functions.get_partition_for_key(val anyelement) SET SCHEMA _timescaledb_internal;
ALTER FUNCTION _timescaledb_functions.get_partition_hash(val anyelement) SET SCHEMA _timescaledb_internal;

UPDATE _timescaledb_catalog.dimension SET partitioning_func_schema = '_timescaledb_internal' WHERE partitioning_func_schema = '_timescaledb_functions' AND partitioning_func IN ('get_partition_for_key','get_partition_hash');

2 changes: 1 addition & 1 deletion src/partitioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#define OPEN_START_TIME -1
#define OPEN_END_TIME PG_INT64_MAX

#define DEFAULT_PARTITIONING_FUNC_SCHEMA INTERNAL_SCHEMA_NAME
#define DEFAULT_PARTITIONING_FUNC_SCHEMA FUNCTIONS_SCHEMA_NAME
#define DEFAULT_PARTITIONING_FUNC_NAME "get_partition_hash"

typedef struct PartitioningFunc
Expand Down
4 changes: 2 additions & 2 deletions src/planner/space_constraint.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ make_partfunc_call(Oid funcid, Oid rettype, List *args, Oid inputcollid)
/*
* Transform a constraint like: device_id = 1
* into
* ((device_id = 1) AND (_timescaledb_internal.get_partition_hash(device_id) = 242423622))
* ((device_id = 1) AND (_timescaledb_functions.get_partition_hash(device_id) = 242423622))
*/
static OpExpr *
transform_space_constraint(PlannerInfo *root, List *rtable, OpExpr *op)
Expand Down Expand Up @@ -223,7 +223,7 @@ transform_space_constraint(PlannerInfo *root, List *rtable, OpExpr *op)
/*
* Transforms a constraint like: s1 = ANY ('{s1_2,s1_2}'::text[])
* into
* ((s1 = ANY ('{s1_2,s1_2}'::text[])) AND (_timescaledb_internal.get_partition_hash(s1) = ANY
* ((s1 = ANY ('{s1_2,s1_2}'::text[])) AND (_timescaledb_functions.get_partition_hash(s1) = ANY
* ('{1583420735,1583420735}'::integer[])))
*/
static ScalarArrayOpExpr *
Expand Down
20 changes: 10 additions & 10 deletions test/expected/broken_tables.out
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ FROM _timescaledb_catalog.chunk_constraint,
WHERE conname = constraint_name
) AS con
ORDER BY chunk_id, dimension_slice_id;
chunk_id | dimension_slice_id | constraint_name | constraint_expr
----------+--------------------+-----------------+---------------------------------------------------------------
chunk_id | dimension_slice_id | constraint_name | constraint_expr
----------+--------------------+-----------------+----------------------------------------------------------------
1 | 1 | constraint_1 | (("time" >= 3) AND ("time" < 6))
1 | 2 | constraint_2 | (_timescaledb_internal.get_partition_hash(tag) < 1073741823)
1 | 2 | constraint_2 | (_timescaledb_functions.get_partition_hash(tag) < 1073741823)
2 | 1 | constraint_1 | (("time" >= 3) AND ("time" < 6))
2 | 3 | constraint_3 | (_timescaledb_internal.get_partition_hash(tag) >= 1073741823)
3 | 3 | constraint_3 | (_timescaledb_internal.get_partition_hash(tag) >= 1073741823)
2 | 3 | constraint_3 | (_timescaledb_functions.get_partition_hash(tag) >= 1073741823)
3 | 3 | constraint_3 | (_timescaledb_functions.get_partition_hash(tag) >= 1073741823)
3 | 4 | constraint_4 | (("time" >= 9) AND ("time" < 12))
(6 rows)

Expand Down Expand Up @@ -102,13 +102,13 @@ FROM _timescaledb_catalog.chunk_constraint,
WHERE conname = constraint_name
) AS con
ORDER BY chunk_id, dimension_slice_id;
chunk_id | dimension_slice_id | constraint_name | constraint_expr
----------+--------------------+-----------------+---------------------------------------------------------------
chunk_id | dimension_slice_id | constraint_name | constraint_expr
----------+--------------------+-----------------+----------------------------------------------------------------
4 | 5 | constraint_5 | (("time" >= 3) AND ("time" < 6))
4 | 6 | constraint_6 | (_timescaledb_internal.get_partition_hash(tag) < 1073741823)
4 | 6 | constraint_6 | (_timescaledb_functions.get_partition_hash(tag) < 1073741823)
5 | 5 | constraint_5 | (("time" >= 3) AND ("time" < 6))
5 | 7 | constraint_7 | (_timescaledb_internal.get_partition_hash(tag) >= 1073741823)
6 | 7 | constraint_7 | (_timescaledb_internal.get_partition_hash(tag) >= 1073741823)
5 | 7 | constraint_7 | (_timescaledb_functions.get_partition_hash(tag) >= 1073741823)
6 | 7 | constraint_7 | (_timescaledb_functions.get_partition_hash(tag) >= 1073741823)
6 | 8 | constraint_8 | (("time" >= 9) AND ("time" < 12))
(6 rows)

Expand Down
6 changes: 3 additions & 3 deletions test/expected/chunk_adaptive.out
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ SELECT id, hypertable_id, interval_length FROM _timescaledb_catalog.dimension;
-- to get determinstic location IDs so that we always spread these
-- values the same way across space partitions
INSERT INTO test_adaptive
SELECT time, random() * 35, _timescaledb_internal.get_partition_hash(time) FROM
SELECT time, random() * 35, _timescaledb_functions.get_partition_hash(time) FROM
generate_series('2017-03-07T18:18:03+00'::timestamptz - interval '175 days',
'2017-03-07T18:18:03+00'::timestamptz,
'2 minutes') as time;
Expand Down Expand Up @@ -295,7 +295,7 @@ SELECT id, hypertable_id, interval_length FROM _timescaledb_catalog.dimension;
(2 rows)

INSERT INTO test_adaptive_no_index
SELECT time, random() * 35, _timescaledb_internal.get_partition_hash(time) FROM
SELECT time, random() * 35, _timescaledb_functions.get_partition_hash(time) FROM
generate_series('2017-03-07T18:18:03+00'::timestamptz - interval '175 days',
'2017-03-07T18:18:03+00'::timestamptz,
'2 minutes') as time;
Expand Down Expand Up @@ -498,7 +498,7 @@ SELECT id, hypertable_id, interval_length FROM _timescaledb_catalog.dimension;
(4 rows)

INSERT INTO test_adaptive_space
SELECT time, random() * 35, _timescaledb_internal.get_partition_hash(time) FROM
SELECT time, random() * 35, _timescaledb_functions.get_partition_hash(time) FROM
generate_series('2017-03-07T18:18:03+00'::timestamptz - interval '175 days',
'2017-03-07T18:18:03+00'::timestamptz,
'2 minutes') as time;
Expand Down
4 changes: 2 additions & 2 deletions test/expected/chunk_utils.out
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ ORDER BY c.id;
--------+------+------+-------
(0 rows)

SELECT _timescaledb_internal.get_partition_for_key('dev1'::text);
SELECT _timescaledb_functions.get_partition_for_key('dev1'::text);
get_partition_for_key
-----------------------
1129986420
(1 row)

SELECT _timescaledb_internal.get_partition_for_key('dev7'::varchar(5));
SELECT _timescaledb_functions.get_partition_for_key('dev7'::varchar(5));
get_partition_for_key
-----------------------
449729092
Expand Down
2 changes: 1 addition & 1 deletion test/expected/create_chunks.out
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ SELECT set_chunk_time_interval('chunk_test', 5::bigint);
SELECT * FROM _timescaledb_catalog.dimension;
id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | compress_interval_length | integer_now_func_schema | integer_now_func
----+---------------+-------------+-------------+---------+------------+--------------------------+--------------------+-----------------+--------------------------+-------------------------+------------------
2 | 1 | tag | integer | f | 3 | _timescaledb_internal | get_partition_hash | | | |
2 | 1 | tag | integer | f | 3 | _timescaledb_functions | get_partition_hash | | | |
1 | 1 | time | integer | t | | | | 5 | | |
(2 rows)

Expand Down
26 changes: 13 additions & 13 deletions test/expected/create_hypertable.out
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ select * from _timescaledb_catalog.dimension;
id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | compress_interval_length | integer_now_func_schema | integer_now_func
----+---------------+-------------+-------------+---------+------------+--------------------------+--------------------+-----------------+--------------------------+-------------------------+------------------
1 | 1 | time | bigint | t | | | | 2592000000000 | | |
2 | 1 | device_id | text | f | 2 | _timescaledb_internal | get_partition_hash | | | |
2 | 1 | device_id | text | f | 2 | _timescaledb_functions | get_partition_hash | | | |
3 | 2 | time | bigint | t | | | | 2592000000000 | | |
4 | 2 | device_id | text | f | 2 | _timescaledb_internal | get_partition_hash | | | |
5 | 2 | location | text | f | 4 | _timescaledb_internal | get_partition_hash | | | |
4 | 2 | device_id | text | f | 2 | _timescaledb_functions | get_partition_hash | | | |
5 | 2 | location | text | f | 4 | _timescaledb_functions | get_partition_hash | | | |
(5 rows)

--test that we can change the number of partitions and that 1 is allowed
Expand All @@ -111,7 +111,7 @@ SELECT set_number_partitions('test_schema.test_table', 1, 'location');
select * from _timescaledb_catalog.dimension WHERE column_name = 'location';
id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | compress_interval_length | integer_now_func_schema | integer_now_func
----+---------------+-------------+-------------+---------+------------+--------------------------+--------------------+-----------------+--------------------------+-------------------------+------------------
5 | 2 | location | text | f | 1 | _timescaledb_internal | get_partition_hash | | | |
5 | 2 | location | text | f | 1 | _timescaledb_functions | get_partition_hash | | | |
(1 row)

SELECT set_number_partitions('test_schema.test_table', 2, 'location');
Expand All @@ -123,7 +123,7 @@ SELECT set_number_partitions('test_schema.test_table', 2, 'location');
select * from _timescaledb_catalog.dimension WHERE column_name = 'location';
id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | compress_interval_length | integer_now_func_schema | integer_now_func
----+---------------+-------------+-------------+---------+------------+--------------------------+--------------------+-----------------+--------------------------+-------------------------+------------------
5 | 2 | location | text | f | 2 | _timescaledb_internal | get_partition_hash | | | |
5 | 2 | location | text | f | 2 | _timescaledb_functions | get_partition_hash | | | |
(1 row)

\set ON_ERROR_STOP 0
Expand Down Expand Up @@ -158,10 +158,10 @@ select * from _timescaledb_catalog.dimension;
id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | compress_interval_length | integer_now_func_schema | integer_now_func
----+---------------+-------------+-------------+---------+------------+--------------------------+--------------------+-----------------+--------------------------+-------------------------+------------------
1 | 1 | time | bigint | t | | | | 2592000000000 | | |
2 | 1 | device_id | text | f | 2 | _timescaledb_internal | get_partition_hash | | | |
2 | 1 | device_id | text | f | 2 | _timescaledb_functions | get_partition_hash | | | |
3 | 2 | time | bigint | t | | | | 2592000000000 | | |
4 | 2 | device_id | text | f | 2 | _timescaledb_internal | get_partition_hash | | | |
5 | 2 | location | text | f | 2 | _timescaledb_internal | get_partition_hash | | | |
4 | 2 | device_id | text | f | 2 | _timescaledb_functions | get_partition_hash | | | |
5 | 2 | location | text | f | 2 | _timescaledb_functions | get_partition_hash | | | |
6 | 2 | id | integer | t | | | | 1000 | | |
(6 rows)

Expand Down Expand Up @@ -636,35 +636,35 @@ CREATE OR REPLACE FUNCTION partfunc_not_immutable(source anyelement)
RETURNS INTEGER LANGUAGE PLPGSQL AS
$BODY$
BEGIN
RETURN _timescaledb_internal.get_partition_hash(source);
RETURN _timescaledb_functions.get_partition_hash(source);
END
$BODY$;
CREATE OR REPLACE FUNCTION partfunc_bad_return_type(source anyelement)
RETURNS BIGINT LANGUAGE PLPGSQL IMMUTABLE AS
$BODY$
BEGIN
RETURN _timescaledb_internal.get_partition_hash(source);
RETURN _timescaledb_functions.get_partition_hash(source);
END
$BODY$;
CREATE OR REPLACE FUNCTION partfunc_bad_arg_type(source text)
RETURNS INTEGER LANGUAGE PLPGSQL IMMUTABLE AS
$BODY$
BEGIN
RETURN _timescaledb_internal.get_partition_hash(source);
RETURN _timescaledb_functions.get_partition_hash(source);
END
$BODY$;
CREATE OR REPLACE FUNCTION partfunc_bad_multi_arg(source anyelement, extra_arg integer)
RETURNS INTEGER LANGUAGE PLPGSQL IMMUTABLE AS
$BODY$
BEGIN
RETURN _timescaledb_internal.get_partition_hash(source);
RETURN _timescaledb_functions.get_partition_hash(source);
END
$BODY$;
CREATE OR REPLACE FUNCTION partfunc_valid(source anyelement)
RETURNS INTEGER LANGUAGE PLPGSQL IMMUTABLE AS
$BODY$
BEGIN
RETURN _timescaledb_internal.get_partition_hash(source);
RETURN _timescaledb_functions.get_partition_hash(source);
END
$BODY$;
create table test_schema.test_partfunc(time timestamptz, temp float, device int);
Expand Down
4 changes: 2 additions & 2 deletions test/expected/ddl-13.out
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ SELECT * FROM test.show_columnsp('_timescaledb_internal._hyper_9_%chunk');
SELECT * FROM _timescaledb_catalog.dimension WHERE hypertable_id = 9;
id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | compress_interval_length | integer_now_func_schema | integer_now_func
----+---------------+-------------+--------------------------+---------+------------+--------------------------+--------------------+-----------------+--------------------------+-------------------------+------------------
15 | 9 | color | character varying | f | 2 | _timescaledb_internal | get_partition_hash | | | |
15 | 9 | color | character varying | f | 2 | _timescaledb_functions | get_partition_hash | | | |
14 | 9 | time | timestamp with time zone | t | | | | 2628000000000 | | |
(2 rows)

Expand Down Expand Up @@ -478,7 +478,7 @@ SELECT * FROM test.show_columnsp('_timescaledb_internal._hyper_9_%chunk');
SELECT * FROM _timescaledb_catalog.dimension WHERE hypertable_id = 9;
id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | compress_interval_length | integer_now_func_schema | integer_now_func
----+---------------+-------------+-----------------------------+---------+------------+--------------------------+--------------------+-----------------+--------------------------+-------------------------+------------------
15 | 9 | colorname | character varying | f | 2 | _timescaledb_internal | get_partition_hash | | | |
15 | 9 | colorname | character varying | f | 2 | _timescaledb_functions | get_partition_hash | | | |
14 | 9 | time_us | timestamp without time zone | t | | | | 2628000000000 | | |
(2 rows)

Expand Down
Loading

0 comments on commit a9751cc

Please sign in to comment.