Summary
pg_partman contains a SQL injection vulnerability in exception-handling paths that pass a parent-table name into a SQL string used by pg_jobmon. If a user who can create or control a partitioned-table name causes an affected maintenance path to run, injected SQL may execute with the privileges of the background worker, potentially including database-wide administrative access and operating-system command execution.
Affected context
The issue occurs in pg_partman deployments where pg_jobmon is installed and the relevant part_config.jobmon setting is enabled. It requires a partman_user or another suitably privileged database principal to establish a parent-table name containing SQL-significant characters, followed by execution of an affected exception path. Risk is greatest when pg_partman_bgw runs with highly privileged PostgreSQL credentials, such as a superuser role.
Exploit preconditions
- Attack surface: PostgreSQL extension functions and background maintenance processing
- Required access: Ability to create or register a partitioned parent table whose name is later used by pg_partman
- Trigger point: An affected pg_partman exception handler invokes pg_jobmon.add_job()
- Required configuration: pg_jobmon is installed and part_config.jobmon is enabled
- Why exploitation is possible: The parent-table name is inserted verbatim into a SQL string literal without adequate parameterization or escaping
- Privilege condition: The resulting statements execute with the privileges assigned to pg_partman_bgw, which may include PostgreSQL superuser privileges
Why this happens
The underlying bug is SQL injection caused by concatenating identifier-derived data into a dynamically constructed SQL string literal. A single quote in the parent-table name can alter the intended literal boundaries and change the meaning of the generated statement when the exception path executes. This behavior maps to CWE-89: Improper Neutralization of Special Elements used in an SQL Command, because input is incorporated into SQL without a safe separation between data and executable command structure.
Potential impact
- An attacker may execute unauthorized SQL statements within the database
- Database contents, schemas, roles, or security controls could be modified
- The attacker could obtain or alter data across database-wide scopes
- The background worker process could be abused to execute operating-system commands
- The PostgreSQL service account and connected application data could be compromised
- Persistent configuration may allow the malicious condition to recur during later maintenance cycles
General mitigation guidance
Mitigation generally focuses on preserving the boundary between identifiers, values, and executable SQL.
Common defensive patterns include:
- Using parameterized SQL or trusted identifier-quoting APIs rather than string concatenation
- Treating table names and other database metadata as untrusted input at every function boundary
- Restricting who may create or register partitioned parent tables
- Running background maintenance roles with only the permissions required for their tasks
- Separating job-monitoring privileges from database-administration and operating-system execution privileges
- Limiting persistence and mutability of partition-management configuration
- Reviewing exception paths as carefully as normal execution paths
- Applying database allowlisting and auditing controls for dynamic SQL operations
How teams detect issues like this
Detection can combine source review, runtime monitoring, and privilege analysis:
- Static analysis can flag concatenation of table names or other external data into SQL strings
- Code review should examine exception handlers, logging or job-reporting calls, and dynamic SQL construction
- Repository policies can require parameterized statements or approved identifier-quoting helpers
- Database auditing can monitor unexpected DDL, role changes, privileged statements, and operating-system command interfaces
- Configuration reviews can identify background workers operating with superuser or otherwise excessive privileges
- Automated security review tools may help identify unsafe dynamic SQL patterns before deployment

