pg.ddx.io  pgaudit/pgaudit GitHub issues and pull requests  
help / color / mirror / Atom feed
From: pgguru (@pgguru) <noreply+pgguru@github.com>
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
Subject: Re: [pgaudit/pgaudit] PR #327: Only flush deferred select/dml audit entries for COPY.
Date: Mon, 27 Jul 2026 19:33:24 +0000
Message-ID: <gh-pgaudit-pgaudit-327-c5095880665@github.com> (raw)
In-Reply-To: <gh-pgaudit-pgaudit-327@github.com>
References: <gh-pgaudit-pgaudit-327@github.com>

Check my thoughts here: the root cause is that `pgaudit_ExecutorCheckPerms_hook` staples the query's `rangeTabls` onto whatever's on top of the audit stack and (with `log_rows` on) defers logging to `ExecutorEnd`. That assumes the stack-top is the statement whose `ExecutorEnd` will actually fire. Any utility command that checks table perms *out of band* (i.e., a bare `ExecCheckPermissions()` with no executor lifecycle behind it) breaks that assumption and orphans the entry on the DDL item.

`ALTER TABLE … ADD FOREIGN KEY` via `RI_Initial_Check()` is just the case someone happened to hit, but it would seem the same thing should reproduce on `ALTER TABLE … VALIDATE CONSTRAINT` for a `NOT VALID` FK due to exercising the same `RI_Initial_Check()` path, and the PR doesn't cover it. Probably worth a second test case there; I'd bet the fix already handles it but it's untested.

Also worth noting this is the *third* patch over the same design smell — there's already a special-case for `CREATE TABLE AS` right in the perms hook, and now COPY. FK validation is the third. Each new out-of-band perms path seems to need its own bespoke fix.  That said, not sure there is much else to do than identify and fix.

One thing that makes me a little nervous about the fix: gating on `T_CopyStmt` hard-codes "COPY is the only utility that checks perms via the hook but never reaches `ExecutorEnd`." I couldn't find another one today (TRUNCATE/LOCK use `pg_class_aclcheck`, CTAS/REFRESH MATVIEW go through a real `ExecutorEnd`), so it's probably complete. But the failure mode flipped: before, a mis-detected command got its DDL clobbered (loud, shows up in tests); now an undetected one would silently drop its SELECT/DML entry, which for an audit extension is arguably worse. A more robust fix would tag the stack item at deferral time with whether an `ExecutorEnd` is expected instead of enumerating command tags, but that's a bigger refactor than this PR's after.  (Not sure if there may be any 19-based considerations either, not super familiar with new nodes/operations here yet.)

view thread (8+ messages)  latest in thread

Message-ID: <gh-pgaudit-pgaudit-327-c5095880665@github.com>
Permalink:  ../gh-pgaudit-pgaudit-327-c5095880665@github.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