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 1wh6BP-00039D-1V for pgsql-bugs@arkaria.postgresql.org; Tue, 07 Jul 2026 13:49:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wh6BN-000pCH-19 for pgsql-bugs@arkaria.postgresql.org; Tue, 07 Jul 2026 13:49:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wh6BN-000pC8-0K for pgsql-bugs@lists.postgresql.org; Tue, 07 Jul 2026 13:49:53 +0000 Received: from mail-wr1-f51.google.com ([209.85.221.51]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wh6BK-000000002jP-3uxN for pgsql-bugs@postgresql.org; Tue, 07 Jul 2026 13:49:53 +0000 Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-475cb71a4ebso4260976f8f.0 for ; Tue, 07 Jul 2026 06:49:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1783432190; cv=none; d=google.com; s=arc-20260327; b=dg8uS1LLWWoq2je2ZbRuAsLkdc8HcnAF/27WglIhmIl9fCdEenY5snmu+D+Ko/kc8I MA9PKXygPEdI2vrHWBZQrHDCFf2jd45/UHJw8lPDNvpCqKRGo8DU/K/TsBQZkeOsxItD 58vYe/uMBONiRdmS2Fwrbl1g6QFYDBlTp0pHjEJjq5+tOUEdK1Ks1hdpJAEsXAcOjZfO m3e9DRmfblZ6M1V8p1ebyqKDa0x64Z/tshzcFaGYdTisHWIC6Hz0lGhAzObweDY8nOPr mXREEvcDTJre2zkMyRIK+D21Gk1Gh11ksWxGWLeh31IF5WKXEGgJVJ1oOPAAwR4+C5cn f6/Q== 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; bh=4UIB4INtBieAAfsUtqUL+R7LDkFUHHZuXkBId8cenpA=; fh=I3eq9JLue6E0avFHTsCcomGp3fA3Ma3e1aqZSlFoqdo=; b=UnH07dYJ0vQtv0SXK5DPufumaQDjJMmS/OXvI6ZWi1V/cvbw88nzz04GVsbkcCsJU2 m0tEh3ISRWT8frilbdeOeuIt7BAXRgKIe85qPgNUE1vU7gAf5uXZP0PbSPo5BFVwpMVd vmm9sT1G/Gq+1dKPDPPXfzlMc4Z31gmAuj8OOy5T6Jn1Xy1F6vbONtcmvIRQ4Q5AMuX+ U+qMA7od3a35UnWOBhqIGoqeVwuD35J9fl62lHjKI+7fHDwO9pdOjVVM6hWLjzvy3kyl 0Om3LiZFhhmMJVJqcldS/i5jzvWWSR4+RypQ9D7yvTuvMHsGZ5MfQ3UmFydLkiH/HfUw 2/Kw==; 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=1783432190; x=1784036990; 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=4UIB4INtBieAAfsUtqUL+R7LDkFUHHZuXkBId8cenpA=; b=r+yofTXBOBiaZGmGGSPHkHwYhh9ihk9TjiejN2LXwq/gL26f4qAwNcst/qQifuIUyx UzgGWHLWjej4dl7oZd6QD5k7w5r/zrbRUOK3RlCdm5BA0NaCcl5/v/0B9W7T+yW4Ey/8 dWXsUKL7yyy8w6J70sdrsd6Qir8YrE6aOpadGWiiVFWjV8bvqxVKrlefmeyNAajBOEqf lthWjVamHZ1qIJKWdCWFqPkg0/yKYtKTltiY3fYFYvn8ejysRkiuFU34eoI3Ya9jTDQn /4Z4tpmvWWt0faKlkPzVl8S4Yw2FsgfIm6yLJBhc/bFV+B7rJHceoY0rR7LDzKAMPkAy 9r8w== X-Gm-Message-State: AOJu0Yxk63K9luBUKTeF/uUIFGnJaXYSZJ70IBFr+Ja1DAsxh7ea/76f JYAlcXZ3iZDIDK0YQi4rVoEnZBhV9jzlHr/eGaKq0OGuINTH96EIlpe9AZT0IxMyALE7rc1RGse i9sbfr70N6Urgk4Rugrkec3tn4x6nD90= X-Gm-Gg: AfdE7cky7VuolXkFMP9vM2gafeNPeGz9Cwys5OEqur7YiVKsJ9aDVJaXCkaSVzDOrJY 4/lmCHCXsEDffZ+mJ1A5x4yo8d8/PdGWiDCq6RSQhBQnptyP53NMKNSeHbrfKeFVaSHu0RIkwtI PZQus+PQGYIUkP4JWdCcIyZFc2eZdEd611QofNiaTa2NvpmRnblyK4ZQw0rIKJftnfMgMmVQjt1 54ZlyELTFjcbc6jBdeKJbDlm3wvNH3iBv8VRqgFbkVy0jQBsCXRyq22mtijKMMKMALxLpCCLbKW 1PZNKSQ= X-Received: by 2002:a5d:53d1:0:b0:475:f0c2:75ac with SMTP id ffacd0b85a97d-47de66fb85cmr4667148f8f.61.1783432190137; Tue, 07 Jul 2026 06:49:50 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Thom Brown Date: Tue, 7 Jul 2026 14:49:23 +0100 X-Gm-Features: AVVi8CcEH43bvR--7YuclS77n5q-Gq4qGJA_geklLiiDglIl6hFD8q-JuGc36ys Message-ID: Subject: Re: EXPLAIN (VERBOSE) fails with for JSON_ARRAYAGG/JSON_OBJECTAGG + window function To: Richard Guo 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 Mon, 6 Jul 2026 at 03:17, Richard Guo wrote: > > On Fri, Jul 3, 2026 at 5:36=E2=80=AFPM Thom Brown wrote: > > Thanks for taking a look at this. It's unfortunate that reconstructing > > the syntax is problematic, because in the case of the original query > > that tripped on this bug, I lose the following from my original query: > > ... > > I guess the relevant information can be gleaned from this, but not for > > JSON_ARRAYAGG(i RETURNING text) because we get the same output whether > > we're returning text or json. > > Yeah, the v1 patch is lossy, and the lost information cannot be > recovered from the plan at all. > > So I'm switching back to the resolve_special_varno() reconstruction I > had tried and set aside. My earlier objection was that it makes the > WindowAgg look like it computes a JSON aggregate when it only passes > through a value computed below. But the lower node still prints > jsonb_agg_strict(name), so the plan does show where the aggregate is > computed, and I think that is a smaller cost than losing the > information outright. Also, partial aggregation already deparses this > way. The combining aggregate's argument is a Var referencing the > partial aggregate's output, and get_agg_expr() resolves it back with > resolve_special_varno() to reprint the aggregate at the finalizing > node. > > I still don't want to hack the planner to keep the JsonConstructorExpr > with its Aggref in make_window_input_target(), because I still think > that that is too invasive and changes which node evaluates the > wrapper. > > Hence, I end up with the attached v2 patch. I've tested it against the output from the 1st patch. Patch 1: ((jsonb_agg_strict(e.emp_name ORDER BY e.emp_name))) Patch 2: (JSON_ARRAYAGG(e.emp_name ORDER BY e.emp_name RETURNING jsonb)) 1: ((jsonb_object_agg_strict(e.emp_name, e.salary))) 2: (JSON_OBJECTAGG(e.emp_name : e.salary ABSENT ON NULL RETURNING jsonb)) With that last one, why is that colon there? Thom