PostgreSQL Source Code  git master
relation.h File Reference
#include "nodes/primnodes.h"
#include "utils/relcache.h"
#include "storage/lockdefs.h"
Include dependency graph for relation.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

Relation relation_open (Oid relationId, LOCKMODE lockmode)
 
Relation try_relation_open (Oid relationId, LOCKMODE lockmode)
 
Relation relation_openrv (const RangeVar *relation, LOCKMODE lockmode)
 
Relation relation_openrv_extended (const RangeVar *relation, LOCKMODE lockmode, bool missing_ok)
 
void relation_close (Relation relation, LOCKMODE lockmode)
 

Function Documentation

◆ relation_close()

void relation_close ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 206 of file relation.c.

References Assert, LockInfoData::lockRelId, MAX_LOCKMODES, NoLock, RelationData::rd_lockInfo, RelationClose(), and UnlockRelationId().

Referenced by AlterExtensionNamespace(), AlterObjectNamespace_oid(), AlterPolicy(), AlterSeqNamespaces(), AlterSequence(), AlterTableNamespace(), analyze_rel(), ATController(), ATExecAlterColumnType(), ATExecAttachPartitionIdx(), ATExecChangeOwner(), ATExecSetTableSpace(), ATPostAlterTypeParse(), ATPrepAlterColumnType(), ATPrepChangePersistence(), ATRewriteCatalogs(), ATSimpleRecursion(), ATTypedTableRecursion(), autoprewarm_database_main(), brin_desummarize_range(), brin_summarize_range(), bt_metap(), bt_page_items(), bt_page_stats(), BuildEventTriggerCache(), calculate_indexes_size(), calculate_toast_table_size(), change_owner_recurse_to_sequences(), check_of_type(), CheckAttributeType(), cluster_rel(), collect_corrupt_items(), collect_visibility_data(), CommentObject(), CreatePolicy(), CreateStatistics(), currval_oid(), dblink_build_sql_delete(), dblink_build_sql_insert(), dblink_build_sql_update(), dblink_get_pkey(), DefineRelation(), DefineVirtualRelation(), do_setval(), ExecAlterExtensionContentsStmt(), ExecSecLabelStmt(), ExecuteTruncateGuts(), expandRelation(), find_composite_type_dependencies(), finish_heap_swap(), generate_partition_qual(), get_object_address_attrdef(), get_object_address_attribute(), get_object_address_publication_rel(), get_partition_qual_relid(), get_raw_page_internal(), get_rels_with_domain(), get_tables_to_cluster(), heap_drop_with_catalog(), index_concurrently_swap(), IndexSetParentIndex(), lastval(), load_typcache_tupdesc(), nextval_internal(), pg_freespace(), pg_get_object_address(), pg_indexes_size(), pg_prewarm(), pg_relation_size(), pg_relpages(), pg_relpages_v1_5(), pg_relpagesbyid(), pg_relpagesbyid_v1_5(), pg_sequence_last_value(), pg_table_size(), pg_total_relation_size(), pg_truncate_visibility_map(), pg_visibility(), pg_visibility_map(), pg_visibility_map_summary(), pgstat_heap(), pgstat_index(), pgstatginindex_internal(), pgstatindex_impl(), pgstattuple_approx_internal(), process_owned_by(), relation_is_updatable(), RelationNameGetTupleDesc(), RemoveAttrDefaultById(), RemoveAttributeById(), RemoveRoleFromObjectPolicy(), rename_constraint_internal(), rename_policy(), renameatt_internal(), RenameRelationInternal(), RenameRewriteRule(), renametrig(), ResetSequence(), satisfies_hash_partition(), set_relation_column_names(), table_close(), transformAlterTableStmt(), transformIndexConstraint(), tuple_data_split_internal(), UpdateRangeTableOfViewParse(), vacuum_rel(), and validatePartitionedIndex().

207 {
208  LockRelId relid = relation->rd_lockInfo.lockRelId;
209 
210  Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
211 
212  /* The relcache does the real work... */
213  RelationClose(relation);
214 
215  if (lockmode != NoLock)
216  UnlockRelationId(&relid, lockmode);
217 }
LockRelId lockRelId
Definition: rel.h:44
void UnlockRelationId(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:184
Definition: rel.h:36
#define NoLock
Definition: lockdefs.h:34
LockInfoData rd_lockInfo
Definition: rel.h:87
void RelationClose(Relation relation)
Definition: relcache.c:2090
#define MAX_LOCKMODES
Definition: lock.h:85
#define Assert(condition)
Definition: c.h:732

◆ relation_open()

Relation relation_open ( Oid  relationId,
LOCKMODE  lockmode 
)

Definition at line 48 of file relation.c.

References AccessShareLock, Assert, CheckRelationLockedByMe(), elog, ERROR, IsBootstrapProcessingMode, LockRelationOid(), MAX_LOCKMODES, MyXactFlags, NoLock, pgstat_initstats(), RelationIdGetRelation(), RelationIsValid, RelationUsesLocalBuffers, and XACT_FLAGS_ACCESSEDTEMPNAMESPACE.

Referenced by AlterObjectNamespace_oid(), AlterPolicy(), AlterSeqNamespaces(), AlterTable(), AlterTableInternal(), AlterTableNamespace(), ATExecAttachPartitionIdx(), ATExecChangeOwner(), ATExecSetTableSpace(), ATPostAlterTypeParse(), ATPrepAlterColumnType(), ATPrepChangePersistence(), ATRewriteCatalogs(), ATSimpleRecursion(), ATTypedTableRecursion(), BuildEventTriggerCache(), calculate_indexes_size(), calculate_toast_table_size(), change_owner_recurse_to_sequences(), check_of_type(), CheckAttributeType(), collect_corrupt_items(), collect_visibility_data(), CreatePolicy(), DefineRelation(), DefineVirtualRelation(), EnableDisableTrigger(), ExecuteTruncateGuts(), expandRelation(), find_composite_type_dependencies(), generate_partition_qual(), get_object_address_attrdef(), get_partition_qual_relid(), get_rels_with_domain(), heap_drop_with_catalog(), index_concurrently_swap(), index_open(), IndexSetParentIndex(), load_typcache_tupdesc(), lock_and_open_sequence(), pg_freespace(), pg_prewarm(), pg_relpagesbyid(), pg_relpagesbyid_v1_5(), pg_truncate_visibility_map(), pg_visibility(), pg_visibility_map(), pg_visibility_map_summary(), pgstatginindex_internal(), pgstatindexbyid(), pgstatindexbyid_v1_5(), pgstattuple_approx_internal(), pgstattuplebyid(), pgstattuplebyid_v1_5(), relation_openrv(), relation_openrv_extended(), RemoveAttrDefaultById(), RemoveAttributeById(), RemoveRoleFromObjectPolicy(), rename_constraint_internal(), rename_policy(), renameatt_internal(), RenameRelationInternal(), RenameRewriteRule(), renametrig(), set_relation_column_names(), table_open(), transformAlterTableStmt(), transformIndexStmt(), tuple_data_split_internal(), UpdateRangeTableOfViewParse(), and validatePartitionedIndex().

49 {
50  Relation r;
51 
52  Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
53 
54  /* Get the lock before trying to open the relcache entry */
55  if (lockmode != NoLock)
56  LockRelationOid(relationId, lockmode);
57 
58  /* The relcache does all the real work... */
59  r = RelationIdGetRelation(relationId);
60 
61  if (!RelationIsValid(r))
62  elog(ERROR, "could not open relation with OID %u", relationId);
63 
64  /*
65  * If we didn't get the lock ourselves, assert that caller holds one,
66  * except in bootstrap mode where no locks are used.
67  */
68  Assert(lockmode != NoLock ||
71 
72  /* Make note that we've accessed a temporary relation */
75 
77 
78  return r;
79 }
#define AccessShareLock
Definition: lockdefs.h:36
#define ERROR
Definition: elog.h:43
#define RelationIsValid(relation)
Definition: rel.h:387
void pgstat_initstats(Relation rel)
Definition: pgstat.c:1751
#define NoLock
Definition: lockdefs.h:34
int MyXactFlags
Definition: xact.c:119
#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE
Definition: xact.h:97
#define MAX_LOCKMODES
Definition: lock.h:85
#define Assert(condition)
Definition: c.h:732
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:302
#define RelationUsesLocalBuffers(relation)
Definition: rel.h:523
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:374
#define elog(elevel,...)
Definition: elog.h:226
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108
Relation RelationIdGetRelation(Oid relationId)
Definition: relcache.c:1991

◆ relation_openrv()

Relation relation_openrv ( const RangeVar relation,
LOCKMODE  lockmode 
)

Definition at line 138 of file relation.c.

References AcceptInvalidationMessages(), NoLock, RangeVarGetRelid, and relation_open().

Referenced by bt_metap(), bt_page_items(), bt_page_stats(), CreateStatistics(), get_object_address_attrdef(), get_object_address_attribute(), get_raw_page_internal(), pg_relpages(), pg_relpages_v1_5(), pgrowlocks(), pgstatindex(), pgstatindex_v1_5(), pgstattuple(), pgstattuple_v1_5(), process_owned_by(), RelationNameGetTupleDesc(), table_openrv(), and transformTableLikeClause().

139 {
140  Oid relOid;
141 
142  /*
143  * Check for shared-cache-inval messages before trying to open the
144  * relation. This is needed even if we already hold a lock on the
145  * relation, because GRANT/REVOKE are executed without taking any lock on
146  * the target relation, and we want to be sure we see current ACL
147  * information. We can skip this if asked for NoLock, on the assumption
148  * that such a call is not the first one in the current command, and so we
149  * should be reasonably up-to-date already. (XXX this all could stand to
150  * be redesigned, but for the moment we'll keep doing this like it's been
151  * done historically.)
152  */
153  if (lockmode != NoLock)
155 
156  /* Look up and lock the appropriate relation using namespace search */
157  relOid = RangeVarGetRelid(relation, lockmode, false);
158 
159  /* Let relation_open do the rest */
160  return relation_open(relOid, NoLock);
161 }
void AcceptInvalidationMessages(void)
Definition: inval.c:681
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:63
unsigned int Oid
Definition: postgres_ext.h:31
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
#define NoLock
Definition: lockdefs.h:34

◆ relation_openrv_extended()

Relation relation_openrv_extended ( const RangeVar relation,
LOCKMODE  lockmode,
bool  missing_ok 
)

Definition at line 173 of file relation.c.

References AcceptInvalidationMessages(), NoLock, OidIsValid, RangeVarGetRelid, and relation_open().

Referenced by get_object_address_publication_rel(), get_relation_by_qualified_name(), and table_openrv_extended().

175 {
176  Oid relOid;
177 
178  /*
179  * Check for shared-cache-inval messages before trying to open the
180  * relation. See comments in relation_openrv().
181  */
182  if (lockmode != NoLock)
184 
185  /* Look up and lock the appropriate relation using namespace search */
186  relOid = RangeVarGetRelid(relation, lockmode, missing_ok);
187 
188  /* Return NULL on not-found */
189  if (!OidIsValid(relOid))
190  return NULL;
191 
192  /* Let relation_open do the rest */
193  return relation_open(relOid, NoLock);
194 }
void AcceptInvalidationMessages(void)
Definition: inval.c:681
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:63
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
#define NoLock
Definition: lockdefs.h:34

◆ try_relation_open()

Relation try_relation_open ( Oid  relationId,
LOCKMODE  lockmode 
)

Definition at line 89 of file relation.c.

References AccessShareLock, Assert, CheckRelationLockedByMe(), elog, ERROR, LockRelationOid(), MAX_LOCKMODES, MyXactFlags, NoLock, ObjectIdGetDatum, pgstat_initstats(), RelationIdGetRelation(), RelationIsValid, RelationUsesLocalBuffers, RELOID, SearchSysCacheExists1, UnlockRelationOid(), and XACT_FLAGS_ACCESSEDTEMPNAMESPACE.

Referenced by autoprewarm_database_main(), cluster_rel(), pg_indexes_size(), pg_relation_size(), pg_table_size(), pg_total_relation_size(), relation_is_updatable(), satisfies_hash_partition(), and vacuum_open_relation().

90 {
91  Relation r;
92 
93  Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
94 
95  /* Get the lock first */
96  if (lockmode != NoLock)
97  LockRelationOid(relationId, lockmode);
98 
99  /*
100  * Now that we have the lock, probe to see if the relation really exists
101  * or not.
102  */
103  if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(relationId)))
104  {
105  /* Release useless lock */
106  if (lockmode != NoLock)
107  UnlockRelationOid(relationId, lockmode);
108 
109  return NULL;
110  }
111 
112  /* Should be safe to do a relcache load */
113  r = RelationIdGetRelation(relationId);
114 
115  if (!RelationIsValid(r))
116  elog(ERROR, "could not open relation with OID %u", relationId);
117 
118  /* If we didn't get the lock ourselves, assert that caller holds one */
119  Assert(lockmode != NoLock ||
121 
122  /* Make note that we've accessed a temporary relation */
125 
126  pgstat_initstats(r);
127 
128  return r;
129 }
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:199
#define AccessShareLock
Definition: lockdefs.h:36
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:183
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define RelationIsValid(relation)
Definition: rel.h:387
void pgstat_initstats(Relation rel)
Definition: pgstat.c:1751
#define NoLock
Definition: lockdefs.h:34
int MyXactFlags
Definition: xact.c:119
#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE
Definition: xact.h:97
#define MAX_LOCKMODES
Definition: lock.h:85
#define Assert(condition)
Definition: c.h:732
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:302
#define RelationUsesLocalBuffers(relation)
Definition: rel.h:523
#define elog(elevel,...)
Definition: elog.h:226
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108
Relation RelationIdGetRelation(Oid relationId)
Definition: relcache.c:1991