ALTER STATISTICS .. OWNER TO CURRENT_ROLE (#5225)

(cherry picked from commit 42322caf90ca094777aa01376e02d1187afc1560)
pull/5209/head
Ahmet Gedemenli 2021-09-02 11:58:40 +03:00 committed by Sait Talha Nisanci
parent 82a3b20fb3
commit 2b263f9a2a
3 changed files with 82 additions and 1 deletions

View File

@ -1275,6 +1275,8 @@ simple_quote_literal(StringInfo buf, const char *val)
* *
* CURRENT_USER - resolved to the user name of the current role being used * CURRENT_USER - resolved to the user name of the current role being used
* SESSION_USER - resolved to the user name of the user that opened the session * SESSION_USER - resolved to the user name of the user that opened the session
* CURRENT_ROLE - same as CURRENT_USER, resolved to the user name of the current role being used
* Postgres treats CURRENT_ROLE is equivalent to CURRENT_USER, and we follow the same approach.
* *
* withQuoteIdentifier is used, because if the results will be used in a query the quotes are needed but if not there * withQuoteIdentifier is used, because if the results will be used in a query the quotes are needed but if not there
* should not be extra quotes. * should not be extra quotes.
@ -1290,7 +1292,9 @@ RoleSpecString(RoleSpec *spec, bool withQuoteIdentifier)
quote_identifier(spec->rolename) : quote_identifier(spec->rolename) :
spec->rolename; spec->rolename;
} }
#if PG_VERSION_NUM >= PG_VERSION_14
case ROLESPEC_CURRENT_ROLE:
#endif
case ROLESPEC_CURRENT_USER: case ROLESPEC_CURRENT_USER:
{ {
return withQuoteIdentifier ? return withQuoteIdentifier ?

View File

@ -320,5 +320,62 @@ NOTICE: dropping metadata on the node (localhost,57638)
(1 row) (1 row)
-- ALTER STATISTICS .. OWNER TO CURRENT_ROLE
CREATE TABLE st1 (a int, b int);
CREATE STATISTICS role_s1 ON a, b FROM st1;
SELECT create_distributed_table('st1','a');
create_distributed_table
---------------------------------------------------------------------
(1 row)
ALTER STATISTICS role_s1 OWNER TO CURRENT_ROLE;
SET citus.enable_ddl_propagation TO off; -- for enterprise
CREATE ROLE role_1 WITH LOGIN SUPERUSER;
NOTICE: not propagating CREATE ROLE/USER commands to worker nodes
HINT: Connect to worker nodes directly to manually create all necessary users and roles.
SET citus.enable_ddl_propagation TO on;
SELECT run_command_on_workers($$CREATE ROLE role_1 WITH LOGIN SUPERUSER;$$);
run_command_on_workers
---------------------------------------------------------------------
(localhost,57637,t,"CREATE ROLE")
(localhost,57638,t,"CREATE ROLE")
(2 rows)
ALTER STATISTICS role_s1 OWNER TO CURRENT_ROLE;
SELECT run_command_on_workers($$SELECT rolname FROM pg_roles WHERE oid IN (SELECT stxowner FROM pg_statistic_ext WHERE stxname LIKE 'role\_s1%');$$);
run_command_on_workers
---------------------------------------------------------------------
(localhost,57637,t,postgres)
(localhost,57638,t,postgres)
(2 rows)
SET ROLE role_1;
ALTER STATISTICS role_s1 OWNER TO CURRENT_ROLE;
SELECT run_command_on_workers($$SELECT rolname FROM pg_roles WHERE oid IN (SELECT stxowner FROM pg_statistic_ext WHERE stxname LIKE 'role\_s1%');$$);
run_command_on_workers
---------------------------------------------------------------------
(localhost,57637,t,role_1)
(localhost,57638,t,role_1)
(2 rows)
SET ROLE postgres;
ALTER STATISTICS role_s1 OWNER TO CURRENT_USER;
SELECT run_command_on_workers($$SELECT rolname FROM pg_roles WHERE oid IN (SELECT stxowner FROM pg_statistic_ext WHERE stxname LIKE 'role\_s1%');$$);
run_command_on_workers
---------------------------------------------------------------------
(localhost,57637,t,postgres)
(localhost,57638,t,postgres)
(2 rows)
SET ROLE to NONE;
ALTER STATISTICS role_s1 OWNER TO CURRENT_ROLE;
SELECT run_command_on_workers($$SELECT rolname FROM pg_roles WHERE oid IN (SELECT stxowner FROM pg_statistic_ext WHERE stxname LIKE 'role\_s1%');$$);
run_command_on_workers
---------------------------------------------------------------------
(localhost,57637,t,postgres)
(localhost,57638,t,postgres)
(2 rows)
set client_min_messages to error; set client_min_messages to error;
drop schema pg14 cascade; drop schema pg14 cascade;

View File

@ -123,5 +123,25 @@ RESET client_min_messages;
SELECT stop_metadata_sync_to_node('localhost', :worker_1_port); SELECT stop_metadata_sync_to_node('localhost', :worker_1_port);
SELECT stop_metadata_sync_to_node('localhost', :worker_2_port); SELECT stop_metadata_sync_to_node('localhost', :worker_2_port);
-- ALTER STATISTICS .. OWNER TO CURRENT_ROLE
CREATE TABLE st1 (a int, b int);
CREATE STATISTICS role_s1 ON a, b FROM st1;
SELECT create_distributed_table('st1','a');
ALTER STATISTICS role_s1 OWNER TO CURRENT_ROLE;
SET citus.enable_ddl_propagation TO off; -- for enterprise
CREATE ROLE role_1 WITH LOGIN SUPERUSER;
SET citus.enable_ddl_propagation TO on;
SELECT run_command_on_workers($$CREATE ROLE role_1 WITH LOGIN SUPERUSER;$$);
ALTER STATISTICS role_s1 OWNER TO CURRENT_ROLE;
SELECT run_command_on_workers($$SELECT rolname FROM pg_roles WHERE oid IN (SELECT stxowner FROM pg_statistic_ext WHERE stxname LIKE 'role\_s1%');$$);
SET ROLE role_1;
ALTER STATISTICS role_s1 OWNER TO CURRENT_ROLE;
SELECT run_command_on_workers($$SELECT rolname FROM pg_roles WHERE oid IN (SELECT stxowner FROM pg_statistic_ext WHERE stxname LIKE 'role\_s1%');$$);
SET ROLE postgres;
ALTER STATISTICS role_s1 OWNER TO CURRENT_USER;
SELECT run_command_on_workers($$SELECT rolname FROM pg_roles WHERE oid IN (SELECT stxowner FROM pg_statistic_ext WHERE stxname LIKE 'role\_s1%');$$);
SET ROLE to NONE;
ALTER STATISTICS role_s1 OWNER TO CURRENT_ROLE;
SELECT run_command_on_workers($$SELECT rolname FROM pg_roles WHERE oid IN (SELECT stxowner FROM pg_statistic_ext WHERE stxname LIKE 'role\_s1%');$$);
set client_min_messages to error; set client_min_messages to error;
drop schema pg14 cascade; drop schema pg14 cascade;