2525#include "access/table.h"
2626#include "storage/lmgr.h"
2727
28+ static inline void validate_relation_kind (Relation r );
2829
2930/* ----------------
3031 * table_open - open a table relation by relation OID
@@ -42,17 +43,7 @@ table_open(Oid relationId, LOCKMODE lockmode)
4243
4344 r = relation_open (relationId , lockmode );
4445
45- if (r -> rd_rel -> relkind == RELKIND_INDEX ||
46- r -> rd_rel -> relkind == RELKIND_PARTITIONED_INDEX )
47- ereport (ERROR ,
48- (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
49- errmsg ("\"%s\" is an index" ,
50- RelationGetRelationName (r ))));
51- else if (r -> rd_rel -> relkind == RELKIND_COMPOSITE_TYPE )
52- ereport (ERROR ,
53- (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
54- errmsg ("\"%s\" is a composite type" ,
55- RelationGetRelationName (r ))));
46+ validate_relation_kind (r );
5647
5748 return r ;
5849}
@@ -76,17 +67,7 @@ try_table_open(Oid relationId, LOCKMODE lockmode)
7667 if (!r )
7768 return NULL ;
7869
79- if (r -> rd_rel -> relkind == RELKIND_INDEX ||
80- r -> rd_rel -> relkind == RELKIND_PARTITIONED_INDEX )
81- ereport (ERROR ,
82- (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
83- errmsg ("\"%s\" is an index" ,
84- RelationGetRelationName (r ))));
85- else if (r -> rd_rel -> relkind == RELKIND_COMPOSITE_TYPE )
86- ereport (ERROR ,
87- (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
88- errmsg ("\"%s\" is a composite type" ,
89- RelationGetRelationName (r ))));
70+ validate_relation_kind (r );
9071
9172 return r ;
9273}
@@ -105,17 +86,7 @@ table_openrv(const RangeVar *relation, LOCKMODE lockmode)
10586
10687 r = relation_openrv (relation , lockmode );
10788
108- if (r -> rd_rel -> relkind == RELKIND_INDEX ||
109- r -> rd_rel -> relkind == RELKIND_PARTITIONED_INDEX )
110- ereport (ERROR ,
111- (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
112- errmsg ("\"%s\" is an index" ,
113- RelationGetRelationName (r ))));
114- else if (r -> rd_rel -> relkind == RELKIND_COMPOSITE_TYPE )
115- ereport (ERROR ,
116- (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
117- errmsg ("\"%s\" is a composite type" ,
118- RelationGetRelationName (r ))));
89+ validate_relation_kind (r );
11990
12091 return r ;
12192}
@@ -137,19 +108,7 @@ table_openrv_extended(const RangeVar *relation, LOCKMODE lockmode,
137108 r = relation_openrv_extended (relation , lockmode , missing_ok );
138109
139110 if (r )
140- {
141- if (r -> rd_rel -> relkind == RELKIND_INDEX ||
142- r -> rd_rel -> relkind == RELKIND_PARTITIONED_INDEX )
143- ereport (ERROR ,
144- (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
145- errmsg ("\"%s\" is an index" ,
146- RelationGetRelationName (r ))));
147- else if (r -> rd_rel -> relkind == RELKIND_COMPOSITE_TYPE )
148- ereport (ERROR ,
149- (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
150- errmsg ("\"%s\" is a composite type" ,
151- RelationGetRelationName (r ))));
152- }
111+ validate_relation_kind (r );
153112
154113 return r ;
155114}
@@ -168,3 +127,22 @@ table_close(Relation relation, LOCKMODE lockmode)
168127{
169128 relation_close (relation , lockmode );
170129}
130+
131+ /* ----------------
132+ * validate_relation_kind - check the relation's kind
133+ *
134+ * Make sure relkind is not index or composite type
135+ * ----------------
136+ */
137+ static inline void
138+ validate_relation_kind (Relation r )
139+ {
140+ if (r -> rd_rel -> relkind == RELKIND_INDEX ||
141+ r -> rd_rel -> relkind == RELKIND_PARTITIONED_INDEX ||
142+ r -> rd_rel -> relkind == RELKIND_COMPOSITE_TYPE )
143+ ereport (ERROR ,
144+ (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
145+ errmsg ("cannot open relation \"%s\"" ,
146+ RelationGetRelationName (r )),
147+ errdetail_relkind_not_supported (r -> rd_rel -> relkind )));
148+ }
0 commit comments