| From: | PG Bug reporting form <noreply(at)postgresql(dot)org> |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Cc: | nakamurajames123(at)gmail(dot)com |
| Subject: | BUG #19921: auto_explain Reads SQL Function Parameters from an Unpinned Buffer |
| Date: | 2026-11-19 01:44:39 |
| Message-ID: | 19921-d97d058e0417cb2b@postgresql.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
The following bug has been logged on the website:
Bug reference: 19921
Logged by: Fin
Email address: nakamurajames123(at)gmail(dot)com
PostgreSQL version: 19rc2
Operating system: wsl linux
Description:
Reproduction Steps
Server configuration (postgresql.conf):
shared_preload_libraries = 'auto_explain'
auto_explain.log_min_duration = 0
auto_explain.log_nested_statements = on
The default value of auto_explain.log_parameter_max_length (-1, which logs
all parameter values) is used.
Execute the following statements as a regular user:
CREATE TABLE t (v text);
INSERT INTO t SELECT repeat('abc', 10) FROM generate_series(1, 100);
CREATE FUNCTION srf(a text) RETURNS SETOF int LANGUAGE sql
AS $$ SELECT g FROM generate_series(1, 10) g $$;
SELECT srf(v) FROM t LIMIT 1;
Expected Behavior
When auto_explain logs statements executed inside a SQL function, it should
only access parameter values that are still valid.
Even if the function terminates early due to a LIMIT clause, auto_explain
must not read memory that is no longer valid.
Actual Behavior
When the outer query terminates early due to LIMIT, the SQL function is
subsequently cleaned up through the shutdown callback (ShutdownSQLFunction).
The issue occurs during this cleanup process:
- The outer Seq Scan has already completed and released its buffer pin.
- However, auto_explain attempts to log the SQL function parameter ($1) by
reading its value directly from the buffer page.
- PostgreSQL's Valgrind annotations mark unpinned buffers as inaccessible.
In a build where these annotations are connected to AddressSanitizer, the
invalid memory access is detected as a use-after-poison.
- Both the master branch (PostgreSQL 20devel) and PostgreSQL 19.0 are
affected.
In a normal build, the query is expected to complete without an error.
However, if another session replaces the buffer page before the parameter is
logged, auto_explain could log an incorrect value or read beyond the valid
memory bounds due to a corrupted varlena length.
These potential consequences have not been directly verified in a normal
build.
Logs
1. psql output, master (PostgreSQL 20devel, 0cf2d43d326).
In the ASan-enabled build, the backend aborts when the invalid memory access
is detected.
SELECT srf(v) FROM t LIMIT 1;
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
2. Server log, master (source path prefixes have been shortened):
2026-11-19 10:34:46.142 KST [3180054] LOG: duration: 0.336 ms plan:
Query Text: SELECT srf(v) FROM t LIMIT 1;
Limit (cost=0.00..0.01 rows=1 width=4)
-> ProjectSet (cost=0.00..7170.40 rows=1360000 width=4)
-> Seq Scan on t (cost=0.00..23.60 rows=1360 width=32)
=================================================================
==3180054==ERROR: AddressSanitizer: use-after-poison on address
0x7b71d1f7bfe0 at pc 0x5bc7c53fe144 bp 0x7fff15f40e10 sp 0x7fff15f40e08
READ of size 1 at 0x7b71d1f7bfe0 thread T0
#0 VARATT_IS_COMPRESSED src/include/varatt.h:396:9
#1 pg_detoast_datum_packed src/backend/utils/fmgr/fmgr.c:1832:6
#2 text_to_cstring src/backend/utils/adt/varlena.c:220:23
#3 FunctionCall1Coll src/backend/utils/fmgr/fmgr.c:1141:11
#4 OutputFunctionCall src/backend/utils/fmgr/fmgr.c:1686:25
#5 OidOutputFunctionCall src/backend/utils/fmgr/fmgr.c:1769:9
#6 BuildParamLogString src/backend/nodes/params.c:386:15
#7 ExplainQueryParameters src/backend/commands/explain.c:1092:8
#8 explain_ExecutorEnd contrib/auto_explain/auto_explain.c:456:4
#9 postquel_end src/backend/executor/functions.c:1456:3
#10 ShutdownSQLFunction src/backend/executor/functions.c:1983:4
#11 ShutdownExprContext src/backend/executor/execUtils.c:1067:4
#12 FreeExprContext src/backend/executor/execUtils.c:426:2
#13 FreeExecutorState src/backend/executor/execUtils.c:211:3
#14 standard_ExecutorEnd src/backend/executor/execMain.c:530:2
#15 PortalCleanup src/backend/commands/portalcmds.c:310:4
#16 PortalDrop src/backend/utils/mmgr/portalmem.c:513:3
#17 exec_simple_query src/backend/tcop/postgres.c:1311:3
#18 PostgresMain src/backend/tcop/postgres.c
Address 0x7b71d1f7bfe0 is a wild pointer inside of access range of size
0x000000000001.
SUMMARY: AddressSanitizer: use-after-poison src/include/varatt.h:396:9 in
VARATT_IS_COMPRESSED
LOG: client backend (PID 3180054) was terminated by signal 6: Aborted
DETAIL: Failed process was running: SELECT srf(v) FROM t LIMIT 1;
The "wild pointer" classification appears because AddressSanitizer is
unaware of the shared memory allocation. The address actually points into a
shared buffer page.
3. PostgreSQL 19.0 (REL_19_STABLE, 19aa5713be2).
The same issue is detected through the same execution path.
READ of size 1 at 0x6ebf95399fe0 thread T0
#0 VARATT_IS_COMPRESSED src/include/varatt.h:350:9
#1 pg_detoast_datum_packed src/backend/utils/fmgr/fmgr.c:1832:6
#6 BuildParamLogString src/backend/nodes/params.c:386:15
#7 ExplainQueryParameters src/backend/commands/explain.c:1090:8
#8 explain_ExecutorEnd contrib/auto_explain/auto_explain.c:457:4
#9 postquel_end src/backend/executor/functions.c:1456:3
#10 ShutdownSQLFunction src/backend/executor/functions.c:1983:4
#13 FreeExecutorState src/backend/executor/execUtils.c:211:3
SUMMARY: AddressSanitizer: use-after-poison src/include/varatt.h:350:9 in
VARATT_IS_COMPRESSED
| From | Date | Subject | |
|---|---|---|---|
| Next Message | PG Bug reporting form | 2026-11-19 03:27:49 | BUG #19922: CYCLE/SEARCH with a window in the recursive term: could not find replacement targetlist entry |
| Previous Message | TAKATSUKA Haruka | 2026-11-19 01:42:03 | Re: BUG #19897: Recovery fails after "LOG: consistent recovery state reached" |