Re: Fix trigger memory leak

From: Abraham Pérez <abraham1798(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Fix trigger memory leak
Date: 2026-11-25 13:13:29
Message-ID: CAF6dGh2455yXoWU8fMW6m2jDg18MRNyLsXU-tqvbGH+kC0xwvw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Paul,

I reviewed v2 and tested it on current master (2bdbde8b3bd). Short version:
the fix is correct, the build is clean, and the leak is gone in every case I
could reproduce. Details below.

Build and tests (configure --enable-debug --enable-cassert --enable-tap-tests):
- v2 applies cleanly to master and builds with no new warnings.
- make check: all 243 tests pass.

Reproducing the leak. For each scenario I inserted/updated 50k rows with a 1 kB
text column through a PL/pgSQL row trigger that returns NEW unchanged (or NULL),
and read the peak ExecutorState size from pg_backend_memory_contexts. Unpatched
master vs. patched:

ExecBRInsertTriggers (BEFORE INSERT on a partition) 105 MB -> 544 kB
ExecIRInsertTriggers (INSTEAD OF INSERT on a view) 105 MB -> 528 kB
ExecIRUpdateTriggers (INSTEAD OF UPDATE on a view, NEW) 104 MB -> 48 kB
ExecIRUpdateTriggers (INSTEAD OF UPDATE on a view, NULL) 104 MB -> 48 kB

So I can confirm the view/partition INSERT cases Andres and Surya described, and
also both INSTEAD OF UPDATE paths. The RETURN NULL ("do nothing") path in
ExecIRUpdateTriggers was leaking as well, and the free you added there is a real
fix, not just symmetry with the others -- that path freed nothing before.

Reading the code, the fix looks right in all four functions. The loop can exit
with newtuple still pointing at the locally materialized copy whenever the last
matching trigger returns the tuple unchanged: that is the branch that used to
store a different tuple, free the old copy and NULL out newtuple, and when it is
not taken, the "if (!newtuple)" guard also prevents a re-fetch, so the copy was
simply never freed. Freeing it after the loop under should_free is correct, and
initializing should_free = false is the right call now that it is read at
function scope.

One case I could not reproduce: a leak via ExecBRUpdateTriggers. With a BEFORE
UPDATE trigger that returns NEW unchanged, both a plain table and a partitioned
table stayed flat (48 kB) on unpatched master. It looks like the UPDATE path
hands this function an already-materialized newslot, so should_free_new is false
and no copy gets made. The new free there is harmless and keeps the four
functions consistent, so I would keep it -- I just wanted to flag that I could
not find an input that actually exercises it, in case you have one worth
mentioning in the commit message, or can confirm it is purely defensive.

The patch adds no regression test, which seems reasonable here: asserting on
memory-context sizes in src/test/regress would be fragile. A
pg_backend_memory_contexts check like the one above could serve as a cheap
guard if desired, but I would not hold the patch for it.

Aside from the ExecBRUpdateTriggers question above, v2 looks good to me.

Thanks,
Abraham Pérez

Browse pgsql-hackers by date

  From Date Subject
Next Message Ayush Tiwari 2026-11-25 13:19:47 pg_commit_ts pages can be written before WAL is flushed
Previous Message Daniel Gustafsson 2026-11-25 13:12:35 Re: OOM kill in xmlelement() with large arrays