*** src/backend/commands/vacuum.c.orig	Mon Sep 30 15:45:57 2002
--- src/backend/commands/vacuum.c	Sat Oct 12 14:21:29 2002
***************
*** 187,192 ****
--- 187,196 ----
  	if (IsTransactionBlock())
  		elog(ERROR, "%s cannot run inside a BEGIN/END block", stmttype);
  
+ 	/* Running VACUUM from a function would free the function context */
+ 	if (!MemoryContextContains(QueryContext, vacstmt))
+ 		elog(ERROR, "%s cannot be executed from a function", stmttype);
+                         
  	/*
  	 * Send info about dead objects to the statistics collector
  	 */
***************
*** 1320,1326 ****
  		usable_free_size = 0;
  	}
  
! 	if (usable_free_size > 0 && num_vtlinks > 0)
  	{
  		qsort((char *) vtlinks, num_vtlinks, sizeof(VTupleLinkData),
  			  vac_cmp_vtlinks);
--- 1324,1331 ----
  		usable_free_size = 0;
  	}
  
! 	/* don't bother to save vtlinks if we will not call repair_frag */
! 	if (fraged_pages->num_pages > 0 && num_vtlinks > 0)
  	{
  		qsort((char *) vtlinks, num_vtlinks, sizeof(VTupleLinkData),
  			  vac_cmp_vtlinks);
***************
*** 1602,1608 ****
  			 */
  			if ((tuple.t_data->t_infomask & HEAP_UPDATED &&
  			 !TransactionIdPrecedes(tuple.t_data->t_xmin, OldestXmin)) ||
! 				(!(tuple.t_data->t_infomask & HEAP_XMAX_INVALID) &&
  				 !(ItemPointerEquals(&(tuple.t_self),
  									 &(tuple.t_data->t_ctid)))))
  			{
--- 1607,1614 ----
  			 */
  			if ((tuple.t_data->t_infomask & HEAP_UPDATED &&
  			 !TransactionIdPrecedes(tuple.t_data->t_xmin, OldestXmin)) ||
! 				(!(tuple.t_data->t_infomask & (HEAP_XMAX_INVALID |
! 											   HEAP_MARKED_FOR_UPDATE)) &&
  				 !(ItemPointerEquals(&(tuple.t_self),
  									 &(tuple.t_data->t_ctid)))))
  			{
***************
*** 1633,1639 ****
  				 * If this tuple is in the begin/middle of the chain then
  				 * we have to move to the end of chain.
  				 */
! 				while (!(tp.t_data->t_infomask & HEAP_XMAX_INVALID) &&
  					   !(ItemPointerEquals(&(tp.t_self),
  										   &(tp.t_data->t_ctid))))
  				{
--- 1639,1646 ----
  				 * If this tuple is in the begin/middle of the chain then
  				 * we have to move to the end of chain.
  				 */
! 				while (!(tp.t_data->t_infomask & (HEAP_XMAX_INVALID |
! 												  HEAP_MARKED_FOR_UPDATE)) &&
  					   !(ItemPointerEquals(&(tp.t_self),
  										   &(tp.t_data->t_ctid))))
  				{
