PL/pgSQL: FOUND is NULL, not false, in event trigger functions

Started by Jeevan Chalke23 days ago7 messageshackers
Jump to latest
#1Jeevan Chalke
jeevan.chalke@enterprisedb.com

Hi,

plpgsql_exec_function() and plpgsql_exec_trigger() both set the magic
variable FOUND to false before running the function body, but
plpgsql_exec_event_trigger() does not. As a result, FOUND is NULL rather
than
false at the start of a PL/pgSQL event trigger function:

CREATE FUNCTION f() RETURNS event_trigger LANGUAGE plpgsql AS $$
BEGIN RAISE NOTICE 'FOUND is %', FOUND; END $$;
CREATE EVENT TRIGGER t ON ddl_command_start EXECUTE FUNCTION f();
CREATE SCHEMA s;
NOTICE: FOUND is <NULL>

The documentation says that "
*FOUND starts out false within each PL/pgSQLfunction call*" [1]https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS, so this
looks like an oversight. Code such as
"IF NOT FOUND" before any statement has set FOUND would behave differently
in
an event trigger than in any other kind of PL/pgSQL function.

The attached patch fixes this by initializing FOUND to false, as the other
two entry points do, and adds a regression test. The test checks that FOUND
is false on entry to an event trigger function, and that a value set by one
call does not carry over to the next. It fails without the fix.

Since this is a behavior difference from the other function types rather
than
a new feature, I think it could also be back-patched, but I'd like to hear
what others think.

Thanks

[1]: https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS
https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS

--
*Jeevan Chalke*
*Senior Principal Engineer, Engineering Manager*
*Product Development*

enterprisedb.com <https://www.enterprisedb.com>

Attachments:

t254391_1
v1-0001-Initialize-FOUND-to-false-in-PL-pgSQL-event-trigg.patchapplication/octet-stream; name=v1-0001-Initialize-FOUND-to-false-in-PL-pgSQL-event-trigg.patchDownload+52-1
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jeevan Chalke (#1)
Re: PL/pgSQL: FOUND is NULL, not false, in event trigger functions

Jeevan Chalke <jeevan.chalke@enterprisedb.com> writes:

plpgsql_exec_function() and plpgsql_exec_trigger() both set the magic
variable FOUND to false before running the function body, but
plpgsql_exec_event_trigger() does not. As a result, FOUND is NULL rather
than false at the start of a PL/pgSQL event trigger function:

Good catch.

Since this is a behavior difference from the other function types rather
than
a new feature, I think it could also be back-patched, but I'd like to hear
what others think.

Given the lack of field complaints, I'm inclined to leave it as-is
in released branches (maybe it's not too late for v19, though).
It seems unlikely that anyone has written any code that would
be affected by this change, but ...

regards, tom lane

#3Daniel Gustafsson
daniel@yesql.se
In reply to: Tom Lane (#2)
Re: PL/pgSQL: FOUND is NULL, not false, in event trigger functions

On 26 Oct 2026, at 20:45, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Given the lack of field complaints, I'm inclined to leave it as-is
in released branches (maybe it's not too late for v19, though).

If we're leaving it as-is in backbranches, wouldn't it be better to opt for
consistency and leave it in 19 as well and only adjust the documentation?

--
Daniel Gustafsson

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Daniel Gustafsson (#3)
Re: PL/pgSQL: FOUND is NULL, not false, in event trigger functions

Daniel Gustafsson <daniel@yesql.se> writes:

On 26 Oct 2026, at 20:45, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Given the lack of field complaints, I'm inclined to leave it as-is
in released branches (maybe it's not too late for v19, though).

If we're leaving it as-is in backbranches, wouldn't it be better to opt for
consistency and leave it in 19 as well and only adjust the documentation?

I don't particularly want to document the broken behavior (especially
if we'd have to further document that it will change in v20). That
would just contribute to user confusion. If you want docs to match
code I'd rather back-patch the code change.

regards, tom lane

#5Daniel Gustafsson
daniel@yesql.se
In reply to: Tom Lane (#4)
Re: PL/pgSQL: FOUND is NULL, not false, in event trigger functions

On 26 Oct 2026, at 21:22, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Daniel Gustafsson <daniel@yesql.se> writes:

On 26 Oct 2026, at 20:45, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Given the lack of field complaints, I'm inclined to leave it as-is
in released branches (maybe it's not too late for v19, though).

If we're leaving it as-is in backbranches, wouldn't it be better to opt for
consistency and leave it in 19 as well and only adjust the documentation?

I don't particularly want to document the broken behavior (especially
if we'd have to further document that it will change in v20). That
would just contribute to user confusion. If you want docs to match
code I'd rather back-patch the code change.

Ah, I misread and thought you were advocating for what you clearly wanted to
avoid. Given the lack of complaints then +1 for at most fixing in 19.
Backpatching takes a non-trivial amount of time, which really isn't a commodity
we have plenty of.

--
Daniel Gustafsson

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Daniel Gustafsson (#5)
Re: PL/pgSQL: FOUND is NULL, not false, in event trigger functions

Daniel Gustafsson <daniel@yesql.se> writes:

Ah, I misread and thought you were advocating for what you clearly wanted to
avoid. Given the lack of complaints then +1 for at most fixing in 19.
Backpatching takes a non-trivial amount of time, which really isn't a commodity
we have plenty of.

Done that way, then.

regards, tom lane

#7Jeevan Chalke
jeevan.chalke@enterprisedb.com
In reply to: Tom Lane (#6)
Re: PL/pgSQL: FOUND is NULL, not false, in event trigger functions

On Tue, Oct 27, 2026 at 2:46 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Daniel Gustafsson <daniel@yesql.se> writes:

Ah, I misread and thought you were advocating for what you clearly

wanted to

avoid. Given the lack of complaints then +1 for at most fixing in 19.
Backpatching takes a non-trivial amount of time, which really isn't a

commodity

we have plenty of.

Done that way, then.

Thanks, Tom.

regards, tom lane

--
*Jeevan Chalke*
*Senior Principal Engineer, Engineering Manager*
*Product Development*

enterprisedb.com <https://www.enterprisedb.com>