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 1xDjIQ-00000000fpt-2UWJ for pgsql-bugs@arkaria.postgresql.org; Mon, 05 Oct 2026 14:04:02 +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 1xDjIP-0000000AniD-2eaq for pgsql-bugs@arkaria.postgresql.org; Mon, 05 Oct 2026 14:04:01 +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 1xDj7t-0000000Amqx-0S5N for pgsql-bugs@lists.postgresql.org; Mon, 05 Oct 2026 13:53:09 +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 1xDj7q-00000000U7R-3AdO for pgsql-bugs@lists.postgresql.org; Mon, 05 Oct 2026 13:53:08 +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=9idXescy6gxHwSwOdOxtQjqQMLaYlRFN0ieKDwqBh5Y=; b=UOVY6WpLee+xeCP4cqSd6z8O/4 URTToSonch4epBoyrwIg9vbdvYDOySn16CK4/CTos9Ns9OXn6j6r+jEcIAlaiGrXq2eYgncnFnwIa nX83L8ZjyKE5BE3PaPetTwObvRfSWzRwUAP4/S9WQwTzgAP2U9PDdjw6IPgzgPYqsuhKBh+FyRPbp QzFRsE3xZbPSs+CbBqgSdZ03kE8brIJP9jaEPYkLOnTmS1ijz/aF7Y/km0CddInxekAfm1wmsQEgY yo9rlz1mo7+vtAPhKkqJ5V0VqjADVEzWLroGKCCwtc2R3YjSCSJmxgf+pwE4nS4DNrfxF7lTCyKgc qO7LtPug==; 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 1xDj7o-00000001ZCs-3oGz for pgsql-bugs@lists.postgresql.org; Mon, 05 Oct 2026 13:53:06 +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 1xDj7n-00000004Toc-2H60 for pgsql-bugs@lists.postgresql.org; Mon, 05 Oct 2026 13:53:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19751: MIN aggregate + FETCH FIRST 1 ROW WITH TIES causes false 21000 error when index scan is forced To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: 1482694023@qq.com Reply-To: 1482694023@qq.com, pgsql-bugs@lists.postgresql.org Date: Mon, 05 Oct 2026 13:52:10 +0000 Message-ID: <19751-db95b8241f0e33d9@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: 19751 Logged by: N J Email address: 1482694023@qq.com PostgreSQL version: 18.6 Operating system: Windows 11 64-bit Description: =20 Environment: PostgreSQL: [fill in your exact version] OS: Windows 11 64-bit Steps to reproduce: 1. Create test table and insert data: CREATE TEMP TABLE readings ( region_id integer NOT NULL, reading integer NOT NULL ); INSERT INTO readings (region_id, reading) VALUES (2, 7), (2, 1); 2. Create index and collect statistics: CREATE INDEX ON readings (region_id, reading); ANALYZE readings; 3. Force index scan and execute the query: SET enable_seqscan =3D off; SELECT MIN(reading) AS minimum FROM ( SELECT reading FROM readings WHERE region_id =3D 2 ) AS scoped ORDER BY minimum FETCH FIRST 1 ROW WITH TIES; Observed result: ERROR: more than one row returned by a subquery used as an expression SQL state: 21000 Expected result: minimum --------- 1 (1 row) Bug analysis: This is a valid standard SQL query. The subquery is only used as a derived table in the FROM clause, there is no scalar subquery in the statement. The 21000 cardinality error should never be raised here. The error only triggers under these conditions: - Sequential scan is disabled, so the optimizer uses an index scan to optimize the MIN() aggregate - The query includes FETCH FIRST 1 ROW WITH TIES - The inner derived table returns multiple rows It appears that when the optimizer combines the index-optimized MIN() path with the WITH TIES logic, it incorrectly generates an execution plan that treats the internal index scan subplan as a scalar subquery, resulting in a spurious error when the subplan returns multiple rows. This is an optimizer correctness bug.