1CREATE EXTENSION test_ddl_deparse;
2
3CREATE OR REPLACE FUNCTION test_ddl_deparse()
4  RETURNS event_trigger LANGUAGE plpgsql AS
5$$
6DECLARE
7	r record;
8	r2 record;
9	cmdtype text;
10	objtype text;
11	tag text;
12BEGIN
13	FOR r IN SELECT * FROM pg_event_trigger_ddl_commands()
14	LOOP
15		-- verify that tags match
16		tag = public.get_command_tag(r.command);
17		IF tag <> r.command_tag THEN
18			RAISE NOTICE 'tag % doesn''t match %', tag, r.command_tag;
19		END IF;
20
21		-- log the operation
22		cmdtype = public.get_command_type(r.command);
23		IF cmdtype <> 'grant' THEN
24			RAISE NOTICE 'DDL test: type %, tag %', cmdtype, tag;
25		ELSE
26			RAISE NOTICE 'DDL test: type %, object type %', cmdtype, r.object_type;
27		END IF;
28
29		-- if alter table, log more
30		IF cmdtype = 'alter table' THEN
31			FOR r2 IN SELECT *
32						FROM unnest(public.get_altertable_subcmdtypes(r.command))
33			LOOP
34				RAISE NOTICE '  subcommand: %', r2.unnest;
35			END LOOP;
36		END IF;
37	END LOOP;
38END;
39$$;
40
41CREATE EVENT TRIGGER test_ddl_deparse
42ON ddl_command_end EXECUTE PROCEDURE test_ddl_deparse();
43