1 -- should fail, return type mismatch
2 create event trigger regress_event_trigger
4 execute procedure pg_backend_pid();
5 ERROR: function pg_backend_pid must return type event_trigger
7 create function test_event_trigger() returns event_trigger as $$
9 RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
12 -- should fail, can't call it as a plain function
13 SELECT test_event_trigger();
14 ERROR: trigger functions can only be called as triggers
15 CONTEXT: compilation of PL/pgSQL function "test_event_trigger" near line 1
16 -- should fail, event triggers cannot have declared arguments
17 create function test_event_trigger_arg(name text)
18 returns event_trigger as $$ BEGIN RETURN 1; END $$ language plpgsql;
19 ERROR: event trigger functions cannot have declared arguments
20 CONTEXT: compilation of PL/pgSQL function "test_event_trigger_arg" near line 1
21 -- should fail, SQL functions cannot be event triggers
22 create function test_event_trigger_sql() returns event_trigger as $$
23 SELECT 1 $$ language sql;
24 ERROR: SQL functions cannot return type event_trigger
25 -- should fail, no elephant_bootstrap entry point
26 create event trigger regress_event_trigger on elephant_bootstrap
27 execute procedure test_event_trigger();
28 ERROR: unrecognized event name "elephant_bootstrap"
30 create event trigger regress_event_trigger on ddl_command_start
31 execute procedure test_event_trigger();
33 create event trigger regress_event_trigger_end on ddl_command_end
34 execute function test_event_trigger();
35 -- should fail, food is not a valid filter variable
36 create event trigger regress_event_trigger2 on ddl_command_start
37 when food in ('sandwich')
38 execute procedure test_event_trigger();
39 ERROR: unrecognized filter variable "food"
40 -- should fail, sandwich is not a valid command tag
41 create event trigger regress_event_trigger2 on ddl_command_start
42 when tag in ('sandwich')
43 execute procedure test_event_trigger();
44 ERROR: filter value "sandwich" not recognized for filter variable "tag"
45 -- should fail, create skunkcabbage is not a valid command tag
46 create event trigger regress_event_trigger2 on ddl_command_start
47 when tag in ('create table', 'create skunkcabbage')
48 execute procedure test_event_trigger();
49 ERROR: filter value "create skunkcabbage" not recognized for filter variable "tag"
50 -- should fail, can't have event triggers on event triggers
51 create event trigger regress_event_trigger2 on ddl_command_start
52 when tag in ('DROP EVENT TRIGGER')
53 execute procedure test_event_trigger();
54 ERROR: event triggers are not supported for DROP EVENT TRIGGER
55 -- should fail, can't have event triggers on global objects
56 create event trigger regress_event_trigger2 on ddl_command_start
57 when tag in ('CREATE ROLE')
58 execute procedure test_event_trigger();
59 ERROR: event triggers are not supported for CREATE ROLE
60 -- should fail, can't have event triggers on global objects
61 create event trigger regress_event_trigger2 on ddl_command_start
62 when tag in ('CREATE DATABASE')
63 execute procedure test_event_trigger();
64 ERROR: event triggers are not supported for CREATE DATABASE
65 -- should fail, can't have event triggers on global objects
66 create event trigger regress_event_trigger2 on ddl_command_start
67 when tag in ('CREATE TABLESPACE')
68 execute procedure test_event_trigger();
69 ERROR: event triggers are not supported for CREATE TABLESPACE
70 -- should fail, can't have same filter variable twice
71 create event trigger regress_event_trigger2 on ddl_command_start
72 when tag in ('create table') and tag in ('CREATE FUNCTION')
73 execute procedure test_event_trigger();
74 ERROR: filter variable "tag" specified more than once
75 -- should fail, can't have arguments
76 create event trigger regress_event_trigger2 on ddl_command_start
77 execute procedure test_event_trigger('argument not allowed');
78 ERROR: syntax error at or near "'argument not allowed'"
79 LINE 2: execute procedure test_event_trigger('argument not allowe...
82 create event trigger regress_event_trigger2 on ddl_command_start
83 when tag in ('create table', 'CREATE FUNCTION')
84 execute procedure test_event_trigger();
86 comment on event trigger regress_event_trigger is 'test comment';
87 -- drop as non-superuser should fail
88 create role regress_evt_user;
89 set role regress_evt_user;
90 create event trigger regress_event_trigger_noperms on ddl_command_start
91 execute procedure test_event_trigger();
92 ERROR: permission denied to create event trigger "regress_event_trigger_noperms"
93 HINT: Must be superuser to create an event trigger.
95 -- test enabling and disabling
96 alter event trigger regress_event_trigger disable;
97 -- fires _trigger2 and _trigger_end should fire, but not _trigger
98 create table event_trigger_fire1 (a int);
99 NOTICE: test_event_trigger: ddl_command_start CREATE TABLE
100 NOTICE: test_event_trigger: ddl_command_end CREATE TABLE
101 alter event trigger regress_event_trigger enable;
102 set session_replication_role = replica;
104 create table event_trigger_fire2 (a int);
105 alter event trigger regress_event_trigger enable replica;
106 -- fires only _trigger
107 create table event_trigger_fire3 (a int);
108 NOTICE: test_event_trigger: ddl_command_start CREATE TABLE
109 alter event trigger regress_event_trigger enable always;
110 -- fires only _trigger
111 create table event_trigger_fire4 (a int);
112 NOTICE: test_event_trigger: ddl_command_start CREATE TABLE
113 reset session_replication_role;
115 create table event_trigger_fire5 (a int);
116 NOTICE: test_event_trigger: ddl_command_start CREATE TABLE
117 NOTICE: test_event_trigger: ddl_command_start CREATE TABLE
118 NOTICE: test_event_trigger: ddl_command_end CREATE TABLE
119 -- non-top-level command
120 create function f1() returns int
124 create table event_trigger_fire6 (a int);
127 NOTICE: test_event_trigger: ddl_command_start CREATE FUNCTION
128 NOTICE: test_event_trigger: ddl_command_start CREATE FUNCTION
129 NOTICE: test_event_trigger: ddl_command_end CREATE FUNCTION
131 NOTICE: test_event_trigger: ddl_command_start CREATE TABLE
132 NOTICE: test_event_trigger: ddl_command_start CREATE TABLE
133 NOTICE: test_event_trigger: ddl_command_end CREATE TABLE
139 -- non-top-level command
140 create procedure p1()
144 create table event_trigger_fire7 (a int);
146 NOTICE: test_event_trigger: ddl_command_start CREATE PROCEDURE
147 NOTICE: test_event_trigger: ddl_command_end CREATE PROCEDURE
149 NOTICE: test_event_trigger: ddl_command_start CREATE TABLE
150 NOTICE: test_event_trigger: ddl_command_start CREATE TABLE
151 NOTICE: test_event_trigger: ddl_command_end CREATE TABLE
153 alter event trigger regress_event_trigger disable;
154 drop table event_trigger_fire2, event_trigger_fire3, event_trigger_fire4, event_trigger_fire5, event_trigger_fire6, event_trigger_fire7;
155 NOTICE: test_event_trigger: ddl_command_end DROP TABLE
156 drop routine f1(), p1();
157 NOTICE: test_event_trigger: ddl_command_end DROP ROUTINE
158 -- regress_event_trigger_end should fire on these commands
159 grant all on table event_trigger_fire1 to public;
160 NOTICE: test_event_trigger: ddl_command_end GRANT
161 comment on table event_trigger_fire1 is 'here is a comment';
162 NOTICE: test_event_trigger: ddl_command_end COMMENT
163 revoke all on table event_trigger_fire1 from public;
164 NOTICE: test_event_trigger: ddl_command_end REVOKE
165 drop table event_trigger_fire1;
166 NOTICE: test_event_trigger: ddl_command_end DROP TABLE
167 create foreign data wrapper useless;
168 NOTICE: test_event_trigger: ddl_command_end CREATE FOREIGN DATA WRAPPER
169 create server useless_server foreign data wrapper useless;
170 NOTICE: test_event_trigger: ddl_command_end CREATE SERVER
171 create user mapping for regress_evt_user server useless_server;
172 NOTICE: test_event_trigger: ddl_command_end CREATE USER MAPPING
173 alter default privileges for role regress_evt_user
174 revoke delete on tables from regress_evt_user;
175 NOTICE: test_event_trigger: ddl_command_end ALTER DEFAULT PRIVILEGES
176 -- alter owner to non-superuser should fail
177 alter event trigger regress_event_trigger owner to regress_evt_user;
178 ERROR: permission denied to change owner of event trigger "regress_event_trigger"
179 HINT: The owner of an event trigger must be a superuser.
180 -- alter owner to superuser should work
181 alter role regress_evt_user superuser;
182 alter event trigger regress_event_trigger owner to regress_evt_user;
183 -- should fail, name collision
184 alter event trigger regress_event_trigger rename to regress_event_trigger2;
185 ERROR: event trigger "regress_event_trigger2" already exists
187 alter event trigger regress_event_trigger rename to regress_event_trigger3;
188 -- should fail, doesn't exist any more
189 drop event trigger regress_event_trigger;
190 ERROR: event trigger "regress_event_trigger" does not exist
191 -- should fail, regress_evt_user owns some objects
192 drop role regress_evt_user;
193 ERROR: role "regress_evt_user" cannot be dropped because some objects depend on it
194 DETAIL: owner of event trigger regress_event_trigger3
195 owner of user mapping for regress_evt_user on server useless_server
196 owner of default privileges on new relations belonging to role regress_evt_user
197 -- cleanup before next test
198 -- these are all OK; the second one should emit a NOTICE
199 drop event trigger if exists regress_event_trigger2;
200 drop event trigger if exists regress_event_trigger2;
201 NOTICE: event trigger "regress_event_trigger2" does not exist, skipping
202 drop event trigger regress_event_trigger3;
203 drop event trigger regress_event_trigger_end;
204 -- test support for dropped objects
205 CREATE SCHEMA schema_one authorization regress_evt_user;
206 CREATE SCHEMA schema_two authorization regress_evt_user;
207 CREATE SCHEMA audit_tbls authorization regress_evt_user;
208 CREATE TEMP TABLE a_temp_tbl ();
209 SET SESSION AUTHORIZATION regress_evt_user;
210 CREATE TABLE schema_one.table_one(a int);
211 CREATE TABLE schema_one."table two"(a int);
212 CREATE TABLE schema_one.table_three(a int);
213 CREATE TABLE audit_tbls.schema_one_table_two(the_value text);
214 CREATE TABLE schema_two.table_two(a int);
215 CREATE TABLE schema_two.table_three(a int, b text);
216 CREATE TABLE audit_tbls.schema_two_table_three(the_value text);
217 CREATE OR REPLACE FUNCTION schema_two.add(int, int) RETURNS int LANGUAGE plpgsql
219 AS $$ BEGIN RETURN coalesce($1,0) + coalesce($2,0); END; $$;
220 CREATE AGGREGATE schema_two.newton
221 (BASETYPE = int, SFUNC = schema_two.add, STYPE = int);
222 RESET SESSION AUTHORIZATION;
223 CREATE TABLE undroppable_objs (
227 INSERT INTO undroppable_objs VALUES
228 ('table', 'schema_one.table_three'),
229 ('table', 'audit_tbls.schema_two_table_three');
230 CREATE TABLE dropped_objects (
235 -- This tests errors raised within event triggers; the one in audit_tbls
236 -- uses 2nd-level recursive invocation via test_evtrig_dropped_objects().
237 CREATE OR REPLACE FUNCTION undroppable() RETURNS event_trigger
238 LANGUAGE plpgsql AS $$
242 PERFORM 1 FROM pg_tables WHERE tablename = 'undroppable_objs';
244 RAISE NOTICE 'table undroppable_objs not found, skipping';
248 SELECT * FROM pg_event_trigger_dropped_objects() JOIN
249 undroppable_objs USING (object_type, object_identity)
251 RAISE EXCEPTION 'object % of type % cannot be dropped',
252 obj.object_identity, obj.object_type;
256 CREATE EVENT TRIGGER undroppable ON sql_drop
257 EXECUTE PROCEDURE undroppable();
258 CREATE OR REPLACE FUNCTION test_evtrig_dropped_objects() RETURNS event_trigger
259 LANGUAGE plpgsql AS $$
263 FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
265 IF obj.object_type = 'table' THEN
266 EXECUTE format('DROP TABLE IF EXISTS audit_tbls.%I',
267 format('%s_%s', obj.schema_name, obj.object_name));
270 INSERT INTO dropped_objects
271 (type, schema, object) VALUES
272 (obj.object_type, obj.schema_name, obj.object_identity);
276 CREATE EVENT TRIGGER regress_event_trigger_drop_objects ON sql_drop
277 WHEN TAG IN ('drop table', 'drop function', 'drop view',
278 'drop owned', 'drop schema', 'alter table')
279 EXECUTE PROCEDURE test_evtrig_dropped_objects();
280 ALTER TABLE schema_one.table_one DROP COLUMN a;
281 DROP SCHEMA schema_one, schema_two CASCADE;
282 NOTICE: drop cascades to 7 other objects
283 DETAIL: drop cascades to table schema_two.table_two
284 drop cascades to table schema_two.table_three
285 drop cascades to function schema_two.add(integer,integer)
286 drop cascades to function schema_two.newton(integer)
287 drop cascades to table schema_one.table_one
288 drop cascades to table schema_one."table two"
289 drop cascades to table schema_one.table_three
290 NOTICE: table "schema_two_table_two" does not exist, skipping
291 NOTICE: table "audit_tbls_schema_two_table_three" does not exist, skipping
292 ERROR: object audit_tbls.schema_two_table_three of type table cannot be dropped
293 CONTEXT: PL/pgSQL function undroppable() line 14 at RAISE
294 SQL statement "DROP TABLE IF EXISTS audit_tbls.schema_two_table_three"
295 PL/pgSQL function test_evtrig_dropped_objects() line 8 at EXECUTE
296 DELETE FROM undroppable_objs WHERE object_identity = 'audit_tbls.schema_two_table_three';
297 DROP SCHEMA schema_one, schema_two CASCADE;
298 NOTICE: drop cascades to 7 other objects
299 DETAIL: drop cascades to table schema_two.table_two
300 drop cascades to table schema_two.table_three
301 drop cascades to function schema_two.add(integer,integer)
302 drop cascades to function schema_two.newton(integer)
303 drop cascades to table schema_one.table_one
304 drop cascades to table schema_one."table two"
305 drop cascades to table schema_one.table_three
306 NOTICE: table "schema_two_table_two" does not exist, skipping
307 NOTICE: table "audit_tbls_schema_two_table_three" does not exist, skipping
308 NOTICE: table "schema_one_table_one" does not exist, skipping
309 NOTICE: table "schema_one_table two" does not exist, skipping
310 NOTICE: table "schema_one_table_three" does not exist, skipping
311 ERROR: object schema_one.table_three of type table cannot be dropped
312 CONTEXT: PL/pgSQL function undroppable() line 14 at RAISE
313 DELETE FROM undroppable_objs WHERE object_identity = 'schema_one.table_three';
314 DROP SCHEMA schema_one, schema_two CASCADE;
315 NOTICE: drop cascades to 7 other objects
316 DETAIL: drop cascades to table schema_two.table_two
317 drop cascades to table schema_two.table_three
318 drop cascades to function schema_two.add(integer,integer)
319 drop cascades to function schema_two.newton(integer)
320 drop cascades to table schema_one.table_one
321 drop cascades to table schema_one."table two"
322 drop cascades to table schema_one.table_three
323 NOTICE: table "schema_two_table_two" does not exist, skipping
324 NOTICE: table "audit_tbls_schema_two_table_three" does not exist, skipping
325 NOTICE: table "schema_one_table_one" does not exist, skipping
326 NOTICE: table "schema_one_table two" does not exist, skipping
327 NOTICE: table "schema_one_table_three" does not exist, skipping
328 SELECT * FROM dropped_objects WHERE schema IS NULL OR schema <> 'pg_toast';
329 type | schema | object
330 --------------+------------+-------------------------------------
331 table column | schema_one | schema_one.table_one.a
332 schema | | schema_two
333 table | schema_two | schema_two.table_two
334 type | schema_two | schema_two.table_two
335 type | schema_two | schema_two.table_two[]
336 table | audit_tbls | audit_tbls.schema_two_table_three
337 type | audit_tbls | audit_tbls.schema_two_table_three
338 type | audit_tbls | audit_tbls.schema_two_table_three[]
339 table | schema_two | schema_two.table_three
340 type | schema_two | schema_two.table_three
341 type | schema_two | schema_two.table_three[]
342 function | schema_two | schema_two.add(integer,integer)
343 aggregate | schema_two | schema_two.newton(integer)
344 schema | | schema_one
345 table | schema_one | schema_one.table_one
346 type | schema_one | schema_one.table_one
347 type | schema_one | schema_one.table_one[]
348 table | schema_one | schema_one."table two"
349 type | schema_one | schema_one."table two"
350 type | schema_one | schema_one."table two"[]
351 table | schema_one | schema_one.table_three
352 type | schema_one | schema_one.table_three
353 type | schema_one | schema_one.table_three[]
356 DROP OWNED BY regress_evt_user;
357 NOTICE: schema "audit_tbls" does not exist, skipping
358 SELECT * FROM dropped_objects WHERE type = 'schema';
359 type | schema | object
360 --------+--------+------------
361 schema | | schema_two
362 schema | | schema_one
363 schema | | audit_tbls
366 DROP ROLE regress_evt_user;
367 DROP EVENT TRIGGER regress_event_trigger_drop_objects;
368 DROP EVENT TRIGGER undroppable;
369 -- Event triggers on relations.
370 CREATE OR REPLACE FUNCTION event_trigger_report_dropped()
371 RETURNS event_trigger
376 FOR r IN SELECT * from pg_event_trigger_dropped_objects()
378 IF NOT r.normal AND NOT r.original THEN
381 RAISE NOTICE 'NORMAL: orig=% normal=% istemp=% type=% identity=% name=% args=%',
382 r.original, r.normal, r.is_temporary, r.object_type,
383 r.object_identity, r.address_names, r.address_args;
386 CREATE EVENT TRIGGER regress_event_trigger_report_dropped ON sql_drop
387 EXECUTE PROCEDURE event_trigger_report_dropped();
388 CREATE OR REPLACE FUNCTION event_trigger_report_end()
389 RETURNS event_trigger
394 FOR r IN SELECT * FROM pg_event_trigger_ddl_commands()
396 RAISE NOTICE 'END: command_tag=% type=% identity=%',
397 r.command_tag, r.object_type, r.object_identity;
400 CREATE EVENT TRIGGER regress_event_trigger_report_end ON ddl_command_end
401 EXECUTE PROCEDURE event_trigger_report_end();
402 CREATE SCHEMA evttrig
403 CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL)
404 CREATE INDEX one_idx ON one (col_b)
405 CREATE TABLE two (col_c INTEGER CHECK (col_c > 0) REFERENCES one DEFAULT 42)
406 CREATE TABLE id (col_d int NOT NULL GENERATED ALWAYS AS IDENTITY);
407 NOTICE: END: command_tag=CREATE SCHEMA type=schema identity=evttrig
408 NOTICE: END: command_tag=CREATE SEQUENCE type=sequence identity=evttrig.one_col_a_seq
409 NOTICE: END: command_tag=CREATE SEQUENCE type=sequence identity=evttrig.one_col_c_seq
410 NOTICE: END: command_tag=CREATE TABLE type=table identity=evttrig.one
411 NOTICE: END: command_tag=CREATE INDEX type=index identity=evttrig.one_pkey
412 NOTICE: END: command_tag=ALTER SEQUENCE type=sequence identity=evttrig.one_col_a_seq
413 NOTICE: END: command_tag=ALTER SEQUENCE type=sequence identity=evttrig.one_col_c_seq
414 NOTICE: END: command_tag=CREATE TABLE type=table identity=evttrig.two
415 NOTICE: END: command_tag=ALTER TABLE type=table identity=evttrig.two
416 NOTICE: END: command_tag=CREATE SEQUENCE type=sequence identity=evttrig.id_col_d_seq
417 NOTICE: END: command_tag=CREATE TABLE type=table identity=evttrig.id
418 NOTICE: END: command_tag=ALTER SEQUENCE type=sequence identity=evttrig.id_col_d_seq
419 NOTICE: END: command_tag=CREATE INDEX type=index identity=evttrig.one_idx
420 -- Partitioned tables with a partitioned index
421 CREATE TABLE evttrig.parted (
423 PARTITION BY RANGE (id);
424 NOTICE: END: command_tag=CREATE TABLE type=table identity=evttrig.parted
425 NOTICE: END: command_tag=CREATE INDEX type=index identity=evttrig.parted_pkey
426 CREATE TABLE evttrig.part_1_10 PARTITION OF evttrig.parted (id)
427 FOR VALUES FROM (1) TO (10);
428 NOTICE: END: command_tag=CREATE TABLE type=table identity=evttrig.part_1_10
429 CREATE TABLE evttrig.part_10_20 PARTITION OF evttrig.parted (id)
430 FOR VALUES FROM (10) TO (20) PARTITION BY RANGE (id);
431 NOTICE: END: command_tag=CREATE TABLE type=table identity=evttrig.part_10_20
432 CREATE TABLE evttrig.part_10_15 PARTITION OF evttrig.part_10_20 (id)
433 FOR VALUES FROM (10) TO (15);
434 NOTICE: END: command_tag=CREATE TABLE type=table identity=evttrig.part_10_15
435 CREATE TABLE evttrig.part_15_20 PARTITION OF evttrig.part_10_20 (id)
436 FOR VALUES FROM (15) TO (20);
437 NOTICE: END: command_tag=CREATE TABLE type=table identity=evttrig.part_15_20
438 ALTER TABLE evttrig.two DROP COLUMN col_c;
439 NOTICE: NORMAL: orig=t normal=f istemp=f type=table column identity=evttrig.two.col_c name={evttrig,two,col_c} args={}
440 NOTICE: NORMAL: orig=f normal=t istemp=f type=table constraint identity=two_col_c_check on evttrig.two name={evttrig,two,two_col_c_check} args={}
441 NOTICE: END: command_tag=ALTER TABLE type=table identity=evttrig.two
442 ALTER TABLE evttrig.one ALTER COLUMN col_b DROP DEFAULT;
443 NOTICE: NORMAL: orig=t normal=f istemp=f type=default value identity=for evttrig.one.col_b name={evttrig,one,col_b} args={}
444 NOTICE: END: command_tag=ALTER TABLE type=table identity=evttrig.one
445 ALTER TABLE evttrig.one DROP CONSTRAINT one_pkey;
446 NOTICE: NORMAL: orig=t normal=f istemp=f type=table constraint identity=one_pkey on evttrig.one name={evttrig,one,one_pkey} args={}
447 NOTICE: END: command_tag=ALTER TABLE type=table identity=evttrig.one
448 ALTER TABLE evttrig.one DROP COLUMN col_c;
449 NOTICE: NORMAL: orig=t normal=f istemp=f type=table column identity=evttrig.one.col_c name={evttrig,one,col_c} args={}
450 NOTICE: NORMAL: orig=f normal=t istemp=f type=default value identity=for evttrig.one.col_c name={evttrig,one,col_c} args={}
451 NOTICE: END: command_tag=ALTER TABLE type=table identity=evttrig.one
452 ALTER TABLE evttrig.id ALTER COLUMN col_d SET DATA TYPE bigint;
453 NOTICE: END: command_tag=ALTER SEQUENCE type=sequence identity=evttrig.id_col_d_seq
454 NOTICE: END: command_tag=ALTER TABLE type=table identity=evttrig.id
455 ALTER TABLE evttrig.id ALTER COLUMN col_d DROP IDENTITY,
456 ALTER COLUMN col_d SET DATA TYPE int;
457 NOTICE: END: command_tag=ALTER TABLE type=table identity=evttrig.id
458 DROP INDEX evttrig.one_idx;
459 NOTICE: NORMAL: orig=t normal=f istemp=f type=index identity=evttrig.one_idx name={evttrig,one_idx} args={}
460 DROP SCHEMA evttrig CASCADE;
461 NOTICE: drop cascades to 4 other objects
462 DETAIL: drop cascades to table evttrig.one
463 drop cascades to table evttrig.two
464 drop cascades to table evttrig.id
465 drop cascades to table evttrig.parted
466 NOTICE: NORMAL: orig=t normal=f istemp=f type=schema identity=evttrig name={evttrig} args={}
467 NOTICE: NORMAL: orig=f normal=t istemp=f type=table identity=evttrig.one name={evttrig,one} args={}
468 NOTICE: NORMAL: orig=f normal=t istemp=f type=sequence identity=evttrig.one_col_a_seq name={evttrig,one_col_a_seq} args={}
469 NOTICE: NORMAL: orig=f normal=t istemp=f type=default value identity=for evttrig.one.col_a name={evttrig,one,col_a} args={}
470 NOTICE: NORMAL: orig=f normal=t istemp=f type=table identity=evttrig.two name={evttrig,two} args={}
471 NOTICE: NORMAL: orig=f normal=t istemp=f type=table identity=evttrig.id name={evttrig,id} args={}
472 NOTICE: NORMAL: orig=f normal=t istemp=f type=table identity=evttrig.parted name={evttrig,parted} args={}
473 NOTICE: NORMAL: orig=f normal=t istemp=f type=table identity=evttrig.part_1_10 name={evttrig,part_1_10} args={}
474 NOTICE: NORMAL: orig=f normal=t istemp=f type=table identity=evttrig.part_10_20 name={evttrig,part_10_20} args={}
475 NOTICE: NORMAL: orig=f normal=t istemp=f type=table identity=evttrig.part_10_15 name={evttrig,part_10_15} args={}
476 NOTICE: NORMAL: orig=f normal=t istemp=f type=table identity=evttrig.part_15_20 name={evttrig,part_15_20} args={}
477 DROP TABLE a_temp_tbl;
478 NOTICE: NORMAL: orig=t normal=f istemp=t type=table identity=pg_temp.a_temp_tbl name={pg_temp,a_temp_tbl} args={}
479 DROP EVENT TRIGGER regress_event_trigger_report_dropped;
480 DROP EVENT TRIGGER regress_event_trigger_report_end;
481 -- only allowed from within an event trigger function, should fail
482 select pg_event_trigger_table_rewrite_oid();
483 ERROR: pg_event_trigger_table_rewrite_oid() can only be called in a table_rewrite event trigger function
484 -- test Table Rewrite Event Trigger
485 CREATE OR REPLACE FUNCTION test_evtrig_no_rewrite() RETURNS event_trigger
486 LANGUAGE plpgsql AS $$
488 RAISE EXCEPTION 'rewrites not allowed';
491 create event trigger no_rewrite_allowed on table_rewrite
492 execute procedure test_evtrig_no_rewrite();
493 create table rewriteme (id serial primary key, foo float, bar timestamptz);
494 insert into rewriteme
495 select x * 1.001 from generate_series(1, 500) as t(x);
496 alter table rewriteme alter column foo type numeric;
497 ERROR: rewrites not allowed
498 CONTEXT: PL/pgSQL function test_evtrig_no_rewrite() line 3 at RAISE
499 alter table rewriteme add column baz int default 0;
500 -- test with more than one reason to rewrite a single table
501 CREATE OR REPLACE FUNCTION test_evtrig_no_rewrite() RETURNS event_trigger
502 LANGUAGE plpgsql AS $$
504 RAISE NOTICE 'Table ''%'' is being rewritten (reason = %)',
505 pg_event_trigger_table_rewrite_oid()::regclass,
506 pg_event_trigger_table_rewrite_reason();
509 alter table rewriteme
510 add column onemore int default 0,
511 add column another int default -1,
512 alter column foo type numeric(10,4);
513 NOTICE: Table 'rewriteme' is being rewritten (reason = 4)
514 -- shouldn't trigger a table_rewrite event
515 alter table rewriteme alter column foo type numeric(12,4);
517 set timezone to 'UTC';
518 alter table rewriteme alter column bar type timestamp;
520 alter table rewriteme alter column bar type timestamptz;
521 set timezone to 'Europe/London';
522 alter table rewriteme alter column bar type timestamp; -- does rewrite
523 NOTICE: Table 'rewriteme' is being rewritten (reason = 4)
525 -- typed tables are rewritten when their type changes. Don't emit table
526 -- name, because firing order is not stable.
527 CREATE OR REPLACE FUNCTION test_evtrig_no_rewrite() RETURNS event_trigger
528 LANGUAGE plpgsql AS $$
530 RAISE NOTICE 'Table is being rewritten (reason = %)',
531 pg_event_trigger_table_rewrite_reason();
534 create type rewritetype as (a int);
535 create table rewritemetoo1 of rewritetype;
536 create table rewritemetoo2 of rewritetype;
537 alter type rewritetype alter attribute a type text cascade;
538 NOTICE: Table is being rewritten (reason = 4)
539 NOTICE: Table is being rewritten (reason = 4)
540 -- but this doesn't work
541 create table rewritemetoo3 (a rewritetype);
542 alter type rewritetype alter attribute a type varchar cascade;
543 ERROR: cannot alter type "rewritetype" because column "rewritemetoo3.a" uses it
544 drop table rewriteme;
545 drop event trigger no_rewrite_allowed;
546 drop function test_evtrig_no_rewrite();
547 -- test Row Security Event Trigger
548 RESET SESSION AUTHORIZATION;
549 CREATE TABLE event_trigger_test (a integer, b text);
550 CREATE OR REPLACE FUNCTION start_command()
551 RETURNS event_trigger AS $$
553 RAISE NOTICE '% - ddl_command_start', tg_tag;
556 CREATE OR REPLACE FUNCTION end_command()
557 RETURNS event_trigger AS $$
559 RAISE NOTICE '% - ddl_command_end', tg_tag;
562 CREATE OR REPLACE FUNCTION drop_sql_command()
563 RETURNS event_trigger AS $$
565 RAISE NOTICE '% - sql_drop', tg_tag;
568 CREATE EVENT TRIGGER start_rls_command ON ddl_command_start
569 WHEN TAG IN ('CREATE POLICY', 'ALTER POLICY', 'DROP POLICY') EXECUTE PROCEDURE start_command();
570 CREATE EVENT TRIGGER end_rls_command ON ddl_command_end
571 WHEN TAG IN ('CREATE POLICY', 'ALTER POLICY', 'DROP POLICY') EXECUTE PROCEDURE end_command();
572 CREATE EVENT TRIGGER sql_drop_command ON sql_drop
573 WHEN TAG IN ('DROP POLICY') EXECUTE PROCEDURE drop_sql_command();
574 CREATE POLICY p1 ON event_trigger_test USING (FALSE);
575 NOTICE: CREATE POLICY - ddl_command_start
576 NOTICE: CREATE POLICY - ddl_command_end
577 ALTER POLICY p1 ON event_trigger_test USING (TRUE);
578 NOTICE: ALTER POLICY - ddl_command_start
579 NOTICE: ALTER POLICY - ddl_command_end
580 ALTER POLICY p1 ON event_trigger_test RENAME TO p2;
581 NOTICE: ALTER POLICY - ddl_command_start
582 NOTICE: ALTER POLICY - ddl_command_end
583 DROP POLICY p2 ON event_trigger_test;
584 NOTICE: DROP POLICY - ddl_command_start
585 NOTICE: DROP POLICY - sql_drop
586 NOTICE: DROP POLICY - ddl_command_end
587 -- Check the object addresses of all the event triggers.
590 pg_describe_object('pg_event_trigger'::regclass, e.oid, 0) as descr,
591 b.type, b.object_names, b.object_args,
592 pg_identify_object(a.classid, a.objid, a.objsubid) as ident
593 FROM pg_event_trigger as e,
594 LATERAL pg_identify_object_as_address('pg_event_trigger'::regclass, e.oid, 0) as b,
595 LATERAL pg_get_object_address(b.type, b.object_names, b.object_args) as a
597 evtname | descr | type | object_names | object_args | ident
598 -------------------+---------------------------------+---------------+---------------------+-------------+--------------------------------------------------------
599 end_rls_command | event trigger end_rls_command | event trigger | {end_rls_command} | {} | ("event trigger",,end_rls_command,end_rls_command)
600 sql_drop_command | event trigger sql_drop_command | event trigger | {sql_drop_command} | {} | ("event trigger",,sql_drop_command,sql_drop_command)
601 start_rls_command | event trigger start_rls_command | event trigger | {start_rls_command} | {} | ("event trigger",,start_rls_command,start_rls_command)
604 DROP EVENT TRIGGER start_rls_command;
605 DROP EVENT TRIGGER end_rls_command;
606 DROP EVENT TRIGGER sql_drop_command;