From cdeca5f590cc3c336c9c217831d68a2ca7f15265 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Tue, 4 Jul 2000 06:11:54 +0000
Subject: [PATCH] Make toast-table creation and deletion work somewhat
 reliably. Don't go through pg_exec_query_dest(), but directly to the
 execution routines.  Also, extend parameter lists so that there's no need to
 change the global setting of allowSystemTableMods, a hack that was certain to
 cause trouble in the event of any error.

---
 src/backend/bootstrap/bootparse.y          |  9 ++-
 src/backend/catalog/heap.c                 | 30 ++++----
 src/backend/catalog/index.c                | 15 ++--
 src/backend/commands/cluster.c             | 11 +--
 src/backend/commands/command.c             | 81 +++++++++++++---------
 src/backend/commands/creatinh.c            | 10 +--
 src/backend/commands/indexcmds.c           | 10 +--
 src/backend/commands/remove.c              |  4 +-
 src/backend/commands/view.c                |  5 +-
 src/backend/executor/execMain.c            | 10 ++-
 src/backend/libpq/be-pqexec.c              |  8 ++-
 src/backend/storage/large_object/inv_api.c |  9 +--
 src/backend/tcop/postgres.c                | 16 ++++-
 src/backend/utils/cache/temprel.c          |  5 +-
 src/include/access/tuptoaster.h            |  6 +-
 src/include/catalog/heap.h                 | 13 ++--
 src/include/catalog/index.h                |  5 +-
 src/include/catalog/pg_opclass.h           |  3 +-
 18 files changed, 148 insertions(+), 102 deletions(-)

diff --git a/src/backend/bootstrap/bootparse.y b/src/backend/bootstrap/bootparse.y
index 0a6f9d55ace..4c13aafc0eb 100644
--- a/src/backend/bootstrap/bootparse.y
+++ b/src/backend/bootstrap/bootparse.y
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/bootstrap/bootparse.y,v 1.30 2000/06/18 22:43:51 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/bootstrap/bootparse.y,v 1.31 2000/07/04 06:11:22 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -166,7 +166,7 @@ Boot_CreateStmt:
 							puts("creating bootstrap relation");
 						tupdesc = CreateTupleDesc(numattr,attrtypes);
 						reldesc = heap_create(LexIDStr($3), tupdesc,
-											  false, true);
+											  false, true, true);
 						if (DebugMode)
 							puts("bootstrap relation created ok");
 					}
@@ -177,7 +177,10 @@ Boot_CreateStmt:
 
 						tupdesc = CreateTupleDesc(numattr,attrtypes);
 						id = heap_create_with_catalog(LexIDStr($3),
-											tupdesc, RELKIND_RELATION, false);
+													  tupdesc,
+													  RELKIND_RELATION,
+													  false,
+													  true);
 						if (!Quiet)
 							printf("CREATED relation %s with OID %u\n",
 								   LexIDStr($3), id);
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index 2187b8db443..bd330b5d1ea 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.137 2000/07/03 23:09:27 wieck Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.138 2000/07/04 06:11:23 tgl Exp $
  *
  *
  * INTERFACE ROUTINES
@@ -172,7 +172,8 @@ Relation
 heap_create(char *relname,
 			TupleDesc tupDesc,
 			bool istemp,
-			bool storage_create)
+			bool storage_create,
+			bool allow_system_table_mods)
 {
 	static unsigned int uniqueId = 0;
 
@@ -189,7 +190,7 @@ heap_create(char *relname,
 	 */
 	AssertArg(natts > 0);
 
-	if (relname && !allowSystemTableMods &&
+	if (relname && !allow_system_table_mods &&
 		IsSystemRelationName(relname) && IsNormalProcessingMode())
 	{
 		elog(ERROR, "Illegal class name '%s'"
@@ -744,7 +745,8 @@ Oid
 heap_create_with_catalog(char *relname,
 						 TupleDesc tupdesc,
 						 char relkind,
-						 bool istemp)
+						 bool istemp,
+						 bool allow_system_table_mods)
 {
 	Relation	pg_class_desc;
 	Relation	new_rel_desc;
@@ -769,9 +771,9 @@ heap_create_with_catalog(char *relname,
 		(istemp && get_temp_rel_by_username(relname) != NULL))
 		elog(ERROR, "Relation '%s' already exists", relname);
 
-	/* save user relation name because heap_create changes it */
 	if (istemp)
 	{
+		/* save user relation name because heap_create changes it */
 		temp_relname = pstrdup(relname);		/* save original value */
 		relname = palloc(NAMEDATALEN);
 		strcpy(relname, temp_relname);	/* heap_create will change this */
@@ -797,7 +799,8 @@ heap_create_with_catalog(char *relname,
 	 *	work of creating the disk file for the relation.
 	 * ----------------
 	 */
-	new_rel_desc = heap_create(relname, tupdesc, istemp, false);
+	new_rel_desc = heap_create(relname, tupdesc, istemp, false,
+							   allow_system_table_mods);
 
 	new_rel_oid = new_rel_desc->rd_att->attrs[0]->attrelid;
 
@@ -1419,7 +1422,8 @@ DeleteTypeTuple(Relation rel)
  * --------------------------------
  */
 void
-heap_drop_with_catalog(const char *relname)
+heap_drop_with_catalog(const char *relname,
+					   bool allow_system_table_mods)
 {
 	Relation	rel;
 	Oid			rid;
@@ -1438,7 +1442,7 @@ heap_drop_with_catalog(const char *relname)
 	 * ----------------
 	 */
 	/* allow temp of pg_class? Guess so. */
-	if (!istemp && !allowSystemTableMods &&
+	if (!istemp && !allow_system_table_mods &&
 		IsSystemRelationName(RelationGetRelationName(rel)))
 		elog(ERROR, "System relation '%s' cannot be destroyed",
 			 RelationGetRelationName(rel));
@@ -1546,15 +1550,9 @@ heap_drop_with_catalog(const char *relname)
 	if (has_toasttable)
 	{
 		char	toast_relname[NAMEDATALEN];
-		bool	old_allow;
 
-		old_allow = allowSystemTableMods;
-		allowSystemTableMods = true;
-
-		sprintf(toast_relname, "pg_toast_%d", rid);
-		heap_drop_with_catalog(toast_relname);
-
-		allowSystemTableMods = old_allow;
+		sprintf(toast_relname, "pg_toast_%u", rid);
+		heap_drop_with_catalog(toast_relname, true);
 	}
 }
 
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 2c5ff64daba..a78f0fa3db9 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.121 2000/06/30 07:04:17 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.122 2000/07/04 06:11:23 tgl Exp $
  *
  *
  * INTERFACE ROUTINES
@@ -946,7 +946,8 @@ index_create(char *heapRelationName,
 			 Node *predicate,
 			 bool islossy,
 			 bool unique,
-			 bool primary)
+			 bool primary,
+			 bool allow_system_table_mods)
 {
 	Relation	heapRelation;
 	Relation	indexRelation;
@@ -989,13 +990,13 @@ index_create(char *heapRelationName,
 												numatts,
 												attNums);
 
-	/* save user relation name because heap_create changes it */
 	if (istemp)
 	{
-		temp_relname = pstrdup(indexRelationName);		/* save original value */
+		/* save user relation name because heap_create changes it */
+		temp_relname = pstrdup(indexRelationName);	/* save original value */
 		indexRelationName = palloc(NAMEDATALEN);
-		strcpy(indexRelationName, temp_relname);		/* heap_create will
-														 * change this */
+		strcpy(indexRelationName, temp_relname);	/* heap_create will
+													 * change this */
 	}
 
 	/* ----------------
@@ -1003,7 +1004,7 @@ index_create(char *heapRelationName,
 	 * ----------------
 	 */
 	indexRelation = heap_create(indexRelationName, indexTupDesc,
-								istemp, false);
+								istemp, false, allow_system_table_mods);
 
 	/* ----------------
 	 *	  construct the index relation descriptor
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index 0fff922545c..38539707cd8 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -15,7 +15,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.56 2000/06/17 23:41:36 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/commands/cluster.c,v 1.57 2000/07/04 06:11:27 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -30,6 +30,7 @@
 #include "catalog/pg_proc.h"
 #include "commands/cluster.h"
 #include "commands/rename.h"
+#include "miscadmin.h"
 #include "utils/builtins.h"
 #include "utils/syscache.h"
 
@@ -140,7 +141,7 @@ cluster(char *oldrelname, char *oldindexname)
 	StartTransactionCommand();
 
 	/* Destroy old heap (along with its index) and rename new. */
-	heap_drop_with_catalog(saveoldrelname);
+	heap_drop_with_catalog(saveoldrelname, allowSystemTableMods);
 
 	CommitTransactionCommand();
 	StartTransactionCommand();
@@ -176,7 +177,8 @@ copy_heap(Oid OIDOldHeap)
 	tupdesc = CreateTupleDescCopy(OldHeapDesc);
 
 	OIDNewHeap = heap_create_with_catalog(NewName, tupdesc,
-										  RELKIND_RELATION, false);
+										  RELKIND_RELATION, false,
+										  allowSystemTableMods);
 
 	if (!OidIsValid(OIDNewHeap))
 		elog(ERROR, "clusterheap: cannot create temporary heap relation\n");
@@ -276,7 +278,8 @@ copy_index(Oid OIDOldIndex, Oid OIDNewHeap)
 				 (Node *) NULL,	/* XXX where's the predicate? */
 				 Old_pg_index_Form->indislossy,
 				 Old_pg_index_Form->indisunique,
-				 Old_pg_index_Form->indisprimary);
+				 Old_pg_index_Form->indisprimary,
+				 allowSystemTableMods);
 
 	setRelhasindexInplace(OIDNewHeap, true, false);
 
diff --git a/src/backend/commands/command.c b/src/backend/commands/command.c
index 595c09dc4ec..19c87104299 100644
--- a/src/backend/commands/command.c
+++ b/src/backend/commands/command.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.82 2000/07/03 23:09:33 wieck Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.83 2000/07/04 06:11:27 tgl Exp $
  *
  * NOTES
  *	  The PerformAddAttribute() code, like most of the relation
@@ -21,8 +21,10 @@
 
 #include "catalog/catalog.h"
 #include "catalog/catname.h"
+#include "catalog/index.h"
 #include "catalog/indexing.h"
 #include "catalog/pg_attrdef.h"
+#include "catalog/pg_opclass.h"
 #include "commands/command.h"
 #include "executor/spi.h"
 #include "catalog/heap.h"
@@ -1184,22 +1186,18 @@ AlterTableCreateToastTable(const char *relationName)
 	Form_pg_attribute  *att;
 	Relation			class_rel;
 	Relation			ridescs[Num_pg_class_indices];
-	Oid					toast_relid = 2;
-	Oid					toast_idxid = 2;
+	Oid					toast_relid;
+	Oid					toast_idxid;
 	bool				has_toastable_attrs = false;
-	bool				old_allow;
 	int					i;
-
 	char				toast_relname[NAMEDATALEN];
 	char				toast_idxname[NAMEDATALEN];
-	char				tmp_query[1024];
 	Relation			toast_rel;
+	AttrNumber			attNums[1];
+	Oid					classObjectId[1];
 
 	/*
-	 * permissions checking.  this would normally be done in utility.c,
-	 * but this particular routine is recursive.
-	 *
-	 * normally, only the owner of a class can change its schema.
+	 * permissions checking.  XXX exactly what is appropriate here?
 	 */
 /*
 	if (!allowSystemTableMods && IsSystemRelationName(relationName))
@@ -1215,7 +1213,7 @@ AlterTableCreateToastTable(const char *relationName)
 	 * Grab an exclusive lock on the target table, which we will NOT
 	 * release until end of transaction.
 	 */
-	rel = heap_openr(relationName, RowExclusiveLock);
+	rel = heap_openr(relationName, AccessExclusiveLock);
 	myrelid = RelationGetRelid(rel);
 
 	/*
@@ -1240,8 +1238,8 @@ AlterTableCreateToastTable(const char *relationName)
 	 * Get the pg_class tuple for the relation
 	 */
 	reltup = SearchSysCacheTuple(RELNAME,
-									 PointerGetDatum(relationName),
-									 0, 0, 0);
+								 PointerGetDatum(relationName),
+								 0, 0, 0);
 
 	if (!HeapTupleIsValid(reltup))
 		elog(ERROR, "ALTER TABLE: relation \"%s\" not found",
@@ -1261,26 +1259,43 @@ AlterTableCreateToastTable(const char *relationName)
 				relationName);
 
 	/*
-	 * Create the toast table and it's index
-	 * This is bad and ugly, because we need to override
-	 * allowSystemTableMods in order to keep the toast
-	 * table- and index-name out of the users namespace.
+	 * Create the toast table and its index
 	 */
-	sprintf(toast_relname, "pg_toast_%d", myrelid);
-	sprintf(toast_idxname, "pg_toast_%d_idx", myrelid);
-
-	old_allow = allowSystemTableMods;
-	allowSystemTableMods = true;
-
-	sprintf(tmp_query, "create table \"%s\" (chunk_id oid, chunk_seq int4, chunk_data text)",
-			toast_relname);
-	pg_exec_query_dest(tmp_query, None, CurrentMemoryContext);
-
-	sprintf(tmp_query, "create index \"%s\" on \"%s\" (chunk_id)",
-			toast_idxname, toast_relname);
-	pg_exec_query_dest(tmp_query, None, CurrentMemoryContext);
-
-	allowSystemTableMods = old_allow;
+	sprintf(toast_relname, "pg_toast_%u", myrelid);
+	sprintf(toast_idxname, "pg_toast_%u_idx", myrelid);
+
+	/* this is pretty painful...  need a tuple descriptor */
+	tupdesc = CreateTemplateTupleDesc(3);
+	TupleDescInitEntry(tupdesc, (AttrNumber) 1,
+					   "chunk_id",
+					   OIDOID,
+					   -1, 0, false);
+	TupleDescInitEntry(tupdesc, (AttrNumber) 2,
+					   "chunk_seq",
+					   INT4OID,
+					   -1, 0, false);
+	TupleDescInitEntry(tupdesc, (AttrNumber) 3,
+					   "chunk_data",
+					   TEXTOID,	/* XXX wouldn't BYTEAOID be better? */
+					   -1, 0, false);
+
+	/* XXX use RELKIND_TOASTVALUE here? */
+	/* XXX what if owning relation is temp?  need we mark toasttable too? */
+	heap_create_with_catalog(toast_relname, tupdesc, RELKIND_RELATION,
+							 false, true);
+
+	/* make the toast relation visible, else index creation will fail */
+	CommandCounterIncrement();
+
+	/* create index on chunk_id */
+	attNums[0] = 1;
+	classObjectId[0] = OID_OPS_OID;
+	index_create(toast_relname, toast_idxname, NULL, NULL, BTREE_AM_OID,
+				 1, attNums, classObjectId,
+				 (Node *) NULL, false, false, false, true);
+
+	/* make the index visible in this transaction */
+	CommandCounterIncrement();
 
 	/*
 	 * Get the OIDs of the newly created objects
@@ -1318,8 +1333,8 @@ AlterTableCreateToastTable(const char *relationName)
 
 	heap_freetuple(reltup);
 
+	heap_close(class_rel, RowExclusiveLock);
 	heap_close(rel, NoLock);
-	heap_close(class_rel, NoLock);
 }
 
 
diff --git a/src/backend/commands/creatinh.c b/src/backend/commands/creatinh.c
index 401379f505c..1c49045330d 100644
--- a/src/backend/commands/creatinh.c
+++ b/src/backend/commands/creatinh.c
@@ -9,9 +9,9 @@
  *
  * IDENTIFICATION
 <<<<<<< creatinh.c
- *	  $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.61 2000/06/12 03:40:29 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.62 2000/07/04 06:11:27 tgl Exp $
 =======
- *	  $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.61 2000/06/12 03:40:29 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.62 2000/07/04 06:11:27 tgl Exp $
 >>>>>>> 1.58
  *
  *-------------------------------------------------------------------------
@@ -27,6 +27,7 @@
 #include "catalog/pg_ipl.h"
 #include "catalog/pg_type.h"
 #include "commands/creatinh.h"
+#include "miscadmin.h"
 #include "utils/syscache.h"
 
 /* ----------------
@@ -146,7 +147,8 @@ DefineRelation(CreateStmt *stmt, char relkind)
 	}
 
 	relationId = heap_create_with_catalog(relname, descriptor,
-										  relkind, stmt->istemp);
+										  relkind, stmt->istemp,
+										  allowSystemTableMods);
 
 	StoreCatalogInheritance(relationId, inheritList);
 
@@ -224,7 +226,7 @@ void
 RemoveRelation(char *name)
 {
 	AssertArg(name);
-	heap_drop_with_catalog(name);
+	heap_drop_with_catalog(name, allowSystemTableMods);
 }
 
 /*
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index bb45f01f673..1446aa8844b 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.32 2000/06/28 03:31:28 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.33 2000/07/04 06:11:27 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -17,6 +17,7 @@
 
 #include "access/genam.h"
 #include "access/heapam.h"
+#include "catalog/catalog.h"
 #include "catalog/catname.h"
 #include "catalog/heap.h"
 #include "catalog/index.h"
@@ -28,6 +29,7 @@
 #include "catalog/pg_proc.h"
 #include "catalog/pg_shadow.h"
 #include "commands/defrem.h"
+#include "miscadmin.h"
 #include "optimizer/clauses.h"
 #include "optimizer/planmain.h"
 #include "optimizer/prep.h"
@@ -38,8 +40,6 @@
 #include "utils/builtins.h"
 #include "utils/fmgroids.h"
 #include "utils/syscache.h"
-#include "miscadmin.h"			/* ReindexDatabase() */
-#include "catalog/catalog.h"	/* ReindexDatabase() */
 
 #define IsFuncIndex(ATTR_LIST) (((IndexElem*)lfirst(ATTR_LIST))->args != NIL)
 
@@ -197,7 +197,7 @@ DefineIndex(char *heapRelationName,
 					 accessMethodId, numberOfAttributes, attributeNumberA,
 					 classObjectId,
 					 (Node *) cnfPred,
-					 lossy, unique, primary);
+					 lossy, unique, primary, allowSystemTableMods);
 	}
 	else
 	{
@@ -215,7 +215,7 @@ DefineIndex(char *heapRelationName,
 					 accessMethodId, numberOfAttributes, attributeNumberA,
 					 classObjectId,
 					 (Node *) cnfPred,
-					 lossy, unique, primary);
+					 lossy, unique, primary, allowSystemTableMods);
 	}
 
 	/*
diff --git a/src/backend/commands/remove.c b/src/backend/commands/remove.c
index a90af396a05..75f3356289d 100644
--- a/src/backend/commands/remove.c
+++ b/src/backend/commands/remove.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/commands/Attic/remove.c,v 1.49 2000/05/28 20:34:50 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/commands/Attic/remove.c,v 1.50 2000/07/04 06:11:29 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -236,7 +236,7 @@ AttributeAndRelationRemove(Oid typeOid)
 			char	   *name;
 
 			name = NameStr(((Form_pg_class) GETSTRUCT(tup))->relname);
-			heap_drop_with_catalog(name);
+			heap_drop_with_catalog(name, allowSystemTableMods);
 		}
 		heap_endscan(scan);
 	}
diff --git a/src/backend/commands/view.c b/src/backend/commands/view.c
index 3b1bfedea23..8503eb3a7eb 100644
--- a/src/backend/commands/view.c
+++ b/src/backend/commands/view.c
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- *	$Id: view.c,v 1.44 2000/06/30 07:06:05 tgl Exp $
+ *	$Id: view.c,v 1.45 2000/07/04 06:11:30 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -17,6 +17,7 @@
 #include "catalog/heap.h"
 #include "commands/creatinh.h"
 #include "commands/view.h"
+#include "miscadmin.h"
 #include "nodes/makefuncs.h"
 #include "parser/parse_relation.h"
 #include "parser/parse_type.h"
@@ -296,5 +297,5 @@ RemoveView(char *viewName)
 	 * We just have to drop the relation; the associated rules will
 	 * be cleaned up automatically.
 	 */
-	heap_drop_with_catalog(viewName);
+	heap_drop_with_catalog(viewName, allowSystemTableMods);
 }
diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index f80fe9abab7..7492c7ae2f6 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -27,7 +27,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.118 2000/06/17 21:48:47 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.119 2000/07/04 06:11:33 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -877,8 +877,12 @@ InitPlan(CmdType operation, Query *parseTree, Plan *plan, EState *estate)
 				 */
 				tupdesc = CreateTupleDescCopy(tupType);
 
-				intoRelationId = heap_create_with_catalog(intoName,
-						   tupdesc, RELKIND_RELATION, parseTree->isTemp);
+				intoRelationId =
+					heap_create_with_catalog(intoName,
+											 tupdesc,
+											 RELKIND_RELATION,
+											 parseTree->isTemp,
+											 allowSystemTableMods);
 
 				FreeTupleDesc(tupdesc);
 
diff --git a/src/backend/libpq/be-pqexec.c b/src/backend/libpq/be-pqexec.c
index c76889a7a72..6834f513113 100644
--- a/src/backend/libpq/be-pqexec.c
+++ b/src/backend/libpq/be-pqexec.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/libpq/Attic/be-pqexec.c,v 1.33 2000/06/28 03:31:41 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/libpq/Attic/be-pqexec.c,v 1.34 2000/07/04 06:11:37 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -136,7 +136,11 @@ PQexec(char *query)
 	 *	pg_exec_query_dest will put the query results in a portal which will
 	 *	end up on the top of the portal stack.
 	 *
-	 * XXX memory context manipulation needs thought here.
+	 * XXX memory context manipulation is WRONG here --- the query needs
+	 * to be executed in a context different from CurrentMemoryContext,
+	 * perhaps a freshly created sub-context.  If I were expecting that
+	 * this code needs to work again, then I'd fix it.  But actually I'm
+	 * planning to rip out this entire module sometime soon...  tgl 7/2000.
 	 * ----------------
 	 */
 	pg_exec_query_dest(query, Local, CurrentMemoryContext);
diff --git a/src/backend/storage/large_object/inv_api.c b/src/backend/storage/large_object/inv_api.c
index 5ca3b7e8263..cbf15626781 100644
--- a/src/backend/storage/large_object/inv_api.c
+++ b/src/backend/storage/large_object/inv_api.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/storage/large_object/inv_api.c,v 1.72 2000/06/28 03:32:04 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/storage/large_object/inv_api.c,v 1.73 2000/07/04 06:11:39 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -137,7 +137,8 @@ inv_create(int flags)
 	 * be located on whatever storage manager the user requested.
 	 */
 
-	heap_create_with_catalog(objname, tupdesc, RELKIND_LOBJECT, false);
+	heap_create_with_catalog(objname, tupdesc, RELKIND_LOBJECT,
+							 false, false);
 
 	/* make the relation visible in this transaction */
 	CommandCounterIncrement();
@@ -165,7 +166,7 @@ inv_create(int flags)
 	classObjectId[0] = INT4_OPS_OID;
 	index_create(objname, indname, NULL, NULL, BTREE_AM_OID,
 				 1, &attNums[0], &classObjectId[0],
-				 (Node *) NULL, false, false, false);
+				 (Node *) NULL, false, false, false, false);
 
 	/* make the index visible in this transaction */
 	CommandCounterIncrement();
@@ -297,7 +298,7 @@ inv_drop(Oid lobjId)
 	 * Since heap_drop_with_catalog will destroy the relcache entry,
 	 * there's no need to drop the refcount in this path.
 	 */
-	heap_drop_with_catalog(RelationGetRelationName(r));
+	heap_drop_with_catalog(RelationGetRelationName(r), false);
 	return 1;
 }
 
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index b034442eaff..3843ce7a2d4 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.165 2000/07/03 20:48:37 petere Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.166 2000/07/04 06:11:43 tgl Exp $
  *
  * NOTES
  *	  this is the "main" module of the postgres backend and
@@ -538,7 +538,7 @@ pg_plan_query(Query *querytree)
  *
  * parse_context references a context suitable for holding the
  * parse/rewrite trees (typically this will be QueryContext).
- * This context must be longer-lived than the CurrentMemoryContext!
+ * This context *must* be longer-lived than the CurrentMemoryContext!
  * In fact, if the query string might contain BEGIN/COMMIT commands,
  * parse_context had better outlive TopTransactionContext!
  *
@@ -559,6 +559,16 @@ pg_exec_query_dest(char *query_string,	/* string to execute */
 	List	   *querytree_list,
 			   *querytree_item;
 
+	/*
+	 * If you called this routine with parse_context = CurrentMemoryContext,
+	 * you blew it.  They *must* be different, else the context reset
+	 * at the bottom of the loop will destroy the querytree list.
+	 * (We really ought to check that parse_context isn't a child of
+	 * CurrentMemoryContext either, but that would take more cycles than
+	 * it's likely to be worth.)
+	 */
+	Assert(parse_context != CurrentMemoryContext);
+
 	/*
 	 * Switch to appropriate context for constructing parsetrees.
 	 */
@@ -1404,7 +1414,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
 	if (!IsUnderPostmaster)
 	{
 		puts("\nPOSTGRES backend interactive interface ");
-		puts("$Revision: 1.165 $ $Date: 2000/07/03 20:48:37 $\n");
+		puts("$Revision: 1.166 $ $Date: 2000/07/04 06:11:43 $\n");
 	}
 
 	/*
diff --git a/src/backend/utils/cache/temprel.c b/src/backend/utils/cache/temprel.c
index 20c94ac57b6..e336e019398 100644
--- a/src/backend/utils/cache/temprel.c
+++ b/src/backend/utils/cache/temprel.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/temprel.c,v 1.25 2000/06/28 03:32:25 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/temprel.c,v 1.26 2000/07/04 06:11:47 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -33,6 +33,7 @@
 
 #include "catalog/heap.h"
 #include "catalog/index.h"
+#include "miscadmin.h"
 #include "utils/catcache.h"
 #include "utils/temprel.h"
 
@@ -112,7 +113,7 @@ remove_all_temp_relations(void)
 
 			/* safe from deallocation */
 			strcpy(relname, temp_rel->user_relname);
-			heap_drop_with_catalog(relname);
+			heap_drop_with_catalog(relname, allowSystemTableMods);
 		}
 		else
 			index_drop(temp_rel->relid);
diff --git a/src/include/access/tuptoaster.h b/src/include/access/tuptoaster.h
index c30f61218e3..da38f65385c 100644
--- a/src/include/access/tuptoaster.h
+++ b/src/include/access/tuptoaster.h
@@ -6,7 +6,7 @@
  *
  * Copyright (c) 2000, PostgreSQL Development Team
  *
- * $Id: tuptoaster.h,v 1.4 2000/07/04 00:04:03 tgl Exp $
+ * $Id: tuptoaster.h,v 1.5 2000/07/04 06:11:50 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -34,10 +34,6 @@ extern void heap_tuple_toast_attrs(Relation rel,
 
 extern varattrib *heap_tuple_untoast_attr(varattrib * attr);
 
-extern void heap_create_toast_table(Oid new_reloid,
-				TupleDesc new_tupdesc, bool istemp);
-				
-
 #endif	 /* TUPLE_TOASTER_ACTIVE */
 
 
diff --git a/src/include/catalog/heap.h b/src/include/catalog/heap.h
index c59509e1529..409e103f4d4 100644
--- a/src/include/catalog/heap.h
+++ b/src/include/catalog/heap.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: heap.h,v 1.30 2000/06/18 22:44:25 tgl Exp $
+ * $Id: heap.h,v 1.31 2000/07/04 06:11:54 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -26,13 +26,18 @@ typedef struct RawColumnDefault
 extern Oid	RelnameFindRelid(const char *relname);
 
 extern Relation heap_create(char *relname, TupleDesc tupDesc,
-							bool istemp, bool storage_create);
+							bool istemp, bool storage_create,
+							bool allow_system_table_mods);
+
 extern bool heap_storage_create(Relation rel);
 
 extern Oid heap_create_with_catalog(char *relname, TupleDesc tupdesc,
-						 char relkind, bool istemp);
+									char relkind, bool istemp,
+									bool allow_system_table_mods);
+
+extern void heap_drop_with_catalog(const char *relname,
+								   bool allow_system_table_mods);
 
-extern void heap_drop_with_catalog(const char *relname);
 extern void heap_truncate(char *relname);
 
 extern void AddRelationRawConstraints(Relation rel,
diff --git a/src/include/catalog/index.h b/src/include/catalog/index.h
index 6676265d666..dd495517d30 100644
--- a/src/include/catalog/index.h
+++ b/src/include/catalog/index.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: index.h,v 1.26 2000/06/30 07:04:06 tgl Exp $
+ * $Id: index.h,v 1.27 2000/07/04 06:11:54 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -37,7 +37,8 @@ extern void index_create(char *heapRelationName,
 			 Node *predicate,
 			 bool islossy,
 			 bool unique,
-			 bool primary);
+			 bool primary,
+			 bool allow_system_table_mods);
 
 extern void index_drop(Oid indexId);
 
diff --git a/src/include/catalog/pg_opclass.h b/src/include/catalog/pg_opclass.h
index b6a8da30cd3..d75bdcb52c4 100644
--- a/src/include/catalog/pg_opclass.h
+++ b/src/include/catalog/pg_opclass.h
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: pg_opclass.h,v 1.33 2000/06/19 03:54:45 tgl Exp $
+ * $Id: pg_opclass.h,v 1.34 2000/07/04 06:11:54 tgl Exp $
  *
  * NOTES
  *	  the genbki.sh script reads this file and generates .bki
@@ -74,6 +74,7 @@ DESCR("");
 #define INT4_OPS_OID 426
 DATA(insert OID =  427 (	oid_ops			 26   ));
 DESCR("");
+#define OID_OPS_OID 427
 DATA(insert OID =  428 (	float4_ops		700   ));
 DESCR("");
 DATA(insert OID =  429 (	char_ops		 18   ));
-- 
GitLab