postgresql/src/backend
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
..
access Remove replication slot advice from MultiXact wraparound hints 2026-07-03 11:20:31 +09:00
archive Fix memory leaks in error reporting with LOG level 2024-05-14 10:41:32 +02:00
backup Restore basebackup_progress_done() to preserve ABI 2026-07-06 09:47:56 +09:00
bootstrap Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
catalog Remove inappropriate translation marker in getObjectIdentityParts(). 2026-06-08 15:24:03 -04:00
commands Remove replication slot advice from MultiXact wraparound hints 2026-07-03 11:20:31 +09:00
executor Fix incorrect logic for hashed IN / NOT IN with non-strict operators 2026-04-24 14:04:05 +12:00
foreign Restrict accesses to non-system views and foreign tables during pg_dump. 2024-08-05 06:05:30 -07:00
jit Fix another case of indirectly casting away const. 2026-06-04 11:37:43 -04:00
lib Accommodate very large dshash tables. 2024-12-17 15:24:45 -06:00
libpq Fix compilation with OpenSSL 4 2026-06-12 13:57:22 +02:00
main Avoid possible crash within libsanitizer. 2025-11-05 11:09:30 -05:00
nodes IS JSON/JSON(): Protect against expressions uncoercible to text 2026-06-11 16:17:58 +02:00
optimizer Silence "may be used uninitialized" compiler warning. 2026-06-18 11:29:49 -05:00
parser Enforce RETURNING typmod on SQL/JSON DEFAULT behavior expressions 2026-07-07 08:26:50 +09:00
partitioning Prevent satisfies_hash_partition from crashing with VARIADIC NULL. 2026-07-06 12:24:10 -04:00
po Translation updates 2026-05-11 13:05:05 +02:00
port Don't treat EINVAL from semget() as a hard failure. 2025-08-13 11:59:47 -04:00
postmaster Fix orphaned processes when startup process fails during PM_STARTUP 2026-04-21 09:40:04 +09:00
regex Harden our regex engine against integer overflow in size calculations. 2026-05-11 05:13:48 -07:00
replication Avoid stale slot access after dropping obsolete synced slots. 2026-06-18 09:35:53 +05:30
rewrite Disallow renaming a rule to "_RETURN". 2026-07-04 11:34:26 -04:00
snowball Avoid null pointer dereference crash after OOM in Snowball stemmers. 2025-02-18 21:23:59 -05:00
statistics Fix size check in statext_dependencies_deserialize() 2026-05-25 14:39:02 +09:00
storage Prevent access to other sessions' empty temp tables 2026-07-03 18:01:03 +03:00
tcop Make stack depth check work with asan's use-after-return 2026-05-28 11:34:12 -04:00
tsearch dict_synonym.c: remove incorrect outlen. 2026-06-08 11:48:27 -07:00
utils Fix EXPLAIN failure when deparsing SQL/JSON aggregates 2026-07-08 08:50:14 +09:00
.gitignore
common.mk Blind attempt to fix LLVM dependency in the backend 2022-09-15 10:53:48 +07:00
Makefile Fix build inconsistency due to the generation of wait-event code 2026-02-02 08:03:02 +09:00
meson.build meson: Fix import library name in Windows 2024-06-20 09:08:36 +02:00
nls.mk Add missing gettext triggers 2024-05-14 12:57:22 +02:00