Skip to content

Commit 35b99a1

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 f98c4fb commit 35b99a1

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
@@ -1776,6 +1776,29 @@ select pg_get_ruledef(oid, true) from pg_rewrite
17761776
43 AS col_b;
17771777
(1 row)
17781778

1779+
-- Test that changing the relkind of a relcache entry doesn't cause
1780+
-- trouble. Prior instances of where it did:
1781+
-- CALDaNm2yXz+zOtv7y5zBd5WKT8O0Ld3YxikuU3dcyCvxF7gypA@mail.gmail.com
1782+
-- CALDaNm3oZA-8Wbps2Jd1g5_Gjrr-x3YWrJPek-mF5Asrrvz2Dg@mail.gmail.com
1783+
CREATE TABLE tt26(c int);
1784+
BEGIN;
1785+
CREATE TABLE tt27(c int);
1786+
SAVEPOINT q;
1787+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
1788+
SELECT * FROM tt27;
1789+
c
1790+
---
1791+
(0 rows)
1792+
1793+
ROLLBACK TO q;
1794+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
1795+
ROLLBACK;
1796+
BEGIN;
1797+
CREATE TABLE tt28(c int);
1798+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
1799+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
1800+
ERROR: "tt28" is already a view
1801+
ROLLBACK;
17791802
-- clean up all the random objects we made above
17801803
DROP SCHEMA temp_view_test CASCADE;
17811804
NOTICE: drop cascades to 27 other objects
@@ -1807,7 +1830,7 @@ drop cascades to view aliased_view_2
18071830
drop cascades to view aliased_view_3
18081831
drop cascades to view aliased_view_4
18091832
DROP SCHEMA testviewschm2 CASCADE;
1810-
NOTICE: drop cascades to 64 other objects
1833+
NOTICE: drop cascades to 65 other objects
18111834
DETAIL: drop cascades to table t1
18121835
drop cascades to view temporal1
18131836
drop cascades to view temporal2
@@ -1872,3 +1895,4 @@ drop cascades to view tt20v
18721895
drop cascades to view tt21v
18731896
drop cascades to view tt22v
18741897
drop cascades to view tt23v
1898+
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
@@ -608,6 +608,29 @@ select pg_get_viewdef('tt23v', true);
608608
select pg_get_ruledef(oid, true) from pg_rewrite
609609
where ev_class = 'tt23v'::regclass and ev_type = '1';
610610

611+
612+
-- Test that changing the relkind of a relcache entry doesn't cause
613+
-- trouble. Prior instances of where it did:
614+
-- CALDaNm2yXz+zOtv7y5zBd5WKT8O0Ld3YxikuU3dcyCvxF7gypA@mail.gmail.com
615+
-- CALDaNm3oZA-8Wbps2Jd1g5_Gjrr-x3YWrJPek-mF5Asrrvz2Dg@mail.gmail.com
616+
CREATE TABLE tt26(c int);
617+
618+
BEGIN;
619+
CREATE TABLE tt27(c int);
620+
SAVEPOINT q;
621+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
622+
SELECT * FROM tt27;
623+
ROLLBACK TO q;
624+
CREATE RULE "_RETURN" AS ON SELECT TO tt27 DO INSTEAD SELECT * FROM tt26;
625+
ROLLBACK;
626+
627+
BEGIN;
628+
CREATE TABLE tt28(c int);
629+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
630+
CREATE RULE "_RETURN" AS ON SELECT TO tt28 DO INSTEAD SELECT * FROM tt26;
631+
ROLLBACK;
632+
633+
611634
-- clean up all the random objects we made above
612635
DROP SCHEMA temp_view_test CASCADE;
613636
DROP SCHEMA testviewschm2 CASCADE;

0 commit comments

Comments
 (0)