Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aedd3d4

Browse files
committed
Fix misapplication of pgstat_count_truncate to wrong relation.
The stanza of ExecuteTruncate[Guts] that truncates a target table's toast relation re-used the loop local variable "rel" to reference the toast rel. This was safe enough when written, but commit d42358e added code below that that supposed "rel" still pointed to the parent table. Therefore, the stats counter update was applied to the wrong relcache entry (the toast rel not the user rel); and if we were unlucky and that relcache entry had been flushed during reindex_relation, very bad things could ensue. (I'm surprised that CLOBBER_CACHE_ALWAYS testing hasn't found this. I'm even more surprised that the problem wasn't detected during the development of d42358e; it must not have been tested in any case with a toast table, as the incorrect stats counts are very obvious.) To fix, replace use of "rel" in that code branch with a more local variable. Adjust test cases added by d42358e so that some of them use tables with toast tables. Per bug #15540 from Pan Bian. Back-patch to 9.5 where d42358e came in. Discussion: https://postgr.es/m/15540-01078812338195c0@postgresql.org
1 parent 6bc8193 commit aedd3d4

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

src/backend/commands/tablecmds.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,19 +1600,22 @@ ExecuteTruncateGuts(List *explicit_rels, List *relids, List *relids_logged,
16001600
heap_create_init_fork(rel);
16011601

16021602
heap_relid = RelationGetRelid(rel);
1603-
toast_relid = rel->rd_rel->reltoastrelid;
16041603

16051604
/*
16061605
* The same for the toast table, if any.
16071606
*/
1607+
toast_relid = rel->rd_rel->reltoastrelid;
16081608
if (OidIsValid(toast_relid))
16091609
{
1610-
rel = relation_open(toast_relid, AccessExclusiveLock);
1611-
RelationSetNewRelfilenode(rel, rel->rd_rel->relpersistence,
1610+
Relation toastrel = relation_open(toast_relid,
1611+
AccessExclusiveLock);
1612+
1613+
RelationSetNewRelfilenode(toastrel,
1614+
toastrel->rd_rel->relpersistence,
16121615
RecentXmin, minmulti);
1613-
if (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED)
1614-
heap_create_init_fork(rel);
1615-
heap_close(rel, NoLock);
1616+
if (toastrel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED)
1617+
heap_create_init_fork(toastrel);
1618+
heap_close(toastrel, NoLock);
16161619
}
16171620

16181621
/*

src/test/regress/expected/stats.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ end
7979
$$ language plpgsql;
8080
-- test effects of TRUNCATE on n_live_tup/n_dead_tup counters
8181
CREATE TABLE trunc_stats_test(id serial);
82-
CREATE TABLE trunc_stats_test1(id serial);
82+
CREATE TABLE trunc_stats_test1(id serial, stuff text);
8383
CREATE TABLE trunc_stats_test2(id serial);
84-
CREATE TABLE trunc_stats_test3(id serial);
84+
CREATE TABLE trunc_stats_test3(id serial, stuff text);
8585
CREATE TABLE trunc_stats_test4(id serial);
8686
-- check that n_live_tup is reset to 0 after truncate
8787
INSERT INTO trunc_stats_test DEFAULT VALUES;

src/test/regress/sql/stats.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ $$ language plpgsql;
7979

8080
-- test effects of TRUNCATE on n_live_tup/n_dead_tup counters
8181
CREATE TABLE trunc_stats_test(id serial);
82-
CREATE TABLE trunc_stats_test1(id serial);
82+
CREATE TABLE trunc_stats_test1(id serial, stuff text);
8383
CREATE TABLE trunc_stats_test2(id serial);
84-
CREATE TABLE trunc_stats_test3(id serial);
84+
CREATE TABLE trunc_stats_test3(id serial, stuff text);
8585
CREATE TABLE trunc_stats_test4(id serial);
8686

8787
-- check that n_live_tup is reset to 0 after truncate

0 commit comments

Comments
 (0)