Skip to content

Commit 6344bc0

Browse files
committed
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 303b26c commit 6344bc0

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/test/regress/expected/create_view.out

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

1993+
-- Test that changing the relkind of a relcache entry doesn't cause
1994+
-- trouble. Prior instances of where it did:
1995+
-- CALDaNm2yXz+zOtv7y5zBd5WKT8O0Ld3YxikuU3dcyCvxF7gypA@mail.gmail.com
1996+
-- CALDaNm3oZA-8Wbps2Jd1g5_Gjrr-x3YWrJPek-mF5Asrrvz2Dg@mail.gmail.com
1997+
CREATE TABLE tt26(c int);
1998+
BEGIN;
1999+
CREATE TABLE tt27(c int);
2000+
SAVEPOINT q;
2001+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
2002+
SELECT * FROM tt27;
2003+
c
2004+
---
2005+
(0 rows)
2006+
2007+
ROLLBACK TO q;
2008+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
2009+
ROLLBACK;
2010+
BEGIN;
2011+
CREATE TABLE tt28(c int);
2012+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
2013+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
2014+
ERROR: "tt28" is already a view
2015+
ROLLBACK;
19932016
-- clean up all the random objects we made above
19942017
DROP SCHEMA temp_view_test CASCADE;
19952018
NOTICE: drop cascades to 27 other objects
@@ -2021,7 +2044,7 @@ drop cascades to view aliased_view_2
20212044
drop cascades to view aliased_view_3
20222045
drop cascades to view aliased_view_4
20232046
DROP SCHEMA testviewschm2 CASCADE;
2024-
NOTICE: drop cascades to 74 other objects
2047+
NOTICE: drop cascades to 75 other objects
20252048
DETAIL: drop cascades to table t1
20262049
drop cascades to view temporal1
20272050
drop cascades to view temporal2
@@ -2096,3 +2119,4 @@ drop cascades to view tt23v
20962119
drop cascades to view tt24v
20972120
drop cascades to view tt25v
20982121
drop cascades to view tt26v
2122+
drop cascades to table tt26

src/test/regress/sql/create_view.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,29 @@ select x + y + z as c1,
686686
from (values(1,2,3)) v(x,y,z);
687687
select pg_get_viewdef('tt26v', true);
688688

689+
690+
-- Test that changing the relkind of a relcache entry doesn't cause
691+
-- trouble. Prior instances of where it did:
692+
-- CALDaNm2yXz+zOtv7y5zBd5WKT8O0Ld3YxikuU3dcyCvxF7gypA@mail.gmail.com
693+
-- CALDaNm3oZA-8Wbps2Jd1g5_Gjrr-x3YWrJPek-mF5Asrrvz2Dg@mail.gmail.com
694+
CREATE TABLE tt26(c int);
695+
696+
BEGIN;
697+
CREATE TABLE tt27(c int);
698+
SAVEPOINT q;
699+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
700+
SELECT * FROM tt27;
701+
ROLLBACK TO q;
702+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
703+
ROLLBACK;
704+
705+
BEGIN;
706+
CREATE TABLE tt28(c int);
707+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
708+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
709+
ROLLBACK;
710+
711+
689712
-- clean up all the random objects we made above
690713
DROP SCHEMA temp_view_test CASCADE;
691714
DROP SCHEMA testviewschm2 CASCADE;

0 commit comments

Comments
 (0)