Skip to content

Commit 5a68f62

Browse files
author
Antonin Houska
committed
Fixed build and tests for PG < 18.
1 parent 15aa7bb commit 5a68f62

File tree

7 files changed

+201
-76
lines changed

7 files changed

+201
-76
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ EXTENSION = pg_rewrite
77
DATA = pg_rewrite--1.0.sql pg_rewrite--1.0--1.1.sql pg_rewrite--1.1--1.2.sql
88
DOCS = pg_rewrite.md
99

10-
REGRESS = pg_rewrite
10+
REGRESS = pg_rewrite generated
1111
#ISOLATION = pg_rewrite_concurrent pg_rewrite_concurrent_partition
1212

1313
PGXS := $(shell $(PG_CONFIG) --pgxs)

expected/generated.out

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
-- Generated columns - some meaningful combinations of source and destination
2+
-- columns.
3+
CREATE TABLE tab7(
4+
i int primary key,
5+
j int,
6+
k int generated always as (i + 1) virtual,
7+
l int generated always AS (i + 1) stored,
8+
m int generated always AS (i + 1) virtual);
9+
CREATE TABLE tab7_new(
10+
i int primary key,
11+
-- Override the value copied from the source table.
12+
j int generated always AS (i - 1) stored,
13+
-- Check that the expression is evaluated correctly on the source
14+
-- table.
15+
k int,
16+
-- The same for stored expression.
17+
l int,
18+
-- Override the value computed on the source table.
19+
m int generated always as (i - 1) virtual);
20+
INSERT INTO tab7(i, j) VALUES (1, 1);
21+
SELECT rewrite_table('tab7', 'tab7_new', 'tab7_orig');
22+
rewrite_table
23+
---------------
24+
25+
(1 row)
26+
27+
SELECT * FROM tab7;
28+
i | j | k | l | m
29+
---+---+---+---+---
30+
1 | 0 | 2 | 2 | 0
31+
(1 row)
32+
33+
CREATE EXTENSION pageinspect;
34+
-- HEAP_HASNULL indicates that the value of 'm' hasn't been copied from the
35+
-- source table.
36+
SELECT raw_flags
37+
FROM heap_page_items(get_raw_page('tab7', 0)),
38+
LATERAL heap_tuple_infomask_flags(t_infomask, t_infomask2);
39+
raw_flags
40+
------------------------------------------------------
41+
{HEAP_HASNULL,HEAP_XMIN_COMMITTED,HEAP_XMAX_INVALID}
42+
(1 row)
43+
44+
-- For PG < 18, test without VIRTUAL columns.
45+
CREATE TABLE tab8(
46+
i int primary key,
47+
j int,
48+
k int generated always AS (i + 1) stored);
49+
CREATE TABLE tab8_new(
50+
i int primary key,
51+
-- Override the value copied from the source table.
52+
j int generated always AS (i - 1) stored,
53+
-- Check that the expression is evaluated correctly on the source
54+
-- table.
55+
k int);
56+
INSERT INTO tab8(i, j) VALUES (1, 1);
57+
SELECT rewrite_table('tab8', 'tab8_new', 'tab8_orig');
58+
rewrite_table
59+
---------------
60+
61+
(1 row)
62+
63+
SELECT * FROM tab8;
64+
i | j | k
65+
---+---+---
66+
1 | 0 | 2
67+
(1 row)
68+

expected/generated_1.out

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
-- Generated columns - some meaningful combinations of source and destination
2+
-- columns.
3+
CREATE TABLE tab7(
4+
i int primary key,
5+
j int,
6+
k int generated always as (i + 1) virtual,
7+
l int generated always AS (i + 1) stored,
8+
m int generated always AS (i + 1) virtual);
9+
ERROR: syntax error at or near "virtual"
10+
LINE 4: k int generated always as (i + 1) virtual,
11+
^
12+
CREATE TABLE tab7_new(
13+
i int primary key,
14+
-- Override the value copied from the source table.
15+
j int generated always AS (i - 1) stored,
16+
-- Check that the expression is evaluated correctly on the source
17+
-- table.
18+
k int,
19+
-- The same for stored expression.
20+
l int,
21+
-- Override the value computed on the source table.
22+
m int generated always as (i - 1) virtual);
23+
ERROR: syntax error at or near "virtual"
24+
LINE 11: m int generated always as (i - 1) virtual);
25+
^
26+
INSERT INTO tab7(i, j) VALUES (1, 1);
27+
ERROR: relation "tab7" does not exist
28+
LINE 1: INSERT INTO tab7(i, j) VALUES (1, 1);
29+
^
30+
SELECT rewrite_table('tab7', 'tab7_new', 'tab7_orig');
31+
ERROR: relation "tab7" does not exist
32+
SELECT * FROM tab7;
33+
ERROR: relation "tab7" does not exist
34+
LINE 1: SELECT * FROM tab7;
35+
^
36+
CREATE EXTENSION pageinspect;
37+
-- HEAP_HASNULL indicates that the value of 'm' hasn't been copied from the
38+
-- source table.
39+
SELECT raw_flags
40+
FROM heap_page_items(get_raw_page('tab7', 0)),
41+
LATERAL heap_tuple_infomask_flags(t_infomask, t_infomask2);
42+
ERROR: relation "tab7" does not exist
43+
-- For PG < 18, test without VIRTUAL columns.
44+
CREATE TABLE tab8(
45+
i int primary key,
46+
j int,
47+
k int generated always AS (i + 1) stored);
48+
CREATE TABLE tab8_new(
49+
i int primary key,
50+
-- Override the value copied from the source table.
51+
j int generated always AS (i - 1) stored,
52+
-- Check that the expression is evaluated correctly on the source
53+
-- table.
54+
k int);
55+
INSERT INTO tab8(i, j) VALUES (1, 1);
56+
SELECT rewrite_table('tab8', 'tab8_new', 'tab8_orig');
57+
rewrite_table
58+
---------------
59+
60+
(1 row)
61+
62+
SELECT * FROM tab8;
63+
i | j | k
64+
---+---+---
65+
1 | 0 | 2
66+
(1 row)
67+

expected/pg_rewrite.out

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -379,43 +379,3 @@ SELECT i FROM tab6 ORDER BY i;
379379
2
380380
(2 rows)
381381

382-
-- Generated columns - some meaningful combinations of source and destination
383-
-- columns.
384-
CREATE TABLE tab7(
385-
i int primary key,
386-
j int,
387-
k int generated always as (i + 1) virtual,
388-
l int generated always AS (i + 1) virtual);
389-
CREATE TABLE tab7_new(
390-
i int primary key,
391-
-- Override the value copied from the source table.
392-
j int generated always AS (i - 1) stored,
393-
-- Check that the expression is evaluated correctly on the source
394-
-- table.
395-
k int,
396-
-- Override the value computed on the source table.
397-
l int generated always as (i - 1) virtual);
398-
INSERT INTO tab7(i, j) VALUES (1, 1);
399-
SELECT rewrite_table('tab7', 'tab7_new', 'tab7_orig');
400-
rewrite_table
401-
---------------
402-
403-
(1 row)
404-
405-
SELECT * FROM tab7 ORDER BY i;
406-
i | j | k | l
407-
---+---+---+---
408-
1 | 0 | 2 | 0
409-
(1 row)
410-
411-
CREATE EXTENSION pageinspect;
412-
-- HEAP_HASNULL indicates that the value of 'l' hasn't been copied from the
413-
-- source table.
414-
SELECT raw_flags
415-
FROM heap_page_items(get_raw_page('tab7', 0)),
416-
LATERAL heap_tuple_infomask_flags(t_infomask, t_infomask2);
417-
raw_flags
418-
------------------------------------------------------
419-
{HEAP_HASNULL,HEAP_XMIN_COMMITTED,HEAP_XMAX_INVALID}
420-
(1 row)
421-

pg_rewrite.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2663,7 +2663,10 @@ build_attrmap_by_name_ext(Relation rel_src, Relation rel_dst)
26632663
{
26642664
Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
26652665
char *attname;
2666-
Oid atttypid, attcol;
2666+
Oid atttypid;
2667+
#if PG_VERSION_NUM >= 180000
2668+
Oid attcol;
2669+
#endif
26672670
int32 atttypmod;
26682671
int j;
26692672

@@ -2675,7 +2678,9 @@ build_attrmap_by_name_ext(Relation rel_src, Relation rel_dst)
26752678
attname = NameStr(outatt->attname);
26762679
atttypid = outatt->atttypid;
26772680
atttypmod = outatt->atttypmod;
2681+
#if PG_VERSION_NUM >= 180000
26782682
attcol = outatt->attcollation;
2683+
#endif
26792684

26802685
/*
26812686
* Now search for an attribute with the same name in the indesc. It
@@ -2710,11 +2715,7 @@ build_attrmap_by_name_ext(Relation rel_src, Relation rel_dst)
27102715
* Found it. Insert NULL into generated virtual columns, the
27112716
* actual value will be computed during query execution.
27122717
*/
2713-
if (outatt->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
2714-
attrMap->exprsOut[i] = (Node *) makeNullConst(atttypid,
2715-
atttypmod,
2716-
attcol);
2717-
else if (outatt->attgenerated == ATTRIBUTE_GENERATED_STORED)
2718+
if (outatt->attgenerated == ATTRIBUTE_GENERATED_STORED)
27182719
{
27192720
/*
27202721
* Initialize the expression to compute the stored value
@@ -2744,14 +2745,23 @@ build_attrmap_by_name_ext(Relation rel_src, Relation rel_dst)
27442745
assign_expr_collations(pstate, expr);
27452746
attrMap->exprsOut[i] = expr;
27462747
}
2748+
#if PG_VERSION_NUM >= 180000
2749+
else if (outatt->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
2750+
attrMap->exprsOut[i] = (Node *) makeNullConst(atttypid,
2751+
atttypmod,
2752+
attcol);
2753+
#endif
27472754

27482755
/*
27492756
* Check type. Also make sure that we have the expression to
27502757
* generate the value of a virtual generated column.
27512758
*/
27522759
if (atttypid != inatt->atttypid ||
2753-
atttypmod != inatt->atttypmod ||
2754-
inatt->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
2760+
atttypmod != inatt->atttypmod
2761+
#if PG_VERSION_NUM >= 180000
2762+
|| inatt->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL
2763+
#endif
2764+
)
27552765
{
27562766
/*
27572767
* Can the input attribute be coerced to the output one?
@@ -2772,9 +2782,11 @@ build_attrmap_by_name_ext(Relation rel_src, Relation rel_dst)
27722782
COERCION_ASSIGNMENT,
27732783
COERCE_IMPLICIT_CAST,
27742784
-1);
2785+
#if PG_VERSION_NUM >= 180000
27752786
/* Here we take the column expression into account. */
27762787
if (inatt->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
27772788
expr = expand_generated_columns_in_expr(expr, rel_src, 1);
2789+
#endif
27782790
/*
27792791
* XXX Do we need to call expression_planner() like
27802792
* ATPrepAlterColumnType() in PG core does?

sql/generated.sql

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-- Generated columns - some meaningful combinations of source and destination
2+
-- columns.
3+
CREATE TABLE tab7(
4+
i int primary key,
5+
j int,
6+
k int generated always as (i + 1) virtual,
7+
l int generated always AS (i + 1) stored,
8+
m int generated always AS (i + 1) virtual);
9+
CREATE TABLE tab7_new(
10+
i int primary key,
11+
-- Override the value copied from the source table.
12+
j int generated always AS (i - 1) stored,
13+
-- Check that the expression is evaluated correctly on the source
14+
-- table.
15+
k int,
16+
-- The same for stored expression.
17+
l int,
18+
-- Override the value computed on the source table.
19+
m int generated always as (i - 1) virtual);
20+
INSERT INTO tab7(i, j) VALUES (1, 1);
21+
SELECT rewrite_table('tab7', 'tab7_new', 'tab7_orig');
22+
SELECT * FROM tab7;
23+
24+
CREATE EXTENSION pageinspect;
25+
-- HEAP_HASNULL indicates that the value of 'm' hasn't been copied from the
26+
-- source table.
27+
SELECT raw_flags
28+
FROM heap_page_items(get_raw_page('tab7', 0)),
29+
LATERAL heap_tuple_infomask_flags(t_infomask, t_infomask2);
30+
31+
-- For PG < 18, test without VIRTUAL columns.
32+
CREATE TABLE tab8(
33+
i int primary key,
34+
j int,
35+
k int generated always AS (i + 1) stored);
36+
CREATE TABLE tab8_new(
37+
i int primary key,
38+
-- Override the value copied from the source table.
39+
j int generated always AS (i - 1) stored,
40+
-- Check that the expression is evaluated correctly on the source
41+
-- table.
42+
k int);
43+
INSERT INTO tab8(i, j) VALUES (1, 1);
44+
SELECT rewrite_table('tab8', 'tab8_new', 'tab8_orig');
45+
SELECT * FROM tab8;

sql/pg_rewrite.sql

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -150,30 +150,3 @@ INSERT INTO tab6(i) VALUES (DEFAULT);
150150
SELECT rewrite_table('tab6', 'tab6_new', 'tab6_orig');
151151
INSERT INTO tab6(i) VALUES (DEFAULT);
152152
SELECT i FROM tab6 ORDER BY i;
153-
154-
-- Generated columns - some meaningful combinations of source and destination
155-
-- columns.
156-
CREATE TABLE tab7(
157-
i int primary key,
158-
j int,
159-
k int generated always as (i + 1) virtual,
160-
l int generated always AS (i + 1) virtual);
161-
CREATE TABLE tab7_new(
162-
i int primary key,
163-
-- Override the value copied from the source table.
164-
j int generated always AS (i - 1) stored,
165-
-- Check that the expression is evaluated correctly on the source
166-
-- table.
167-
k int,
168-
-- Override the value computed on the source table.
169-
l int generated always as (i - 1) virtual);
170-
INSERT INTO tab7(i, j) VALUES (1, 1);
171-
SELECT rewrite_table('tab7', 'tab7_new', 'tab7_orig');
172-
SELECT * FROM tab7 ORDER BY i;
173-
174-
CREATE EXTENSION pageinspect;
175-
-- HEAP_HASNULL indicates that the value of 'l' hasn't been copied from the
176-
-- source table.
177-
SELECT raw_flags
178-
FROM heap_page_items(get_raw_page('tab7', 0)),
179-
LATERAL heap_tuple_infomask_flags(t_infomask, t_infomask2);

0 commit comments

Comments
 (0)