Skip to content

Commit da47112

Browse files
anarazelreshke
authored andcommitted
Prevent pgstats from getting confused when relkind of a relation changes
When the relkind of a relache entry changes, because a table is converted into a view, pgstats can get confused in 15+, leading to crashes or assertion failures. For HEAD, Tom fixed this in b23cd18, by removing support for converting a table to a view, removing the source of the inconsistency. This commit just adds an assertion that a relcache entry's relkind does not change, just in case we end up with another case of that in the future. As there's no cases of changing relkind anymore, we can't add a test that that's handled correctly. For 15, fix the problem by not maintaining the association with the old pgstat entry when the relkind changes during a relcache invalidation processing. In that case the pgstat entry needs to be unlinked first, to avoid PgStat_TableStatus->relation getting out of sync. Also add a test reproducing the issues. No known problem exists in 11-14, so just add the test there. Reported-by: vignesh C <vignesh21@gmail.com> Author: Andres Freund <andres@anarazel.de> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/CALDaNm2yXz+zOtv7y5zBd5WKT8O0Ld3YxikuU3dcyCvxF7gypA@mail.gmail.com Discussion: https://postgr.es/m/CALDaNm3oZA-8Wbps2Jd1g5_Gjrr-x3YWrJPek-mF5Asrrvz2Dg@mail.gmail.com Backpatch: 15-
1 parent 2e27d4c commit da47112

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

src/test/regress/expected/create_view.out

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,29 @@ select pg_get_viewdef('tt26v', true);
19971997
FROM ( VALUES (1,2,3)) v(x, y, z);
19981998
(1 row)
19991999

2000+
-- Test that changing the relkind of a relcache entry doesn't cause
2001+
-- trouble. Prior instances of where it did:
2002+
-- CALDaNm2yXz+zOtv7y5zBd5WKT8O0Ld3YxikuU3dcyCvxF7gypA@mail.gmail.com
2003+
-- CALDaNm3oZA-8Wbps2Jd1g5_Gjrr-x3YWrJPek-mF5Asrrvz2Dg@mail.gmail.com
2004+
CREATE TABLE tt26(c int);
2005+
BEGIN;
2006+
CREATE TABLE tt27(c int);
2007+
SAVEPOINT q;
2008+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
2009+
SELECT * FROM tt27;
2010+
c
2011+
---
2012+
(0 rows)
2013+
2014+
ROLLBACK TO q;
2015+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
2016+
ROLLBACK;
2017+
BEGIN;
2018+
CREATE TABLE tt28(c int);
2019+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
2020+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
2021+
ERROR: "tt28" is already a view
2022+
ROLLBACK;
20002023
-- test display negative operator of const-folder expression
20012024
create table tdis(a int, b int, c int);
20022025
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' as the Greenplum Database data distribution key for this table.
@@ -2044,7 +2067,7 @@ drop cascades to view aliased_view_2
20442067
drop cascades to view aliased_view_3
20452068
drop cascades to view aliased_view_4
20462069
DROP SCHEMA testviewschm2 CASCADE;
2047-
NOTICE: drop cascades to 76 other objects
2070+
NOTICE: drop cascades to 77 other objects
20482071
DETAIL: drop cascades to table t1
20492072
drop cascades to view temporal1
20502073
drop cascades to view temporal2
@@ -2119,5 +2142,6 @@ drop cascades to view tt23v
21192142
drop cascades to view tt24v
21202143
drop cascades to view tt25v
21212144
drop cascades to view tt26v
2145+
drop cascades to table tt26
21222146
drop cascades to table tdis
21232147
drop cascades to view tdis_v1

src/test/regress/expected/create_view_optimizer.out

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,29 @@ select pg_get_viewdef('tt26v', true);
19951995
FROM ( VALUES (1,2,3)) v(x, y, z);
19961996
(1 row)
19971997

1998+
-- Test that changing the relkind of a relcache entry doesn't cause
1999+
-- trouble. Prior instances of where it did:
2000+
-- CALDaNm2yXz+zOtv7y5zBd5WKT8O0Ld3YxikuU3dcyCvxF7gypA@mail.gmail.com
2001+
-- CALDaNm3oZA-8Wbps2Jd1g5_Gjrr-x3YWrJPek-mF5Asrrvz2Dg@mail.gmail.com
2002+
CREATE TABLE tt26(c int);
2003+
BEGIN;
2004+
CREATE TABLE tt27(c int);
2005+
SAVEPOINT q;
2006+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
2007+
SELECT * FROM tt27;
2008+
c
2009+
---
2010+
(0 rows)
2011+
2012+
ROLLBACK TO q;
2013+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
2014+
ROLLBACK;
2015+
BEGIN;
2016+
CREATE TABLE tt28(c int);
2017+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
2018+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
2019+
ERROR: "tt28" is already a view
2020+
ROLLBACK;
19982021
-- test display negative operator of const-folder expression
19992022
create table tdis(a int, b int, c int);
20002023
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' as the Apache Cloudberry data distribution key for this table.
@@ -2042,7 +2065,7 @@ drop cascades to view aliased_view_2
20422065
drop cascades to view aliased_view_3
20432066
drop cascades to view aliased_view_4
20442067
DROP SCHEMA testviewschm2 CASCADE;
2045-
NOTICE: drop cascades to 76 other objects
2068+
NOTICE: drop cascades to 77 other objects
20462069
DETAIL: drop cascades to table t1
20472070
drop cascades to view temporal1
20482071
drop cascades to view temporal2
@@ -2117,5 +2140,6 @@ drop cascades to view tt23v
21172140
drop cascades to view tt24v
21182141
drop cascades to view tt25v
21192142
drop cascades to view tt26v
2143+
drop cascades to table tt26
21202144
drop cascades to table tdis
21212145
drop cascades to view tdis_v1

src/test/regress/sql/create_view.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,28 @@ select x + y + z as c1,
692692
from (values(1,2,3)) v(x,y,z);
693693
select pg_get_viewdef('tt26v', true);
694694

695+
696+
-- Test that changing the relkind of a relcache entry doesn't cause
697+
-- trouble. Prior instances of where it did:
698+
-- CALDaNm2yXz+zOtv7y5zBd5WKT8O0Ld3YxikuU3dcyCvxF7gypA@mail.gmail.com
699+
-- CALDaNm3oZA-8Wbps2Jd1g5_Gjrr-x3YWrJPek-mF5Asrrvz2Dg@mail.gmail.com
700+
CREATE TABLE tt26(c int);
701+
702+
BEGIN;
703+
CREATE TABLE tt27(c int);
704+
SAVEPOINT q;
705+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
706+
SELECT * FROM tt27;
707+
ROLLBACK TO q;
708+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
709+
ROLLBACK;
710+
711+
BEGIN;
712+
CREATE TABLE tt28(c int);
713+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
714+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
715+
ROLLBACK;
716+
695717
-- test display negative operator of const-folder expression
696718
create table tdis(a int, b int, c int);
697719
create view tdis_v1 as select a,b,c, -1::int from tdis group by 1,2,3,4;

0 commit comments

Comments
 (0)