From: dwsteele (@dwsteele) <noreply+dwsteele@github.com>
To: pgaudit/pgaudit <noreply+pgaudit-pgaudit@github.com>
Subject: [pgaudit/pgaudit] PR #325: Skip executor hooks when SELECT/DML auditing is not enabled.
Date: Fri, 26 Jun 2026 10:36:49 +0000
Message-ID: <gh-pgaudit-pgaudit-325@github.com> (raw)
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.patchdiff --git a/pgaudit.c b/pgaudit.cindex 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);