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 1xR5j2-00000000fn8-1Oil for pgsql-bugs@arkaria.postgresql.org; Wed, 11 Nov 2026 10:38:44 +0000 Received: from localhost ([127.0.0.1]:44518 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xR5iz-0000000AE0t-1eoQ for pgsql-bugs@arkaria.postgresql.org; Wed, 11 Nov 2026 10:38:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:41336) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xR3zz-00000009bam-0b9o for pgsql-bugs@lists.postgresql.org; Wed, 11 Nov 2026 08:48:07 +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 1xR3zy-00000000LK3-0elY for pgsql-bugs@lists.postgresql.org; Wed, 11 Nov 2026 08:48:06 +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=Oznq8HWqvnUPn6hMzevtBTbf1jYwXZMApfBoxCp0W5M=; b=1WEVLaXc5pOoR9e2hokqh6ZyvP UUeLZnj+oV+xIdKQ3gVQOqhqOJMioHRZue44+1tmpOHgGAJjH+WFlgcLz4Rtua7EERP+TV4gYOQIN 3KIIKHSoN6iivAwtZVIwYNX0W+lsfboBgW7q9GHAs9bruKg5JmtH8BHMgDxENfVeOKIfwS1ydwFHk murW+/6wVNAfbXgw03V4Ef4r9/rObVkthNSZRJlr4ivHyk4X39c6Jfxd+mjUJA88kLu4pzNonmFLD Jt3qehzell/lWc8i3Y6yM8NTZCTUTj1DUDpgIgCOug4sU5Zrys7WjtgqnV7NzTJFuyRZ9KXRYItwo BcILYfaw==; 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 1xR3zw-00000000wx1-2HHE for pgsql-bugs@lists.postgresql.org; Wed, 11 Nov 2026 08:48: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 1xR3zv-00000002VPx-2A0Z for pgsql-bugs@lists.postgresql.org; Wed, 11 Nov 2026 08:48:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19870: WinGetFuncArgInPartition(..., WINDOW_SEEK_TAIL, ...) returns wrong rows under IGNORE NULLS To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: 303677365@qq.com Reply-To: 303677365@qq.com, pgsql-bugs@lists.postgresql.org Date: Wed, 11 Nov 2026 08:48:00 +0000 Message-ID: <19870-a58b65793ce2a466@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: 19870 Logged by: chunling qin Email address: 303677365@qq.com PostgreSQL version: 19rc1 Operating system: Linux x86_64; platform-independent (reproduced on Description: =20 **Description:** A user-defined C window function that calls the public API `WinGetFuncArgInPartition(winobj, argno, relpos, WINDOW_SEEK_TAIL, ...)` gets wrong rows when the call runs with `IGNORE NULLS`: 1. **Wrong results on data with NO NULLs** =E2=80=94 where `IGNORE NULLS` m= ust be a no-op: with `seektype=3DTAIL, relpos=3D-2` the IGNORE result differs from t= he RESPECT result for every row (5/5 diffs), and the returned values come from the wrong rows. With `seektype=3DCURRENT` or `HEAD` the two agree (0 diffs), so the defect is specific to the TAIL branch. 2. **Internal error for large positive relpos**: `relpos =3D INT_MAX` with TAIL + IGNORE NULLS raises `ERROR: XX000: invalid memory alloc request size 1073741824` (it tries to expand the not-null bitmap for an out-of-range row position), while TAIL + RESPECT NULLS correctly returns NULL. The server stays up; only the statement fails. Commit b8d82d17875 fixed wrong results of some built-in paths under IGNORE NULLS, but this public-API TAIL branch still computes the starting row position as if the not-null bitmap could be consulted there, and applies relpos twice. **Reproduction (minimal C window function + SQL; verified on master):** C probe (compile with `cc -fPIC -shared -I"$(pg_config --includedir-server)" -o /tmp/probe.so probe.c`): ```c #include "postgres.h" #include "fmgr.h" #include "windowapi.h" PG_MODULE_MAGIC; PG_FUNCTION_INFO_V1(win_partition_arg); /* win_partition_arg(value, relpos int4, seektype int4) -> value seektype: 0=3DCURRENT 1=3DHEAD 2=3DTAIL (WINDOW_SEEK_*) */ Datum win_partition_arg(PG_FUNCTION_ARGS) { WindowObject winobj =3D PG_WINDOW_OBJECT(); bool isnull =3D false; bool isout =3D false; int32 relpos; int32 seektype; Datum result; WinCheckAndInitializeNullTreatment(winobj, true, fcinfo); relpos =3D DatumGetInt32(WinGetFuncArgCurrent(winobj, 1, &isnull)); seektype =3D DatumGetInt32(WinGetFuncArgCurrent(winobj, 2, &isnull)= ); result =3D WinGetFuncArgInPartition(winobj, 0, relpos, seektype, false, &isnull, &isout); if (isnull) PG_RETURN_NULL(); PG_RETURN_DATUM(result); } ``` SQL (5 rows, **no NULLs** =E2=80=94 IGNORE NULLS must be a no-op here): ```sql CREATE FUNCTION win_partition_arg(anyelement, int4, int4) RETURNS anyelement AS '/tmp/probe.so', 'win_partition_arg' LANGUAGE C WINDOW; CREATE TABLE b1_txt (k int, x text); INSERT INTO b1_txt VALUES (1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e'); -- (1) diffs per seektype (RESPECT vs IGNORE must be 0 everywhere on no-NULL data): SELECT 'CURRENT rp=3D2' AS seektype, count(*) FILTER (WHERE r IS DISTINCT F= ROM i) AS diffs FROM (SELECT win_partition_arg(x,2,0) RESPECT NULLS OVER w AS r, win_partition_arg(x,2,0) IGNORE NULLS OVER w AS i FROM b1_txt WINDOW w AS (ORDER BY k)) s UNION ALL SELECT 'HEAD rp=3D2', count(*) FILTER (WHERE r IS DISTINCT FROM i) FROM (SELECT win_partition_arg(x,2,1) RESPECT NULLS OVER w AS r, win_partition_arg(x,2,1) IGNORE NULLS OVER w AS i FROM b1_txt WINDOW w AS (ORDER BY k)) s UNION ALL SELECT 'TAIL rp=3D-2', count(*) FILTER (WHERE r IS DISTINCT FROM i) FROM (SELECT win_partition_arg(x,-2,2) RESPECT NULLS OVER w AS r, win_partition_arg(x,-2,2) IGNORE NULLS OVER w AS i FROM b1_txt WINDOW w AS (ORDER BY k)) s; -- CURRENT rp=3D2 | 0 -- HEAD rp=3D2 | 0 -- TAIL rp=3D-2 | 5 <- bug: every row wrong -- (2) the actual wrong values (TAIL, relpos=3D-1): SELECT k, win_partition_arg(x,-1,2) RESPECT NULLS OVER w AS respect, win_partition_arg(x,-1,2) IGNORE NULLS OVER w AS ignore FROM b1_txt WINDOW w AS (ORDER BY k) ORDER BY k; -- 1..5: respect =3D d on every row; ignore =3D c on every row (wrong rows returned) -- (3) large positive relpos, TAIL: SELECT win_partition_arg(x,2147483647,2) RESPECT NULLS OVER w FROM b1_txt WINDOW w AS (ORDER BY k) LIMIT 1; -- NULL (correct) SELECT win_partition_arg(x,2147483647,2) IGNORE NULLS OVER w FROM b1_txt WINDOW w AS (ORDER BY k) LIMIT 1; -- ERROR: XX000: invalid memory alloc request size 1073741824 (should return NULL) ``` **Expected behavior:** on data with no NULLs, `IGNORE NULLS` must return exactly what `RESPECT NULLS` returns for every seektype; and an out-of-range relpos must yield NULL, not an internal error =E2=80=94 as already happens = for CURRENT/HEAD. **Related:** b8d82d17875 (fixed some built-in IGNORE NULLS paths) and 69b20fdd20d (fixed the first_value/nth_value infinite loop) =E2=80=94 this = TAIL branch of the public API is the remaining face of the same family.