public inbox for [email protected]  
help / color / mirror / Atom feed
pgsql: Fix EXPLAIN failure when deparsing SQL/JSON aggregates
5+ messages / 1 participants
[nested] [flat]

* pgsql: Fix EXPLAIN failure when deparsing SQL/JSON aggregates
@ 2026-07-07 23:51  Richard Guo <[email protected]>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-07-07 23:51 UTC (permalink / raw)
  To: [email protected]

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 <[email protected]>
Author: Richard Guo <[email protected]>
Discussion: https://postgr.es/m/CAA-aLv5QYTaMOk=Qhv6cgwceeHETZV8YJvWZ_rH+yVZCuchATA@mail.gmail.com
Backpatch-through: 16

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/96ab9a990eed2265c6b073646d1566a1dd2bd3f7

Modified Files
--------------
src/backend/utils/adt/ruleutils.c     |  32 +++++++++++
src/test/regress/expected/sqljson.out | 101 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/sqljson.sql      |  41 ++++++++++++++
3 files changed, 174 insertions(+)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix EXPLAIN failure when deparsing SQL/JSON aggregates
@ 2026-07-07 23:51  Richard Guo <[email protected]>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-07-07 23:51 UTC (permalink / raw)
  To: [email protected]

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 <[email protected]>
Author: Richard Guo <[email protected]>
Discussion: https://postgr.es/m/CAA-aLv5QYTaMOk=Qhv6cgwceeHETZV8YJvWZ_rH+yVZCuchATA@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/eaa561fb6e278ed7ce293ed7d1e7ddaa7354180b

Modified Files
--------------
src/backend/utils/adt/ruleutils.c     |  32 +++++++++++
src/test/regress/expected/sqljson.out | 101 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/sqljson.sql      |  41 ++++++++++++++
3 files changed, 174 insertions(+)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix EXPLAIN failure when deparsing SQL/JSON aggregates
@ 2026-07-07 23:51  Richard Guo <[email protected]>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-07-07 23:51 UTC (permalink / raw)
  To: [email protected]

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 <[email protected]>
Author: Richard Guo <[email protected]>
Discussion: https://postgr.es/m/CAA-aLv5QYTaMOk=Qhv6cgwceeHETZV8YJvWZ_rH+yVZCuchATA@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/45364e49688a2a845c6b43b182da3b5f3d10cff8

Modified Files
--------------
src/backend/utils/adt/ruleutils.c     |  32 +++++++++++
src/test/regress/expected/sqljson.out | 101 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/sqljson.sql      |  41 ++++++++++++++
3 files changed, 174 insertions(+)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix EXPLAIN failure when deparsing SQL/JSON aggregates
@ 2026-07-07 23:51  Richard Guo <[email protected]>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-07-07 23:51 UTC (permalink / raw)
  To: [email protected]

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 <[email protected]>
Author: Richard Guo <[email protected]>
Discussion: https://postgr.es/m/CAA-aLv5QYTaMOk=Qhv6cgwceeHETZV8YJvWZ_rH+yVZCuchATA@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/dcda1f07da48bcb591de47cd99fb45f841482fb5

Modified Files
--------------
src/backend/utils/adt/ruleutils.c     | 32 ++++++++++++
src/test/regress/expected/sqljson.out | 98 +++++++++++++++++++++++++++++++++++
src/test/regress/sql/sqljson.sql      | 41 +++++++++++++++
3 files changed, 171 insertions(+)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix EXPLAIN failure when deparsing SQL/JSON aggregates
@ 2026-07-07 23:51  Richard Guo <[email protected]>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-07-07 23:51 UTC (permalink / raw)
  To: [email protected]

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 <[email protected]>
Author: Richard Guo <[email protected]>
Discussion: https://postgr.es/m/CAA-aLv5QYTaMOk=Qhv6cgwceeHETZV8YJvWZ_rH+yVZCuchATA@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/485527190a97f7eb57a1ce28e140d9e5f3f60f0a

Modified Files
--------------
src/backend/utils/adt/ruleutils.c     | 32 ++++++++++++
src/test/regress/expected/sqljson.out | 98 +++++++++++++++++++++++++++++++++++
src/test/regress/sql/sqljson.sql      | 41 +++++++++++++++
3 files changed, 171 insertions(+)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2026-07-07 23:51 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-07-07 23:51 pgsql: Fix EXPLAIN failure when deparsing SQL/JSON aggregates Richard Guo <[email protected]>
2026-07-07 23:51 pgsql: Fix EXPLAIN failure when deparsing SQL/JSON aggregates Richard Guo <[email protected]>
2026-07-07 23:51 pgsql: Fix EXPLAIN failure when deparsing SQL/JSON aggregates Richard Guo <[email protected]>
2026-07-07 23:51 pgsql: Fix EXPLAIN failure when deparsing SQL/JSON aggregates Richard Guo <[email protected]>
2026-07-07 23:51 pgsql: Fix EXPLAIN failure when deparsing SQL/JSON aggregates Richard Guo <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox