pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
ddl_command_end not firing DROP events
3+ messages / 2 participants
[nested] [flat]

* ddl_command_end not firing DROP events
@ 2018-07-08 17:46 Demitri Muna <postgresql@demitri.com>
  2018-07-08 18:19 ` Re: ddl_command_end not firing DROP events Alvaro Herrera <alvherre@2ndquadrant.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Demitri Muna @ 2018-07-08 17:46 UTC (permalink / raw)
  To: pgsql-general

Hi,

I’ve created a trigger where I want to capture schema-changing events. I’m finding that DROP events are not being triggered when using “ddl_command_end". The test case below demonstrates this. I am running PostgreSQL 10.4. The trigger is fired for the CREATE event, but not DROP TYPE or DROP TABLE.

I came across the same question on the mailing list, but the solution was to use the “sql_drop” trigger instead.

https://www.postgresql.org/message-id/CAHE3wgjX-N%3DX9mccp4Bs-y-%3DHE%3DqayHEpXpCs1jMT%3DW0ZsYc5A%40...

The documentation suggests that one should be able to use “ddl_command_end” instead of creating two triggers (i.e. one for DROPs, another for everything else).

Test:

CREATE FUNCTION test_event_trigger_for_drops()
        RETURNS event_trigger LANGUAGE plpgsql AS $$
DECLARE
    obj record;
BEGIN
    FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands()
    LOOP
        RAISE NOTICE 'caught % event on ''%''',
                     obj.command_tag,
                     obj.object_identity;
    END LOOP;
END
$$;
CREATE EVENT TRIGGER test_event_trigger_for_drops
   ON ddl_command_end
   EXECUTE PROCEDURE test_event_trigger_for_drops();

CREATE TYPE foo AS (f1 int, f2 text);
DROP TYPE foo;
CREATE TABLE drop_test ( pk int ) WITH ( OIDS = FALSE );
DROP TABLE drop_test;

-- for ease of copy/paste to delete the above
DROP EVENT TRIGGER test_event_trigger_for_drops;
DROP FUNCTION test_event_trigger_for_drops;

Cheers,
Demitri





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: ddl_command_end not firing DROP events
  2018-07-08 17:46 ddl_command_end not firing DROP events Demitri Muna <postgresql@demitri.com>
@ 2018-07-08 18:19 ` Alvaro Herrera <alvherre@2ndquadrant.com>
  2018-07-09 04:09   ` Re: ddl_command_end not firing DROP events Demitri Muna <postgresql@demitri.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Alvaro Herrera @ 2018-07-08 18:19 UTC (permalink / raw)
  To: Demitri Muna <postgresql@demitri.com>; +Cc: pgsql-general

On 2018-Jul-08, Demitri Muna wrote:

Hi

> I’ve created a trigger where I want to capture schema-changing events.
> I’m finding that DROP events are not being triggered when using
> “ddl_command_end". The test case below demonstrates this. I am running
> PostgreSQL 10.4. The trigger is fired for the CREATE event, but not
> DROP TYPE or DROP TABLE.
> 
> I came across the same question on the mailing list, but the solution
> was to use the “sql_drop” trigger instead.
> 
> https://www.postgresql.org/message-id/CAHE3wgjX-N%3DX9mccp4Bs-y-%3DHE%3DqayHEpXpCs1jMT%3DW0ZsYc5A%40...

As I recall, handling of DROP events in the ddl_command_end event is not
completely consistent. You may even find that some DROP events are not
returned by pg_event_trigger_ddl_commands.  I suggest you stick to
pg_event_trigger_dropped_objects() instead (for which you need
sql_drop).

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: ddl_command_end not firing DROP events
  2018-07-08 17:46 ddl_command_end not firing DROP events Demitri Muna <postgresql@demitri.com>
  2018-07-08 18:19 ` Re: ddl_command_end not firing DROP events Alvaro Herrera <alvherre@2ndquadrant.com>
@ 2018-07-09 04:09   ` Demitri Muna <postgresql@demitri.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Demitri Muna @ 2018-07-09 04:09 UTC (permalink / raw)
  To: pgsql-general

Hi,

On Jul 8, 2018, at 2:19 PM, Alvaro Herrera <alvherre@2ndquadrant.com> wrote:

> As I recall, handling of DROP events in the ddl_command_end event is not
> completely consistent. You may even find that some DROP events are not
> returned by pg_event_trigger_ddl_commands.  I suggest you stick to
> pg_event_trigger_dropped_objects() instead (for which you need
> sql_drop).

That's what I'm seeing as well and have a workaround in place that does just that. But to track schema changes requires two triggers and two functions. Which is fine, it works, but I'm wondering if it's a documentation issue or an outright bug.

Cheers,
Demitri

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2018-07-09 04:09 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-07-08 17:46 ddl_command_end not firing DROP events Demitri Muna <postgresql@demitri.com>
2018-07-08 18:19 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2018-07-09 04:09   ` Demitri Muna <postgresql@demitri.com>

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