]> begriffs open source - pg_scribe/blob - wal2sql/wal2sql--0.1.sql
Convert more tests
[pg_scribe] / wal2sql / wal2sql--0.1.sql
1 -- complain if script is sourced in psql, rather than via CREATE EXTENSION
2 \echo Use "CREATE EXTENSION wal2sql" to load this file. \quit
3
4 --
5 -- Event trigger function to capture DDL commands and emit them into the
6 -- logical replication stream via pg_logical_emit_message().
7 --
8 -- This function is called by the wal2sql_ddl_trigger event trigger on
9 -- ddl_command_end events. It captures the DDL command text and emits it
10 -- with the 'ddl' prefix so it will be picked up by the wal2sql logical
11 -- decoding plugin's message_cb callback.
12 --
13 CREATE FUNCTION wal2sql_ddl_handler()
14 RETURNS event_trigger
15 LANGUAGE plpgsql
16 AS $$
17 DECLARE
18     ddl_command text;
19 BEGIN
20     -- Get the current query text (the DDL command being executed)
21     ddl_command := current_query();
22
23     -- Emit the DDL command into the logical replication stream.
24     -- Parameters:
25     --   transactional = true: message is part of current transaction
26     --   prefix = 'ddl': identifies this as a DDL message for wal2sql plugin
27     --   content = ddl_command: the actual DDL SQL text
28     PERFORM pg_logical_emit_message(true, 'ddl', ddl_command);
29 END;
30 $$;
31
32 --
33 -- Event trigger that fires on DDL command completion
34 --
35 -- This trigger fires after any DDL command completes successfully,
36 -- calling wal2sql_ddl_handler() to emit the command into the replication stream.
37 --
38 CREATE EVENT TRIGGER wal2sql_ddl_trigger
39 ON ddl_command_end
40 EXECUTE FUNCTION wal2sql_ddl_handler();