mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-25 17:17:20 +02:00
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 commitd42358efb
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 d42358efb; 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 byd42358efb
so that some of them use tables with toast tables. Per bug #15540 from Pan Bian. Back-patch to 9.5 whered42358efb
came in. Discussion: https://postgr.es/m/15540-01078812338195c0@postgresql.org
This commit is contained in:
parent
6bc8193193
commit
aedd3d4dbd
@ -1600,19 +1600,22 @@ ExecuteTruncateGuts(List *explicit_rels, List *relids, List *relids_logged,
|
|||||||
heap_create_init_fork(rel);
|
heap_create_init_fork(rel);
|
||||||
|
|
||||||
heap_relid = RelationGetRelid(rel);
|
heap_relid = RelationGetRelid(rel);
|
||||||
toast_relid = rel->rd_rel->reltoastrelid;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The same for the toast table, if any.
|
* The same for the toast table, if any.
|
||||||
*/
|
*/
|
||||||
|
toast_relid = rel->rd_rel->reltoastrelid;
|
||||||
if (OidIsValid(toast_relid))
|
if (OidIsValid(toast_relid))
|
||||||
{
|
{
|
||||||
rel = relation_open(toast_relid, AccessExclusiveLock);
|
Relation toastrel = relation_open(toast_relid,
|
||||||
RelationSetNewRelfilenode(rel, rel->rd_rel->relpersistence,
|
AccessExclusiveLock);
|
||||||
|
|
||||||
|
RelationSetNewRelfilenode(toastrel,
|
||||||
|
toastrel->rd_rel->relpersistence,
|
||||||
RecentXmin, minmulti);
|
RecentXmin, minmulti);
|
||||||
if (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED)
|
if (toastrel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED)
|
||||||
heap_create_init_fork(rel);
|
heap_create_init_fork(toastrel);
|
||||||
heap_close(rel, NoLock);
|
heap_close(toastrel, NoLock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -79,9 +79,9 @@ end
|
|||||||
$$ language plpgsql;
|
$$ language plpgsql;
|
||||||
-- test effects of TRUNCATE on n_live_tup/n_dead_tup counters
|
-- test effects of TRUNCATE on n_live_tup/n_dead_tup counters
|
||||||
CREATE TABLE trunc_stats_test(id serial);
|
CREATE TABLE trunc_stats_test(id serial);
|
||||||
CREATE TABLE trunc_stats_test1(id serial);
|
CREATE TABLE trunc_stats_test1(id serial, stuff text);
|
||||||
CREATE TABLE trunc_stats_test2(id serial);
|
CREATE TABLE trunc_stats_test2(id serial);
|
||||||
CREATE TABLE trunc_stats_test3(id serial);
|
CREATE TABLE trunc_stats_test3(id serial, stuff text);
|
||||||
CREATE TABLE trunc_stats_test4(id serial);
|
CREATE TABLE trunc_stats_test4(id serial);
|
||||||
-- check that n_live_tup is reset to 0 after truncate
|
-- check that n_live_tup is reset to 0 after truncate
|
||||||
INSERT INTO trunc_stats_test DEFAULT VALUES;
|
INSERT INTO trunc_stats_test DEFAULT VALUES;
|
||||||
|
@ -79,9 +79,9 @@ $$ language plpgsql;
|
|||||||
|
|
||||||
-- test effects of TRUNCATE on n_live_tup/n_dead_tup counters
|
-- test effects of TRUNCATE on n_live_tup/n_dead_tup counters
|
||||||
CREATE TABLE trunc_stats_test(id serial);
|
CREATE TABLE trunc_stats_test(id serial);
|
||||||
CREATE TABLE trunc_stats_test1(id serial);
|
CREATE TABLE trunc_stats_test1(id serial, stuff text);
|
||||||
CREATE TABLE trunc_stats_test2(id serial);
|
CREATE TABLE trunc_stats_test2(id serial);
|
||||||
CREATE TABLE trunc_stats_test3(id serial);
|
CREATE TABLE trunc_stats_test3(id serial, stuff text);
|
||||||
CREATE TABLE trunc_stats_test4(id serial);
|
CREATE TABLE trunc_stats_test4(id serial);
|
||||||
|
|
||||||
-- check that n_live_tup is reset to 0 after truncate
|
-- check that n_live_tup is reset to 0 after truncate
|
||||||
|
Loading…
Reference in New Issue
Block a user