| From: | Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Event triggers and the commands embedded in CREATE SCHEMA |
| Date: | 2026-11-24 18:35:21 |
| Message-ID: | CAN4CZFNE58E1LcVnN7sw04mFSiiwLJohHNLqusFiC15nTEMeJg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hello
The commands embedded in CREATE SCHEMA don't fire event triggers on
their own: we only get a single ddl_command_start for the CREATE
SCHEMA itself, and the other commands only show up at ddl_command_end
with pg_event_trigger_ddl_commands().
We ran into this with pg_tde, which needs both the start and end
events for create/alter relation commands, and in this case, the start
events are completely missing:
CREATE FUNCTION et() RETURNS event_trigger LANGUAGE plpgsql AS
$$BEGIN RAISE NOTICE '% %', tg_event, tg_tag; END$$;
CREATE EVENT TRIGGER s ON ddl_command_start WHEN TAG IN ('CREATE TABLE')
EXECUTE FUNCTION et();
CREATE TABLE t1 (a int); -- fires
CREATE SCHEMA s1 CREATE TABLE t2 (a int); -- doesn't fire
This is basically the same issue as my earlier thread about EXPLAIN
ANALYZE CTAS [1]. As with the EXPLAIN case, it can be handled with
some workarounds in the current releases, but it's not an ideal / well
explained behavior.
The documentation doesn't mention this limitation, but it was actively
discussed during the initial event trigger implementation:
Robert argued for trapping the operation, not the command [2]:
> I think it would be a much better idea to decree from the beginning
> that we're trapping the *operation* of creating a table, rather than
> the *command* create table. Then, the behavior is clear and
> well-defined from day one, and it doesn't depend on how we happen to
> implement things internally right at the moment. If there's some way
> of creating a table without firing the trigger, it's a bug.
and Alvaro specifically warned about CREATE SCHEMA [3]:
> However, if this is to work in a robust way, these things should not be
> ignored. In particular, firing triggers on each top-level command _is_
> going to get some objects ignored; the people writing the triggers _are_
> going to forget to handle CREATE SCHEMA properly most of the time.
(Uh, we are one of those people who forgot this)
Later Tom argued against exposing subcommands in general, as they are
mostly implementation artifacts, but he also pointed out that CREATE
SCHEMA is different [4]:
> schemacmds.c: CreateSchemaCommand uses ProcessUtility for any subcommand
> of a CREATE SCHEMA statement. This is really just direct execution of
> something the user typed, it's not the system "creating" a subcommand.
Even the ddl_command_end part was added later because of pg_audit.
The attached patch handles the elements like the commands of an
extension script: they run as complete queries
(PROCESS_UTILITY_QUERY), so each of them fires its own events with its
own tag. To keep the order correct, ProcessUtilitySlow() now processes
the CREATE SCHEMA itself without the elements, and runs them with the
new CreateSchemaElements() only after the CREATE SCHEMA events. Event
triggers see CREATE SCHEMA s CREATE TABLE t ... the same way as CREATE
SCHEMA s followed by CREATE TABLE s.t ...: the schema's
ddl_command_end fires first and only lists the schema, then each
element fires its own start/end events.
I want to point out some things, as these are visible behavior changes:
1. pg_event_trigger_ddl_commands() at the end of CREATE SCHEMA no
longer contains the objects created by the elements, they are reported
in the events of the elements instead. A tool that only looks at the
CREATE SCHEMA event (and doesn't care about other DDL) would miss
them, but I don't see how such a tool could work correctly with the
separate commands anyway.
2. The foreign keys that transformCreateSchemaStmtElements() splits
out of the CREATE TABLE elements are executed as ALTER TABLE commands
at the end, so they now fire ALTER TABLE events too, and the
ddl_command_end of a CREATE TABLE element fires before its foreign
keys exist. They were already reported as separate ALTER TABLE
commands in the command list before, and they have to run after all
tables exist, so I don't think this can be done differently, but the
documentation now mentions it.
3. current_query() in the events of the elements is still the complete
CREATE SCHEMA text, the same way as with multiple commands sent in one
query string.
4. The ddl_command_end of the CREATE SCHEMA fires before the elements
are executed, so it also fires if one of the elements fails later.
That includes the up-front checks of the elements, for example an
element that specifies a different schema, which previously failed
before any ddl_command_end. Everything is rolled back in that case, so
this only matters for triggers with non-transactional side effects.
[1]: https://www.postgresql.org/message-id/CAN4CZFOycbakF5swg0i_hH2dZTkMP2%2BoA%3DnoU0VFFFe3687ojw%40mail.gmail.com
[2]: https://www.postgresql.org/message-id/CA+TgmoYAUf6_nG5nTp23i38qpWNtdcG1HUtPWm=iubay5rrMXg@mail.gmail.com
[3]: https://www.postgresql.org/message-id/1323707987-sup-1264@alvh.no-ip.org
[4]: https://www.postgresql.org/message-id/19773.1358464930@sss.pgh.pa.us
[5]: https://www.postgresql.org/message-id/20150521053500.GB3934913@tornado.leadboat.com
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Fire-event-triggers-for-the-commands-embedded-in-.patch | application/octet-stream | 26.1 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Greg Burd | 2026-11-24 18:44:15 | Re: Hash index: stop registering pin-only bucket pages in WAL (remove REGBUF_NO_CHANGE) |
| Previous Message | Haoyu Huang | 2026-11-24 18:17:08 | Re: Better shared data structure management and resizable shared data structures |