Hello
I discovered that there is a lot of code duplication in heapam.c.
In particular relation_openrv and relation_openrv_extended procedures
and also heap_openrv and heap_openrv_extended procedures are almost the
same. Here is a patch that fixes this.
--
Best regards,
Aleksander Alekseev
http://eax.me/
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 34ba385..e9db6ad 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -1188,13 +1188,17 @@ try_relation_open(Oid relationId, LOCKMODE lockmode)
}
/* ----------------
- * relation_openrv - open any relation specified by a RangeVar
+ * relation_openrv_extended - open any relation specified by a RangeVar
*
- * Same as relation_open, but the relation is specified by a RangeVar.
+ * Same as relation_openrv, but with an additional missing_ok argument
+ * allowing a NULL return rather than an error if the relation is not
+ * found. (Note that some other causes, such as permissions problems,
+ * will still result in an ereport.)
* ----------------
*/
Relation
-relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
+relation_openrv_extended(const RangeVar *relation, LOCKMODE lockmode,
+ bool missing_ok)
{
Oid relOid;
@@ -1213,43 +1217,26 @@ relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
AcceptInvalidationMessages();
/* Look up and lock the appropriate relation using namespace search */
- relOid = RangeVarGetRelid(relation, lockmode, false);
+ relOid = RangeVarGetRelid(relation, lockmode, missing_ok);
+
+ /* Return NULL on not-found */
+ if (!OidIsValid(relOid))
+ return NULL;
/* Let relation_open do the rest */
return relation_open(relOid, NoLock);
}
/* ----------------
- * relation_openrv_extended - open any relation specified by a RangeVar
+ * relation_openrv - open any relation specified by a RangeVar
*
- * Same as relation_openrv, but with an additional missing_ok argument
- * allowing a NULL return rather than an error if the relation is not
- * found. (Note that some other causes, such as permissions problems,
- * will still result in an ereport.)
+ * Same as relation_open, but the relation is specified by a RangeVar.
* ----------------
*/
Relation
-relation_openrv_extended(const RangeVar *relation, LOCKMODE lockmode,
- bool missing_ok)
+relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
{
- Oid relOid;
-
- /*
- * Check for shared-cache-inval messages before trying to open the
- * relation. See comments in relation_openrv().
- */
- if (lockmode != NoLock)
- AcceptInvalidationMessages();
-
- /* Look up and lock the appropriate relation using namespace search */
- relOid = RangeVarGetRelid(relation, lockmode, missing_ok);
-
- /* Return NULL on not-found */
- if (!OidIsValid(relOid))
- return NULL;
-
- /* Let relation_open do the rest */
- return relation_open(relOid, NoLock);
+ return relation_openrv_extended(relation, lockmode, false);
}
/* ----------------
@@ -1275,6 +1262,24 @@ relation_close(Relation relation, LOCKMODE lockmode)
UnlockRelationId(&relid, lockmode);
}
+/*
+ * Check Relation after opening. Internal procedure used by heap_open and
+ * heap_openrv_* procedures.
+ */
+static void
+heap_open_check_relation(Relation r)
+{
+ if (r->rd_rel->relkind == RELKIND_INDEX)
+ ereport(ERROR,
+ (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ errmsg("\"%s\" is an index",
+ RelationGetRelationName(r))));
+ else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
+ ereport(ERROR,
+ (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+ errmsg("\"%s\" is a composite type",
+ RelationGetRelationName(r))));
+}
/* ----------------
* heap_open - open a heap relation by relation OID
@@ -1291,17 +1296,7 @@ heap_open(Oid relationId, LOCKMODE lockmode)
Relation r;
r = relation_open(relationId, lockmode);
-
- if (r->rd_rel->relkind == RELKIND_INDEX)
- ereport(ERROR,
- (errcode(ERRCODE_WRONG_OBJECT_TYPE),
- errmsg("\"%s\" is an index",
- RelationGetRelationName(r))));
- else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
- ereport(ERROR,
- (errcode(ERRCODE_WRONG_OBJECT_TYPE),
- errmsg("\"%s\" is a composite type",
- RelationGetRelationName(r))));
+ heap_open_check_relation(r);
return r;
}
@@ -1316,22 +1311,7 @@ heap_open(Oid relationId, LOCKMODE lockmode)
Relation
heap_openrv(const RangeVar *relation, LOCKMODE lockmode)
{
- Relation r;
-
- r = relation_openrv(relation, lockmode);
-
- if (r->rd_rel->relkind == RELKIND_INDEX)
- ereport(ERROR,
- (errcode(ERRCODE_WRONG_OBJECT_TYPE),
- errmsg("\"%s\" is an index",
- RelationGetRelationName(r))));
- else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
- ereport(ERROR,
- (errcode(ERRCODE_WRONG_OBJECT_TYPE),
- errmsg("\"%s\" is a composite type",
- RelationGetRelationName(r))));
-
- return r;
+ return heap_openrv_extended(relation, lockmode, false);
}
/* ----------------
@@ -1351,18 +1331,7 @@ heap_openrv_extended(const RangeVar *relation, LOCKMODE lockmode,
r = relation_openrv_extended(relation, lockmode, missing_ok);
if (r)
- {
- if (r->rd_rel->relkind == RELKIND_INDEX)
- ereport(ERROR,
- (errcode(ERRCODE_WRONG_OBJECT_TYPE),
- errmsg("\"%s\" is an index",
- RelationGetRelationName(r))));
- else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
- ereport(ERROR,
- (errcode(ERRCODE_WRONG_OBJECT_TYPE),
- errmsg("\"%s\" is a composite type",
- RelationGetRelationName(r))));
- }
+ heap_open_check_relation(r);
return r;
}
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers