Wrong result for whole-row IS NOT NULL with dropped columns

Поиск
Список
Период
Сортировка
Искать

Wrong result for whole-row IS NOT NULL with dropped columns

От:
"Matheus Alcantara" <matheusssilv97@gmail.com>
Дата:
Вложения:
Hi,

While working on another thread related with virtual generated columns I
found that a whole-row IS NOT NULL test returns false for a row with no
null fields if the table also has a dropped column:

CREATE TABLE t (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL, dropme int);
ALTER TABLE t DROP COLUMN dropme;
INSERT INTO t VALUES (1);

SELECT t, t IS NOT NULL AS is_not_null FROM t;
 t   | is_not_null
-------+-------------
(1,2) | f
(1 row)

The same happens with a CHECK constraint, which can't be added because
no row can satisfy it, and when it's added as NOT VALID every new row
is rejected:

ALTER TABLE t ADD CONSTRAINT wr CHECK (t IS NOT NULL);
ERROR:  check constraint "wr" of relation "t" is violated by some row

With a stored generated column, or without the dropped column, it
works as expected.

EXPLAIN shows that the planner has folded the whole test to a
constant:

EXPLAIN (VERBOSE, COSTS OFF) SELECT t IS NOT NULL FROM t;
      QUERY PLAN
--------------------------
Seq Scan on public.t
 Output: false
(2 rows)

IIUC what happens is that to expand the virtual column the whole-row
Var is turned into a RowExpr, and since this is a named rowtype
expandRTE() includes a null int4 Const as placeholder for the dropped
column. Then eval_const_expressions() breaks ROW(...) IS [NOT] NULL
into tests on each field, sees the null constant and refutes the
IS NOT NULL. The executor doesn't have this problem because
ExecEvalRowNullInt() skips dropped attributes, so the result depends on
whether the test was simplified at plan time or not.

Virtual generated columns are just the easiest way to reach this. The
same placeholder is added by coerce_record_to_complex() when casting a
row to a named rowtype, so this is also wrong on master without any
generated column involved:

CREATE TABLE ts (a int, dropme int, b int);
ALTER TABLE ts DROP COLUMN dropme;
EXPLAIN (VERBOSE, COSTS OFF) SELECT ROW(a, b)::ts IS NOT NULL FROM ts;
     QUERY PLAN
-----------------------
Seq Scan on public.ts
 Output: false
(2 rows)

The attached patch fixes this by looking up the rowtype's tuple
descriptor when simplifying a NullTest on a RowExpr of a named
composite type and skipping the fields that correspond to dropped
columns, which matches what the executor does. With the patch the
EXPLAIN above shows ((a IS NOT NULL) AND (b IS NOT NULL)). I've added
regression tests on generated_virtual.sql and rowtypes.sql.

The folding code is old, so the ROW()::type case is not new. I've
checked that the EXPLAIN above also shows "Output: false" on PG 14, so I
think that all supported branches are affected and this should be
backpatched. The virtual generated column case only applies to 18 and
master, so perhaps for 17 and older we need a different fix. 

Thoughts?

--
Matheus Alcantara
EDB: https://www.enterprisedb.com

Re: Wrong result for whole-row IS NOT NULL with dropped columns

От:
Sho Ito <i.sho.0628@gmail.com>
Дата:
On Wed, Oct 21, 2026 at 12:05 AM Matheus Alcantara <matheusssilv97@gmail.com> wrote:
The attached patch fixes this by looking up the rowtype's tuple
descriptor when simplifying a NullTest on a RowExpr of a named
composite type and skipping the fields that correspond to dropped
columns, which matches what the executor does.

Hi Matheus,

I reviewed v1 with assertions enabled,
and found no blocking issue in the changes.

Thanks,
Sho Ito 

--
=======================================
Sho Ito (Sean)

=======================================

Re: Wrong result for whole-row IS NOT NULL with dropped columns

От:
Joao Rito <joaovictor.rito@gmail.com>
Дата:
Hi,

I also had a look at v1, and I agree with Sho that it looks good. A few
results that may help with the test coverage and the backpatch question.

Tested against master 952bab8b74, Debian bookworm container, ./configure
--enable-cassert --enable-debug --enable-tap-tests.

Applying and building
- v1 applies cleanly with git am, and the build has no compiler warnings.
- make check: 243 tests, PASS.

Behavior on master without the patch
The problem is wider than the virtual generated column case. Every case
below gives a wrong answer on master and the right one with v1. The server
log had no warnings or leaks with the patch.

1. WHERE clause. With the table from the report (a int, b int GENERATED
ALWAYS AS (a * 2) VIRTUAL, dropped column), "SELECT count(*) FROM t WHERE t
IS NOT NULL" returns 0. The plan is "Result / One-Time Filter: false", so
all rows are lost silently. With v1 the filter is "((a IS NOT NULL) AND ((a
* 2) IS NOT NULL))" and the count is 2. I think this is the most serious
case, and a test for it would be good to have.

2. ROW(...)::type, with no generated column: a table (a int, dropme int, b
int) with dropme dropped. ROW(a, b)::ts IS NOT NULL is false for (1, 2). The
same happens with dropped columns at the start and at the end of the table,
and with a generic plan: PREPARE q(int) AS SELECT ROW($1, 2)::ts IS NOT NULL
returns false for q(1) under plan_cache_mode = force_generic_plan. A fully
constant ROW(1, 2)::ts is correct, because it is folded to a constant.

3. Composite types: CREATE TYPE ct AS (x int, y int, z int) followed by
ALTER TYPE ct DROP ATTRIBUTE y behaves the same way as a table.

4. A stored view. This one is a little different:

   CREATE TABLE src (x int, y int, z int);
   INSERT INTO src VALUES (1, NULL, 3), (1, 2, 3);
   CREATE TYPE ct3 AS (x int, y int, z int);
   CREATE VIEW v AS SELECT ROW(x, y, z)::ct3 IS NOT NULL AS nn FROM src;
   CREATE VIEW v2 AS SELECT ROW(x, y, z)::ct3 AS r FROM src;
   ALTER TYPE ct3 DROP ATTRIBUTE y;
   SELECT nn FROM v;                                         -- f, t
   SELECT r IS NOT NULL FROM (SELECT r FROM v2 OFFSET 0) s;  -- t, t

   Here the stored RowExpr still has the old Var src.y for the dropped
   attribute, not a null Const, so the planner and the executor disagree for
   the first row. With v1 both give t, t, and EXPLAIN shows "((src.x IS NOT
   NULL) AND (src.z IS NOT NULL))". So checking attisdropped in the tuple
   descriptor, as v1 does, is the right approach; looking for a null Const
   would not cover this case.

5. Other places where the same folding shows up: a CHECK (t IS NOT NULL)
constraint added NOT VALID rejects a valid row, and a whole-row test on a
partitioned parent with a dropped column returns false while the same test
on the partition (which has no dropped column) returns true.

Cases that are already correct on master, and stay correct with v1: a table
with a single live column, a nested row (ROW(1, ROW(1, 2)::ts) IS NOT NULL),
and a domain over the composite type (the CoerceToDomain is not simplified).

Code
- Changing the early "return makeBoolConst(false, false)" into "refuted =
  true; break;" is needed so that the tuple descriptor is released. Looks
  right to me.
- The "i < tupdesc->natts" check looks defensive only, since a RowExpr of a
  named type has one argument per attribute, dropped ones included. Fine to
  keep.
- Tests: the two new tests cover the generated column and the cast. Maybe
  add the WHERE case from item 1 (the plan with One-Time Filter: false is
  easy to check with EXPLAIN), and the ALTER TYPE ... DROP ATTRIBUTE case.

Backpatch
- v1 does not apply to REL_18_STABLE or REL_17_STABLE. The conflict is in
  the third clauses.c hunk, which touches code that exists only on master.
- I think the same fix works on older branches, since the ROW()::type path
  from item 2 is not related to virtual generated columns. On 17,
  TupleDescAttr() has to be used instead of TupleDescCompactAttr(), and the
  generated_virtual test applies only to 18 and later. I prepared that port
  for REL_17_STABLE but could not finish building and testing it, so I
  cannot confirm yet that it works there.

Thanks for working on this.

Regards,
Joao Rito


Re: Wrong result for whole-row IS NOT NULL with dropped columns

От:
"Matheus Alcantara" <matheusssilv97@gmail.com>
Дата:
Вложения:
Thanks Joao and Sho Ito for testing the reviewing the patch!

On Tue Nov 10, 2026 at 10:01 AM -03, Joao Rito wrote:
> 4. A stored view. This one is a little different:
>
>    CREATE TABLE src (x int, y int, z int);
>    INSERT INTO src VALUES (1, NULL, 3), (1, 2, 3);
>    CREATE TYPE ct3 AS (x int, y int, z int);
>    CREATE VIEW v AS SELECT ROW(x, y, z)::ct3 IS NOT NULL AS nn FROM src;
>    CREATE VIEW v2 AS SELECT ROW(x, y, z)::ct3 AS r FROM src;
>    ALTER TYPE ct3 DROP ATTRIBUTE y;
>    SELECT nn FROM v;                                         -- f, t
>    SELECT r IS NOT NULL FROM (SELECT r FROM v2 OFFSET 0) s;  -- t, t
>
>    Here the stored RowExpr still has the old Var src.y for the dropped
>    attribute, not a null Const, so the planner and the executor disagree for
>    the first row. With v1 both give t, t, and EXPLAIN shows "((src.x IS NOT
>    NULL) AND (src.z IS NOT NULL))". So checking attisdropped in the tuple
>    descriptor, as v1 does, is the right approach; looking for a null Const
>    would not cover this case.
>

Nice catch, I was not aware of this case. I think that it's worth to
have a test for it too, since it makes sure that we don't move to a
approach that only looks for the null Const in the future, so I've
added it on rowtypes.sql.

> - The "i < tupdesc->natts" check looks defensive only, since a RowExpr of a
>   named type has one argument per attribute, dropped ones included. Fine to
>   keep.

Yeah, I've kept it just to be safe.

> - Tests: the two new tests cover the generated column and the cast. Maybe
>   add the WHERE case from item 1 (the plan with One-Time Filter: false is
>   easy to check with EXPLAIN), and the ALTER TYPE ... DROP ATTRIBUTE case.
>

Done in the attached v2. The WHERE case is on generated_virtual.sql and
the ALTER TYPE ... DROP ATTRIBUTE case is on rowtypes.sql.

> Backpatch
> - v1 does not apply to REL_18_STABLE or REL_17_STABLE. The conflict is in
>   the third clauses.c hunk, which touches code that exists only on master.
> - I think the same fix works on older branches, since the ROW()::type path
>   from item 2 is not related to virtual generated columns. On 17,
>   TupleDescAttr() has to be used instead of TupleDescCompactAttr(), and the
>   generated_virtual test applies only to 18 and later. I prepared that port
>   for REL_17_STABLE but could not finish building and testing it, so I
>   cannot confirm yet that it works there.
>

I agree. I'm also attaching patches for each back branch down to 14.
Some notes about the differences:

On REL_18 the fix is the same but without the expr_is_nonnullable()
check, which only exists on master.

On REL_17 and older there are no virtual generated columns, so only the
rowtypes.sql tests are included, and TupleDescAttr() is used instead of
TupleDescCompactAttr(), as you mentioned.

On REL_14 the rowtypes.sql tests use regular tables instead of temp
tables, because EXPLAIN VERBOSE on 14 shows the real temp schema name
instead of pg_temp, which make the expected output unstable.

--
Matheus Alcantara
EDB: https://www.enterprisedb.com

Re: Wrong result for whole-row IS NOT NULL with dropped columns

От:
João Rito <joaovictor.rito@gmail.com>
Дата:
Hi Matheus,

Thanks for v2 and for the back-branch patches.

I read the patches. v2 covers the points I raised:
- The WHERE case is now in generated_virtual.sql (gtest20e), with the
EXPLAIN of the Filter and the count(*) result, plus the CHECK
constraint case.
- The ALTER TYPE ... DROP ATTRIBUTE case and the stored view case are
in rowtypes.sql, with the EXPLAIN output showing "(x IS NOT NULL) AND
(z IS NOT NULL)".
- The back-branch patches match what you describe: REL_18 without
expr_is_nonnullable(), REL_17 and older with TupleDescAttr() and only
the rowtypes.sql tests, and REL_14 with regular tables instead of temp
tables.

I also tested them:
- v2 applies to current master (8d87367b39e), and make check passes
(all 243 tests).
- With the v2 tests but the old clauses.c, generated_virtual and
rowtypes fail (2 of 243), so the new tests catch the bug.
- The WHERE case from the report (count(*) ... WHERE t IS NOT NULL)
returns 0 on master and 2 with v2.
- The back-branch patches apply and make check passes on REL_18_STABLE
(235 tests), REL_17_STABLE (229) and REL_14_STABLE (220). I did not
test REL_15 and REL_16.

I have no remaining concerns about the code. One small optional idea:
the view test only checks the view that has the IS NOT NULL inside it.
It could also check a view that stores only the ROW(...)::type and
applies IS NOT NULL outside it, but I think the current test is
enough.

Thanks,
João


Re: Wrong result for whole-row IS NOT NULL with dropped columns

От:
Matheus Alcantara <matheusssilv97@gmail.com>
Дата:
On 11/11/26 19:28, João Rito wrote:
> Hi Matheus,
> 
> Thanks for v2 and for the back-branch patches.
> 
> I read the patches. v2 covers the points I raised:
> - The WHERE case is now in generated_virtual.sql (gtest20e), with the
> EXPLAIN of the Filter and the count(*) result, plus the CHECK
> constraint case.
> - The ALTER TYPE ... DROP ATTRIBUTE case and the stored view case are
> in rowtypes.sql, with the EXPLAIN output showing "(x IS NOT NULL) AND
> (z IS NOT NULL)".
> - The back-branch patches match what you describe: REL_18 without
> expr_is_nonnullable(), REL_17 and older with TupleDescAttr() and only
> the rowtypes.sql tests, and REL_14 with regular tables instead of temp
> tables.
> 
> I also tested them:
> - v2 applies to current master (8d87367b39e), and make check passes
> (all 243 tests).
> - With the v2 tests but the old clauses.c, generated_virtual and
> rowtypes fail (2 of 243), so the new tests catch the bug.
> - The WHERE case from the report (count(*) ... WHERE t IS NOT NULL)
> returns 0 on master and 2 with v2.
> - The back-branch patches apply and make check passes on REL_18_STABLE
> (235 tests), REL_17_STABLE (229) and REL_14_STABLE (220). I did not
> test REL_15 and REL_16.
> 
> I have no remaining concerns about the code. One small optional idea:
> the view test only checks the view that has the IS NOT NULL inside it.
> It could also check a view that stores only the ROW(...)::type and
> applies IS NOT NULL outside it, but I think the current test is
> enough.
> 

Thank you Joao for testing the patch!

--
Matheus Alcantara
EDB: https://www.enterprisedb.com


FAQ