Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 857dd35

Browse files
author
Amit Kapila
committed
Eliminate duplicate code in table.c.
Additionally improve the error message similar to how it was done in 2ed532e. Author: Junwang Zhao, Aleksander Alekseev Reviewed-by: Amit Kapila, Alvaro Herrera, Kyotaro Horiguchi Discussion: https://postgr.es/m/CAEG8a3KbVtBm_BYf5tGsKHvmMieQVsq_jBPOg75VViQB7ACL8Q%40mail.gmail.com
1 parent 0a5f06b commit 857dd35

File tree

2 files changed

+26
-47
lines changed

2 files changed

+26
-47
lines changed

src/backend/access/table/table.c

+24-46
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
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+
}

src/test/regress/expected/tid.out

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ DROP SEQUENCE tid_seq;
6161
-- Index, fails with incorrect relation type
6262
CREATE INDEX tid_ind ON tid_tab(a);
6363
SELECT currtid2('tid_ind'::text, '(0,1)'::tid); -- fails
64-
ERROR: "tid_ind" is an index
64+
ERROR: cannot open relation "tid_ind"
65+
DETAIL: This operation is not supported for indexes.
6566
DROP INDEX tid_ind;
6667
-- Partitioned table, no storage
6768
CREATE TABLE tid_part (a int) PARTITION BY RANGE (a);

0 commit comments

Comments
 (0)