PostgreSQL Source Code  git master
relation.c File Reference
#include "postgres.h"
#include "access/relation.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "storage/lmgr.h"
#include "utils/inval.h"
#include "utils/syscache.h"
Include dependency graph for relation.c:

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.

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 }
Assert(fmt[strlen(fmt) - 1] !='\n')
void UnlockRelationId(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:213
#define MAX_LOCKMODES
Definition: lock.h:82
#define NoLock
Definition: lockdefs.h:34
void RelationClose(Relation relation)
Definition: relcache.c:2160
LockRelId lockRelId
Definition: rel.h:46
Definition: rel.h:39
LockInfoData rd_lockInfo
Definition: rel.h:114

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_multi_page_stats(), bt_page_items_internal(), bt_page_stats_internal(), 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(), gist_page_items(), heap_drop_with_catalog(), heap_force_common(), 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_impl(), 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(), rename_constraint_internal(), rename_policy(), renameatt_internal(), RenameRelationInternal(), RenameRewriteRule(), renametrig(), ResetSequence(), satisfies_hash_partition(), SequenceChangePersistence(), set_relation_column_names(), swap_relation_files(), table_close(), transformAlterTableStmt(), transformIndexConstraint(), tuple_data_split_internal(), vacuum_rel(), validatePartitionedIndex(), and verify_heapam().

◆ relation_open()

Relation relation_open ( Oid  relationId,
LOCKMODE  lockmode 
)

Definition at line 48 of file relation.c.

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 ERROR
Definition: elog.h:39
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:109
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:331
#define AccessShareLock
Definition: lockdefs.h:36
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:414
void pgstat_init_relation(Relation rel)
#define RelationUsesLocalBuffers(relation)
Definition: rel.h:638
#define RelationIsValid(relation)
Definition: rel.h:477
Relation RelationIdGetRelation(Oid relationId)
Definition: relcache.c:2054
int MyXactFlags
Definition: xact.c:136
#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE
Definition: xact.h:102

References AccessShareLock, Assert(), CheckRelationLockedByMe(), elog(), ERROR, IsBootstrapProcessingMode, LockRelationOid(), MAX_LOCKMODES, MyXactFlags, NoLock, pgstat_init_relation(), 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(), bt_multi_page_stats(), 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(), expandTableLikeClause(), find_composite_type_dependencies(), generate_partition_qual(), get_partition_qual_relid(), get_rels_with_domain(), heap_drop_with_catalog(), heap_force_common(), 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(), rename_constraint_internal(), rename_policy(), renameatt_internal(), RenameRelationInternal(), RenameRewriteRule(), renametrig(), satisfies_hash_partition(), set_relation_column_names(), swap_relation_files(), table_open(), transformAlterTableStmt(), transformIndexStmt(), transformStatsStmt(), tuple_data_split_internal(), validatePartitionedIndex(), and verify_heapam().

◆ relation_openrv()

Relation relation_openrv ( const RangeVar relation,
LOCKMODE  lockmode 
)

Definition at line 138 of file relation.c.

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:746
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:79
unsigned int Oid
Definition: postgres_ext.h:31
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48

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

Referenced by bt_metap(), bt_multi_page_stats(), bt_page_items_internal(), bt_page_stats_internal(), CreateStatistics(), expandTableLikeClause(), 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().

◆ relation_openrv_extended()

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

Definition at line 173 of file relation.c.

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 }
#define OidIsValid(objectId)
Definition: c.h:764

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

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

◆ try_relation_open()

Relation try_relation_open ( Oid  relationId,
LOCKMODE  lockmode 
)

Definition at line 89 of file relation.c.

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 
127 
128  return r;
129 }
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:228
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
@ RELOID
Definition: syscache.h:89
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:191

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

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