]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Add tuple deformation test for virtual generated columns
authorDavid Rowley <drowley@postgresql.org>
Wed, 17 Jun 2026 04:57:16 +0000 (16:57 +1200)
committerDavid Rowley <drowley@postgresql.org>
Wed, 17 Jun 2026 04:57:16 +0000 (16:57 +1200)
Add coverage for a virtual generated NOT NULL column followed by a
physically stored NOT NULL column.  This exercises the tuple deformation
case fixed by 89eafad297a, where TupleDescFinalize() could incorrectly
treat a virtual generated column as part of the guaranteed physical column
prefix and compute cached offsets past it.

Without that fix, deforming the following column could read from the wrong
tuple offset.

Author: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/A4BC563C-0CA3-4EF3-952A-EA41F9E5BF1E%40gmail.com

src/test/regress/expected/generated_stored.out
src/test/regress/expected/generated_virtual.out
src/test/regress/sql/generated_stored.sql
src/test/regress/sql/generated_virtual.sql

index 7866ae0ebbe8a1462aacd40ef16310c952748291..f87a756b231e749961f22f96f88218bc9815fe67 100644 (file)
@@ -721,6 +721,11 @@ ERROR:  null value in column "b" of relation "gtest21b" violates not-null constr
 DETAIL:  Failing row contains (null, null).
 ALTER TABLE gtest21b ALTER COLUMN b DROP NOT NULL;
 INSERT INTO gtest21b (a) VALUES (0);  -- ok now
+-- virtual generated columns are not physically stored, even when not null
+--CREATE TABLE gtest21c (a int NOT NULL, b int GENERATED ALWAYS AS (a * 2) VIRTUAL NOT NULL, c int NOT NULL);
+--INSERT INTO gtest21c (a, c) VALUES (10, 42);
+--SELECT a, b, c FROM gtest21c;
+--DROP TABLE gtest21c;
 -- not-null constraint with partitioned table
 CREATE TABLE gtestnn_parent (
     f1 int,
index 24d5dbf46ca19cf0dd9cc7ceddbbcfce244f24a9..b8d5def44db0e37821edc88ad19a4b863e4350cc 100644 (file)
@@ -727,6 +727,16 @@ ERROR:  null value in column "b" of relation "gtest21b" violates not-null constr
 DETAIL:  Failing row contains (null, virtual).
 ALTER TABLE gtest21b ALTER COLUMN b DROP NOT NULL;
 INSERT INTO gtest21b (a) VALUES (0);  -- ok now
+-- virtual generated columns are not physically stored, even when not null
+CREATE TABLE gtest21c (a int NOT NULL, b int GENERATED ALWAYS AS (a * 2) VIRTUAL NOT NULL, c int NOT NULL);
+INSERT INTO gtest21c (a, c) VALUES (10, 42);
+SELECT a, b, c FROM gtest21c;
+ a  | b  | c  
+----+----+----
+ 10 | 20 | 42
+(1 row)
+
+DROP TABLE gtest21c;
 -- not-null constraint with partitioned table
 CREATE TABLE gtestnn_parent (
     f1 int,
index 6746cd4632b6ca24c810609745657da99b17094e..71b0ba6d8d7bfb94cd2bbf3b6d23ac33f00f68d5 100644 (file)
@@ -368,6 +368,11 @@ INSERT INTO gtest21b (a) VALUES (NULL);  -- error
 ALTER TABLE gtest21b ALTER COLUMN b DROP NOT NULL;
 INSERT INTO gtest21b (a) VALUES (0);  -- ok now
 
+-- virtual generated columns are not physically stored, even when not null
+--CREATE TABLE gtest21c (a int NOT NULL, b int GENERATED ALWAYS AS (a * 2) VIRTUAL NOT NULL, c int NOT NULL);
+--INSERT INTO gtest21c (a, c) VALUES (10, 42);
+--SELECT a, b, c FROM gtest21c;
+--DROP TABLE gtest21c;
 -- not-null constraint with partitioned table
 CREATE TABLE gtestnn_parent (
     f1 int,
index 9c2bb6590b34905013898e61061736abc198f18d..9e3dc99c71d62261cd40b8e632a0629ddc791403 100644 (file)
@@ -374,6 +374,12 @@ INSERT INTO gtest21b (a) VALUES (NULL);  -- error
 ALTER TABLE gtest21b ALTER COLUMN b DROP NOT NULL;
 INSERT INTO gtest21b (a) VALUES (0);  -- ok now
 
+-- virtual generated columns are not physically stored, even when not null
+CREATE TABLE gtest21c (a int NOT NULL, b int GENERATED ALWAYS AS (a * 2) VIRTUAL NOT NULL, c int NOT NULL);
+INSERT INTO gtest21c (a, c) VALUES (10, 42);
+SELECT a, b, c FROM gtest21c;
+DROP TABLE gtest21c;
+
 -- not-null constraint with partitioned table
 CREATE TABLE gtestnn_parent (
     f1 int,