Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * table.c
4 : * Generic routines for table related code.
5 : *
6 : * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : *
10 : * IDENTIFICATION
11 : * src/backend/access/table/table.c
12 : *
13 : *
14 : * NOTES
15 : * This file contains table_ routines that implement access to tables (in
16 : * contrast to other relation types like indexes) that are independent of
17 : * individual table access methods.
18 : *
19 : *-------------------------------------------------------------------------
20 : */
21 :
22 : #include "postgres.h"
23 :
24 : #include "access/relation.h"
25 : #include "access/table.h"
26 : #include "storage/lmgr.h"
27 :
28 : static inline void validate_relation_kind(Relation r);
29 :
30 : /* ----------------
31 : * table_open - open a table relation by relation OID
32 : *
33 : * This is essentially relation_open plus check that the relation
34 : * is not an index nor a composite type. (The caller should also
35 : * check that it's not a view or foreign table before assuming it has
36 : * storage.)
37 : * ----------------
38 : */
39 : Relation
1539 andres 40 GIC 14023646 : table_open(Oid relationId, LOCKMODE lockmode)
1539 andres 41 ECB : {
42 : Relation r;
43 :
1539 andres 44 GIC 14023646 : r = relation_open(relationId, lockmode);
1539 andres 45 ECB :
257 akapila 46 GNC 14023640 : validate_relation_kind(r);
47 :
949 michael 48 GIC 14023640 : return r;
49 : }
50 :
949 michael 51 ECB :
52 : /* ----------------
53 : * try_table_open - open a table relation by relation OID
54 : *
55 : * Same as table_open, except return NULL instead of failing
56 : * if the relation does not exist.
57 : * ----------------
58 : */
949 michael 59 EUB : Relation
949 michael 60 GIC 148356 : try_table_open(Oid relationId, LOCKMODE lockmode)
949 michael 61 ECB : {
62 : Relation r;
63 :
949 michael 64 GIC 148356 : r = try_relation_open(relationId, lockmode);
65 :
66 : /* leave if table does not exist */
67 148356 : if (!r)
949 michael 68 UIC 0 : return NULL;
69 :
257 akapila 70 GNC 148356 : validate_relation_kind(r);
71 :
1539 andres 72 CBC 148356 : return r;
73 : }
74 :
75 : /* ----------------
76 : * table_openrv - open a table relation specified
77 : * by a RangeVar node
78 : *
79 : * As above, but relation is specified by a RangeVar.
80 : * ----------------
81 : */
82 : Relation
1539 andres 83 GIC 39267 : table_openrv(const RangeVar *relation, LOCKMODE lockmode)
1539 andres 84 ECB : {
85 : Relation r;
86 :
1539 andres 87 GIC 39267 : r = relation_openrv(relation, lockmode);
88 :
257 akapila 89 GNC 39257 : validate_relation_kind(r);
90 :
1539 andres 91 GIC 39254 : return r;
92 : }
93 :
94 : /* ----------------
95 : * table_openrv_extended - open a table relation specified
96 : * by a RangeVar node
1539 andres 97 ECB : *
98 : * As above, but optionally return NULL instead of failing for
99 : * relation-not-found.
100 : * ----------------
101 : */
102 : Relation
1539 andres 103 GIC 325997 : table_openrv_extended(const RangeVar *relation, LOCKMODE lockmode,
104 : bool missing_ok)
105 : {
106 : Relation r;
107 :
108 325997 : r = relation_openrv_extended(relation, lockmode, missing_ok);
1539 andres 109 ECB :
1539 andres 110 GIC 325930 : if (r)
257 akapila 111 GNC 325826 : validate_relation_kind(r);
112 :
1539 andres 113 GIC 325930 : return r;
114 : }
115 :
116 : /* ----------------
117 : * table_close - close a table
118 : *
119 : * If lockmode is not "NoLock", we then release the specified lock.
120 : *
121 : * Note that it is often sensible to hold a lock beyond relation_close;
122 : * in that case, the lock is released automatically at xact end.
123 : * ----------------
124 : */
125 : void
126 14666668 : table_close(Relation relation, LOCKMODE lockmode)
127 : {
128 14666668 : relation_close(relation, lockmode);
129 14666668 : }
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
257 akapila 138 GNC 14537079 : validate_relation_kind(Relation r)
139 : {
140 14537079 : if (r->rd_rel->relkind == RELKIND_INDEX ||
141 14537076 : r->rd_rel->relkind == RELKIND_PARTITIONED_INDEX ||
142 14537076 : r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
143 3 : 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 14537076 : }
|