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