pg.ddx.io pgaudit/pgaudit GitHub issues and pull requests
help / color / mirror / Atom feedRe: [pgaudit/pgaudit] PR #325: Skip executor hooks when SELECT/DML auditing is not enabled.
9+ messages / 2 participants
[nested] [flat]
* Re: [pgaudit/pgaudit] PR #325: Skip executor hooks when SELECT/DML auditing is not enabled.
@ 2026-06-26 10:36 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: dwsteele (@dwsteele) @ 2026-06-26 10:36 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
@dwsteele assigned @dwsteele.
^ permalink raw [nested|flat] 9+ messages in thread
* [pgaudit/pgaudit] PR #325: Skip executor hooks when SELECT/DML auditing is not enabled.
@ 2026-06-26 10:36 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
0 siblings, 0 replies; 9+ messages in thread
From: dwsteele (@dwsteele) @ 2026-06-26 10:36 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
The ExecutorStart, ExecutorCheckPerms, and ExecutorEnd hooks ran for every statement whenever pgaudit was loaded, regardless of pgaudit.log. With pgaudit.log = 'ddl, role' this meant each INSERT and COPY-driven statement still pushed an audit stack item (creating a memory context and copying the parameter list), looked up the audit role, and built object identities for every relation in log_select_dml(), only to discard the result because neither DDL nor ROLE applies to DML. Under high insert load this is a large amount of pure overhead.
Gate the three executor hooks on a new audit_executor_enabled() predicate that is true only when a class handled by the executor path (READ, WRITE, MISC, or FUNCTION) is enabled, or object-level auditing is configured via pgaudit.role. FUNCTION is included because function-execute auditing relies on an executor stack item created by ExecutorStart, and the role check keeps object auditing working. DDL, ROLE, and MISC_SET are logged through ProcessUtility and event triggers, so they no longer pay any executor-path cost.
The role lookup and bitmap test are otherwise unchanged, so the set of statements that get logged is identical; only redundant per-statement work is removed.
This was written as a performance optimization but there is an argument that it could also be preventative against certain classes of bugs introduced by extensions that call the ExecutorCheckPerms hook without respecting the proper order of executor hooks. In that case pgAudit may throw an error. Skipping the executor hooks entire might prevent that without the extension needing to disable pgAudit, depending on settings, which might be an argument for a backpatch.
NOTE: this patch is more readable with white space changes ignored.
Reported-by: Harun Kucuk (@harunkucuk5)
inline patch 1: pgaudit.c (+70/-34 lines) download inline-1.patch
diff --git a/pgaudit.c b/pgaudit.c
index c5cca00..12d5c29 100644
--- a/pgaudit.c
+++ b/pgaudit.c
@@ -71,6 +71,16 @@ PG_FUNCTION_INFO_V1(pgaudit_sql_drop);
#define LOG_NONE 0 /* nothing */
#define LOG_ALL (0xFFFFFFFF) /* All */
+/*
+ * Classes where auditing is driven by the executor hooks (ExecutorStart,
+ * ExecutorCheckPerms, ExecutorEnd). log_select_dml() emits READ, WRITE, or
+ * (for unclassified commands) MISC records, and function-execute auditing
+ * (LOG_FUNCTION) relies on an executor stack item created by
+ * pgaudit_ExecutorStart_hook(). When none of these classes are enabled the
+ * executor hooks have no work to do.
+ */
+#define LOG_EXECUTOR (LOG_READ | LOG_WRITE | LOG_MISC | LOG_FUNCTION)
+
/* GUC variable for pgaudit.log, which defines the classes to log. */
static char *auditLog = NULL;
@@ -1427,6 +1437,24 @@ static object_access_hook_type next_object_access_hook = NULL;
static ExecutorStart_hook_type next_ExecutorStart_hook = NULL;
static ExecutorEnd_hook_type next_ExecutorEnd_hook = NULL;
+/*
+ * Are the executor hooks required for the current configuration?
+ *
+ * The executor hooks perform per-statement work -- pushing a stack item,
+ * looking up the audit role, and building object identities for every relation
+ * -- on behalf of SELECT/DML and function-execute auditing. None of that
+ * produces output unless a class in LOG_EXECUTOR is enabled or object-level
+ * auditing is configured via pgaudit.role. Skipping the hooks entirely
+ * otherwise avoids significant overhead for high-volume DML under
+ * configurations such as pgaudit.log = 'ddl, role'.
+ */
+static inline bool
+audit_executor_enabled(void)
+{
+ return (auditLogBitmap & LOG_EXECUTOR) != 0 ||
+ (auditRole != NULL && auditRole[0] != '\0');
+}
+
/*
* Hook ExecutorStart to get the query text and basic command type for queries
* that do not contain a table and so can't be idenitified accurately in
@@ -1438,7 +1466,7 @@ pgaudit_ExecutorStart_hook(QueryDesc *queryDesc, int eflags)
AuditEventStackItem *stackItem = NULL;
MemoryContext contextOld;
- if (!internalStatement && !IsParallelWorker())
+ if (audit_executor_enabled() && !internalStatement && !IsParallelWorker())
{
/* Push the audit event onto the stack */
stackItem = stack_push();
@@ -1523,47 +1551,54 @@ pgaudit_ExecutorCheckPerms_hook(List *rangeTabls,
List *permInfos,
bool ereport_on_violation)
{
- Oid auditOid;
-
- /* Get the audit oid if the role exists */
- auditOid = get_role_oid(auditRole, true);
-
- /* Log DML if the audit role is valid or session logging is enabled */
- if ((auditOid != InvalidOid || auditLogBitmap != 0) &&
+ /*
+ * Only do audit work when an executor-driven class is enabled or object
+ * auditing via pgaudit.role might apply. This short-circuits the role
+ * lookup and per-statement DML processing for configurations such as
+ * pgaudit.log = 'ddl, role'.
+ */
+ if (audit_executor_enabled() &&
!IsAbortedTransactionBlockState() && !IsParallelWorker())
{
- /* If auditLogRows is on, wait for rows processed to be set */
- if (auditLogRows && auditEventStack != NULL)
+ /* Get the audit oid if the role exists */
+ Oid auditOid = get_role_oid(auditRole, true);
+
+ /* Log DML if the audit role is valid or session logging is enabled */
+ if (auditOid != InvalidOid || auditLogBitmap != 0)
{
- /* Check if the top item is SELECT/INSERT for CREATE TABLE AS */
- if (auditEventStack->auditEvent.commandTag == T_SelectStmt &&
- auditEventStack->next != NULL &&
- auditEventStack->next->auditEvent.command == CMDTAG_CREATE_TABLE_AS &&
- auditEventStack->auditEvent.rangeTabls != NULL)
+ /* If auditLogRows is on, wait for rows processed to be set */
+ if (auditLogRows && auditEventStack != NULL)
{
- /*
- * First, log the INSERT event for CREATE TABLE AS here.
- * The SELECT event for CREATE TABLE AS will be logged
- * in pgaudit_ExecutorEnd_hook() later to get rows.
- */
- log_select_dml(auditOid, rangeTabls, permInfos);
+ /* Check if the top item is SELECT/INSERT for CREATE TABLE AS */
+ if (auditEventStack->auditEvent.commandTag == T_SelectStmt &&
+ auditEventStack->next != NULL &&
+ auditEventStack->next->auditEvent.command == CMDTAG_CREATE_TABLE_AS &&
+ auditEventStack->auditEvent.rangeTabls != NULL)
+ {
+ /*
+ * First, log the INSERT event for CREATE TABLE AS here.
+ * The SELECT event for CREATE TABLE AS will be logged
+ * in pgaudit_ExecutorEnd_hook() later to get rows.
+ */
+ log_select_dml(auditOid, rangeTabls, permInfos);
+ }
+ else
+ {
+ /*
+ * Save auditOid, rangeTabls, and permInfos to call
+ * log_select_dml() in pgaudit_ExecutorEnd_hook() later.
+ */
+ auditEventStack->auditEvent.auditOid = auditOid;
+ auditEventStack->auditEvent.rangeTabls = rangeTabls;
+ auditEventStack->auditEvent.permInfos = permInfos;
+ }
}
else
{
- /*
- * Save auditOid, rangeTabls, and permInfos to call
- * log_select_dml() in pgaudit_ExecutorEnd_hook() later.
- */
- auditEventStack->auditEvent.auditOid = auditOid;
- auditEventStack->auditEvent.rangeTabls = rangeTabls;
- auditEventStack->auditEvent.permInfos = permInfos;
+ STACK_NOT_EMPTY();
+ log_select_dml(auditOid, rangeTabls, permInfos);
}
}
- else
- {
- STACK_NOT_EMPTY();
- log_select_dml(auditOid, rangeTabls, permInfos);
- }
}
/* Call the next hook function */
@@ -1584,7 +1619,8 @@ pgaudit_ExecutorEnd_hook(QueryDesc *queryDesc)
AuditEventStackItem *stackItem = NULL;
AuditEventStackItem *auditEventStackFull = NULL;
- if (auditLogRows && !internalStatement && !IsParallelWorker())
+ if (auditLogRows && audit_executor_enabled() &&
+ !internalStatement && !IsParallelWorker())
{
/* Find an item from the stack by the query memory context */
stackItem = stack_find_context(queryDesc->estate->es_query_cxt);
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgaudit/pgaudit] PR #325: Skip executor hooks when SELECT/DML auditing is not enabled.
@ 2026-06-26 10:36 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: dwsteele (@dwsteele) @ 2026-06-26 10:36 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
@dwsteele applied label "enhancement".
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgaudit/pgaudit] PR #325 (approved): Skip executor hooks when SELECT/DML auditing is not enabled.
@ 2026-07-02 19:17 "pgguru (@pgguru)" <noreply+pgguru@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: pgguru (@pgguru) @ 2026-07-02 19:17 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
Makes sense; I've hit the aforementioned Executor errors when other extension hooks are involved.
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgaudit/pgaudit] PR #325: Skip executor hooks when SELECT/DML auditing is not enabled.
@ 2026-07-03 04:14 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: dwsteele (@dwsteele) @ 2026-07-03 04:14 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
@pgguru So the question is -- should we back patch? It's possible this could prevent certain classes of errors but I don't have any specific examples. It does not cause any regressions in our tests but that doesn't guarantee that there would be no regressions in production environments.
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgaudit/pgaudit] PR #325: Skip executor hooks when SELECT/DML auditing is not enabled.
@ 2026-07-20 14:32 "pgguru (@pgguru)" <noreply+pgguru@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: pgguru (@pgguru) @ 2026-07-20 14:32 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
What sort of regressions might this cause—someone relying on the previous behavior? (From what I can see, there shouldn't be any user-visible changes, so doesn't seem likely.). Some analysis indicates that the patch should map cleanly.
What is the official policy on back patches? If this fixes some of the hook ordering/stack errors seen when combining multiple extensions, then I could consider this a bug fix. However if it is only a performance improvement I'm not certain this would be considered a bugfix per se (i.e., it's not fixing a performance regression). That said, if the policy is more flexible in terms of the contract here, the patch itself seems fairly straightforward, so I'd be in favor of improving performance.
(I assume the reporting user would also appreciate the bump without having to wait for Pg 19).
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgaudit/pgaudit] PR #325: Skip executor hooks when SELECT/DML auditing is not enabled.
@ 2026-07-21 03:46 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: dwsteele (@dwsteele) @ 2026-07-21 03:46 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
> What sort of regressions might this cause—someone relying on the previous behavior?
According to the regression tests, none, but of course there could be something outside out tests.
> What is the official policy on back patches?
Officially only bugs. My thought was that this patch is also defensive like 3c17922f but I don't have any instances of a bug it would fix.
> I assume the reporting user would also appreciate the bump without having to wait for Pg 19
Possibly true, but since I never heard back from them I'm not inclined to pursue it. I'll commit it for PG19 only.
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgaudit/pgaudit] PR #325: Skip executor hooks when SELECT/DML auditing is not enabled.
@ 2026-07-21 03:51 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: dwsteele (@dwsteele) @ 2026-07-21 03:51 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
@dwsteele closed this.
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgaudit/pgaudit] PR #325: Skip executor hooks when SELECT/DML auditing is not enabled.
@ 2026-07-21 03:51 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: dwsteele (@dwsteele) @ 2026-07-21 03:51 UTC (permalink / raw)
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
@dwsteele merged this (commit cc75ac44a391).
^ permalink raw [nested|flat] 9+ messages in thread
end of thread, other threads:[~2026-07-21 03:51 UTC | newest]
Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-06-26 10:36 [pgaudit/pgaudit] PR #325: Skip executor hooks when SELECT/DML auditing is not enabled. "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-06-26 10:36 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-06-26 10:36 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-07-02 19:17 ` Re: [pgaudit/pgaudit] PR #325 (approved): Skip executor hooks when SELECT/DML auditing is not enabled. "pgguru (@pgguru)" <noreply+pgguru@github.com>
2026-07-03 04:14 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-07-20 14:32 ` "pgguru (@pgguru)" <noreply+pgguru@github.com>
2026-07-21 03:46 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-07-21 03:51 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-07-21 03:51 ` "dwsteele (@dwsteele)" <noreply+dwsteele@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