White duck holding a magnifying glass on a dirt farm path, examining animal footprints in the soil.

Erratum: the check_function_bodies post said that SQL-language functions are always parsed and bound when they are created, whatever that parameter is set to. That is true only of the BEGIN ATOMIC form. A SQL function whose body is a string is checked at creation only when check_function_bodies is on, and its names are resolved when it is called, which is most of what this post is about.

A PL/pgSQL function takes one argument it never declared. The argument is search_path, the caller picks the value, and a growing number of the callers are not your application.

The default is "$user", public. The context is user: any role can change it for its own session at any time, and it can also be attached to a role, to a database, or to a single function. The value is a list of schemas. An unqualified name is looked up in each one in order, the first match wins, and an unqualified CREATE goes to the first schema on the list that exists and that you have USAGE on. Two schemas are searched whether you list them or not. pg_catalog goes ahead of everything you listed. The session’s temporary schema, pg_temp, goes ahead of that once the session has one, though only for relations and types; it is never searched for functions or operators. SELECT current_schemas(true) shows the path PostgreSQL is using, implicit entries included, and it is the first thing to run when a name isn’t resolving the way you expect.

Names on the list that match no schema are skipped without comment. That is how the default works for a role that has no schema named after it, and it is also why nearly every mistake in this parameter is a silent one. The classic:

1=> SET search_path = 'app, public';
2SET
3=> SELECT count(*) FROM widgets;
4ERROR: relation "widgets" does not exist

That statement set the path to a single schema whose name is app, public, comma and space included. There is no such schema, so the effective path is pg_catalog and nothing else. Drop the quotes and it does what was meant. ALTER ROLE ... SET and ALTER SYSTEM SET have the same trap; in postgresql.conf and in set_config(), the whole list does go inside one pair of quotes. A schema created with a mixed-case name is a variation on the theme: written without double quotes it folds to lower case, matches nothing, and nobody tells you.

Searched first is not the same as chosen

Having pg_catalog at the front looks like a guarantee that lower() always means the built-in. It isn’t one. For functions, path order only decides between candidates with identical argument types; candidates with different argument types compete on how well they fit the call, wherever they sit on the path. The built-in for strings is lower(text). Put a lower(varchar) in public, and every call on a varchar column goes to that one, since an exact match beats a cast.

This is CVE-2018-1058. Before PostgreSQL 15, any role could create objects in public, and public is on everyone’s default path, the superuser’s included. On 14.24, a function created by a role with nothing but LOGIN ran as the superuser the first time the superuser typed SELECT lower(full_name) FROM people.

PostgreSQL 15 took CREATE on public away from PUBLIC, but only for databases that start life on 15 or later. Anything carried forward keeps the old grant, whether by pg_upgrade or by dump and restore, and pg_upgrade carries it into template1 as well, so databases created after the upgrade have it too. I upgraded a 14 cluster to 18.6, created a new database, and an unprivileged role could still create a table in its public schema. Run \dn+ public in each database; an access privileges line beginning =UC/ means everyone can create there. The fix is REVOKE CREATE ON SCHEMA public FROM PUBLIC, and template1 needs it as well.

The temporary schema is the other place anyone can create objects, and it is searched before everything else. That matters for SECURITY DEFINER functions, which run with their owner’s privileges and their caller’s path. The repair is a SET search_path clause on the function, and the part that gets missed is that SET search_path = admin is not enough, because pg_temp is still implicitly first. 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. The form in the manual is SET search_path = admin, pg_temp. Naming pg_temp last is the only way to make it last. This lists the definer functions whose path does not end with it:

1SELECT p.oid::regprocedure, p.proconfig
2 FROM pg_proc p
3 WHERE p.prosecdef
4 AND NOT EXISTS (SELECT 1 FROM unnest(p.proconfig) c
5 WHERE c ~ '^search_path=(.*, )?pg_temp$');

Everyone else’s search_path

The security problem gets the CVE numbers. The more common problem is plain breakage, and it comes from the way functions are stored.

A view, a column default, a CHECK constraint and an index expression are all stored already parsed, with every name resolved to a specific object. The path stopped mattering to them the moment they were created. A function body is different: PL/pgSQL functions, and SQL functions written as a string, are stored as text and resolved again against the path of whoever calls them. When an index expression, a constraint, a trigger or a materialized view uses such a function, “whoever calls them” covers a good deal more than the application. This is the path the function sees:

Caller 14 to 16 17 and later
The application’s session whatever it set whatever it set
ANALYZE, CLUSTER, CREATE INDEX, REINDEX, REFRESH MATERIALIZED VIEW typed into that session the session’s pg_catalog, pg_temp
vacuumdb, reindexdb, clusterdb empty pg_catalog, pg_temp
Autovacuum empty pg_catalog, pg_temp
pg_restore building an index empty pg_catalog, pg_temp
The logical replication apply worker empty empty
A postgres_fdw connection from another server pg_catalog pg_catalog

The empty entries arrived in security releases: March 2018 for autovacuum, pg_dump and the other client programs, whose release notes told everyone to fix their functions “to not assume anything about what search path they are invoked under,” and August 2020 for logical replication. PostgreSQL 17 extended the rule to maintenance commands no matter who runs them. Every dump file since 2018 has carried the line SELECT pg_catalog.set_config('search_path', '', false); in its header, just above check_function_bodies and row_security.

A function that does assume fails the same way everywhere, with function squish(text) does not exist or relation "tlds" does not exist, about an object that plainly exists. What that costs depends on who was calling.

Autovacuum cannot analyze the table. It fails, logs the error, and tries again on its next pass, indefinitely; in pg_stat_user_tables the table shows a last_autoanalyze that is null or long out of date, and a climbing n_mod_since_analyze. On 16 and earlier an ANALYZE typed into a session with the application’s path still works, which is how this stays hidden for years.

pg_restore reports the failed CREATE INDEX and carries on without the index. A CHECK constraint is worse: the constraint’s function fails on the first row of the COPY, and the table restores empty. pg_restore prints that error too, carries on, and finishes with errors ignored on restore: 1 and exit status 1. Nothing says a table is missing its rows.

Logical replication stops. An index expression on the subscriber, or a trigger enabled for replication, hits the error; the apply worker exits, restarts after wal_retrieve_retry_interval, and hits it again, and nothing behind that transaction is applied. On 15 and later, apply_error_count in pg_stat_subscription_stats is the counter to watch.

pg_upgrade is where this usually surfaces, in one of two ways. If the function in an index expression is an inlinable SQL function, the upgrade itself fails. pg_upgrade --check passes; the real run then dies at “Restoring database schemas in the new cluster,” because re-creating the index plans the expression, and planning inlines the function under a path with nothing of yours on it. I got that going from 14 to 16, 16 to 17, and 16 to 18. If the function is PL/pgSQL, the upgrade succeeds and the failure moves to the vacuumdb --all --analyze-in-stages that pg_upgrade tells you to run next, which stops at the first affected table. In my 16-to-17 test that left the rest of that database, and every database after it in the list, with no statistics at all, and everything before it with only the coarse first-stage ones. (pg_upgrade 18 carries most statistics across, so less is lost, but the vacuumdb --analyze-only it also asks for fails the same way.) None of that is new; it has been true since 2018. What 17 adds is that the application’s own ANALYZE, REINDEX and REFRESH MATERIALIZED VIEW, which worked the day before, now fail too.

There are three repairs. Schema-qualify every name in the function body. Or put the path on the function, ALTER FUNCTION app.norm(text) SET search_path = app, pg_temp, which needs no change to the body and takes effect at once; the stalled subscription above resumed on its own a few seconds after I ran it. Or, for SQL-language functions on 14 and later, write the body as BEGIN ATOMIC ... END, which is parsed when the function is created and stored the way a view is. They differ in cost. A SQL function with a SET clause is never inlined, and in my test that made it three to four times slower than the same function without one; the qualified and BEGIN ATOMIC versions inline as before. PL/pgSQL is never inlined anyway, and there the clause cost a fraction of a microsecond per call, less from 17 on.

PostgreSQL 19 at least names the problem. On the release candidate, the missing-function error reads DETAIL: A function of that name exists, but it is not in the search_path.

Tenants and poolers

One schema per customer, and SET search_path = tenant_a when a request comes in, is a popular way to do multi-tenancy. Behind a transaction-mode pooler it leaks: a session-level SET belongs to the server connection, not to the client that issued it. With PgBouncer 1.26.1 in front of PostgreSQL 16 or 17 and a single server connection, a second client that had set nothing ran an unqualified SELECT and got a row from the first client’s tenant.

SET LOCAL search_path inside the transaction is the form that is correct on every version and behind any transaction-mode pooler. PostgreSQL 18 gives poolers what they need to handle the session-level form: the server now reports search_path to the client whenever it changes, the way it reports client_encoding, and PgBouncer 1.26 tracks every reported parameter a client can change, per client. The same test against 18.6 gave each client its own path on the shared connection. That tracking arrived in September, and PgBouncer’s own 1.26.1 changelog says bugs in it have been reported and are not yet fixed, so I would still use SET LOCAL. (psql 19 uses the same report for a new %S prompt escape, which shows the current path when the server is 18 or later.)

Prepared statements follow the path too. Since 9.3 a change of search_path forces a re-parse, so a prepared SELECT * FROM items reads tenant B’s table after the switch. If two tenants’ tables have drifted apart in shape, it fails instead, with cached plan must not change result type.

So: leave the server default alone. If the application’s objects live in their own schema, put that on the application’s role with ALTER ROLE ... SET search_path, not in postgresql.conf, where it changes what every other role and tool sees. Give every SECURITY DEFINER function a path of its own, and do the same for every function that an index, constraint, trigger or materialized view depends on. The setting is a convenience for whoever is typing. Nothing stored in the database should care what it is.