Skip to content

Commit 90cbc63

Browse files
committed
Have TRUNCATE advance the affected table's relfrozenxid to RecentXmin, to
avoid a later needless VACUUM for Xid-wraparound purposes. We can do this since the table is known to be left empty, so no Xid remains on it. Per discussion.
1 parent dfed001 commit 90cbc63

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/backend/catalog/index.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.282 2007/03/29 00:15:37 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.283 2007/05/16 17:28:20 alvherre Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -1188,9 +1188,12 @@ index_update_stats(Relation rel, bool hasindex, bool isprimary,
11881188
* setNewRelfilenode - assign a new relfilenode value to the relation
11891189
*
11901190
* Caller must already hold exclusive lock on the relation.
1191+
*
1192+
* The relation is marked with relfrozenxid=freezeXid (InvalidTransactionId
1193+
* must be passed for indexes)
11911194
*/
11921195
void
1193-
setNewRelfilenode(Relation relation)
1196+
setNewRelfilenode(Relation relation, TransactionId freezeXid)
11941197
{
11951198
Oid newrelfilenode;
11961199
RelFileNode newrnode;
@@ -1204,6 +1207,10 @@ setNewRelfilenode(Relation relation)
12041207
relation->rd_rel->relkind == RELKIND_INDEX);
12051208
/* Can't change for shared tables or indexes */
12061209
Assert(!relation->rd_rel->relisshared);
1210+
/* Indexes must have Invalid frozenxid; other relations must not */
1211+
Assert((relation->rd_rel->relkind == RELKIND_INDEX &&
1212+
freezeXid == InvalidTransactionId) ||
1213+
TransactionIdIsNormal(freezeXid));
12071214

12081215
/* Allocate a new relfilenode */
12091216
newrelfilenode = GetNewRelFileNode(relation->rd_rel->reltablespace,
@@ -1241,6 +1248,7 @@ setNewRelfilenode(Relation relation)
12411248
rd_rel->relfilenode = newrelfilenode;
12421249
rd_rel->relpages = 0; /* it's empty until further notice */
12431250
rd_rel->reltuples = 0;
1251+
rd_rel->relfrozenxid = freezeXid;
12441252
simple_heap_update(pg_class, &tuple->t_self, tuple);
12451253
CatalogUpdateIndexes(pg_class, tuple);
12461254

@@ -1957,7 +1965,7 @@ reindex_index(Oid indexId)
19571965
/*
19581966
* We'll build a new physical relation for the index.
19591967
*/
1960-
setNewRelfilenode(iRel);
1968+
setNewRelfilenode(iRel, InvalidTransactionId);
19611969
}
19621970

19631971
/* Initialize the index and rebuild */

src/backend/commands/tablecmds.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.223 2007/05/14 20:24:41 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.224 2007/05/16 17:28:20 alvherre Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -616,7 +616,7 @@ ExecuteTruncate(TruncateStmt *stmt)
616616
* the relfilenode value. The old storage file is scheduled for
617617
* deletion at commit.
618618
*/
619-
setNewRelfilenode(rel);
619+
setNewRelfilenode(rel, RecentXmin);
620620

621621
heap_relid = RelationGetRelid(rel);
622622
toast_relid = rel->rd_rel->reltoastrelid;
@@ -629,7 +629,7 @@ ExecuteTruncate(TruncateStmt *stmt)
629629
if (OidIsValid(toast_relid))
630630
{
631631
rel = relation_open(toast_relid, AccessExclusiveLock);
632-
setNewRelfilenode(rel);
632+
setNewRelfilenode(rel, RecentXmin);
633633
heap_close(rel, NoLock);
634634
}
635635

src/include/catalog/index.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/index.h,v 1.73 2007/01/09 02:14:15 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/index.h,v 1.74 2007/05/16 17:28:20 alvherre Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -53,7 +53,7 @@ extern void FormIndexDatum(IndexInfo *indexInfo,
5353
Datum *values,
5454
bool *isnull);
5555

56-
extern void setNewRelfilenode(Relation relation);
56+
extern void setNewRelfilenode(Relation relation, TransactionId freezeXid);
5757

5858
extern void index_build(Relation heapRelation,
5959
Relation indexRelation,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy