pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: andrei.pozolotin@gmail.com
Subject: BUG #16177: pg_event_trigger_ddl_commands() returns empty set for ddl_command_start and "drop table"
Date: Sun, 22 Dec 2019 22:53:04 +0000
Message-ID: <16177-053a34714817c3e1@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      16177
Logged by:          Andrei Pozolotin
Email address:      andrei.pozolotin@gmail.com
PostgreSQL version: 11.6
Operating system:   linux
Description:        

1. use case is to delete rows with oid references  to the table before "drop
table"

2. one way to do that is with event_trigger, see code snippet below

3. the problem is that:
pg_event_trigger_ddl_commands() returns empty set for ddl_command_start and
"drop table"

4. this issue was already mentioned before:
https://postgrespro.com/list/thread-id/2394772

5. sample code:

        CREATE OR REPLACE FUNCTION pglogical_assign_repset()
            RETURNS event_trigger AS $$
            DECLARE obj record;
            BEGIN
                RAISE NOTICE 'pglogical assign: % %', tg_event, tg_tag;
                FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands()
                LOOP
                    IF obj.object_type = 'table' THEN
                        IF obj.schema_name = 'public' THEN
                            IF tg_tag IN ('CREATE TABLE') THEN
                                PERFORM
pglogical.replication_set_add_table('default', obj.objid);
                            END IF;
                            IF tg_tag IN ('DROP TABLE') THEN
                                PERFORM
pglogical.replication_set_remove_table('default', obj.objid);
                            END IF;
                        END IF;
                    END IF;
                END LOOP;
            END;
            $$ LANGUAGE plpgsql;
        ---
        DROP EVENT TRIGGER IF EXISTS
            pglogical_assign_repset_create;
        CREATE EVENT TRIGGER
            pglogical_assign_repset_create
            ON ddl_command_end
            WHEN TAG IN ('CREATE TABLE')
            EXECUTE FUNCTION pglogical_assign_repset();
        ---
        DROP EVENT TRIGGER IF EXISTS
            pglogical_assign_repset_delete;
        CREATE EVENT TRIGGER
            pglogical_assign_repset_delete
            ON ddl_command_start
            WHEN TAG IN ('DROP TABLE')
            EXECUTE FUNCTION pglogical_assign_repset();
        ---



view thread (5+ messages)  latest in thread

Message-ID: <16177-053a34714817c3e1@postgresql.org>
Permalink:  ../16177-053a34714817c3e1@postgresql.org/
Also on:    postgresql.org/message-id/16177-053a34714817c3e1@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, andrei.pozolotin@gmail.com
  Subject: Re: BUG #16177: pg_event_trigger_ddl_commands() returns empty set for ddl_command_start and "drop table"
  In-Reply-To: <16177-053a34714817c3e1@postgresql.org>
  References: <16177-053a34714817c3e1@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox