Summary
A SQL injection vulnerability in pg_partman allows a user who owns a template table to influence generated SQL through a specially crafted primary-key column name. When the background worker applies the key to a child partition, the SQL may execute with elevated database privileges and could enable database-wide compromise or operating-system command execution.
Affected context
The issue occurs in pg_partman deployments where the extension's background worker inherits primary-key properties from a template table and constructs an ALTER TABLE statement using catalog-sourced column names. Risk is present when a partman_user can create or control template-table column identifiers and the background worker operates with broad PostgreSQL privileges, particularly when its role is configured as a superuser.
Exploit preconditions
- Attack surface: PostgreSQL extension background-worker processing of partition creation
- Required access: Permission to own or modify a pg_partman template table and create a crafted column identifier
- Trigger point: A later partition-creation operation that causes the worker to apply the primary key
- Why exploitation is possible: The worker manually quotes catalog identifiers without escaping embedded double-quote characters, allowing attacker-controlled text to alter the generated SQL
- Privilege dependency: The resulting statement executes with the privileges assigned to the pg_partman background-worker role
Why this happens
The vulnerability results from constructing SQL identifiers through manual string interpolation without applying the database driver's identifier-quoting rules. Although the source value is read from PostgreSQL catalog metadata, an authorized user can influence that metadata by choosing a column name containing quote characters. When the value is inserted into an ALTER TABLE statement without correct escaping, it can change the statement's structure and introduce unintended SQL operations. This behavior maps to CWE-89: Improper Neutralization of Special Elements used in an SQL Command, commonly known as SQL injection.
Potential impact
- The attacker may execute unintended SQL with the background-worker role's privileges.
- Database contents, schemas, roles, or configuration may be modified or destroyed.
- A superuser-configured worker could enable database-wide compromise.
- The attacker could potentially invoke operating-system commands as the PostgreSQL service account.
- Malicious catalog identifiers may persist and trigger again during subsequent partition creation.
General mitigation guidance
Mitigation generally focuses on preventing user-controlled identifiers from being concatenated into SQL text and reducing the privileges available to automation workers.
Common defensive patterns include:
- Use native identifier-quoting APIs or parameterized database interfaces designed for SQL identifiers; ordinary value parameters are not a substitute for identifier handling.
- Validate template-table identifiers against an explicit schema and naming policy before they reach SQL-generation logic.
- Prefer fixed, metadata-driven SQL structures over dynamically assembled statements wherever possible.
- Run background workers under a dedicated role with only the privileges required for partition maintenance.
- Avoid using database superuser privileges for routine extension automation.
- Review existing template and catalog identifiers for unexpected quoting characters and remove or rename unauthorized objects.
- Apply database policy controls that limit who can create or alter pg_partman template tables.
How teams detect issues like this
Organizations can look for this class of issue through:
- Static analysis rules that flag SQL concatenation involving catalog values, identifiers, or object names.
- Code review checks for manually added quote characters around dynamic SQL identifiers.
- Tests using identifiers containing reserved words, whitespace, quote characters, and other boundary cases.
- Database audit logging for unexpected ALTER TABLE statements, role changes, or command execution initiated by the background-worker role.
- Policy enforcement requiring dedicated, least-privilege roles for partition-management automation.
- Automated code-review or SAST tools configured to identify unsafe dynamic SQL construction.

