postgresql/src
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
..
backend Fix EXPLAIN failure when deparsing SQL/JSON aggregates 2026-07-08 08:50:14 +09:00
bin pg_dump: check for _beginthreadex() failure in parallel dump 2026-07-07 18:11:44 +03:00
common unicode_case.c: defend against truncated UTF8. 2026-07-07 13:35:23 -07:00
fe_utils psql: Fix expanded aligned output 2026-06-08 14:37:57 +09:00
include Restore basebackup_progress_done() to preserve ABI 2026-07-06 09:47:56 +09:00
interfaces libpq: Drain all pending bytes from SSL/GSS during pqReadData() 2026-07-07 18:46:58 +03:00
makefiles Optimize pg_popcount() with AVX-512 instructions. 2024-04-06 21:56:23 -05:00
pl plpython: Fix NULL pointer dereferences for broken sequence and mapping objects 2026-06-29 11:40:49 +09:00
port Make pg_mkdir_p() tolerant of a concurrent directory creation. 2026-06-19 12:52:00 -04:00
template Remove AIX support 2024-02-28 15:17:23 +04:00
test Fix EXPLAIN failure when deparsing SQL/JSON aggregates 2026-07-08 08:50:14 +09:00
timezone Guard against unsafe conditions in usage of pg_strftime(). 2026-05-11 05:13:49 -07:00
tools Modernize pg_bsd_indent's error/warning reporting code. 2026-06-15 12:22:55 -04:00
tutorial Update copyright for 2024 2024-01-03 20:49:05 -05:00
.gitignore
DEVELOPERS
Makefile Remove distprep 2023-11-06 15:18:04 +01:00
Makefile.global.in Don't put library-supplied -L/-I switches before user-supplied ones. 2025-07-29 15:17:41 -04:00
Makefile.shlib Remove AIX support 2024-02-28 15:17:23 +04:00
meson.build Update copyright for 2024 2024-01-03 20:49:05 -05:00
nls-global.mk Fix update-po for the PGXS case 2025-10-16 20:21:05 +02:00