From 20b4c46e263ca32d97e35aebc1662a9f0fd433fe Mon Sep 17 00:00:00 2001
From: "Marc G. Fournier" <scrappy@hub.org>
Date: Thu, 24 Oct 1996 07:55:54 +0000
Subject: [PATCH] Fixes: Errors when PQexec() in backend creates temp          
     relations and transaction is aborted

Submitted by: wieck@sapserv.debis.de (Jan Wieck)
---
 src/backend/catalog/heap.c          | 19 ++++++++++++++++---
 src/backend/postmaster/postmaster.c |  4 ++--
 src/backend/utils/cache/relcache.c  | 11 +++++++++--
 src/include/utils/rel.h             |  4 +++-
 4 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index 6e9634aa95a..a06163ae2f9 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *    $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.1.1.1 1996/07/09 06:21:15 scrappy Exp $
+ *    $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.2 1996/10/24 07:54:45 scrappy Exp $
  *
  * INTERFACE ROUTINES
  *	heap_creatr()		- Create an uncataloged heap relation
@@ -315,6 +315,13 @@ heap_creatr(char *name,
  	rdesc->rd_rel->reltype = relid;
      }
 
+    /* ----------------
+     *  remember if this is a temp relation
+     * ----------------
+     */
+
+    rdesc->rd_istemp = isTemp;
+
     /* ----------------
      *	have the storage manager create the relation.
      * ----------------
@@ -1306,6 +1313,9 @@ heap_destroy(char *relname)
      * ----------------
      */
     (void) smgrunlink(rdesc->rd_rel->relsmgr, rdesc);
+    if(rdesc->rd_istemp) {
+        rdesc->rd_tmpunlinked = TRUE;
+    }
     heap_close(rdesc);
 }
 
@@ -1320,6 +1330,9 @@ heap_destroyr(Relation rdesc)
 {
     ReleaseTmpRelBuffers(rdesc);
     (void) smgrunlink(rdesc->rd_rel->relsmgr, rdesc);
+    if(rdesc->rd_istemp) {
+        rdesc->rd_tmpunlinked = TRUE;
+    }
     heap_close(rdesc);
     RemoveFromTempRelList(rdesc);
 }
@@ -1357,7 +1370,7 @@ InitTempRelList()
     tempRels = (TempRelList*)malloc(sizeof(TempRelList));
     tempRels->size = TEMP_REL_LIST_SIZE;
     tempRels->rels = (Relation*)malloc(sizeof(Relation) * tempRels->size);
-    memset(tempRels->rels, sizeof(Relation) * tempRels->size , 0);
+    memset(tempRels->rels, 0, sizeof(Relation) * tempRels->size);
     tempRels->num = 0;
 }
 
@@ -1418,7 +1431,7 @@ DestroyTempRels()
     for (i=0;i<tempRels->num;i++) {
 	rdesc = tempRels->rels[i];
 	/* rdesc may be NULL if it has been removed from the list already */
-	if (rdesc) 
+	if (rdesc)
 	    heap_destroyr(rdesc);
     }
     free(tempRels->rels);
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index cc9d2865c4a..d47e27651b4 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -10,7 +10,7 @@
  *
  *
  * IDENTIFICATION
- *    $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.15 1996/10/13 18:38:04 momjian Exp $
+ *    $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.16 1996/10/24 07:55:07 scrappy Exp $
  *
  * NOTES
  *
@@ -876,7 +876,7 @@ BackendStartup(StartupInfo *packet, /* client's startup packet */
     static char	envEntry[4][2 * ARGV_SIZE];
     
     for (i = 0; i < 4; ++i) {
-	memset(envEntry[i], 2*ARGV_SIZE,0);
+	memset(envEntry[i], 0, 2*ARGV_SIZE);
     }
     /*
      * Set up the necessary environment variables for the backend
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index f10d3da7f61..81d9ca59898 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *    $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.2 1996/10/23 07:41:00 scrappy Exp $
+ *    $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.3 1996/10/24 07:55:29 scrappy Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1418,7 +1418,14 @@ RelationPurgeLocalRelation(bool xactCommitted)
 	     * remove the file if we abort. This is so that files for 
 	     * tables created inside a transaction block get removed.
 	     */
-	    smgrunlink(reln->rd_rel->relsmgr, reln);
+	    if(reln->rd_istemp) {
+	        if(!(reln->rd_tmpunlinked)) {
+		    smgrunlink(reln->rd_rel->relsmgr, reln);
+		    reln->rd_tmpunlinked = TRUE;
+		} 
+	    } else {
+		smgrunlink(reln->rd_rel->relsmgr, reln);
+	    }
 	}
 	
 	reln->rd_islocal = FALSE;
diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h
index 7f8750cb7c1..59ca8d4342a 100644
--- a/src/include/utils/rel.h
+++ b/src/include/utils/rel.h
@@ -6,7 +6,7 @@
  *
  * Copyright (c) 1994, Regents of the University of California
  *
- * $Id: rel.h,v 1.3 1996/10/19 04:31:45 scrappy Exp $
+ * $Id: rel.h,v 1.4 1996/10/24 07:55:54 scrappy Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -26,6 +26,8 @@ typedef struct RelationData {
     uint16		rd_refcnt; 	/* reference count */
     bool		rd_islocal; 	/* uses the local buffer mgr */
     bool		rd_isnailed; 	/* rel is nailed in cache */
+    bool		rd_istemp;	/* rel is a temp rel */
+    bool		rd_tmpunlinked;	/* temp rel already unlinked */
     Form_pg_am 		rd_am; 		/* AM tuple */
     Form_pg_class	rd_rel;		/* RELATION tuple */
     Oid			rd_id;		/* relations's object id */
-- 
GitLab