Skip to content

Commit

Permalink
Move dist_internal 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:

- set_dist_id(uuid)
- set_peer_dist_id(uuid)
- validate_as_data_node()
- show_connection_cache()
- ping_data_node(name, interval)
- remote_txn_heal_data_node(oid)
  • Loading branch information
svenklemm committed Aug 22, 2023
1 parent e998327 commit 4256009
Show file tree
Hide file tree
Showing 21 changed files with 189 additions and 175 deletions.
4 changes: 2 additions & 2 deletions sql/data_node.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
-- LICENSE-APACHE for a copy of the license.

-- Check if a data node is up
CREATE OR REPLACE FUNCTION _timescaledb_internal.ping_data_node(node_name NAME, timeout INTERVAL = NULL) RETURNS BOOLEAN
CREATE OR REPLACE FUNCTION _timescaledb_functions.ping_data_node(node_name NAME, timeout INTERVAL = NULL) RETURNS BOOLEAN
AS '@MODULE_PATHNAME@', 'ts_data_node_ping' LANGUAGE C VOLATILE;

CREATE OR REPLACE FUNCTION _timescaledb_internal.remote_txn_heal_data_node(foreign_server_oid oid)
CREATE OR REPLACE FUNCTION _timescaledb_functions.remote_txn_heal_data_node(foreign_server_oid oid)
RETURNS INT
AS '@MODULE_PATHNAME@', 'ts_remote_txn_heal_data_node'
LANGUAGE C STRICT;
8 changes: 4 additions & 4 deletions sql/dist_internal.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.

CREATE OR REPLACE FUNCTION _timescaledb_internal.set_dist_id(dist_id UUID) RETURNS BOOL
CREATE OR REPLACE FUNCTION _timescaledb_functions.set_dist_id(dist_id UUID) RETURNS BOOL
AS '@MODULE_PATHNAME@', 'ts_dist_set_id' LANGUAGE C VOLATILE STRICT;

CREATE OR REPLACE FUNCTION _timescaledb_internal.set_peer_dist_id(dist_id UUID) RETURNS BOOL
CREATE OR REPLACE FUNCTION _timescaledb_functions.set_peer_dist_id(dist_id UUID) RETURNS BOOL
AS '@MODULE_PATHNAME@', 'ts_dist_set_peer_id' LANGUAGE C VOLATILE STRICT;

-- Function to validate that a node has local settings to function as
-- a data node. Throws error if validation fails.
CREATE OR REPLACE FUNCTION _timescaledb_internal.validate_as_data_node() RETURNS void
CREATE OR REPLACE FUNCTION _timescaledb_functions.validate_as_data_node() RETURNS void
AS '@MODULE_PATHNAME@', 'ts_dist_validate_as_data_node' LANGUAGE C VOLATILE STRICT;

CREATE OR REPLACE FUNCTION _timescaledb_internal.show_connection_cache()
CREATE OR REPLACE FUNCTION _timescaledb_functions.show_connection_cache()
RETURNS TABLE (
node_name name,
user_name name,
Expand Down
8 changes: 8 additions & 0 deletions sql/updates/latest-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ ALTER FUNCTION _timescaledb_internal.to_date(bigint) SET SCHEMA _timescaledb_fun
ALTER FUNCTION _timescaledb_internal.to_interval(bigint) SET SCHEMA _timescaledb_functions;
ALTER FUNCTION _timescaledb_internal.interval_to_usec(interval) SET SCHEMA _timescaledb_functions;
ALTER FUNCTION _timescaledb_internal.time_to_internal(anyelement) SET SCHEMA _timescaledb_functions;

ALTER FUNCTION _timescaledb_internal.set_dist_id(uuid) SET SCHEMA _timescaledb_functions;
ALTER FUNCTION _timescaledb_internal.set_peer_dist_id(uuid) SET SCHEMA _timescaledb_functions;
ALTER FUNCTION _timescaledb_internal.validate_as_data_node() SET SCHEMA _timescaledb_functions;
ALTER FUNCTION _timescaledb_internal.show_connection_cache() SET SCHEMA _timescaledb_functions;

ALTER FUNCTION _timescaledb_internal.ping_data_node(name, interval) SET SCHEMA _timescaledb_functions;
ALTER FUNCTION _timescaledb_internal.remote_txn_heal_data_node(oid) SET SCHEMA _timescaledb_functions;
6 changes: 6 additions & 0 deletions sql/updates/reverse-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ ALTER FUNCTION _timescaledb_functions.interval_to_usec(interval) SET SCHEMA _tim
ALTER FUNCTION _timescaledb_functions.time_to_internal(anyelement) SET SCHEMA _timescaledb_internal;
ALTER FUNCTION _timescaledb_functions.subtract_integer_from_now(regclass, bigint) SET SCHEMA _timescaledb_internal;

ALTER FUNCTION _timescaledb_functions.set_dist_id(uuid) SET SCHEMA _timescaledb_internal;
ALTER FUNCTION _timescaledb_functions.set_peer_dist_id(uuid) SET SCHEMA _timescaledb_internal;
ALTER FUNCTION _timescaledb_functions.validate_as_data_node() SET SCHEMA _timescaledb_internal;
ALTER FUNCTION _timescaledb_functions.show_connection_cache() SET SCHEMA _timescaledb_internal;
ALTER FUNCTION _timescaledb_functions.ping_data_node(name, interval) SET SCHEMA _timescaledb_internal;
ALTER FUNCTION _timescaledb_functions.remote_txn_heal_data_node(oid) SET SCHEMA _timescaledb_internal;
4 changes: 2 additions & 2 deletions tsl/src/data_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ static void
data_node_validate_as_data_node(TSConnection *conn)
{
PGresult *res =
remote_connection_exec(conn, "SELECT _timescaledb_internal.validate_as_data_node()");
remote_connection_exec(conn, "SELECT _timescaledb_functions.validate_as_data_node()");

if (PQresultStatus(res) != PGRES_TUPLES_OK)
ereport(ERROR,
Expand Down Expand Up @@ -588,7 +588,7 @@ add_distributed_id_to_data_node(TSConnection *conn)
{
Datum id_string = DirectFunctionCall1(uuid_out, dist_util_get_id());
PGresult *res = remote_connection_queryf_ok(conn,
"SELECT _timescaledb_internal.set_dist_id('%s')",
"SELECT _timescaledb_functions.set_dist_id('%s')",
DatumGetCString(id_string));
remote_result_close(res);
}
Expand Down
2 changes: 1 addition & 1 deletion tsl/src/remote/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,7 @@ remote_connection_set_peer_dist_id(TSConnection *conn)
bool success = true;

res = remote_connection_execf(conn,
"SELECT * FROM _timescaledb_internal.set_peer_dist_id('%s')",
"SELECT * FROM _timescaledb_functions.set_peer_dist_id('%s')",
DatumGetCString(id_string));
success = PQresultStatus(res) == PGRES_TUPLES_OK;
PQclear(res);
Expand Down
24 changes: 12 additions & 12 deletions tsl/test/expected/data_node.out
Original file line number Diff line number Diff line change
Expand Up @@ -668,26 +668,26 @@ SELECT * FROM _timescaledb_catalog.chunk_data_node;
----------+---------------+-----------
(0 rows)

SELECT * FROM _timescaledb_internal.ping_data_node('data_node_1');
SELECT * FROM _timescaledb_functions.ping_data_node('data_node_1');
ping_data_node
----------------
t
(1 row)

-- Ensure timeout returned by argument
SELECT * FROM _timescaledb_internal.ping_data_node('data_node_1', interval '0s');
SELECT * FROM _timescaledb_functions.ping_data_node('data_node_1', interval '0s');
ping_data_node
----------------
f
(1 row)

SELECT * FROM _timescaledb_internal.ping_data_node('data_node_1', interval '3s');
SELECT * FROM _timescaledb_functions.ping_data_node('data_node_1', interval '3s');
ping_data_node
----------------
t
(1 row)

SELECT * FROM _timescaledb_internal.ping_data_node('data_node_1', interval '1 day');
SELECT * FROM _timescaledb_functions.ping_data_node('data_node_1', interval '1 day');
ping_data_node
----------------
t
Expand All @@ -708,13 +708,13 @@ NOTICE: adding not-null constraint to column "time"

\set ON_ERROR_STOP 0
-- Throw ERROR for non-existing data node
SELECT * FROM _timescaledb_internal.ping_data_node('data_node_123456789');
SELECT * FROM _timescaledb_functions.ping_data_node('data_node_123456789');
ERROR: server "data_node_123456789" does not exist
-- ERROR on NULL
SELECT * FROM _timescaledb_internal.ping_data_node(NULL);
SELECT * FROM _timescaledb_functions.ping_data_node(NULL);
ERROR: data node name cannot be NULL
-- ERROR when not passing TimescaleDB data node
SELECT * FROM _timescaledb_internal.ping_data_node('pg_data_node_1');
SELECT * FROM _timescaledb_functions.ping_data_node('pg_data_node_1');
ERROR: server "pg_data_node_1" does not exist
-- ERROR on attaching to non-distributed hypertable
SELECT * FROM attach_data_node('data_node_1', 'standalone');
Expand Down Expand Up @@ -1524,23 +1524,23 @@ RESET ROLE;
DROP DATABASE :DN_DBNAME_1;
CREATE DATABASE :DN_DBNAME_1 OWNER :ROLE_1;
\c :DN_DBNAME_1
CREATE SCHEMA _timescaledb_internal;
GRANT ALL ON SCHEMA _timescaledb_internal TO :ROLE_1;
CREATE FUNCTION _timescaledb_internal.set_dist_id(uuid UUID)
CREATE SCHEMA _timescaledb_functions;
GRANT ALL ON SCHEMA _timescaledb_functions TO :ROLE_1;
CREATE FUNCTION _timescaledb_functions.set_dist_id(uuid UUID)
RETURNS BOOL LANGUAGE PLPGSQL AS
$BODY$
BEGIN
RETURN true;
END
$BODY$;
CREATE FUNCTION _timescaledb_internal.set_peer_dist_id(uuid UUID)
CREATE FUNCTION _timescaledb_functions.set_peer_dist_id(uuid UUID)
RETURNS BOOL LANGUAGE PLPGSQL AS
$BODY$
BEGIN
RETURN true;
END
$BODY$;
CREATE FUNCTION _timescaledb_internal.validate_as_data_node()
CREATE FUNCTION _timescaledb_functions.validate_as_data_node()
RETURNS BOOL LANGUAGE PLPGSQL AS
$BODY$
BEGIN
Expand Down
4 changes: 2 additions & 2 deletions tsl/test/expected/dist_commands.out
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ SELECT is_access_node_session_on_data_node();

-- Ensure peer dist id is already set and can be set only once
\set ON_ERROR_STOP 0
SELECT * FROM test.remote_exec(ARRAY[:'DATA_NODE_1'], $$ SELECT * FROM _timescaledb_internal.set_peer_dist_id('77348176-09da-4a80-bc78-e31bdf5e63ec'); $$);
NOTICE: [db_dist_commands_1]: SELECT * FROM _timescaledb_internal.set_peer_dist_id('77348176-09da-4a80-bc78-e31bdf5e63ec')
SELECT * FROM test.remote_exec(ARRAY[:'DATA_NODE_1'], $$ SELECT * FROM _timescaledb_functions.set_peer_dist_id('77348176-09da-4a80-bc78-e31bdf5e63ec'); $$);
NOTICE: [db_dist_commands_1]: SELECT * FROM _timescaledb_functions.set_peer_dist_id('77348176-09da-4a80-bc78-e31bdf5e63ec')
ERROR: [db_dist_commands_1]: distributed peer ID already set
\set ON_ERROR_STOP 1
-- Repeat is_access_node_session_on_data_node() test again, but this time using connections openned from
Expand Down
4 changes: 2 additions & 2 deletions tsl/test/expected/remote_connection_cache.out
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ NOTICE: adding not-null constraint to column "time"
INSERT INTO testtable VALUES ('2021-09-19', 1, 13.2);
-- Should show valid connections for ROLE_1
SELECT node_name, user_name, invalidated
FROM _timescaledb_internal.show_connection_cache()
FROM _timescaledb_functions.show_connection_cache()
WHERE user_name=:'ROLE_1'
ORDER BY 1,2;
node_name | user_name | invalidated
Expand All @@ -68,7 +68,7 @@ BEGIN;
-- fetch.
ALTER ROLE :ROLE_1 RENAME TO bob;
SELECT node_name, user_name, invalidated
FROM _timescaledb_internal.show_connection_cache()
FROM _timescaledb_functions.show_connection_cache()
WHERE user_name='bob'
ORDER BY 1,2;
node_name | user_name | invalidated
Expand Down
Loading

0 comments on commit 4256009

Please sign in to comment.