set_config(‘search_path’) does not truncate names over 63 bytes, unlike SET

Started by Manav Kumar26 days ago8 messagesbugs
Jump to latest
#1Manav Kumar
mkumar@yugabyte.com

*PostgreSQL version:* 18.6
*Operating system:* Debian 13 (official postgres:18 Docker image), x86_64

*Short description:*
search_path set via set_config() keeps names longer than NAMEDATALEN-1
untruncated, unlike SET

Details:

When search_path is set with set_config(), a schema name longer than 63
bytes
(NAMEDATALEN - 1) is stored and reported in full, but a truncated name is
used
for lookup. SET truncates the same name up front and raises a NOTICE. So
SHOW / current_setting() / pg_settings report a value that differs from the
one
actually in effect, with no notice.

Repro:

CREATE SCHEMA
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; --
70 chars
-- NOTICE: identifier "aaa...(70)" will be truncated to "aaa...(63)"

SELECT set_config('search_path', repeat('a', 70) || ', public', false);
-- no NOTICE
SHOW search_path; -- aaa...(70), public
SELECT current_setting('search_path'); -- aaa...(70), public
SELECT current_schemas(false); -- {aaa...(63),public}
CREATE TABLE t (i int); -- created in schema aaa...(63)

SET search_path =
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
public;
-- NOTICE: identifier "aaa...(70)" will be truncated to "aaa...(63)"
SHOW search_path; -- aaa...(63), public

The truncation at use time happens in SplitIdentifierString() (called from
check_search_path() and recomputeNamespacePath()), which works on a copy, so
the stored string keeps the full name.

Expected: the two ways of setting search_path should agree. Either
set_config() / check_search_path() store the truncated names (as SET does),
or at least raise the same NOTICE when a name will be truncated. As it is,
an application that reads back current_setting('search_path') sees a value
that isn't the one in effect.

The same applies to other list GUCs split with SplitIdentifierString(),
such as
temp_tablespaces.

#2shihao zhong
zhong950419@gmail.com
In reply to: Manav Kumar (#1)
Re: set_config(‘search_path’) does not truncate names over 63 bytes, unlike SET

Hi Manav,

I am not sure if this is a bug.

The long name still finds the right schema. Only the text you read
back is longer.

SET truncates because the parser truncates identifiers. If you give
SET a quoted string, it behaves just like set_config().

Thanks,
SHihao

#3Daniel Gustafsson
daniel@yesql.se
In reply to: shihao zhong (#2)
Re: set_config(‘search_path’) does not truncate names over 63 bytes, unlike SET

On 4 Nov 2026, at 04:46, shihao zhong <zhong950419@gmail.com> wrote:

I am not sure if this is a bug.

It's not a bug, but it's not ideal that it can be used to force the server to
send a message which triggers an errorpath leading to fatal error shutdown in
the protocol code.

postgres=# SELECT set_config('search_path', repeat('a', 10000000) || ', public', false);
lost synchronization with server: got message type "S", length 10000025
The connection to the server was lost. Attempting reset: Succeeded.

Everything works as intended but it would be nice to handle this in a more
graceful way.

--
Daniel Gustafsson

#4shihao zhong
zhong950419@gmail.com
In reply to: Daniel Gustafsson (#3)
Re: set_config(‘search_path’) does not truncate names over 63 bytes, unlike SET

Hi Daniel,

That is a good point!

It does not need 10MB. Anything over 30000
bytes does it. libpq only accepts messages that long for a few types,
and ParameterStatus is not one of them. search_path became
GUC_REPORT in 18, so the following patch needs backport to 18.

It is worse with ALTER ROLE. A role with a search_path that long can't
login anymore. But likely nobody is doing that.

ALTER ROLE r SET search_path = '<30100 bytes>';

$ psql -U r
psql: error: ... lost synchronization with server

The attached 001 adds ParameterStatus to VALID_LONG_MESSAGE_TYPE
002 is optional test.

Thanks,
Shihao

Attachments:

t254406_4
v1-0002-Add-a-test-for-long-ParameterStatus-messages.patchapplication/octet-stream; name=v1-0002-Add-a-test-for-long-ParameterStatus-messages.patchDownload+14-1
v1-0001-libpq-Accept-long-ParameterStatus-messages.patchapplication/octet-stream; name=v1-0001-libpq-Accept-long-ParameterStatus-messages.patchDownload+1-1
#5Daniel Gustafsson
daniel@yesql.se
In reply to: shihao zhong (#4)
Re: set_config(‘search_path’) does not truncate names over 63 bytes, unlike SET

On 4 Nov 2026, at 15:47, shihao zhong <zhong950419@gmail.com> wrote:

The attached 001 adds ParameterStatus to VALID_LONG_MESSAGE_TYPE

I haven't had time to investigate what could be done here (if anything), but
I'm sure it isn't weakening protections to handle a synthetic case.

--
Daniel Gustafsson

#6shihao zhong
zhong950419@gmail.com
In reply to: Daniel Gustafsson (#5)
Re: set_config(‘search_path’) does not truncate names over 63 bytes, unlike SET

Hi Daniel,

I'm sure it isn't weakening protections to handle a synthetic case.

My original thought was that we already had a mechanism for messages
over 30k, so why not just reuse it.

Going back, if we want to fix this, I see two directions.

1. Client side. I guess your concern is that adding
PqMsg_ParameterStatus turns off the protection for all ParameterStatus
messages. I did that because other GUCs like application_name could
also run into this issue. But given there are only a handful of
GUC_REPORT parameters, I thought at first it might not be a big deal.

If we want to keep the fix on the client side, we may need a whitelist
for these GUCs which is complex

2. Limit the length in guc. A long value then fails with a normal guc
error instead of a disconnect. I can do it in a check hook one by one
or create a new mechanism to limit the size of string guc?

I feel if we want to do that in server side we can start with check hook
on these two GUC, what do you think?

Thanks,
Shihao

#7Daniel Gustafsson
daniel@yesql.se
In reply to: shihao zhong (#6)
Re: set_config(‘search_path’) does not truncate names over 63 bytes, unlike SET

On 4 Nov 2026, at 22:05, shihao zhong <zhong950419@gmail.com> wrote:

I'm sure it isn't weakening protections to handle a synthetic case.

My original thought was that we already had a mechanism for messages
over 30k, so why not just reuse it.

I see it the other way around, we have a protection against messages over 30k,
and that framing makes it unappealing here.

2. Limit the length in guc. A long value then fails with a normal guc
error instead of a disconnect. I can do it in a check hook one by one
or create a new mechanism to limit the size of string guc?

This is the only possibility IMHO. Is there a case where setting a longer
value than what SET allows will be useful for anything?

--
Daniel Gustafsson

#8shihao zhong
zhong950419@gmail.com
In reply to: Daniel Gustafsson (#7)
Re: set_config(‘search_path’) does not truncate names over 63 bytes, unlike SET

Hi Daniel,

This is the only possibility IMHO. Is there a case where setting a
longer value than what SET allows will be useful for anything?

I don't think so. A schema name over 63 bytes is never useful, since
the lookup only uses the first 63 bytes. But SET truncates each name,
not the list. A SET search_path with 500 names of 63 bytes is about
32kB, and it drops the connection the same way. So a limit per name is
not enough.

So 3 changes in the 0001
1. It limits every GUC_REPORT because extension can use this flag to
shoot themself in their foot.
2. It handles client -> server encoding conversion by
MAX_CONVERSION_GROWTH
3. Move the magic number 30000 to a #define.

0002 is an optional test.

Thanks,
Shihao

Attachments:

t254406_8
v2-0001-Limit-the-length-of-reported-string-GUCs.patchapplication/octet-stream; name=v2-0001-Limit-the-length-of-reported-string-GUCs.patchDownload+44-6
v2-0002-Add-a-test-for-the-search_path-length-limit.patchapplication/octet-stream; name=v2-0002-Add-a-test-for-the-search_path-length-limit.patchDownload+63-1