aboutsummaryrefslogtreecommitdiff
path: root/src/backend/access/heap/heapam.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2000-08-03 19:19:38 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2000-08-03 19:19:38 +0000
commit61aca818c486dbe000ce94c77cb1dd1f379baf67 (patch)
tree6d266f3253db8cb2ea9d59bb63dff89f8f7a1c56 /src/backend/access/heap/heapam.c
parentc298d74d4957845bb03a67092c30b53e5e0d01c2 (diff)
downloadpostgresql-61aca818c486dbe000ce94c77cb1dd1f379baf67.tar.gz
postgresql-61aca818c486dbe000ce94c77cb1dd1f379baf67.zip
Modify heap_open()/heap_openr() API per pghackers discussion of 11 July.
These two routines will now ALWAYS elog() on failure, whether you ask for a lock or not. If you really want to get a NULL return on failure, call the new routines heap_open_nofail()/heap_openr_nofail(). By my count there are only about three places that actually want that behavior. There were rather more than three places that were missing the check they needed to make under the old convention :-(.
Diffstat (limited to 'src/backend/access/heap/heapam.c')
-rw-r--r--src/backend/access/heap/heapam.c106
1 files changed, 80 insertions, 26 deletions
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 379d0ecc552..1e22c030462 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -8,13 +8,14 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.82 2000/07/22 11:18:46 wieck Exp $
+ * $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.83 2000/08/03 19:18:54 tgl Exp $
*
*
* INTERFACE ROUTINES
* heapgettup - fetch next heap tuple from a scan
* heap_open - open a heap relation by relationId
* heap_openr - open a heap relation by name
+ * heap_open[r]_nofail - same, but return NULL on failure instead of elog
* heap_close - close a heap relation
* heap_beginscan - begin relation scan
* heap_rescan - restart a relation scan
@@ -560,19 +561,17 @@ fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
#endif /* defined(DISABLE_COMPLEX_MACRO)*/
-
/* ----------------------------------------------------------------
* heap access method interface
* ----------------------------------------------------------------
*/
+
/* ----------------
* heap_open - open a heap relation by relationId
*
- * If lockmode is "NoLock", no lock is obtained on the relation,
- * and the caller must check for a NULL return value indicating
- * that no such relation exists.
- * Otherwise, an error is raised if the relation does not exist,
- * and the specified kind of lock is obtained on the relation.
+ * If lockmode is not "NoLock", the specified kind of lock is
+ * obtained on the relation.
+ * An error is raised if the relation does not exist.
* ----------------
*/
Relation
@@ -592,17 +591,15 @@ heap_open(Oid relationId, LOCKMODE lockmode)
/* The relcache does all the real work... */
r = RelationIdGetRelation(relationId);
- /* Under no circumstances will we return an index as a relation. */
- if (RelationIsValid(r) && r->rd_rel->relkind == RELKIND_INDEX)
- elog(ERROR, "%s is an index relation", RelationGetRelationName(r));
-
- if (lockmode == NoLock)
- return r; /* caller must check RelationIsValid! */
-
if (!RelationIsValid(r))
elog(ERROR, "Relation %u does not exist", relationId);
- LockRelation(r, lockmode);
+ /* Under no circumstances will we return an index as a relation. */
+ if (r->rd_rel->relkind == RELKIND_INDEX)
+ elog(ERROR, "%s is an index relation", RelationGetRelationName(r));
+
+ if (lockmode != NoLock)
+ LockRelation(r, lockmode);
return r;
}
@@ -610,11 +607,9 @@ heap_open(Oid relationId, LOCKMODE lockmode)
/* ----------------
* heap_openr - open a heap relation by name
*
- * If lockmode is "NoLock", no lock is obtained on the relation,
- * and the caller must check for a NULL return value indicating
- * that no such relation exists.
- * Otherwise, an error is raised if the relation does not exist,
- * and the specified kind of lock is obtained on the relation.
+ * If lockmode is not "NoLock", the specified kind of lock is
+ * obtained on the relation.
+ * An error is raised if the relation does not exist.
* ----------------
*/
Relation
@@ -624,7 +619,6 @@ heap_openr(const char *relationName, LOCKMODE lockmode)
Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
-
/* ----------------
* increment access statistics
* ----------------
@@ -635,17 +629,77 @@ heap_openr(const char *relationName, LOCKMODE lockmode)
/* The relcache does all the real work... */
r = RelationNameGetRelation(relationName);
+ if (!RelationIsValid(r))
+ elog(ERROR, "Relation '%s' does not exist", relationName);
+
+ /* Under no circumstances will we return an index as a relation. */
+ if (r->rd_rel->relkind == RELKIND_INDEX)
+ elog(ERROR, "%s is an index relation", RelationGetRelationName(r));
+
+ if (lockmode != NoLock)
+ LockRelation(r, lockmode);
+
+ return r;
+}
+
+/* ----------------
+ * heap_open_nofail - open a heap relation by relationId,
+ * do not raise error on failure
+ *
+ * The caller must check for a NULL return value indicating
+ * that no such relation exists.
+ * No lock is obtained on the relation, either.
+ * ----------------
+ */
+Relation
+heap_open_nofail(Oid relationId)
+{
+ Relation r;
+
+ /* ----------------
+ * increment access statistics
+ * ----------------
+ */
+ IncrHeapAccessStat(local_open);
+ IncrHeapAccessStat(global_open);
+
+ /* The relcache does all the real work... */
+ r = RelationIdGetRelation(relationId);
+
/* Under no circumstances will we return an index as a relation. */
if (RelationIsValid(r) && r->rd_rel->relkind == RELKIND_INDEX)
elog(ERROR, "%s is an index relation", RelationGetRelationName(r));
- if (lockmode == NoLock)
- return r; /* caller must check RelationIsValid! */
+ return r;
+}
- if (!RelationIsValid(r))
- elog(ERROR, "Relation '%s' does not exist", relationName);
+/* ----------------
+ * heap_openr_nofail - open a heap relation by name,
+ * do not raise error on failure
+ *
+ * The caller must check for a NULL return value indicating
+ * that no such relation exists.
+ * No lock is obtained on the relation, either.
+ * ----------------
+ */
+Relation
+heap_openr_nofail(const char *relationName)
+{
+ Relation r;
- LockRelation(r, lockmode);
+ /* ----------------
+ * increment access statistics
+ * ----------------
+ */
+ IncrHeapAccessStat(local_openr);
+ IncrHeapAccessStat(global_openr);
+
+ /* The relcache does all the real work... */
+ r = RelationNameGetRelation(relationName);
+
+ /* Under no circumstances will we return an index as a relation. */
+ if (RelationIsValid(r) && r->rd_rel->relkind == RELKIND_INDEX)
+ elog(ERROR, "%s is an index relation", RelationGetRelationName(r));
return r;
}