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

Поиск
Список
Период
Сортировка
Искать

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

От:
Jeevan 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/pgSQL
function call
" [1], 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

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


enterprisedb.com

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

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Jeevan Chalke  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


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

От:
Daniel Gustafsson <daniel@yesql.se>
Дата:
> On 26 Oct 2026, at 20:45, Tom Lane  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



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

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Daniel Gustafsson  writes:
>> On 26 Oct 2026, at 20:45, Tom Lane  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


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

От:
Daniel Gustafsson <daniel@yesql.se>
Дата:
> On 26 Oct 2026, at 21:22, Tom Lane  wrote:
> 
> Daniel Gustafsson  writes:
>>> On 26 Oct 2026, at 20:45, Tom Lane  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



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

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Daniel Gustafsson  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


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

От:
Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Дата:


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
FAQ