postgresql/src/backend/utils
Richard Guo dcda1f07da Fix EXPLAIN failure when deparsing SQL/JSON aggregates
If an expression containing an aggregate is evaluated above the plan
node that computes the aggregate, as happens with window functions or
with expressions postponed to above the final sort, setrefs.c replaces
the Aggref or WindowFunc with a Var referencing the lower node's
output.  For SQL/JSON aggregates such as JSON_ARRAYAGG and
JSON_OBJECTAGG, deparsing the containing JsonConstructorExpr then
failed with "invalid JsonConstructorExpr underlying node type", since
get_json_agg_constructor() did not expect a Var there.

Fix by resolving the Var back to the underlying Aggref or WindowFunc
and deparsing the constructor as if the aggregate were computed at the
current node.  The JsonConstructorExpr retains the RETURNING clause
and the ABSENT/NULL ON NULL and WITH UNIQUE options, and the arguments
come from the resolved aggregate, so the original JSON aggregate
syntax is reproduced in full.  This mirrors how get_agg_expr() already
looks through such a Var when deparsing a combining aggregate.

Reported-by: Thom Brown <thom@linux.com>
Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAA-aLv5QYTaMOk=Qhv6cgwceeHETZV8YJvWZ_rH+yVZCuchATA@mail.gmail.com
Backpatch-through: 16
2026-07-08 08:50:14 +09:00
..
activity Re-introduce pgstat_drop_entry(), keeping ABI compatibility 2026-06-23 07:59:00 +09:00
adt Fix EXPLAIN failure when deparsing SQL/JSON aggregates 2026-07-08 08:50:14 +09:00
cache Fix missed checks for hashability of container-type equality. 2026-06-08 11:48:17 -04:00
error Fix incorrect message-printing in win32security.c. 2025-10-13 17:56:45 -04:00
fmgr Revise GUC names quoting in messages again 2024-05-17 11:44:26 +02:00
hash Fix invalid format string in HASH_DEBUG code 2025-08-15 18:06:35 +12:00
init Fix compilation warning with SerializeClientConnectionInfo() 2025-08-14 16:22:00 +09:00
mb pg_mblen_range, pg_mblen_with_len: Valgrind after encoding ereport. 2026-02-14 12:16:20 -08:00
misc Document wal_compression=on 2026-07-01 09:05:25 +07:00
mmgr Make palloc_array() and friends safe against integer overflow. 2026-05-11 05:13:48 -07:00
resowner Fix unsafe order of operations in ResourceOwnerReleaseAll(). 2026-06-22 18:03:23 -04:00
sort Be more careful to preserve consistency of a tuplestore. 2026-03-30 13:59:54 -04:00
time Update TransactionXmin when MyProc->xmin is updated 2024-12-21 23:42:52 +02:00
.gitignore Fix build inconsistency due to the generation of wait-event code 2026-02-02 08:03:02 +09:00
errcodes.txt Introduce transaction_timeout 2024-02-15 23:56:12 +02:00
Gen_dummy_probes.pl Update copyright for 2024 2024-01-03 20:49:05 -05:00
Gen_fmgrtab.pl Update copyright for 2024 2024-01-03 20:49:05 -05:00
generate-errcodes.pl Update copyright for 2024 2024-01-03 20:49:05 -05:00
Makefile Fix build inconsistency due to the generation of wait-event code 2026-02-02 08:03:02 +09:00
meson.build Update copyright for 2024 2024-01-03 20:49:05 -05:00
postprocess_dtrace.sed Update copyright for 2024 2024-01-03 20:49:05 -05:00
probes.d Update copyright for 2024 2024-01-03 20:49:05 -05:00