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 1tTkyy-009oMk-Gf for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Jan 2025 16:57: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 1tTkyx-003YlW-Nl for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Jan 2025 16:57:07 +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 1tTkyx-003Yl1-Fd for pgsql-bugs@lists.postgresql.org; Fri, 03 Jan 2025 16:57:07 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tTkyv-001v4f-0k for pgsql-bugs@lists.postgresql.org; Fri, 03 Jan 2025 16:57:06 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 503GuvSQ446343; Fri, 3 Jan 2025 11:56:57 -0500 From: Tom Lane To: dqetool@126.com cc: pgsql-bugs@lists.postgresql.org, David Rowley Subject: Re: BUG #18764: server closed the connection unexpectedly In-reply-to: <18764-63ad667ea26e877a@postgresql.org> References: <18764-63ad667ea26e877a@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Fri, 03 Jan 2025 07:36:29 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <446341.1735923417.1@sss.pgh.pa.us> Date: Fri, 03 Jan 2025 11:56:57 -0500 Message-ID: <446342.1735923417@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > CREATE TABLE t0 (c1 INT, c2 DECIMAL); > INSERT INTO t0 VALUES (0, NULL); > INSERT INTO t0 VALUES (8, NULL); > SELECT c2 AS ca2, c2 AS ca3 FROM t0 UNION SELECT DISTINCT ca8 AS ca5, ca7 AS > ca6 FROM (SELECT c1 AS ca7, c2 AS ca8 FROM t0) AS ta1 JOIN (SELECT c1 AS > ca10, c1 AS ca11 FROM t0) AS ta2 ON TRUE; -- server closed the connection > unexpectedly Thanks for the report! It's crashing here: #0 pg_detoast_datum (datum=0x0) at fmgr.c:1834 #1 0x0000000000951603 in DatumGetNumeric (X=0) at ../../../../src/include/postgres.h:314 #2 numeric_fast_cmp (x=0, y=8, ssup=) at numeric.c:2301 #3 0x0000000000a19019 in ApplySortComparator (datum1=0, isNull1=false, datum2=8, isNull2=false, ssup=0x1d92f60) at ../../../../src/include/utils/sortsupport.h:224 #4 0x0000000000a1acc5 in comparetup_heap_tiebreak (a=0x1d98a78, b=0x1d98a90, state=0x1d92b10) at tuplesortvariants.c:1133 #5 0x0000000000a1ab1b in comparetup_heap (a=0x1d98a78, b=0x1d98a90, state=0x1d92b10) at tuplesortvariants.c:1086 #6 0x0000000000a133e7 in qsort_tuple (data=0x1d98a78, n=2, compare=0xa1aa99 , arg=0x1d92b10) at ../../../../src/include/lib/sort_template.h:316 #7 0x0000000000a17cfe in tuplesort_sort_memtuples (state=0x1d92b10) at tuplesort.c:2721 #8 0x0000000000a15529 in tuplesort_performsort (state=0x1d92b10) at tuplesort.c:1382 #9 0x0000000000722b13 in ExecSort (pstate=0x1d61db0) at nodeSort.c:160 #10 0x00000000006f717f in ExecScanFetch ( recheckMtd=0x724f30 , accessMtd=0x724f40 , node=0x1d61c10) at execScan.c:131 I think what is happening is that the executor thinks the sort column is numeric but it's actually integer. If you EXPLAIN VERBOSE the query you see ... -> Subquery Scan on "*SELECT* 2" Output: "*SELECT* 2".ca5, "*SELECT* 2".ca6 -> Sort Output: t0_1.c2, t0_1.c1 Sort Key: t0_1.c2, t0_1.c1 USING < -> HashAggregate Output: t0_1.c2, t0_1.c1 ... That "USING <" annotation is unexpected. I'm pretty sure it's there because the sort operator doesn't match the default btree opclass of the column type, which would be the case if we were trying to use numeric_lt to sort the integer "c1" column. git bisect'ing shows that the failure started here: 66c0185a3d14bbbf51d0fc9d267093ffec735231 is the first bad commit commit 66c0185a3d14bbbf51d0fc9d267093ffec735231 Author: David Rowley Date: Mon Mar 25 14:31:14 2024 +1300 Allow planner to use Merge Append to efficiently implement UNION But it may be that this is a pre-existing problem that just happened to be exposed by the change in plan shape following that commit. Needs more digging. regards, tom lane