summaryrefslogtreecommitdiff
path: root/src/backend/executor
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>1999-09-18 19:08:25 +0000
committerTom Lane <tgl@sss.pgh.pa.us>1999-09-18 19:08:25 +0000
commitbd272cace63effa23d68a6b344a07e326b6a41e2 (patch)
tree3026c545c238bd38eadc7fb1fac89d636cf51673 /src/backend/executor
parent6c86fd5ba49bc03949ea1c788023f39da9c0b9fe (diff)
downloadpostgresql-bd272cace63effa23d68a6b344a07e326b6a41e2.tar.gz
Mega-commit to make heap_open/heap_openr/heap_close take an
additional argument specifying the kind of lock to acquire/release (or 'NoLock' to do no lock processing). Ensure that all relations are locked with some appropriate lock level before being examined --- this ensures that relevant shared-inval messages have been processed and should prevent problems caused by concurrent VACUUM. Fix several bugs having to do with mismatched increment/decrement of relation ref count and mismatched heap_open/close (which amounts to the same thing). A bogus ref count on a relation doesn't matter much *unless* a SI Inval message happens to arrive at the wrong time, which is probably why we got away with this sloppiness for so long. Repair missing grab of AccessExclusiveLock in DROP TABLE, ALTER/RENAME TABLE, etc, as noted by Hiroshi. Recommend 'make clean all' after pulling this update; I modified the Relation struct layout slightly. Will post further discussion to pghackers list shortly.
Diffstat (limited to 'src/backend/executor')
-rw-r--r--src/backend/executor/execAmi.c60
-rw-r--r--src/backend/executor/execMain.c19
-rw-r--r--src/backend/executor/execUtils.c10
-rw-r--r--src/backend/executor/nodeAppend.c6
4 files changed, 42 insertions, 53 deletions
diff --git a/src/backend/executor/execAmi.c b/src/backend/executor/execAmi.c
index 40a94fd02c..9c1b7b8bb4 100644
--- a/src/backend/executor/execAmi.c
+++ b/src/backend/executor/execAmi.c
@@ -1,11 +1,11 @@
/*-------------------------------------------------------------------------
*
* execAmi.c
- * miscellanious executor access method routines
+ * miscellaneous executor access method routines
*
* Copyright (c) 1994, Regents of the University of California
*
- * $Id: execAmi.c,v 1.41 1999/07/17 20:16:56 momjian Exp $
+ * $Id: execAmi.c,v 1.42 1999/09/18 19:06:47 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -52,7 +52,6 @@
static Pointer ExecBeginScan(Relation relation, int nkeys, ScanKey skeys,
bool isindex, ScanDirection dir, Snapshot snapshot);
-static Relation ExecOpenR(Oid relationOid, bool isindex);
/* ----------------------------------------------------------------
* ExecOpenScanR
@@ -90,47 +89,33 @@ ExecOpenScanR(Oid relOid,
* abstraction someday -cim 9/9/89
* ----------------
*/
- relation = ExecOpenR(relOid, isindex);
- scanDesc = ExecBeginScan(relation,
- nkeys,
- skeys,
- isindex,
- dir,
- snapshot);
-
- if (returnRelation != NULL)
- *returnRelation = relation;
- if (scanDesc != NULL)
- *returnScanDesc = scanDesc;
-}
-
-/* ----------------------------------------------------------------
- * ExecOpenR
- *
- * returns a relation descriptor given an object id.
- * ----------------------------------------------------------------
- */
-static Relation
-ExecOpenR(Oid relationOid, bool isindex)
-{
- Relation relation;
-
- relation = (Relation) NULL;
/* ----------------
* open the relation with the correct call depending
* on whether this is a heap relation or an index relation.
+ *
+ * Do not lock the rel here; beginscan will acquire AccessShareLock.
* ----------------
*/
if (isindex)
- relation = index_open(relationOid);
+ relation = index_open(relOid);
else
- relation = heap_open(relationOid);
+ relation = heap_open(relOid, NoLock);
if (relation == NULL)
- elog(DEBUG, "ExecOpenR: relation == NULL, heap_open failed.");
+ elog(ERROR, "ExecOpenScanR: failed to open relation %u", relOid);
+
+ scanDesc = ExecBeginScan(relation,
+ nkeys,
+ skeys,
+ isindex,
+ dir,
+ snapshot);
- return relation;
+ if (returnRelation != NULL)
+ *returnRelation = relation;
+ if (scanDesc != NULL)
+ *returnScanDesc = scanDesc;
}
/* ----------------------------------------------------------------
@@ -243,15 +228,20 @@ ExecCloseR(Plan *node)
if (scanDesc != NULL)
heap_endscan(scanDesc);
+ /*
+ * endscan released AccessShareLock acquired by beginscan. If we are
+ * holding any stronger locks on the rel, they should be held till end of
+ * xact. Therefore, we need only close the rel and not release locks.
+ */
if (relation != NULL)
- heap_close(relation);
+ heap_close(relation, NoLock);
/* ----------------
* if this is an index scan then we have to take care
* of the index relations as well..
* ----------------
*/
- if (nodeTag(node) == T_IndexScan)
+ if (IsA(node, IndexScan))
{
IndexScan *iscan = (IndexScan *) node;
IndexScanState *indexstate;
diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index 48c0a885f3..97dffe548f 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -26,7 +26,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.93 1999/07/17 20:16:57 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.94 1999/09/18 19:06:47 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -587,14 +587,12 @@ InitPlan(CmdType operation, Query *parseTree, Plan *plan, EState *estate)
resultRelationIndex = resultRelation;
rtentry = rt_fetch(resultRelationIndex, rangeTable);
resultRelationOid = rtentry->relid;
- resultRelationDesc = heap_open(resultRelationOid);
+ resultRelationDesc = heap_open(resultRelationOid, RowExclusiveLock);
if (resultRelationDesc->rd_rel->relkind == RELKIND_SEQUENCE)
elog(ERROR, "You can't change sequence relation %s",
resultRelationDesc->rd_rel->relname.data);
- LockRelation(resultRelationDesc, RowExclusiveLock);
-
resultRelationInfo = makeNode(RelationInfo);
resultRelationInfo->ri_RangeTableIndex = resultRelationIndex;
resultRelationInfo->ri_RelationDesc = resultRelationDesc;
@@ -636,8 +634,7 @@ InitPlan(CmdType operation, Query *parseTree, Plan *plan, EState *estate)
{
rm = lfirst(l);
relid = ((RangeTblEntry *) nth(rm->rti - 1, rangeTable))->relid;
- relation = heap_open(relid);
- LockRelation(relation, RowShareLock);
+ relation = heap_open(relid, RowShareLock);
if (!(rm->info & ROW_MARK_FOR_UPDATE))
continue;
erm = (execRowMark *) palloc(sizeof(execRowMark));
@@ -758,7 +755,8 @@ InitPlan(CmdType operation, Query *parseTree, Plan *plan, EState *estate)
*/
setheapoverride(true);
- intoRelationDesc = heap_open(intoRelationId);
+ intoRelationDesc = heap_open(intoRelationId,
+ AccessExclusiveLock);
setheapoverride(false);
}
@@ -809,14 +807,15 @@ EndPlan(Plan *plan, EState *estate)
}
/*
- * close the result relations if necessary
+ * close the result relations if necessary,
+ * but hold locks on them until xact commit
*/
if (resultRelationInfo != NULL)
{
Relation resultRelationDesc;
resultRelationDesc = resultRelationInfo->ri_RelationDesc;
- heap_close(resultRelationDesc);
+ heap_close(resultRelationDesc, NoLock);
/*
* close indices on the result relation
@@ -828,7 +827,7 @@ EndPlan(Plan *plan, EState *estate)
* close the "into" relation if necessary
*/
if (intoRelationDesc != NULL)
- heap_close(intoRelationDesc);
+ heap_close(intoRelationDesc, NoLock);
}
/* ----------------------------------------------------------------
diff --git a/src/backend/executor/execUtils.c b/src/backend/executor/execUtils.c
index 4bacf3f7bd..197995c346 100644
--- a/src/backend/executor/execUtils.c
+++ b/src/backend/executor/execUtils.c
@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/executor/execUtils.c,v 1.48 1999/07/16 04:58:47 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/executor/execUtils.c,v 1.49 1999/09/18 19:06:48 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -772,7 +772,7 @@ ExecOpenIndices(Oid resultRelationOid,
* open pg_index
* ----------------
*/
- indexRd = heap_openr(IndexRelationName);
+ indexRd = heap_openr(IndexRelationName, AccessShareLock);
/* ----------------
* form a scan key
@@ -856,7 +856,7 @@ ExecOpenIndices(Oid resultRelationOid,
* ----------------
*/
heap_endscan(indexSd);
- heap_close(indexRd);
+ heap_close(indexRd, AccessShareLock);
/* ----------------
* Now that we've collected the index information into three
@@ -913,7 +913,7 @@ ExecOpenIndices(Oid resultRelationOid,
/*
* Hack for not btree and hash indices: they use relation
- * level exclusive locking on updation (i.e. - they are
+ * level exclusive locking on update (i.e. - they are
* not ready for MVCC) and so we have to exclusively lock
* indices here to prevent deadlocks if we will scan them
* - index_beginscan places AccessShareLock, indices
@@ -1010,7 +1010,7 @@ ExecCloseIndices(RelationInfo *resultRelationInfo)
continue;
/*
- * Notes in ExecOpenIndices.
+ * See notes in ExecOpenIndices.
*/
if (relationDescs[i]->rd_rel->relam != BTREE_AM_OID &&
relationDescs[i]->rd_rel->relam != HASH_AM_OID)
diff --git a/src/backend/executor/nodeAppend.c b/src/backend/executor/nodeAppend.c
index 19a1e53b69..bd515d51f9 100644
--- a/src/backend/executor/nodeAppend.c
+++ b/src/backend/executor/nodeAppend.c
@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/executor/nodeAppend.c,v 1.24 1999/07/17 19:01:21 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/executor/nodeAppend.c,v 1.25 1999/09/18 19:06:48 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -257,7 +257,7 @@ ExecInitAppend(Append *node, EState *estate, Plan *parent)
reloid = rtentry->relid;
rri = makeNode(RelationInfo);
rri->ri_RangeTableIndex = es_rri->ri_RangeTableIndex;
- rri->ri_RelationDesc = heap_open(reloid);
+ rri->ri_RelationDesc = heap_open(reloid, RowExclusiveLock);
rri->ri_NumIndices = 0;
rri->ri_IndexRelationDescs = NULL; /* index descs */
rri->ri_IndexRelationInfo = NULL; /* index key info */
@@ -484,7 +484,7 @@ ExecEndAppend(Append *node)
resultRelationInfo = (RelationInfo *) lfirst(resultRelationInfoList);
resultRelationDesc = resultRelationInfo->ri_RelationDesc;
- heap_close(resultRelationDesc);
+ heap_close(resultRelationDesc, NoLock);
pfree(resultRelationInfo);
resultRelationInfoList = lnext(resultRelationInfoList);
}