Index: doc/src/sgml/ref/declare.sgml
===================================================================
RCS file: /var/lib/cvs/pgsql-server/doc/src/sgml/ref/declare.sgml,v
retrieving revision 1.22
diff -c -r1.22 declare.sgml
*** doc/src/sgml/ref/declare.sgml	6 Apr 2003 22:41:52 -0000	1.22
--- doc/src/sgml/ref/declare.sgml	14 Apr 2003 00:38:46 -0000
***************
*** 96,102 ****
          Specifies that the cursor cannot be used outside of the
          transaction that created it. If neither <literal>WITHOUT
          HOLD</literal> nor <literal>WITH HOLD</literal> is specified,
!         <literal>WITH HOLD</literal> is the default.
         </para>
        </listitem>
       </varlistentry>
--- 96,102 ----
          Specifies that the cursor cannot be used outside of the
          transaction that created it. If neither <literal>WITHOUT
          HOLD</literal> nor <literal>WITH HOLD</literal> is specified,
!         <literal>WITHOUT HOLD</literal> is the default.
         </para>
        </listitem>
       </varlistentry>
Index: src/backend/catalog/sql_features.txt
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/backend/catalog/sql_features.txt,v
retrieving revision 1.3
diff -c -r1.3 sql_features.txt
*** src/backend/catalog/sql_features.txt	23 Jan 2003 23:38:55 -0000	1.3
--- src/backend/catalog/sql_features.txt	14 Apr 2003 00:38:46 -0000
***************
*** 95,101 ****
  E121	Basic cursor support	07	Positioned DELETE statement	NO	
  E121	Basic cursor support	08	CLOSE statement	YES	
  E121	Basic cursor support	10	FETCH statement implicit NEXT	YES	
! E121	Basic cursor support	17	WITH HOLD cursors	NO	
  E131	Null value support (nulls in lieu of values)			YES	
  E141	Basic integrity constraints			YES	
  E141	Basic integrity constraints	01	NOT NULL constraints	YES	
--- 95,101 ----
  E121	Basic cursor support	07	Positioned DELETE statement	NO	
  E121	Basic cursor support	08	CLOSE statement	YES	
  E121	Basic cursor support	10	FETCH statement implicit NEXT	YES	
! E121	Basic cursor support	17	WITH HOLD cursors	YES	
  E131	Null value support (nulls in lieu of values)			YES	
  E141	Basic integrity constraints			YES	
  E141	Basic integrity constraints	01	NOT NULL constraints	YES	
***************
*** 214,225 ****
  F401	Extended joined table	04	CROSS JOIN	YES	
  F411	Time zone specification			YES	
  F421	National character			YES	
! F431	Read-only scrollable cursors			NO	
  F431	Read-only scrollable cursors	01	FETCH with explicit NEXT	YES	
! F431	Read-only scrollable cursors	02	FETCH FIRST	NO	
! F431	Read-only scrollable cursors	03	FETCH LAST	NO	
  F431	Read-only scrollable cursors	04	FETCH PRIOR	YES	
! F431	Read-only scrollable cursors	05	FETCH ABSOLUTE	NO	
  F431	Read-only scrollable cursors	06	FETCH RELATIVE	YES	
  F441	Extended set function support			YES	
  F451	Character set definition			NO	
--- 214,225 ----
  F401	Extended joined table	04	CROSS JOIN	YES	
  F411	Time zone specification			YES	
  F421	National character			YES	
! F431	Read-only scrollable cursors			YES	
  F431	Read-only scrollable cursors	01	FETCH with explicit NEXT	YES	
! F431	Read-only scrollable cursors	02	FETCH FIRST	YES	
! F431	Read-only scrollable cursors	03	FETCH LAST	YES	
  F431	Read-only scrollable cursors	04	FETCH PRIOR	YES	
! F431	Read-only scrollable cursors	05	FETCH ABSOLUTE	YES	
  F431	Read-only scrollable cursors	06	FETCH RELATIVE	YES	
  F441	Extended set function support			YES	
  F451	Character set definition			NO	
***************
*** 319,325 ****
  T211	Basic trigger capability	05	Ability to specify a search condition that must be true before the trigger is invoked	NO	
  T211	Basic trigger capability	06	Support for run-time rules for the interaction of triggers and constraints	NO	
  T211	Basic trigger capability	07	TRIGGER privilege	YES	
! T211	Basic trigger capability	08	Multiple triggers for the same the event are executed in the order in which they were created	NO	
  T212	Enhanced trigger capability			YES	
  T231	SENSITIVE cursors			YES	
  T241	START TRANSACTION statement			YES	
--- 319,325 ----
  T211	Basic trigger capability	05	Ability to specify a search condition that must be true before the trigger is invoked	NO	
  T211	Basic trigger capability	06	Support for run-time rules for the interaction of triggers and constraints	NO	
  T211	Basic trigger capability	07	TRIGGER privilege	YES	
! T211	Basic trigger capability	08	Multiple triggers for the same the event are executed in the order in which they were created	NO	intentionally omitted
  T212	Enhanced trigger capability			YES	
  T231	SENSITIVE cursors			YES	
  T241	START TRANSACTION statement			YES	
Index: src/backend/commands/portalcmds.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/backend/commands/portalcmds.c,v
retrieving revision 1.11
diff -c -r1.11 portalcmds.c
*** src/backend/commands/portalcmds.c	27 Mar 2003 16:51:27 -0000	1.11
--- src/backend/commands/portalcmds.c	22 Apr 2003 05:37:20 -0000
***************
*** 22,27 ****
--- 22,28 ----
  #include "executor/executor.h"
  #include "optimizer/planner.h"
  #include "rewrite/rewriteHandler.h"
+ #include "storage/ipc.h"
  #include "utils/memutils.h"
  
  static long DoRelativeFetch(Portal portal,
***************
*** 34,40 ****
--- 35,49 ----
  								 CommandDest dest);
  static void DoPortalRewind(Portal portal);
  static Portal PreparePortal(DeclareCursorStmt *stmt);
+ static void CleanupHeldPortals(void);
  
+ /*
+  * To ensure the cleanup of the temp files created for holdable cursors,
+  * we need to add a callback exactly once for backend that needs to do
+  * this cleanup. If true, a callback has already been added for this
+  * backend.
+  */
+ static bool cursorCleanupCB = false;
  
  /*
   * PerformCursorOpen
***************
*** 680,685 ****
--- 689,705 ----
  	/* XXX: Should SortMem be used for this? */
  	portal->holdStore = tuplestore_begin_heap(true, true, SortMem);
  
+ 	/*
+ 	 * If this is the first holdable cursor that has been created by
+ 	 * this backend, we need to register a callback to ensure that any
+ 	 * temp files our tuplestore creates get cleaned up.
+ 	 */
+ 	if (!cursorCleanupCB)
+ 	{
+ 		on_shmem_exit(CleanupHeldPortals, 0);
+ 		cursorCleanupCB = true;
+ 	}
+ 
  	/* Set the destination to output to the tuplestore */
  	queryDesc->dest = Tuplestore;
  
***************
*** 767,770 ****
--- 787,802 ----
  	MemoryContextDelete(PortalGetHeapMemory(portal));
  
  	PortalGetHeapMemory(portal) = NULL;
+ }
+ 
+ /*
+  * Cleans up any long-term resources used by any holdable cursors
+  * created by this backend. This callback is invoked when the backend
+  * exits. At present, the only work we need to do here is to remove any
+  * temp files used to store holdable cursor results.
+  */
+ static void
+ CleanupHeldPortals(void)
+ {
+ 	CleanupTempFiles(true);
  }
Index: src/backend/storage/file/fd.c
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/backend/storage/file/fd.c,v
retrieving revision 1.97
diff -c -r1.97 fd.c
*** src/backend/storage/file/fd.c	4 Apr 2003 20:42:12 -0000	1.97
--- src/backend/storage/file/fd.c	22 Apr 2003 05:03:18 -0000
***************
*** 1109,1148 ****
   * AtEOXact_Files
   *
   * This routine is called during transaction commit or abort or backend
!  * exit (it doesn't particularly care which).  All still-open temporary-file
!  * VFDs are closed, which also causes the underlying files to be deleted.
!  * Furthermore, all "allocated" stdio files are closed.
   */
  void
  AtEOXact_Files(void)
  {
! 	Index		i;
  
  	if (SizeVfdCache > 0)
  	{
  		Assert(FileIsNotOpen(0));		/* Make sure ring not corrupted */
  		for (i = 1; i < SizeVfdCache; i++)
  		{
! 			if ((VfdCache[i].fdstate & FD_TEMPORARY) &&
! 				(VfdCache[i].fdstate & FD_TXN_TEMPORARY) &&
! 				VfdCache[i].fileName != NULL)
! 				FileClose(i);
  		}
  	}
- 
- 	while (numAllocatedFiles > 0)
- 		FreeFile(allocatedFiles[0]);
- 
- 	/*
- 	 * Reset the tempfile name counter to 0; not really necessary, but
- 	 * helps keep the names from growing unreasonably long.
- 	 */
- 	tempFileCounter = 0;
  }
  
- 
  /*
!  * Remove old temporary files
   *
   * This should be called during postmaster startup.  It will forcibly
   * remove any leftover files created by OpenTemporaryFile.
--- 1109,1167 ----
   * AtEOXact_Files
   *
   * This routine is called during transaction commit or abort or backend
!  * exit (it doesn't particularly care which).  All still-open
!  * per-transaction temporary file VFDs are closed, which also causes the
!  * underlying files to be deleted. Furthermore, all "allocated" stdio
!  * files are closed.
   */
  void
  AtEOXact_Files(void)
  {
! 	CleanupTempFiles(false);
! 
! 	while (numAllocatedFiles > 0)
! 		FreeFile(allocatedFiles[0]);
! }
! 
! /*
!  * Close temporary files and delete their underlying files.
!  *
!  * procExit: if true, this is being called as the backend process is
!  * exiting. If that's the case, we should remove all temporary files; if
!  * that's not the case, we need only remove per-transaction temporary
!  * files, since that means this function has been called for a
!  * transaction commit/abort.
!  */
! void
! CleanupTempFiles(bool procExit)
! {
! 	Index i;
  
  	if (SizeVfdCache > 0)
  	{
  		Assert(FileIsNotOpen(0));		/* Make sure ring not corrupted */
  		for (i = 1; i < SizeVfdCache; i++)
  		{
! 			unsigned short fdstate = VfdCache[i].fdstate;
! 
! 			if ((fdstate & FD_TEMPORARY) && VfdCache[i].fileName != NULL)
! 			{
! 				/*
! 				 * If we're in the process of exiting a backend process,
! 				 * close all temporary files. Otherwise, only close
! 				 * temporary files local to the current transaction.
! 				 */
! 				if (procExit)
! 					FileClose(i);
! 				else if (fdstate & FD_TXN_TEMPORARY)
! 					FileClose(i);
! 			}
  		}
  	}
  }
  
  /*
!  * Remove temporary files left over from a prior postmaster session
   *
   * This should be called during postmaster startup.  It will forcibly
   * remove any leftover files created by OpenTemporaryFile.
Index: src/include/storage/fd.h
===================================================================
RCS file: /var/lib/cvs/pgsql-server/src/include/storage/fd.h,v
retrieving revision 1.37
diff -c -r1.37 fd.h
*** src/include/storage/fd.h	27 Mar 2003 16:51:29 -0000	1.37
--- src/include/storage/fd.h	14 Apr 2003 00:38:46 -0000
***************
*** 73,78 ****
--- 73,79 ----
  /* Miscellaneous support routines */
  extern void closeAllVfds(void);
  extern void AtEOXact_Files(void);
+ extern void CleanupTempFiles(bool procExit);
  extern void RemovePgTempFiles(void);
  extern int	pg_fsync(int fd);
  extern int	pg_fdatasync(int fd);
