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 290 of file lockcmds.c.

291 {
292  AclResult aclresult;
294 
295  /* Verify adequate privilege */
296  if (lockmode == AccessShareLock)
298  else if (lockmode == RowExclusiveLock)
300  else
302 
303  aclresult = pg_class_aclcheck(reloid, userid, aclmask);
304 
305  return aclresult;
306 }
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1322
AclResult
Definition: acl.h:181
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:5007
#define AccessShareLock
Definition: lockdefs.h:36
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ACL_DELETE
Definition: parsenodes.h:85
#define ACL_INSERT
Definition: parsenodes.h:82
#define ACL_UPDATE
Definition: parsenodes.h:84
#define ACL_SELECT
Definition: parsenodes.h:83
#define ACL_TRUNCATE
Definition: parsenodes.h:86
uint32 AclMode
Definition: parsenodes.h:80

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

Referenced by LockViewRecurse_walker(), and RangeVarCallbackForLockTable().

◆ LockTableCommand()

void LockTableCommand ( LockStmt lockstmt)

Definition at line 42 of file lockcmds.c.

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

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().

◆ LockTableRecurse()

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

Definition at line 118 of file lockcmds.c.

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

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().

◆ LockViewRecurse()

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

Definition at line 255 of file lockcmds.c.

257 {
258  LockViewRecurse_context context;
259  Relation view;
260  Query *viewquery;
261 
262  /* caller has already locked the view */
263  view = table_open(reloid, NoLock);
264  viewquery = get_view_query(view);
265 
266  /*
267  * If the view has the security_invoker property set, check permissions as
268  * the current user. Otherwise, check permissions as the view owner.
269  */
270  context.lockmode = lockmode;
271  context.nowait = nowait;
272  if (RelationHasSecurityInvoker(view))
273  context.check_as_user = GetUserId();
274  else
275  context.check_as_user = view->rd_rel->relowner;
276  context.viewoid = reloid;
277  context.ancestor_views = lappend_oid(ancestor_views, reloid);
278 
279  LockViewRecurse_walker((Node *) viewquery, &context);
280 
282 
283  table_close(view, NoLock);
284 }
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
List * list_delete_last(List *list)
Definition: list.c:916
static bool LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
Definition: lockcmds.c:178
Oid GetUserId(void)
Definition: miscinit.c:492
#define RelationHasSecurityInvoker(relation)
Definition: rel.h:420
Query * get_view_query(Relation view)
Definition: nodes.h:574
Form_pg_class rd_rel
Definition: rel.h:109
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

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

Referenced by LockTableCommand(), and LockViewRecurse_walker().

◆ LockViewRecurse_walker()

static bool LockViewRecurse_walker ( Node node,
LockViewRecurse_context context 
)
static

Definition at line 178 of file lockcmds.c.

179 {
180  if (node == NULL)
181  return false;
182 
183  if (IsA(node, Query))
184  {
185  Query *query = (Query *) node;
186  ListCell *rtable;
187 
188  foreach(rtable, query->rtable)
189  {
190  RangeTblEntry *rte = lfirst(rtable);
191  AclResult aclresult;
192 
193  Oid relid = rte->relid;
194  char relkind = rte->relkind;
195  char *relname = get_rel_name(relid);
196 
197  /*
198  * The OLD and NEW placeholder entries in the view's rtable are
199  * skipped.
200  */
201  if (relid == context->viewoid &&
202  (strcmp(rte->eref->aliasname, "old") == 0 ||
203  strcmp(rte->eref->aliasname, "new") == 0))
204  continue;
205 
206  /* Currently, we only allow plain tables or views to be locked. */
207  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
208  relkind != RELKIND_VIEW)
209  continue;
210 
211  /*
212  * We might be dealing with a self-referential view. If so, we
213  * can just stop recursing, since we already locked it.
214  */
215  if (list_member_oid(context->ancestor_views, relid))
216  continue;
217 
218  /*
219  * Check permissions as the specified user. This will either be
220  * the view owner or the current user.
221  */
222  aclresult = LockTableAclCheck(relid, context->lockmode,
223  context->check_as_user);
224  if (aclresult != ACLCHECK_OK)
225  aclcheck_error(aclresult, get_relkind_objtype(relkind), relname);
226 
227  /* We have enough rights to lock the relation; do so. */
228  if (!context->nowait)
229  LockRelationOid(relid, context->lockmode);
230  else if (!ConditionalLockRelationOid(relid, context->lockmode))
231  ereport(ERROR,
232  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
233  errmsg("could not obtain lock on relation \"%s\"",
234  relname)));
235 
236  if (relkind == RELKIND_VIEW)
237  LockViewRecurse(relid, context->lockmode, context->nowait,
238  context->ancestor_views);
239  else if (rte->inh)
240  LockTableRecurse(relid, context->lockmode, context->nowait);
241  }
242 
243  return query_tree_walker(query,
245  context,
247  }
248 
249  return expression_tree_walker(node,
251  context);
252 }
@ ACLCHECK_OK
Definition: acl.h:182
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3512
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:701
static AclResult LockTableAclCheck(Oid relid, LOCKMODE lockmode, Oid userid)
Definition: lockcmds.c:290
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2570
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:2015
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23
#define IsA(nodeptr, _type_)
Definition: nodes.h:624
ObjectType get_relkind_objtype(char relkind)
char * aliasname
Definition: primnodes.h:42
List * rtable
Definition: parsenodes.h:148
Alias * eref
Definition: parsenodes.h:1161

References aclcheck_error(), ACLCHECK_OK, Alias::aliasname, LockViewRecurse_context::ancestor_views, LockViewRecurse_context::check_as_user, 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, and LockViewRecurse_context::viewoid.

Referenced by LockViewRecurse().

◆ RangeVarCallbackForLockTable()

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

Definition at line 72 of file lockcmds.c.

74 {
75  LOCKMODE lockmode = *(LOCKMODE *) arg;
76  char relkind;
77  char relpersistence;
78  AclResult aclresult;
79 
80  if (!OidIsValid(relid))
81  return; /* doesn't exist, so no permissions check */
82  relkind = get_rel_relkind(relid);
83  if (!relkind)
84  return; /* woops, concurrently dropped; no permissions
85  * check */
86 
87  /* Currently, we only allow plain tables or views to be locked */
88  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
89  relkind != RELKIND_VIEW)
90  ereport(ERROR,
91  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
92  errmsg("cannot lock relation \"%s\"",
93  rv->relname),
95 
96  /*
97  * Make note if a temporary relation has been accessed in this
98  * transaction.
99  */
100  relpersistence = get_rel_persistence(relid);
101  if (relpersistence == RELPERSISTENCE_TEMP)
103 
104  /* Check permissions. */
105  aclresult = LockTableAclCheck(relid, lockmode, GetUserId());
106  if (aclresult != ACLCHECK_OK)
108 }
#define OidIsValid(objectId)
Definition: c.h:710
int LOCKMODE
Definition: lockdefs.h:26
char get_rel_persistence(Oid relid)
Definition: lsyscache.c:2059
void * arg
int errdetail_relkind_not_supported(char relkind)
Definition: pg_class.c:24
char * relname
Definition: primnodes.h:68
int MyXactFlags
Definition: xact.c:135
#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE
Definition: xact.h:102

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

Referenced by LockTableCommand().