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 1xDm9u-00000000hV1-0Fj1 for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Oct 2026 17:07:26 +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 1xDm9t-0000000C5j6-0HAJ for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Oct 2026 17:07:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xDm9s-0000000C5ix-3795 for pgsql-hackers@lists.postgresql.org; Mon, 05 Oct 2026 17:07:24 +0000 Received: from mail-qk2-x0f.google.com ([2607:f8b0:4864:34::f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1xDm9q-00000000Z10-0bCp for pgsql-hackers@postgresql.org; Mon, 05 Oct 2026 17:07:23 +0000 Received: by mail-qk2-x0f.google.com with SMTP id d75a77b69052e-5337b1cdcadso21888241cf.3 for ; Mon, 05 Oct 2026 10:07:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791220040; x=1791824840; darn=postgresql.org; h=content-disposition:content-type:mime-version:message-id:subject:to :from:date:from:to:cc:subject:date:message-id:reply-to:content-type; bh=b4xeNmKB/sLL2jPT/my79B3mcpiudHZu3HdToT/SrHs=; b=Rg+oQoVpZHPTLIVDO+sxKFm22xwfCTk4F4JbE/eOI7A6inlDR3RjXHEfben+wP7Abp jwyrxqNb+0OXjGMUKo+yjS3y5uApkhK2uQM3ZeLhll0JNncQTtRJ46LfuFDlHFMtJUcU ZAlVV+Isb1TeuaE2lv1mnu0OtD2gkUmGHkHEoXWs30jXWgXEhkwWi0orB6GoNtdu3nU3 6UJBgpTuPwQzcfq7B4p/TjitrbGcuNBQ9DTJt4NdqKn5fYbDv5strrj1x9Id3jwvUCCF EpxZAJIesTd1cPZm838oXMZpq907LNKKn64ZijXsrtes4QlYVRhPhZMw+djBgdyejK3V eCuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791220040; x=1791824840; h=content-disposition:content-type:mime-version:message-id:subject:to :from:date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=b4xeNmKB/sLL2jPT/my79B3mcpiudHZu3HdToT/SrHs=; b=QdfSzkD3UmHjPStUkXSTZ2A3AVMt9IFTZo2a0DlxTH0FZPwAz57yWF5SCM5uYaaw2/ Eg8umH4C1H4XIxBEEGEMKOekhBNnND13p7LOMwPVt+pizF/b6oX2MqHDqJGO1dQpx8zB Egjxkh+WEylCRXKzO/FNEgf/TLT5TLcTWwos9Q0d9BWjskrG8gwXM+rI+B271NAuHAO5 +Yw7yccteKxgddseTsFjZHJPQbgw9wYAY8W/H8Z8EXBUKB05uQQFPSgAX1IAsfzxO6jD zSYcFZgt4F2JFZtZmq/LwMjLdnLOTKxMbaivt6L27wG+DlwIcJDquZGi5ccLUIntMkt3 OpNQ== X-Gm-Message-State: AFuF++nEZVisDNTgRHcIcU1PTnRi3ogrRYJ6K+zhzjNMvEaGagb/K8tn fD7W2bisMl1T4Z66cvni0TG50MytnQUD4Z6Hbha4FlbAnoR+3bOvwNZdzmROrw== X-Gm-Gg: AYBFou3k36kKptOyG9BnAHk0IqiwXJtNJbtgFm1J8++KKVNJXMKFNoKpUWk9QkFe8Sp ctFVFpGD6YCTxdRmiWx7716mvcXP/KgqodUO/8q8/wph+Es71BIkG4w1JvD2ppYLdZ/n+EoVhXy 7CdXMxO4MmB/iu1cQScNZuP74T9/RVBHZeh9/ZMTZM0g63CfpkpAhWtbLhcFZMU2S88R2tjSbvs PNJnPDPB2Yn4Hrf2KwE8BM8WDYHnCYKu/UKyVyT3Esgjpy53a8872Avgt2crbCB5Tf5HyIQq90t vFgae3kEBAoaHdU3zwExpA6tYCvAP6gPh0BX1GV09WnAIzMUdK++D3hgLi7MtdzkLF9axgXTQtQ zhmPKGaf3n8BjSYO8iD71DE9yE2q0pQCnL5W4sBZEW+cs8KhyT/3FfUvnkZvGPs18xRJtvvfhyO NnQvLxWbpY0nzaxI5rRiq+5nf1hidLLMGakRHd4OnAok5TXCgHLp+k0oQ3ljS1uSscVGtvWWKZX 42rd82FHi9WQ/bv33tlZ3/1jbV+zwmffgfUZigK6AG0VBAKD3Y61U77JfjcvJIC9a5maGbr+erm kHp5qJ8dTNCLhvHmcZc5NBFhpQ== X-Received: by 2002:a05:620a:454c:b0:93c:6abf:c8ae with SMTP id af79cd13be357-93cf1791c20mr1990676885a.50.1791220039992; Mon, 05 Oct 2026 10:07:19 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id af79cd13be357-93cca26371bsm917422885a.30.2026.10.05.10.07.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Oct 2026 10:07:19 -0700 (PDT) Date: Mon, 5 Oct 2026 12:07:17 -0500 From: Nathan Bossart To: pgsql-hackers@postgresql.org Subject: COPY FROM ... WHERE fails for negated operators Message-ID: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="sN+IHSkMLaZuUJgQ" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --sN+IHSkMLaZuUJgQ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline The following steps produce a "cache lookup failed for function" error: CREATE TABLE t (a int); COPY t FROM stdin WHERE NOT (a > 0); 1 0 \. I think what's happening is that negate_clause() is calling get_negator() with a 0 argument because DoCopy() neglects to call fix_opfuncids() (like RelationGetIndexPredicate() does). The attached patch fixes this. -- nathan --sN+IHSkMLaZuUJgQ Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0001-Fix-COPY-FROM-.-WHERE-for-negated-operators.patch From f5b6cf2c5eefe24b1fb39e85174d6516f2cba36c Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 5 Oct 2026 12:06:11 -0500 Subject: [PATCH v1 1/1] Fix COPY FROM ... WHERE for negated operators. COPY FROM runs its WHERE clause through eval_const_expressions() and then hands it straight to ExecInitQual(), skipping the fix_opfuncids() step that every other such caller applies. That goes unnoticed until negate_clause() replaces a NOT with the operator's negator, since the OpExpr it builds has no opfuncid, at which point the executor fails with "cache lookup failed for function 0". For example, COPY tab FROM stdin WHERE NOT (a > 0) has failed this way since COPY FROM ... WHERE was added. To fix, call fix_opfuncids() on the result, as the relcache already does for index predicates. Oversight in commit 31f3817402. Backpatch-through: 14 --- src/backend/commands/copy.c | 2 ++ src/test/regress/expected/copy2.out | 7 ++++++- src/test/regress/sql/copy2.sql | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 003b70852bb..95b58cd2c84 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -29,6 +29,7 @@ #include "miscadmin.h" #include "nodes/makefuncs.h" #include "nodes/miscnodes.h" +#include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" #include "parser/parse_coerce.h" #include "parser/parse_collate.h" @@ -209,6 +210,7 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt, whereClause = (Node *) canonicalize_qual((Expr *) whereClause, false); whereClause = (Node *) make_ands_implicit((Expr *) whereClause); + fix_opfuncids(whereClause); } tupDesc = RelationGetDescr(rel); diff --git a/src/test/regress/expected/copy2.out b/src/test/regress/expected/copy2.out index 919eabd5f78..df6fcd91799 100644 --- a/src/test/regress/expected/copy2.out +++ b/src/test/regress/expected/copy2.out @@ -184,6 +184,7 @@ LINE 1: COPY x TO stdout WHERE a = 1; HINT: Try the COPY (SELECT ... WHERE ...) TO variant. COPY x from stdin WHERE a = 50004; COPY x from stdin WHERE a > 60003; +COPY x from stdin WHERE NOT (a < 70003); COPY x from stdin WHERE f > 60003; ERROR: column "f" does not exist LINE 1: COPY x from stdin WHERE f > 60003; @@ -234,12 +235,13 @@ SELECT * FROM x; 50004 | 25 | 35 | 45 | before trigger fired 60004 | 25 | 35 | 45 | before trigger fired 60005 | 26 | 36 | 46 | before trigger fired + 70003 | 24 | 34 | 44 | before trigger fired 1 | 1 | stuff | test_1 | after trigger fired 2 | 2 | stuff | test_2 | after trigger fired 3 | 3 | stuff | test_3 | after trigger fired 4 | 4 | stuff | test_4 | after trigger fired 5 | 5 | stuff | test_5 | after trigger fired -(28 rows) +(29 rows) -- check copy out COPY x TO stdout; @@ -266,6 +268,7 @@ COPY x TO stdout; 50004 25 35 45 before trigger fired 60004 25 35 45 before trigger fired 60005 26 36 46 before trigger fired +70003 24 34 44 before trigger fired 1 1 stuff test_1 after trigger fired 2 2 stuff test_2 after trigger fired 3 3 stuff test_3 after trigger fired @@ -295,6 +298,7 @@ Delimiter before trigger fired 35 before trigger fired 35 before trigger fired 36 before trigger fired +34 before trigger fired stuff after trigger fired stuff after trigger fired stuff after trigger fired @@ -324,6 +328,7 @@ I'm null before trigger fired 25 before trigger fired 25 before trigger fired 26 before trigger fired +24 before trigger fired 1 after trigger fired 2 after trigger fired 3 after trigger fired diff --git a/src/test/regress/sql/copy2.sql b/src/test/regress/sql/copy2.sql index 696ceeeaf82..ec6f5e8cfdc 100644 --- a/src/test/regress/sql/copy2.sql +++ b/src/test/regress/sql/copy2.sql @@ -195,6 +195,12 @@ COPY x from stdin WHERE a > 60003; 60005 26 36 46 56 \. +COPY x from stdin WHERE NOT (a < 70003); +70001 22 32 42 52 +70002 23 33 43 53 +70003 24 34 44 54 +\. + COPY x from stdin WHERE f > 60003; \. -- 2.55.0 --sN+IHSkMLaZuUJgQ--