PostgreSQL Source Code  git master
lockcmds.c File Reference
#include "postgres.h"
#include "access/table.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "catalog/pg_inherits.h"
#include "commands/lockcmds.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "parser/parse_clause.h"
#include "rewrite/rewriteHandler.h"
#include "storage/lmgr.h"
#include "utils/acl.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
Include dependency graph for lockcmds.c:

Go to the source code of this file.

Data Structures

struct  LockViewRecurse_context
 

Functions

static void LockTableRecurse (Oid reloid, LOCKMODE lockmode, bool nowait)
 
static AclResult LockTableAclCheck (Oid relid, LOCKMODE lockmode, Oid userid)
 
static void RangeVarCallbackForLockTable (const RangeVar *rv, Oid relid, Oid oldrelid, void *arg)
 
static void LockViewRecurse (Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views)
 
void LockTableCommand (LockStmt *lockstmt)
 
static bool LockViewRecurse_walker (Node *node, LockViewRecurse_context *context)
 

Function Documentation

◆ LockTableAclCheck()

static AclResult LockTableAclCheck ( Oid  relid,
LOCKMODE  lockmode,
Oid  userid 
)
static

Definition at line 275 of file lockcmds.c.

References AccessShareLock, ACL_DELETE, ACL_INSERT, ACL_SELECT, ACL_TRUNCATE, ACL_UPDATE, aclmask(), pg_class_aclcheck(), and RowExclusiveLock.

Referenced by LockViewRecurse_walker(), and RangeVarCallbackForLockTable().

276 {
277  AclResult aclresult;
279 
280  /* Verify adequate privilege */
281  if (lockmode == AccessShareLock)
282  aclmask = ACL_SELECT;
283  else if (lockmode == RowExclusiveLock)
285  else
286  aclmask = ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE;
287 
288  aclresult = pg_class_aclcheck(reloid, userid, aclmask);
289 
290  return aclresult;
291 }
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1313
#define AccessShareLock
Definition: lockdefs.h:36
#define ACL_DELETE
Definition: parsenodes.h:77
uint32 AclMode
Definition: parsenodes.h:72
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ACL_UPDATE
Definition: parsenodes.h:76
AclResult
Definition: acl.h:177
#define ACL_SELECT
Definition: parsenodes.h:75
#define ACL_INSERT
Definition: parsenodes.h:74
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4563
#define ACL_TRUNCATE
Definition: parsenodes.h:78

◆ LockTableCommand()

void LockTableCommand ( LockStmt lockstmt)

Definition at line 41 of file lockcmds.c.

References get_rel_relkind(), RangeVar::inh, lfirst, LockTableRecurse(), LockViewRecurse(), LockStmt::mode, NIL, LockStmt::nowait, RangeVarCallbackForLockTable(), RangeVarGetRelidExtended(), LockStmt::relations, and RVR_NOWAIT.

Referenced by standard_ProcessUtility().

42 {
43  ListCell *p;
44 
45  /*
46  * Iterate over the list and process the named relations one at a time
47  */
48  foreach(p, lockstmt->relations)
49  {
50  RangeVar *rv = (RangeVar *) lfirst(p);
51  bool recurse = rv->inh;
52  Oid reloid;
53 
54  reloid = RangeVarGetRelidExtended(rv, lockstmt->mode,
55  lockstmt->nowait ? RVR_NOWAIT : 0,
57  (void *) &lockstmt->mode);
58 
59  if (get_rel_relkind(reloid) == RELKIND_VIEW)
60  LockViewRecurse(reloid, lockstmt->mode, lockstmt->nowait, NIL);
61  else if (recurse)
62  LockTableRecurse(reloid, lockstmt->mode, lockstmt->nowait);
63  }
64 }
#define NIL
Definition: pg_list.h:65
bool nowait
Definition: parsenodes.h:3332
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1915
static void LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views)
Definition: lockcmds.c:248
unsigned int Oid
Definition: postgres_ext.h:31
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:236
bool inh
Definition: primnodes.h:69
static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait)
Definition: lockcmds.c:116
List * relations
Definition: parsenodes.h:3330
#define lfirst(lc)
Definition: pg_list.h:190
static void RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid, void *arg)
Definition: lockcmds.c:71

◆ LockTableRecurse()

static void LockTableRecurse ( Oid  reloid,
LOCKMODE  lockmode,
bool  nowait 
)
static

Definition at line 116 of file lockcmds.c.

References ConditionalLockRelationOid(), ereport, errcode(), errmsg(), ERROR, find_all_inheritors(), get_rel_name(), lfirst_oid, LockRelationOid(), NoLock, ObjectIdGetDatum, relname, RELOID, SearchSysCacheExists1, and UnlockRelationOid().

Referenced by LockTableCommand(), and LockViewRecurse_walker().

117 {
118  List *children;
119  ListCell *lc;
120 
121  children = find_all_inheritors(reloid, NoLock, NULL);
122 
123  foreach(lc, children)
124  {
125  Oid childreloid = lfirst_oid(lc);
126 
127  /* Parent already locked. */
128  if (childreloid == reloid)
129  continue;
130 
131  if (!nowait)
132  LockRelationOid(childreloid, lockmode);
133  else if (!ConditionalLockRelationOid(childreloid, lockmode))
134  {
135  /* try to throw error by name; relation could be deleted... */
136  char *relname = get_rel_name(childreloid);
137 
138  if (!relname)
139  continue; /* child concurrently dropped, just skip it */
140  ereport(ERROR,
141  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
142  errmsg("could not obtain lock on relation \"%s\"",
143  relname)));
144  }
145 
146  /*
147  * Even if we got the lock, child might have been concurrently
148  * dropped. If so, we can skip it.
149  */
150  if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(childreloid)))
151  {
152  /* Release useless lock */
153  UnlockRelationOid(childreloid, lockmode);
154  continue;
155  }
156  }
157 }
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:151
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:199
int errcode(int sqlerrcode)
Definition: elog.c:610
NameData relname
Definition: pg_class.h:38
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:183
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
#define ereport(elevel,...)
Definition: elog.h:144
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:165
int errmsg(const char *fmt,...)
Definition: elog.c:824
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108
Definition: pg_list.h:50
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1840
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ LockViewRecurse()

static void LockViewRecurse ( Oid  reloid,
LOCKMODE  lockmode,
bool  nowait,
List ancestor_views 
)
static

Definition at line 248 of file lockcmds.c.

References LockViewRecurse_context::ancestor_views, get_view_query(), lappend_oid(), list_delete_last(), LockViewRecurse_context::lockmode, LockViewRecurse_walker(), NoLock, LockViewRecurse_context::nowait, RelationData::rd_rel, table_close(), table_open(), LockViewRecurse_context::viewoid, and LockViewRecurse_context::viewowner.

Referenced by LockTableCommand(), and LockViewRecurse_walker().

249 {
250  LockViewRecurse_context context;
251 
252  Relation view;
253  Query *viewquery;
254 
255  view = table_open(reloid, NoLock);
256  viewquery = get_view_query(view);
257 
258  context.lockmode = lockmode;
259  context.nowait = nowait;
260  context.viewowner = view->rd_rel->relowner;
261  context.viewoid = reloid;
262  context.ancestor_views = lappend_oid(ancestor_views, reloid);
263 
264  LockViewRecurse_walker((Node *) viewquery, &context);
265 
266  (void) list_delete_last(context.ancestor_views);
267 
268  table_close(view, NoLock);
269 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static bool LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
Definition: lockcmds.c:176
Definition: nodes.h:529
Form_pg_class rd_rel
Definition: rel.h:109
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
#define NoLock
Definition: lockdefs.h:34
List * list_delete_last(List *list)
Definition: list.c:877
Query * get_view_query(Relation view)
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ LockViewRecurse_walker()

static bool LockViewRecurse_walker ( Node node,
LockViewRecurse_context context 
)
static

Definition at line 176 of file lockcmds.c.

References aclcheck_error(), ACLCHECK_OK, Alias::aliasname, LockViewRecurse_context::ancestor_views, ConditionalLockRelationOid(), RangeTblEntry::eref, ereport, errcode(), errmsg(), ERROR, expression_tree_walker(), get_rel_name(), get_relkind_objtype(), RangeTblEntry::inh, IsA, lfirst, list_member_oid(), LockViewRecurse_context::lockmode, LockRelationOid(), LockTableAclCheck(), LockTableRecurse(), LockViewRecurse(), LockViewRecurse_context::nowait, QTW_IGNORE_JOINALIASES, query_tree_walker(), RangeTblEntry::relid, RangeTblEntry::relkind, relname, Query::rtable, LockViewRecurse_context::viewoid, and LockViewRecurse_context::viewowner.

Referenced by LockViewRecurse().

177 {
178  if (node == NULL)
179  return false;
180 
181  if (IsA(node, Query))
182  {
183  Query *query = (Query *) node;
184  ListCell *rtable;
185 
186  foreach(rtable, query->rtable)
187  {
188  RangeTblEntry *rte = lfirst(rtable);
189  AclResult aclresult;
190 
191  Oid relid = rte->relid;
192  char relkind = rte->relkind;
193  char *relname = get_rel_name(relid);
194 
195  /*
196  * The OLD and NEW placeholder entries in the view's rtable are
197  * skipped.
198  */
199  if (relid == context->viewoid &&
200  (strcmp(rte->eref->aliasname, "old") == 0 ||
201  strcmp(rte->eref->aliasname, "new") == 0))
202  continue;
203 
204  /* Currently, we only allow plain tables or views to be locked. */
205  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
206  relkind != RELKIND_VIEW)
207  continue;
208 
209  /* Check infinite recursion in the view definition. */
210  if (list_member_oid(context->ancestor_views, relid))
211  ereport(ERROR,
212  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
213  errmsg("infinite recursion detected in rules for relation \"%s\"",
214  get_rel_name(relid))));
215 
216  /* Check permissions with the view owner's privilege. */
217  aclresult = LockTableAclCheck(relid, context->lockmode, context->viewowner);
218  if (aclresult != ACLCHECK_OK)
219  aclcheck_error(aclresult, get_relkind_objtype(relkind), relname);
220 
221  /* We have enough rights to lock the relation; do so. */
222  if (!context->nowait)
223  LockRelationOid(relid, context->lockmode);
224  else if (!ConditionalLockRelationOid(relid, context->lockmode))
225  ereport(ERROR,
226  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
227  errmsg("could not obtain lock on relation \"%s\"",
228  relname)));
229 
230  if (relkind == RELKIND_VIEW)
231  LockViewRecurse(relid, context->lockmode, context->nowait, context->ancestor_views);
232  else if (rte->inh)
233  LockTableRecurse(relid, context->lockmode, context->nowait);
234  }
235 
236  return query_tree_walker(query,
238  context,
240  }
241 
242  return expression_tree_walker(node,
244  context);
245 }
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:151
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2273
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
static bool LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
Definition: lockcmds.c:176
int errcode(int sqlerrcode)
Definition: elog.c:610
static void LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views)
Definition: lockcmds.c:248
NameData relname
Definition: pg_class.h:38
unsigned int Oid
Definition: postgres_ext.h:31
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3294
static AclResult LockTableAclCheck(Oid relid, LOCKMODE lockmode, Oid userid)
Definition: lockcmds.c:275
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:43
static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait)
Definition: lockcmds.c:116
AclResult
Definition: acl.h:177
#define ereport(elevel,...)
Definition: elog.h:144
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:674
#define lfirst(lc)
Definition: pg_list.h:190
char * aliasname
Definition: primnodes.h:42
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1839
int errmsg(const char *fmt,...)
Definition: elog.c:824
ObjectType get_relkind_objtype(char relkind)
Alias * eref
Definition: parsenodes.h:1115
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1840
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23

◆ RangeVarCallbackForLockTable()

static void RangeVarCallbackForLockTable ( const RangeVar rv,
Oid  relid,
Oid  oldrelid,
void *  arg 
)
static

Definition at line 71 of file lockcmds.c.

References aclcheck_error(), ACLCHECK_OK, ereport, errcode(), errmsg(), ERROR, get_rel_persistence(), get_rel_relkind(), get_relkind_objtype(), GetUserId(), LockTableAclCheck(), MyXactFlags, OidIsValid, RangeVar::relname, and XACT_FLAGS_ACCESSEDTEMPNAMESPACE.

Referenced by LockTableCommand().

73 {
74  LOCKMODE lockmode = *(LOCKMODE *) arg;
75  char relkind;
76  char relpersistence;
77  AclResult aclresult;
78 
79  if (!OidIsValid(relid))
80  return; /* doesn't exist, so no permissions check */
81  relkind = get_rel_relkind(relid);
82  if (!relkind)
83  return; /* woops, concurrently dropped; no permissions
84  * check */
85 
86  /* Currently, we only allow plain tables or views to be locked */
87  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
88  relkind != RELKIND_VIEW)
89  ereport(ERROR,
90  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
91  errmsg("\"%s\" is not a table or view",
92  rv->relname)));
93 
94  /*
95  * Make note if a temporary relation has been accessed in this
96  * transaction.
97  */
98  relpersistence = get_rel_persistence(relid);
99  if (relpersistence == RELPERSISTENCE_TEMP)
101 
102  /* Check permissions. */
103  aclresult = LockTableAclCheck(relid, lockmode, GetUserId());
104  if (aclresult != ACLCHECK_OK)
106 }
int LOCKMODE
Definition: lockdefs.h:26
Oid GetUserId(void)
Definition: miscinit.c:448
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1915
int errcode(int sqlerrcode)
Definition: elog.c:610
#define OidIsValid(objectId)
Definition: c.h:644
char * relname
Definition: primnodes.h:68
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3294
static AclResult LockTableAclCheck(Oid relid, LOCKMODE lockmode, Oid userid)
Definition: lockcmds.c:275
#define ERROR
Definition: elog.h:43
int MyXactFlags
Definition: xact.c:119
#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE
Definition: xact.h:97
AclResult
Definition: acl.h:177
#define ereport(elevel,...)
Definition: elog.h:144
char get_rel_persistence(Oid relid)
Definition: lsyscache.c:1990
int errmsg(const char *fmt,...)
Definition: elog.c:824
ObjectType get_relkind_objtype(char relkind)
void * arg