PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 "storage/lock.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}
#define Assert(condition)
Definition c.h:943
void UnlockRelationId(LockRelId *relid, LOCKMODE lockmode)
Definition lmgr.c:214
#define MAX_LOCKMODES
Definition lock.h:85
#define NoLock
Definition lockdefs.h:34
void RelationClose(Relation relation)
Definition relcache.c:2210
LockRelId lockRelId
Definition rel.h:46
LockInfoData rd_lockInfo
Definition rel.h:114

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

Referenced by AcquireRewriteLocks(), 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(), bt_metap(), bt_multi_page_stats(), bt_page_items_internal(), bt_page_stats_internal(), buffer_create_toy(), BuildEventTriggerCache(), calculate_indexes_size(), calculate_toast_table_size(), change_owner_recurse_to_sequences(), check_of_type(), CheckAttributeType(), cluster_rel(), cluster_rel_recheck(), 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(), dropconstraint_internal(), evict_rel(), ExecAlterExtensionContentsStmt(), ExecAlterOwnerStmt(), 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_repack(), grow_rel(), hash_bitmap_info(), heap_drop_with_catalog(), heap_force_common(), index_concurrently_swap(), IndexSetParentIndex(), invalidate_rel_block(), load_typcache_tupdesc(), modify_rel_block(), pg_buffercache_evict_relation(), pg_buffercache_mark_dirty_relation(), pg_freespace(), pg_get_expr_worker(), pg_get_object_address(), pg_get_sequence_data(), 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(), pgstathashindex(), pgstatindex_impl(), pgstattuple_approx_internal(), process_owned_by(), ProcessSingleRelationByOid(), read_buffers(), read_rel_block_ll(), read_stream_for_blocks(), 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(), statatt_get_type(), swap_relation_files(), table_close(), transformAlterTableStmt(), 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... */
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:40
#define elog(elevel,...)
Definition elog.h:228
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition lmgr.c:107
bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode, bool orstronger)
Definition lmgr.c:334
#define AccessShareLock
Definition lockdefs.h:36
#define IsBootstrapProcessingMode()
Definition miscadmin.h:495
void pgstat_init_relation(Relation rel)
static int fb(int x)
#define RelationUsesLocalBuffers(relation)
Definition rel.h:648
#define RelationIsValid(relation)
Definition rel.h:491
Relation RelationIdGetRelation(Oid relationId)
Definition relcache.c:2089
int MyXactFlags
Definition xact.c:138
#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE
Definition xact.h:103

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

Referenced by AcquireRewriteLocks(), 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(), buffer_create_toy(), 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(), dropconstraint_internal(), EnableDisableTrigger(), evict_rel(), ExecuteTruncateGuts(), expandRelation(), expandTableLikeClause(), find_composite_type_dependencies(), fireRIRrules(), generate_partition_qual(), get_partition_qual_relid(), get_rels_with_domain(), grow_rel(), hash_bitmap_info(), heap_drop_with_catalog(), heap_force_common(), index_concurrently_swap(), index_open(), IndexSetParentIndex(), invalidate_rel_block(), load_typcache_tupdesc(), modify_rel_block(), pg_buffercache_evict_relation(), pg_buffercache_mark_dirty_relation(), 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(), read_buffers(), read_rel_block_ll(), read_stream_for_blocks(), relation_openrv(), relation_openrv_extended(), RemoveAttrDefaultById(), RemoveAttributeById(), rename_constraint_internal(), rename_policy(), renameatt_internal(), RenameRelationInternal(), RenameRewriteRule(), renametrig(), RewriteQuery(), rewriteTargetView(), satisfies_hash_partition(), sequence_open(), set_relation_column_names(), statatt_get_type(), 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:930
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition namespace.h:98
unsigned int Oid
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(), parserOpenPropGraph(), 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:858

References AcceptInvalidationMessages(), fb(), 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 */
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 */
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:229
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
#define SearchSysCacheExists1(cacheId, key1)
Definition syscache.h:100

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

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