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 1wfKkK-005Oni-2i for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Jul 2026 16:58:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wfKkI-002PF9-1U for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Jul 2026 16:58:38 +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 1wfKkI-002PF1-0h for pgsql-bugs@lists.postgresql.org; Thu, 02 Jul 2026 16:58:38 +0000 Received: from mail-wr1-f46.google.com ([209.85.221.46]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wfKkG-00000001FBN-1w19 for pgsql-bugs@postgresql.org; Thu, 02 Jul 2026 16:58:37 +0000 Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-4758b2a9e2aso1338613f8f.2 for ; Thu, 02 Jul 2026 09:58:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1783011514; cv=none; d=google.com; s=arc-20260327; b=nJ7Jjl+lzf5lZQm4AeIpZ2JnPPpaUDl55dinXVXlCu1u2EYK6MK/BzIyfjp3SXZ3zK Wu2e9/3jBjV3KjnOx/KtF0tnTVUce2qRZDAG4FWY6Wr0vKSMaUCQVDAZ6/gAHX/1+8DJ heXmmgOh0zUsanI+tGgR5/mLS42BIyPj4d0AI4AYC1tWNNa7B5g+jK1ICyCG/Oi3S1ZR d496HbGu51hbVLyBcr1wNgplPYYJItKJeSSkkC803jecv5CbwyROqL85+5nWyLIQ4n3e 9eJV6F6m0t7TtwRAjF7ISN53pIP1kWkTM5I/f5yyz5W8/iVOQ1mF9KLYsbyI/+N9krhD z7FA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=to:subject:message-id:date:from:mime-version; bh=t53i/36NiSss7w0o+1R0OE77RwqwpA4PBRoAYzbEZwQ=; fh=7ME8Hg/rCF+UwtNclnz3OTgtlFpopYHO471DZgxX86c=; b=svapj8E1n1NQQU5pN0o16XzJwF3/M032rD+ct9gCZ0vUkcueThAelaq/yqZOSI7Gd1 B/Hs4c7wSbhtlrtmm2BEjk24cQTZSYBPUr4E2X2WbPjl+eVUAGHONJKhDPl4rH30cAwi TlbWZ8XnK/VnZTuJL/S8XgACa6K+u0nvfBX+M90feHpnXZQs8+W9f/7N9/E/FL3SJoRq akj3YYX0HRaFW3JhTixz3MVvybI749KTJnYfiziM0RjbiI+Mfis0uFtrybeFeETUicBN WqTGx3ocMA9ugw9Wu4Qk8PpPPVkHobG3Ajz8PjUq19gtlNbKV7xqMTwAwUE9JbYBzo9H 1sUA==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783011514; x=1783616314; h=to:subject:message-id:date:from:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=t53i/36NiSss7w0o+1R0OE77RwqwpA4PBRoAYzbEZwQ=; b=DduF/+qbCPuP8z5aOxnBsqREHjlzj994Em2ywPyayycCSILsD3lAOXUmxPm4SAR6Zi EpzRUWtoAeRXe+aHifPy253pWvxxJnuFDt/MtL8CddQ04351fJ3C8iifCty4jRtVJWSy W5sc/BRJWJcKzhMd5/tYb1I1fiZ2AcTZGRL5S0Yue5nc9dU6IP/S73UiCm377vq34skq bmKr22QFcUKBmT3fMPumnK0w0UTZgcLBUs+a2s/B7lk4A9uIFrpD5/mRvSsN3uyieO2q MjQmjKqVAjoeCY3PmGGBDpVCOLAKMoywK/qsvgnhwzV8DdWIocTd8a/xcIc00qzJqLBn cTew== X-Gm-Message-State: AOJu0YzqApJuVq9PkC26zgLDQ80c2BnsNplsLWhEc9dSVFzsleryBB4m H6cCHy4Or36fSMR07ahfhBdzWacJ8XorTgbmaMwKNIFNVnZJcKPbzVmtiGoDyhVAY3IDbl5K7LE UXW4a55NwdlfIclLi7G0YMjxEsYU9qHL4O14C+tLbVA== X-Gm-Gg: AfdE7cmXgKL4nhhhQb/t7EJ6IQWud58v8MgyE2Ms3LL89UruXYB+MiF1Aqc5f20kzDQ cfoIIGmwMcGxZ5NKjtxz2AceYx25QJHpboeQDKQ4b3z0gADaJuPfQ5puv3aMmUCnnHktE3jSbnS glRIme6FKYDBP74DBWc//c+cvRz+1X0vChCU07S6pJJnWBx3lYpCOLo8GkN6PPirHp6L6AbvlyQ cJ8Vxvegn0UeiBOtxF7J+rDHf3AwebgLn+DVTkeVHRdD6fSKUFvvbrV0p3HWV+ilEjPrrRI92pH vuHzM8c= X-Received: by 2002:a5d:584e:0:b0:476:cb01:6387 with SMTP id ffacd0b85a97d-477586065a8mr10905071f8f.17.1783011514068; Thu, 02 Jul 2026 09:58:34 -0700 (PDT) MIME-Version: 1.0 From: Thom Brown Date: Thu, 2 Jul 2026 17:58:07 +0100 X-Gm-Features: AVVi8Cef0b6Ojqb0EW2zNTpt8BOHB3xOGMMXwZauSEf7Yc28wb_YKVWVgu8y74o Message-ID: Subject: EXPLAIN (VERBOSE) fails with for JSON_ARRAYAGG/JSON_OBJECTAGG + window function To: pgsql-bugs Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, EXPLAIN (VERBOSE) throws an internal error when a query's target list contains an SQL/JSON aggregate (JSON_ARRAYAGG or JSON_OBJECTAGG) together with a window function (although I suspect it's for anything that forces a plan node on top of the aggregation node). I ran into it when trying to construct an obscenely complex SELECT statement. The query itself executes fine. Only the attempt to deparse its plan for EXPLAIN (VERBOSE) fails. I'm using the latest master branch, but I believe the bug is also present in 16, 17 and 18, since the responsible code has been unchanged since SQL/JSON constructors were added by commit 7081ac46ace in 2023. Debian, Linux 6.12.90 (6.12.90+deb13.1-amd64), x86_64 gcc (Debian 14.2.0-19) 14.2.0 configure options: --with-llvm --with-libxml --with-ossp-uuid --with-lz4 --with-liburing --with-openssl No non-default server settings are required to reproduce. Reproducible test case: 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; Result: ERROR: invalid JsonConstructorExpr underlying node type: 6 The same happens with JSON_OBJECTAGG. Note that without EXPLAIN (VERBOSE), or just using EXPLAIN with any options except VERBOSE, the query runs and returns correct results. Removing the window function makes EXPLAIN (VERBOSE) work, because then a single Aggregate node computes the aggregate and there is no upper node to reference it from. Expected result: WindowAgg Output: g, JSON_ARRAYAGG(name RETURNING jsonb), row_number() OVER w1 ... -> HashAggregate Output: g, jsonb_agg_strict(name) Regards Thom