25
25
#include "access/table.h"
26
26
#include "storage/lmgr.h"
27
27
28
+ static inline void validate_relation_kind (Relation r );
28
29
29
30
/* ----------------
30
31
* table_open - open a table relation by relation OID
@@ -42,17 +43,7 @@ table_open(Oid relationId, LOCKMODE lockmode)
42
43
43
44
r = relation_open (relationId , lockmode );
44
45
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 );
56
47
57
48
return r ;
58
49
}
@@ -76,17 +67,7 @@ try_table_open(Oid relationId, LOCKMODE lockmode)
76
67
if (!r )
77
68
return NULL ;
78
69
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 );
90
71
91
72
return r ;
92
73
}
@@ -105,17 +86,7 @@ table_openrv(const RangeVar *relation, LOCKMODE lockmode)
105
86
106
87
r = relation_openrv (relation , lockmode );
107
88
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 );
119
90
120
91
return r ;
121
92
}
@@ -137,19 +108,7 @@ table_openrv_extended(const RangeVar *relation, LOCKMODE lockmode,
137
108
r = relation_openrv_extended (relation , lockmode , missing_ok );
138
109
139
110
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 );
153
112
154
113
return r ;
155
114
}
@@ -168,3 +127,22 @@ table_close(Relation relation, LOCKMODE lockmode)
168
127
{
169
128
relation_close (relation , lockmode );
170
129
}
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