pg.ddx.io  pgaudit/pgaudit GitHub issues and pull requests  
help / color / mirror / Atom feed
From: zjyzh (@zjyzh) <noreply+zjyzh@github.com>
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
Subject: [pgaudit/pgaudit] issue #333: PG19: backend crash / "pgaudit stack is empty" from RI fast-path permission checks (after PostgreSQL commit 5aeb36f3273)
Date: Tue, 29 Sep 2026 17:38:41 +0000
Message-ID: <gh-pgaudit-pgaudit-333@github.com> (raw)


PostgreSQL commit [5aeb36f3273](https://github.com/postgres/postgres/commit/5aeb36f3273c768831284082f85b63066e84f16d) (`REL_19_STABLE`, 2026-09-24) makes the RI fast path call `ExecutorCheckPerms_hook` from inside the FK trigger. The lists it passes live in a per-tuple context that is reset after each check.

pgaudit treats every hook call as the statement's own check. That causes four problems:

1. **Crash with `log_rows = on`.** The list pointers are saved on the audit stack item and read at `ExecutorEnd` after they have been freed (signal 11).
2. **`ERROR: pgaudit stack is empty` at COMMIT.** Deferred FK checks run with an empty stack, so the COMMIT fails.
3. **Wrong attribution.** Under `SET CONSTRAINTS ALL IMMEDIATE` or `CALL`/`DO` + `COMMIT`, the check is audited against that utility statement's event.
4. **Extra audit lines.** Without `log_rows`, FK reads log as `READ,SELECT` on the user's statement. This breaks the test "FK references do not log".

## Reproduce

PostgreSQL `REL_19_STABLE` (after 5aeb36f3273) with pgaudit `main`:

```sql
SET pgaudit.log = 'read, write';
SET pgaudit.log_rows = on;
CREATE TABLE ri_pk (id int PRIMARY KEY);
CREATE TABLE ri_fk (id int REFERENCES ri_pk (id));
INSERT INTO ri_pk VALUES (1), (2);
INSERT INTO ri_fk VALUES (1), (2);   -- backend crash
```

The existing regression test also fails:

```
LOG:  client backend (PID 2295) was terminated by signal 11: Segmentation fault
DETAIL:  Failed process was running: INSERT INTO bbb VALUES (1);
```

Without `log_rows`, the same test shows extra lines:

```
+NOTICE:  AUDIT: OBJECT,67,1,READ,SELECT,TABLE,public.aaa,INSERT INTO bbb VALUES (1),<not logged>
+NOTICE:  AUDIT: SESSION,67,1,READ,SELECT,TABLE,public.aaa,INSERT INTO bbb VALUES (1),<not logged>
```

## Proposed fix

Only audit a statement's own permission check. That is either:
- the call made during pgaudit's `ExecutorStart` hook for the top stack item (a static saved and restored with `PG_TRY`/`PG_FINALLY`), or
- the first check of a `COPY` statement.

Pass all other calls (the RI fast path, `RI_Initial_Check`, calls with an empty stack) to the next hook without auditing. This keeps the pre-5aeb36f3273 output.

With this fix, the regression suite passes. The added tests cover multi-row FKs, deferred FKs at COMMIT, `SET CONSTRAINTS`, `CALL`/`DO` + `COMMIT` and `COPY FROM`, each with `log_rows` on and off.

## Related

- `de00323` only skips calls where `ereport_on_violation = false`. The fast path passes `true`, so the fast path is not covered.
- #80 (RI trigger selects logged), #212 (hook called with an empty stack)


view thread (8+ messages)  latest in thread

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