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.94.2) (envelope-from ) id 1s95Qk-00HENP-Dc for pgsql-bugs@arkaria.postgresql.org; Mon, 20 May 2024 16:00:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s95Qk-003Hao-1W for pgsql-bugs@arkaria.postgresql.org; Mon, 20 May 2024 16:00:06 +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.94.2) (envelope-from ) id 1s95Qj-003Hag-NM for pgsql-bugs@lists.postgresql.org; Mon, 20 May 2024 16:00:05 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s95Qg-00190H-T0 for pgsql-bugs@postgresql.org; Mon, 20 May 2024 16:00:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Reply-To: Message-ID:Subject:To:Sender:From:Date:Cc:Content-ID:Content-Description: In-Reply-To:References; bh=LsU6XGzijvxklJA4/aLGGdtn8+DDvI1TGyCk3a1xzPU=; b=qy Gy1zNLUcKpOfOQxemRhIzNP9OUJ4yBHzTfx9qZsJrsScoPb9/gfA8ldDjojtf9kV+7c7LKar38NI8 Lh6w/J37eyB4RpJ47Fo4OICyUMXDsUiuk5g46I33+HqvYcsYpSi7zBrazvO8tOUB9SzOJPhGRPtt5 DYbQGUmJI0aMOJo=; Received: from depesz by depesz.com with local (Exim 4.96) (envelope-from ) id 1s95Qd-00G0Bo-39 for pgsql-bugs@postgresql.org; Mon, 20 May 2024 18:00:00 +0200 Date: Mon, 20 May 2024 17:59:59 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: pgsql-bugs mailing list Subject: UNION removes almost all rows (not duplicates) - in fresh build of pg17! Message-ID: Reply-To: depesz@depesz.com MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk So, test case is trivial: #v+ $ select count(*) from pg_class where relkind = 'r'; count ─────── 68 (1 row) $ select count(*) from pg_class where relkind = 'i'; count ─────── 164 (1 row) $ select count(*) from ( select * from pg_class where relkind = 'r' union select * from pg_class where relkind = 'i' ); count ─────── 1 (1 row) #v- explain shows unexpected: #v+ $ explain (analyze) select count(*) from ( select * from pg_class where relkind = 'r' union select * from pg_class where relkind = 'i' ); QUERY PLAN ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Aggregate (cost=42.43..42.44 rows=1 width=8) (actual time=0.108..0.109 rows=1 loops=1) -> Unique (cost=0.00..39.53 rows=232 width=236) (actual time=0.013..0.105 rows=1 loops=1) -> Append (cost=0.00..39.53 rows=232 width=236) (actual time=0.012..0.099 rows=232 loops=1) -> Seq Scan on pg_class (cost=0.00..19.19 rows=68 width=263) (actual time=0.012..0.058 rows=68 loops=1) Filter: (relkind = 'r'::"char") Rows Removed by Filter: 348 -> Seq Scan on pg_class pg_class_1 (cost=0.00..19.19 rows=164 width=263) (actual time=0.002..0.030 rows=164 loops=1) Filter: (relkind = 'i'::"char") Rows Removed by Filter: 252 Planning Time: 0.161 ms Execution Time: 0.129 ms (11 rows) #v- It seems that I get 232 correct rows from Append, but then Unique removes all of them, except for one? For whatever it's worth returned row is: #v+ $ select * from pg_class where relkind = 'r' union select * from pg_class where relkind = 'i' \gx ─[ RECORD 1 ]───────┬─────────────────────── oid │ 2619 relname │ pg_statistic relnamespace │ 11 reltype │ 10029 reloftype │ 0 relowner │ 10 relam │ 2 relfilenode │ 2619 reltablespace │ 0 relpages │ 19 reltuples │ 410 relallvisible │ 19 reltoastrelid │ 2840 relhasindex │ t relisshared │ f relpersistence │ p relkind │ r relnatts │ 31 relchecks │ 0 relhasrules │ f relhastriggers │ f relhassubclass │ f relrowsecurity │ f relforcerowsecurity │ f relispopulated │ t relreplident │ n relispartition │ f relrewrite │ 0 relfrozenxid │ 730 relminmxid │ 1 relacl │ {pgdba=arwdDxtm/pgdba} reloptions │ [null] relpartbound │ [null] #v- As for test environment: Debian testing on amd64, self-compiled from git HEAD at d2a04470aa6401c1938cc107e0b2c56c22a2321f Did I do something wrong? Best regards, depesz