PostgreSQLDev & Eng
Content type: Concept
8 min read
PostgreSQL's search_path hides bugs that only appear in production
- Reading
- 8 min
- Tables
- 1
- Code
- 1
An apparently harmless session parameter, search_path, is resolved differently by autovacuum, logical replication, and pg_upgrade, and that explains silent failures that only show up months later in production databases.
Christophe Pettus, a consultant at PGX Inc., published on Planet PostgreSQL a detailed analysis of a parameter every DBA thinks they understand: search_path. The piece, part of a series on GUCs (Grand Unified Configuration), shows that the problem isn't the parameter itself, but the fact that different internal PostgreSQL processes resolve it in different ways. That creates bugs that don't show up in the development environment and only explode once autovacuum, logical replication, or pg_upgrade come into play.
What search_path actually does
search_path defines which schemas PostgreSQL searches for an unqualified name, in the order listed. The default is "$user", public. Two schemas are always searched even when they don't appear in the explicit list: pg_catalog comes before everything else, and the session's temporary schema, pg_temp, comes before that, but only for relations and types, never for functions.
The command SELECT current_schemas(true) shows the effective path, including the implicit entries. It's the first diagnostic to run when a name doesn't resolve as expected.
Names in the list that don't correspond to any schema are ignored without warning. That's how the default works for a role with no matching schema, and it's also why almost every error with this parameter is silent. The classic example cited by Pettus:

SET search_path = 'app, public';
SELECT count(*) FROM widgets;
-- ERROR: relation "widgets" does not existThe quotes turned app, public into a single schema name, which doesn't exist. The effective path becomes just pg_catalog. Removing the quotes fixes it. ALTER ROLE ... SET and ALTER SYSTEM SET have the same trap.
pg_catalog first doesn't guarantee anything
A common mistake is assuming that, with pg_catalog always first, a call to lower() can never land on a different function. That's not true: for functions, path order only breaks ties between candidates with identical argument types. Candidates with different types compete for the best fit for the call, regardless of where they sit in the path.
The built-in for strings is lower(text). If someone creates a lower(varchar) in public, every call on a varchar column starts going to that function, because an exact type match beats an implicit cast. That mechanism is exactly what gave rise to CVE-2018-1058: before PostgreSQL 15, any role could create objects in public, a schema that's in everyone's default path, including the superuser's.
PostgreSQL 15 removed CREATE on public from the PUBLIC role by default, but only for databases created from scratch on version 15 or later. Databases migrated via pg_upgrade or dump/restore keep the old grant, and pg_upgrade carries that over even to template1, which means databases created after the upgrade also inherit the gap. The \dn+ public command shows whether the privilege is still open: a line starting with =UC/ indicates that any role can create there. The fix is REVOKE CREATE ON SCHEMA public FROM PUBLIC, also applied to template1.
SECURITY DEFINER functions and pg_temp
The temporary schema is the other place where any role can create objects, and it's searched before everything else. That matters especially for SECURITY DEFINER functions, which run with the owner's privileges but with the caller's path.
The fix seems obvious, SET search_path = admin, but it's not enough, because pg_temp is still implicitly first. Pettus tested this directly:
I tested it: a role with no rights on admin.pwds created a temporary table named pwds, and a definer function with exactly that clause read the temporary table.
Christophe Pettus, PostgreSQL consultant at PGX Inc.
The correct form, according to the manual, is SET search_path = admin, pg_temp. Listing pg_temp last is the only way to guarantee that it actually ends up last.
The detail that matters most for those who maintain indexes: who calls it changes the path
Here's the central point of the article for anyone who cares about performance and integrity: a view, a column DEFAULT, a CHECK, and an index expression are stored with names already resolved. The path stops mattering for them at creation time.
A PL/pgSQL function, or an SQL function written as a string, is different: it's stored as text and resolved again, against the caller's path, every time it runs. When an index, a CHECK, a trigger, or a MATERIALIZED VIEW uses that function, "the caller" stops being just the application.
Behavior varies by process, and it has changed across versions:
| Caller | Versions 14 to 16 | 17 onward |
|---|---|---|
| Application session | whatever it sets | whatever it sets |
ANALYZE, CREATE INDEX, REINDEX, REFRESH MATERIALIZED VIEW typed in session | session path | pg_catalog, pg_temp |
vacuumdb, reindexdb, clusterdb | empty | empty |
| autovacuum | empty | empty |
| logical replication worker | empty | empty |
postgres_fdw connection | pg_catalog | pg_catalog |
The empty values arrived in security patches: March 2018 for autovacuum and the client programs, August 2020 for logical replication. PostgreSQL 17 extended the rule to manually typed maintenance commands, which broke routines that had worked for years. Every dump file since 2018 carries in its header the line SELECT pg_catalog.set_config('search_path', '', false);, right above check_function_bodies.
A function that assumes a path fails the same way everywhere, with errors like function squish(text) does not exist, about an object that clearly exists. The cost depends on who called it:
- Autovacuum can't analyze the table, fails, logs the error, and tries again on the next pass, indefinitely. In
pg_stat_user_tables, this shows up as a null or stalelast_autoanalyzeandn_mod_since_analyzeclimbing without stopping. - pg_restore reports the failure on
CREATE INDEXand moves on without the index. It's worse with aCHECK: the function fails on the first line ofCOPY, and the table is restored empty, with the process ending inerrors ignored on restore: 1. - Logical replication stops for good: an index expression or a trigger enabled for replication on the subscriber generates the error, the apply worker exits, retries after
wal_retrieve_retry_interval, and gets stuck on the same transaction. In versions 15+, theapply_error_countcounter inpg_stat_subscription_statsexposes this.
Where this usually blows up: pg_upgrade
According to Pettus, pg_upgrade is where the problem usually shows up, in two ways. If the function in an index expression is an SQL function the planner can inline, the upgrade itself fails: pg_upgrade --check passes, but the actual run dies at the "Restoring database schemas in the new cluster" stage, because recreating the index plans the expression under an empty path.
If the function is PL/pgSQL, the upgrade finishes successfully and the problem shifts to the vacuumdb --all --analyze-in-stages run that pg_upgrade itself recommends afterward, which stops at the first affected table. In Pettus's test going from 16 to 17, that left the rest of that database, and every subsequent database in the list, with no statistics at all. Version 18 of pg_upgrade preserves more statistics during the process, but the vacuumdb --analyze-only it also suggests fails the same way.
The recommended fix comes in three forms, with different costs:
- Qualify every name in the function body with the explicit schema.
- Put the path on the function itself:
ALTER FUNCTION app.norm(text) SET search_path = app, pg_temp, which doesn't require changing the body and takes effect immediately. - For SQL functions (14+), write the body as
BEGIN ATOMIC ... END, which is parsed and stored already resolved at creation time, like a view.
They're not equivalent in performance: an SQL function with a SET clause is never inlined by the planner, and in the author's test that made it three to four times slower than the version without the clause. The qualified version and BEGIN ATOMIC keep being inlined normally. In PL/pgSQL, which is never inlined anyway, the cost of the clause is a fraction of a microsecond per call, even smaller from version 17 onward.
Schema-based multi-tenancy behind a pooler
A popular multi-tenancy architecture uses one schema per customer and switches the path with SET search_path = tenant_a on every request. Behind a pooler in transaction mode, that leaks: a session SET belongs to the physical connection to the server, not to the logical client that issued the command.
Pettus reproduced the leak with PgBouncer 1.26.1 in front of PostgreSQL 16 and 17: with a single server connection, a second client that had set nothing ran an unqualified SELECT and got back a row from the first client's tenant.
SET LOCAL search_path inside the transaction is the correct form in any version and behind any transaction-mode pooler. PostgreSQL 18 started reporting search_path to the client whenever it changes, the same way it already does with client_encoding, and PgBouncer 1.26 started tracking these parameters per client. In the author's test against version 18.6, each client kept its own path on the shared connection, but PgBouncer's own 1.26.1 changelog admits there are still unfixed bugs in that tracking.
What this means for database operators
Pettus's practical recommendation is straightforward: don't touch the server's default search_path. If the application's objects live in their own schema, that goes in ALTER ROLE ... SET search_path, never in postgresql.conf, which changes what every role and every tool sees. Every SECURITY DEFINER function, and every function used in an index, a CHECK, a trigger, or a MATERIALIZED VIEW, needs its own explicit path. search_path is a convenience for someone typing into psql; nothing stored in the database should depend on it.
Translated from the Brazilian Portuguese original · Read the original
How to update MySQL replication filters without rebuilding the replica
Percona has published a walkthrough for excluding and re-including tables on a live MySQL replica using mydumper and binlog coordinates, without touching the primary or rebuilding anything.





