Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wfPc6-005R3k-17 for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Jul 2026 22:10:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wfPby-003Uz9-1u for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Jul 2026 22:10:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wfPby-003Uz0-16 for pgsql-bugs@lists.postgresql.org; Thu, 02 Jul 2026 22:10:22 +0000 Received: from mail-oo1-xc2a.google.com ([2607:f8b0:4864:20::c2a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wfPbw-00000001G96-39d6 for pgsql-bugs@postgresql.org; Thu, 02 Jul 2026 22:10:21 +0000 Received: by mail-oo1-xc2a.google.com with SMTP id 006d021491bc7-6a0e525db1eso1496162eaf.1 for ; Thu, 02 Jul 2026 15:10:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1783030220; cv=none; d=google.com; s=arc-20260327; b=P+HlUuQEiEhNbW/ki8kMq2a54mkJzbwEHlhG1j0AVthLmuiKkFKQH5O8LT9h999mzO hmmzp5eO9hSAgD6zH2Fl3HS68wsrxMqRXSgPsl52c3DMYLSmAFw1NvqVca+Rozg0Cy20 /fs//ankTdjpUXopjwMYcTh0SSCGAn6i9WqFC7ztmKVK9ijKSz1wcVinpAovYtCH8C7p eclYxw8NrdmnbkAvO4j4J8Jk6+z4Wd/bc+hRGppDBo8B0Z4tgbiK+7g9YR1h4yfpeOBp +Cr0maTIGQQAoVqN0Otsp52X286R03b5xWtxjhY1RNlQz4UA5d9On9kTsQR5nJDrX2h/ McVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=+LHCLGkD6amC9Eck2mWqlOQQTUQ2oeolMG3ve/HN2Bs=; fh=fNPwCKKQjS7loeG0QdVScHdB5f+5X6umzsZdLK/kF0g=; b=P+VX64XjcrtoDSKABxfqJyZY8QydIeAuOf4nBqjSwAmYw8Fvs7Rmw1fdlN4+52G42H oSNnD5pTaIwn6/xwrTuJWtBAk3cP7DpgRlcHe9MfV616aWMrKhb1CpQG5ZLKJ2S5Yu4p XhZQJty2WdWihPDa2y3y8kVpObWX1G6PCCdc6ZiyJxmWVBCsiKQQFlOPLpvAoVTbgRiB ScNL2hyivOmL8BZxddTJftXY/O2+XrOMNa/7EL86YWnLDikHMXWKFyKuZr8iu754NcDV L9iPkGjnNXqD5bpwx2jnZ2aI5Ifxo8zdo+zLa96zc1XP9jivA9bWYAqtPAcmO1CZZjj8 RwZA==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783030220; x=1783635020; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=+LHCLGkD6amC9Eck2mWqlOQQTUQ2oeolMG3ve/HN2Bs=; b=f+kEVXT4CCqdWIaOKu9eCHcJtyGzOaLlmh9sT8LN4XtgxebnRauBGv4q75DAKrCzni AEjnnrHeiyvGjMpN0tbzKER3LdRc9gppcIJJHv3l731QwXKrc7BBbBJi9XoucGL6vRHS ldBMiXw+9XQ+d5wFRZ+6g8vBHcFvYoHUMxDSftUCLigeJC08Vuvvs5yUPiNDjLC8Dozc kOIZLv5PNdXSBaLWhuAYRInK1kufvGgHGsqnkKwvPtkZ029PcwFXToNIqO6V5ZJCHxFI y+HmtDqgSfRlza7SXklJ5PFuibFQplYVNZyoJl8di2vhDa1GSwEkrNwaOrKjbhGM1TYd if0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783030220; x=1783635020; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=+LHCLGkD6amC9Eck2mWqlOQQTUQ2oeolMG3ve/HN2Bs=; b=c0Z8lMwMF5kQBitDuSccjEGafBj12X7TsoZpG3oHW8W1aZqxsLi98ibrKd6Ahl7FnJ MYP2acZ0L5vY2L7XBy5Sh7yKyUFLiMSQ9dbzqeH4HM20s9JyK5WnyVKIQc9qyiZe4sQ5 DPpdGNhupAs6Eudyj0PHFs15LGGGtZuiEdACYc6wSSnHgvTz0imrnt7E5Zh7/2+JS7OI KRzApkZjeBrMQrJbAYdDXP2urGwhRrHCY2LA4dpXoBCW2UKXte+a9bQFJ70NHgADftwf RzNq+KYOnfyb5wVRc1TjG0gZCUt1I3Bf2w8X+HIjG7uB9w7j+sKq1a8cr+ZUxp9KEOi8 UWcA== X-Gm-Message-State: AOJu0YwtCGSmlHJHAKZfm5mo81zHrxD2Wo/ssqWCEhFc+iTQiMuAIESa 2r1ibHEZ5MiwrhAYfF42S/s8LeLIRKOx3i/hTYqwV6hBLMCYxt9HSoRHdALJqKPgirJ30D/EyBb uVpQIKIr/PEG5DjMgQwp+ut9cGE1Pd2rXMclt X-Gm-Gg: AfdE7cn+DthUMw0XGKgBVcEIdqZVbp1IXS92l+YRk6pUcb7uwU+toEdxMat0UhVlkDS jAWwXhqde20O868U+IRlS5lSLiXDG2urYEpOJAo6ci0e/H3L2pwYwiJAeoXHSDz+xQy6tdqZymx tfx5FteaDxyOYelJ7bCxEJhqhiET9zlNpm+woIJ1uNu2CjUCzaT/LbVXd5wVSfew5qfctdXHdR4 LCfy4WP/Ke8pD/WTQCQjdyj/xAVztUmOiJqcp3KuzCYCCV7GNIkVfqZfUQhSe2bZN8w9ehcpA== X-Received: by 2002:a05:6820:1ca8:b0:6a3:1e40:6332 with SMTP id 006d021491bc7-6a31e40751emr857611eaf.59.1783030220059; Thu, 02 Jul 2026 15:10:20 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Richard Guo Date: Fri, 3 Jul 2026 07:10:08 +0900 X-Gm-Features: AVVi8CccPfJGaXpsL5Z2Dw4SyuC4ubzWy-hMHBLFfnIddwfIg8UQ5mIhU-3uCGE Message-ID: Subject: Re: EXPLAIN (VERBOSE) fails with for JSON_ARRAYAGG/JSON_OBJECTAGG + window function To: Thom Brown Cc: pgsql-bugs Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Jul 3, 2026 at 1:58=E2=80=AFAM Thom Brown wrote: > CREATE TEMP TABLE t (g int, name text); > INSERT INTO t VALUES (1, 'a'), (1, 'b'), (2, 'c'); > > EXPLAIN (VERBOSE) > SELECT g, > JSON_ARRAYAGG(name RETURNING jsonb) AS names, > row_number() OVER (ORDER BY g) AS rn > FROM t > GROUP BY g; > ERROR: invalid JsonConstructorExpr underlying node type: 6 Reproduced here. get_json_agg_constructor() expects that ctor->func is Aggref or WindowFunc, but what it gets here is a Var. This is because the query has both window functions and grouped aggregates. make_window_input_target() flattens the final tlist using pull_var_clause, which pulls the Aggref out of its JsonConstructorExpr wrapper. Then fix_upper_expr() matches that inner Aggref against the Agg subplan's tlist and replaces it with an OUTER Var. A simple fix is: --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -12389,6 +12389,8 @@ get_json_agg_constructor(JsonConstructorExpr *ctor, deparse_context *context, get_windowfunc_expr_helper((WindowFunc *) ctor->func, context, funcname, options.data, is_json_objectagg); + else if (IsA(ctor->func, Var)) + get_rule_expr((Node *) ctor->func, context, false); else elog(ERROR, "invalid JsonConstructorExpr underlying node type: %d", nodeTag(ctor->func)); Will work on a patch on it. - Richard