From: | kaido vaikla <kaido(dot)vaikla(at)gmail(dot)com> |
---|---|
To: | pgsql-admin(at)postgresql(dot)org |
Subject: | event_trigger, 'DROP SCHEMA' |
Date: | 2024-07-26 17:32:40 |
Message-ID: | CA+427g8LVfbbv5K36SxE8kK-_dqn4XhJTiKSx6XkLZ6givSo8Q@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-admin |
Hi,
Goal is, drop schema should trigger drop role.
It does not work if default privileges are altered.
*Function and event trigger:*
kvpg2=# CREATE or replace FUNCTION trigger_after_drop_schema() RETURNS
event_trigger
LANGUAGE plpgsql AS $$
DECLARE
r RECORD;
BEGIN
FOR r IN SELECT * FROM pg_event_trigger_dropped_objects()
LOOP
execute format ('drop role IF EXISTS r_%I', r.object_name);
END LOOP;
END;
$$;
CREATE FUNCTION
kvpg2=# CREATE EVENT TRIGGER after_drop_schema
ON sql_drop
WHEN TAG IN ('DROP SCHEMA')
EXECUTE PROCEDURE trigger_after_drop_schema();
CREATE EVENT TRIGGER
*Example 1* (just user)
kvpg2=# create schema test;
CREATE SCHEMA
kvpg2=# create role r_test;
CREATE ROLE
kvpg2=# drop schema test;
DROP SCHEMA
kvpg2=# \du r_test
List of roles
Role name | Attributes
-----------+------------
*Example 2* (user with usage on schema)
kvpg2=# create schema test;
CREATE SCHEMA
kvpg2=# create role r_test;
CREATE ROLE
kvpg2=# grant usage on schema test to r_test ;
GRANT
kvpg2=# drop schema test;
DROP SCHEMA
kvpg2=# \du r_test
List of roles
Role name | Attributes
-----------+------------
*Example 3* (user with usage on schema and altered default privileges)
kvpg2=# create schema test;
CREATE SCHEMA
kvpg2=# create role r_test;
CREATE ROLE
kvpg2=# grant usage on schema test to r_test ;
GRANT
kvpg2=# alter default privileges for role postgres in schema test grant
select on tables to r_test;
ALTER DEFAULT PRIVILEGES
kvpg2=# drop schema test;
ERROR: null values cannot be formatted as an SQL identifier
CONTEXT: PL/pgSQL function trigger_after_drop_schema() line 7 at EXECUTE
Question, what i do wrong?
br
Kaido
From | Date | Subject | |
---|---|---|---|
Next Message | Zaid Shabbir | 2024-07-26 17:33:52 | Re: pg_restore but for full user and roles, etc |
Previous Message | Wells Oliver | 2024-07-26 17:28:56 | Re: pg_restore but for full user and roles, etc |