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 "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 205 of file relation.c.

206 {
207  LockRelId relid = relation->rd_lockInfo.lockRelId;
208 
209  Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
210 
211  /* The relcache does the real work... */
212  RelationClose(relation);
213 
214  if (lockmode != NoLock)
215  UnlockRelationId(&relid, lockmode);
216 }
#define Assert(condition)
Definition: c.h:858
void UnlockRelationId(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:212
#define MAX_LOCKMODES
Definition: lock.h:82
#define NoLock
Definition: lockdefs.h:34
void RelationClose(Relation relation)
Definition: relcache.c:2194
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(), AlterTableNamespace(), analyze_rel(), ATController(), ATExecAlterColumnType(), ATExecAttachPartitionIdx(), ATExecChangeOwner(), ATExecSetTableSpace(), ATPostAlterTypeParse(), ATPrepAlterColumnType(), ATPrepChangePersistence(), ATRewriteCatalogs(), ATSimpleRecursion(), ATTypedTableRecursion(), autoprewarm_database_main(), binary_upgrade_add_sub_rel_state(), 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(), checkPartition(), cluster_rel(), collect_corrupt_items(), collect_visibility_data(), CommentObject(), CreatePolicy(), CreateStatistics(), dblink_build_sql_delete(), dblink_build_sql_insert(), dblink_build_sql_update(), dblink_get_pkey(), DefineRelation(), DefineVirtualRelation(), 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(), load_typcache_tupdesc(), pg_freespace(), pg_get_expr_worker(), pg_get_object_address(), pg_indexes_size(), pg_prewarm(), pg_relation_size(), pg_relpages_impl(), 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(), satisfies_hash_partition(), sequence_close(), 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 47 of file relation.c.

48 {
49  Relation r;
50 
51  Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
52 
53  /* Get the lock before trying to open the relcache entry */
54  if (lockmode != NoLock)
55  LockRelationOid(relationId, lockmode);
56 
57  /* The relcache does all the real work... */
58  r = RelationIdGetRelation(relationId);
59 
60  if (!RelationIsValid(r))
61  elog(ERROR, "could not open relation with OID %u", relationId);
62 
63  /*
64  * If we didn't get the lock ourselves, assert that caller holds one,
65  * except in bootstrap mode where no locks are used.
66  */
67  Assert(lockmode != NoLock ||
70 
71  /* Make note that we've accessed a temporary relation */
74 
76 
77  return r;
78 }
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition: lmgr.c:330
#define AccessShareLock
Definition: lockdefs.h:36
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:454
void pgstat_init_relation(Relation rel)
#define RelationUsesLocalBuffers(relation)
Definition: rel.h:637
#define RelationIsValid(relation)
Definition: rel.h:478
Relation RelationIdGetRelation(Oid relationId)
Definition: relcache.c:2063
int MyXactFlags
Definition: xact.c:134
#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(), binary_upgrade_add_sub_rel_state(), bt_multi_page_stats(), BuildEventTriggerCache(), calculate_indexes_size(), calculate_toast_table_size(), change_owner_recurse_to_sequences(), check_of_type(), CheckAttributeType(), checkPartition(), 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(), hash_bitmap_info(), heap_drop_with_catalog(), heap_force_common(), index_concurrently_swap(), index_open(), IndexSetParentIndex(), load_typcache_tupdesc(), 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(), pgstathashindex(), 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(), sequence_open(), 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 137 of file relation.c.

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

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 172 of file relation.c.

174 {
175  Oid relOid;
176 
177  /*
178  * Check for shared-cache-inval messages before trying to open the
179  * relation. See comments in relation_openrv().
180  */
181  if (lockmode != NoLock)
183 
184  /* Look up and lock the appropriate relation using namespace search */
185  relOid = RangeVarGetRelid(relation, lockmode, missing_ok);
186 
187  /* Return NULL on not-found */
188  if (!OidIsValid(relOid))
189  return NULL;
190 
191  /* Let relation_open do the rest */
192  return relation_open(relOid, NoLock);
193 }
#define OidIsValid(objectId)
Definition: c.h:775

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 88 of file relation.c.

89 {
90  Relation r;
91 
92  Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
93 
94  /* Get the lock first */
95  if (lockmode != NoLock)
96  LockRelationOid(relationId, lockmode);
97 
98  /*
99  * Now that we have the lock, probe to see if the relation really exists
100  * or not.
101  */
102  if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(relationId)))
103  {
104  /* Release useless lock */
105  if (lockmode != NoLock)
106  UnlockRelationOid(relationId, lockmode);
107 
108  return NULL;
109  }
110 
111  /* Should be safe to do a relcache load */
112  r = RelationIdGetRelation(relationId);
113 
114  if (!RelationIsValid(r))
115  elog(ERROR, "could not open relation with OID %u", relationId);
116 
117  /* If we didn't get the lock ourselves, assert that caller holds one */
118  Assert(lockmode != NoLock ||
120 
121  /* Make note that we've accessed a temporary relation */
124 
126 
127  return r;
128 }
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:227
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:95

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

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