Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5deadfe

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

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
@@ -1591,19 +1591,22 @@ ExecuteTruncateGuts(List *explicit_rels, List *relids, List *relids_logged,
15911591
heap_create_init_fork(rel);
15921592

15931593
heap_relid=RelationGetRelid(rel);
1594-
toast_relid=rel->rd_rel->reltoastrelid;
15951594

15961595
/*
15971596
* The same for the toast table, if any.
15981597
*/
1598+
toast_relid=rel->rd_rel->reltoastrelid;
15991599
if (OidIsValid(toast_relid))
16001600
{
1601-
rel=relation_open(toast_relid,AccessExclusiveLock);
1602-
RelationSetNewRelfilenode(rel,rel->rd_rel->relpersistence,
1601+
Relationtoastrel=relation_open(toast_relid,
1602+
AccessExclusiveLock);
1603+
1604+
RelationSetNewRelfilenode(toastrel,
1605+
toastrel->rd_rel->relpersistence,
16031606
RecentXmin,minmulti);
1604-
if (rel->rd_rel->relpersistence==RELPERSISTENCE_UNLOGGED)
1605-
heap_create_init_fork(rel);
1606-
heap_close(rel,NoLock);
1607+
if (toastrel->rd_rel->relpersistence==RELPERSISTENCE_UNLOGGED)
1608+
heap_create_init_fork(toastrel);
1609+
heap_close(toastrel,NoLock);
16071610
}
16081611

16091612
/*

‎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
CREATETABLEtrunc_stats_test(idserial);
82-
CREATETABLEtrunc_stats_test1(idserial);
82+
CREATETABLEtrunc_stats_test1(idserial, stufftext);
8383
CREATETABLEtrunc_stats_test2(idserial);
84-
CREATETABLEtrunc_stats_test3(idserial);
84+
CREATETABLEtrunc_stats_test3(idserial, stufftext);
8585
CREATETABLEtrunc_stats_test4(idserial);
8686

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp