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.98.2) (envelope-from ) id 1xJmBQ-00000000QYG-1wUm for pgsql-bugs@arkaria.postgresql.org; Thu, 22 Oct 2026 06:21:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xJmBO-00000009A74-326l for pgsql-bugs@arkaria.postgresql.org; Thu, 22 Oct 2026 06:21:46 +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.98.2) (envelope-from ) id 1xJcTo-00000006tRC-2WgY for pgsql-bugs@lists.postgresql.org; Wed, 21 Oct 2026 20:00:08 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xJcTl-00000000HRW-3WGr for pgsql-bugs@lists.postgresql.org; Wed, 21 Oct 2026 20:00:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=UpSkOnIqNJ7/jnJqg+elZzWlYq9p/Wa/ukn2DV+oG/s=; b=bllOezp5aS0pDGmjcIAVK9ARqm JAL12XwOHIad7rAninYO/b3yxQw7FtPL/e2D4R84Dm8BswTimh7XoEUDxwnIJx4grflA/QvYiZvi/ WXuNkW4UYegvNTZXOFtivSQdUHGcjANPd9cJMJIihQuzEydWsiAmW7Iczofieo0SuKWCDjflzWDHT IyLxIfIjU0NxZ+sauqrbaEJO2o4TkTmlqtob2gVQ8ElTtCJLC6PTpaUi8a6mgjoth7epQdxlrfY9v lG32LNg7JnAebsJr37Tm3NeH05040mdHGBPkXX6tT6owxww/E80hrhAbxZ56YW2qrads8V+MLcL08 v6MrG8/Q==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xJcTl-00000000ism-2XVp for pgsql-bugs@lists.postgresql.org; Wed, 21 Oct 2026 20:00:05 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xJcTk-000000026uO-2X85 for pgsql-bugs@lists.postgresql.org; Wed, 21 Oct 2026 20:00:04 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19783: Sorted GROUPING SETS return a grouping column's value in a set that leaves it out To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: alpkeles99@gmail.com Reply-To: alpkeles99@gmail.com, pgsql-bugs@lists.postgresql.org Date: Wed, 21 Oct 2026 19:59:51 +0000 Message-ID: <19783-070e4bf0c73c1076@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19783 Logged by: Alperen Keles Email address: alpkeles99@gmail.com PostgreSQL version: 19rc1 Operating system: macOS 26 arm64 Description: =20 Hello, With a sorted grouping-sets plan (GroupAggregate), a column that is not in the current grouping set can show its real value instead of NULL, while GROUPING() for the same row says it is not grouped. It happens when the row has a NULL somewhere and the target list reads a column that comes after the grouping columns in the Agg's input: CREATE TABLE t (id int PRIMARY KEY, a int, b int, c int); INSERT INTO t VALUES (1, 10, NULL, 100); SET enable_hashagg =3D off; SELECT a, b, c, GROUPING(a, b) AS g FROM t GROUP BY GROUPING SETS ((id, a), (id, b)); a | b | c | g ----+---+-----+--- 10 | | 100 | 1 10 | | 100 | 2 (2 rows) The second row belongs to the set (id, b), so a should be NULL there (g =3D 2 says so). 18.6 and 14.24, and master with enable_hashagg =3D on, give a | b | c | g ----+---+-----+--- 10 | | 100 | 1 | | 100 | 2 With b =3D 20 instead of NULL, or without c in the select list, the result is right. The plan is GroupAggregate Output: a, b, c, GROUPING(a, b), id Group Key: t.id, t.a Sort Key: t.id, t.b Group Key: t.id, t.b -> Sort Output: a, b, id, c Sort Key: t.id, t.a Reproduced on master dd568b047ac (2026-10-21) and 158b6d5109f, macOS arm64, and on master a261fc2b722 and 19rc1 (REL_19_STABLE 1212d5d33b1), Ubuntu 24.04 x86_64, all --enable-cassert. Not on 18.6 (REL_18_STABLE a85b5523e67 on macOS, dfbc1507cdb on Ubuntu) or 14.24 (REL_14_STABLE 14904052e65, Ubuntu). As far as I can tell, prepare_projection_slot() in nodeAgg.c deforms the input slot up to the last grouping column and then sets tts_isnull[] to true for the grouping columns not in the current set. Projecting c then deforms the slot further. Since c456e391138 ("Optimize tuple deformation"), slot_deform_heap_tuple() calls populate_isnull_array() from attribute 0 whenever the tuple has a null bitmap, so tts_isnull[] for the columns already deformed is rewritten from the bitmap, and a's forced NULL is lost. The value in tts_values[] is still a's. This looks like the same overwrite as in the "Whole row var issue" thread, fixed for missing attributes in d260d7d003f; master with that commit still gives the result above. Found with a query fuzzer. Regards, Alperen Keles