BUG #18613: Incorrect output for foreign tables with NOT NULL constraints - Mailing list pgsql-bugs

From PG Bug reporting form
Subject BUG #18613: Incorrect output for foreign tables with NOT NULL constraints
Date
Msg-id 18613-08a94418126ae1a0@postgresql.org
Whole thread Raw
Responses Re: BUG #18613: Incorrect output for foreign tables with NOT NULL constraints
Re: BUG #18613: Incorrect output for foreign tables with NOT NULL constraints
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      18613
Logged by:          Maxim Michkov
Email address:      m.michkov@arenadata.io
PostgreSQL version: 17rc1
Operating system:   Ubuntu 22.04
Description:

If foreign table data contains nulls while the table definition specifies a
NOT NULL constraint, some queries return wrong outputs. For example, for
file /data.csv with the following contents:

1,10
2,20
3,null
4,40

The following queries returns incorrect result:

CREATE EXTENSION file_fdw;
CREATE SERVER file_server FOREIGN DATA WRAPPER file_fdw;
CREATE FOREIGN TABLE t(a int, b int not null) SERVER file_server OPTIONS (
filename '/data.csv', format 'csv', null 'null' );
SELECT * FROM t WHERE b IS NOT NULL;
SELECT * FROM t WHERE b IS NULL;

Outputs:

postgres=# SELECT * FROM t WHERE b IS NOT NULL;
 a | b  
---+----
 1 | 10
 2 | 20
 3 |   
 4 | 40
(4 rows)

postgres=# SELECT * FROM t WHERE b IS NULL;
 a | b 
---+---
(0 rows)

Expected an error (stating that the foreign table contains NULLs) or the
following output:

postgres=# SELECT * FROM t WHERE b IS NOT NULL;
 a | b  
---+----
 1 | 10
 2 | 20
 4 | 40
(3 rows)

postgres=# SELECT * FROM t WHERE b IS NULL;
 a | b 
---+---
 3 |  
(1 row)

The bug appeared after this patch:
https://www.postgresql.org/message-id/E1rS92d-002gbO-EJ%40gemulon.postgresql.org
The patch added optimizations for IS [NOT] NULL checks for columns with NOT
NULL, however it is possible for foreign tables to contain nulls even in
that case. A possible solutions could be reverting that patch, or adding a
special case for foreign tables, or ignoring NOT NULL constraints on foreign
tables in general (because we cannot check they are valid).


pgsql-bugs by date:

Previous
From: PG Bug reporting form
Date:
Subject: BUG #18612: Postgres crash with segfault on disk full - ____strtof_l_internal (strtod_l.c:1019)
Next
From: Duncan Sands
Date:
Subject: pg_restore with --disable-triggers discards ENABLE ALWAYS