postgres.git / summary / log / commit / refs

commit    95d9604471a319b6ccfd0a7d884fd604d3da5b20
Author:   Nathan Bossart <nathan@postgresql.org>
Date:     Tue Oct 06 14:04:32 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0

    Fix COPY FROM ... WHERE for negated operators.
    
    COPY FROM runs its WHERE clause through eval_const_expressions(),
    which among other things replaces NOT above an operator with the
    operator's negator, if it has one (e.g., NOT (a > 0) becomes
    a <= 0).  The node it builds for the negator has no opfuncid, and
    since COPY FROM never calls fix_opfuncids() on the clause, nothing
    fills it in before the executor needs it.  So
    
    	COPY tab FROM stdin WHERE NOT (a > 0)
    
    fails with "cache lookup failed for function 0".
    
    To fix, call fix_opfuncids() on the clause, as the relcache does
    for index predicates.
    
    Oversight in commit 31f3817402.
    
    Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
    Discussion: https://postgr.es/m/asPZRTci4oBvxR9A%40nathan
    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 13ca317c991..2b141215ad8 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -28,6 +28,7 @@ #include "mb/pg_wchar.h" #include "miscadmin.h" #include "nodes/makefuncs.h" +#include "nodes/nodeFuncs.h" #include "optimizer/optimizer.h" #include "parser/parse_coerce.h" #include "parser/parse_collate.h" @@ -189,6 +190,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 64ea33aeae8..e5f9540292f 100644 --- a/src/test/regress/expected/copy2.out +++ b/src/test/regress/expected/copy2.out @@ -159,6 +159,7 @@ LINE 1: COPY x TO stdout WHERE a = 1; ^ 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; @@ -206,12 +207,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; @@ -238,6 +240,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 @@ -267,6 +270,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 @@ -296,6 +300,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 aa949739921..b2cfe6523a6 100644 --- a/src/test/regress/sql/copy2.sql +++ b/src/test/regress/sql/copy2.sql @@ -179,6 +179,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; \. [parent: 3a09e91a49a2]