Tom Lane wrote:
> Alvaro Herrera <[EMAIL PROTECTED]> writes:
> > I suppose it would be pretty trivial to set the relfrozenxid to
> > RecentXmin or something during TRUNCATE.
> 
> I had the idea we were doing that already --- at least I'm pretty sure I
> remember it being discussed.  But I see it's not being done in HEAD.

Patch to do it attached.  I am thinking we can do something similar in
CLUSTER as well.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.
Index: src/backend/catalog/index.c
===================================================================
RCS file: /home/alvherre/Code/cvs/pgsql/src/backend/catalog/index.c,v
retrieving revision 1.282
diff -c -p -r1.282 index.c
*** src/backend/catalog/index.c	29 Mar 2007 00:15:37 -0000	1.282
--- src/backend/catalog/index.c	15 May 2007 22:06:12 -0000
*************** index_update_stats(Relation rel, bool ha
*** 1188,1196 ****
   * setNewRelfilenode		- assign a new relfilenode value to the relation
   *
   * Caller must already hold exclusive lock on the relation.
   */
  void
! setNewRelfilenode(Relation relation)
  {
  	Oid			newrelfilenode;
  	RelFileNode newrnode;
--- 1188,1199 ----
   * setNewRelfilenode		- assign a new relfilenode value to the relation
   *
   * Caller must already hold exclusive lock on the relation.
+  *
+  * The relation is marked with relfrozenxid=freezeXid (InvalidTransactionId
+  * must be passed for indexes)
   */
  void
! setNewRelfilenode(Relation relation, TransactionId freezeXid)
  {
  	Oid			newrelfilenode;
  	RelFileNode newrnode;
*************** setNewRelfilenode(Relation relation)
*** 1204,1209 ****
--- 1207,1216 ----
  		   relation->rd_rel->relkind == RELKIND_INDEX);
  	/* Can't change for shared tables or indexes */
  	Assert(!relation->rd_rel->relisshared);
+ 	/* Indexes must have Invalid frozenxid; other relations must not */
+ 	Assert((relation->rd_rel->relkind == RELKIND_INDEX &&
+ 			freezeXid == InvalidTransactionId) ||
+ 		   TransactionIdIsNormal(freezeXid));
  
  	/* Allocate a new relfilenode */
  	newrelfilenode = GetNewRelFileNode(relation->rd_rel->reltablespace,
*************** setNewRelfilenode(Relation relation)
*** 1241,1246 ****
--- 1248,1254 ----
  	rd_rel->relfilenode = newrelfilenode;
  	rd_rel->relpages = 0;		/* it's empty until further notice */
  	rd_rel->reltuples = 0;
+ 	rd_rel->relfrozenxid = freezeXid;
  	simple_heap_update(pg_class, &tuple->t_self, tuple);
  	CatalogUpdateIndexes(pg_class, tuple);
  
*************** reindex_index(Oid indexId)
*** 1957,1963 ****
  			/*
  			 * We'll build a new physical relation for the index.
  			 */
! 			setNewRelfilenode(iRel);
  		}
  
  		/* Initialize the index and rebuild */
--- 1965,1971 ----
  			/*
  			 * We'll build a new physical relation for the index.
  			 */
! 			setNewRelfilenode(iRel, InvalidTransactionId);
  		}
  
  		/* Initialize the index and rebuild */
Index: src/backend/commands/tablecmds.c
===================================================================
RCS file: /home/alvherre/Code/cvs/pgsql/src/backend/commands/tablecmds.c,v
retrieving revision 1.223
diff -c -p -r1.223 tablecmds.c
*** src/backend/commands/tablecmds.c	14 May 2007 20:24:41 -0000	1.223
--- src/backend/commands/tablecmds.c	15 May 2007 22:08:33 -0000
*************** ExecuteTruncate(TruncateStmt *stmt)
*** 616,622 ****
  		 * the relfilenode value.	The old storage file is scheduled for
  		 * deletion at commit.
  		 */
! 		setNewRelfilenode(rel);
  
  		heap_relid = RelationGetRelid(rel);
  		toast_relid = rel->rd_rel->reltoastrelid;
--- 616,622 ----
  		 * the relfilenode value.	The old storage file is scheduled for
  		 * deletion at commit.
  		 */
! 		setNewRelfilenode(rel, RecentXmin);
  
  		heap_relid = RelationGetRelid(rel);
  		toast_relid = rel->rd_rel->reltoastrelid;
*************** ExecuteTruncate(TruncateStmt *stmt)
*** 629,635 ****
  		if (OidIsValid(toast_relid))
  		{
  			rel = relation_open(toast_relid, AccessExclusiveLock);
! 			setNewRelfilenode(rel);
  			heap_close(rel, NoLock);
  		}
  
--- 629,635 ----
  		if (OidIsValid(toast_relid))
  		{
  			rel = relation_open(toast_relid, AccessExclusiveLock);
! 			setNewRelfilenode(rel, RecentXmin);
  			heap_close(rel, NoLock);
  		}
  
Index: src/include/catalog/index.h
===================================================================
RCS file: /home/alvherre/Code/cvs/pgsql/src/include/catalog/index.h,v
retrieving revision 1.73
diff -c -p -r1.73 index.h
*** src/include/catalog/index.h	9 Jan 2007 02:14:15 -0000	1.73
--- src/include/catalog/index.h	15 May 2007 21:59:31 -0000
*************** extern void FormIndexDatum(IndexInfo *in
*** 53,59 ****
  			   Datum *values,
  			   bool *isnull);
  
! extern void setNewRelfilenode(Relation relation);
  
  extern void index_build(Relation heapRelation,
  			Relation indexRelation,
--- 53,59 ----
  			   Datum *values,
  			   bool *isnull);
  
! extern void setNewRelfilenode(Relation relation, TransactionId freezeXid);
  
  extern void index_build(Relation heapRelation,
  			Relation indexRelation,
---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

               http://www.postgresql.org/docs/faq

Reply via email to