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 "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 reloid, 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  reloid,
LOCKMODE  lockmode,
Oid  userid 
)
static

Definition at line 280 of file lockcmds.c.

281 {
282  AclResult aclresult;
284 
285  /* any of these privileges permit any lock mode */
287 
288  /* SELECT privileges also permit ACCESS SHARE and below */
289  if (lockmode <= AccessShareLock)
290  aclmask |= ACL_SELECT;
291 
292  /* INSERT privileges also permit ROW EXCLUSIVE and below */
293  if (lockmode <= RowExclusiveLock)
294  aclmask |= ACL_INSERT;
295 
296  aclresult = pg_class_aclcheck(reloid, userid, aclmask);
297 
298  return aclresult;
299 }
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1365
AclResult
Definition: acl.h:182
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4079
#define AccessShareLock
Definition: lockdefs.h:36
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ACL_DELETE
Definition: parsenodes.h:79
uint64 AclMode
Definition: parsenodes.h:74
#define ACL_MAINTAIN
Definition: parsenodes.h:90
#define ACL_INSERT
Definition: parsenodes.h:76
#define ACL_UPDATE
Definition: parsenodes.h:78
#define ACL_SELECT
Definition: parsenodes.h:77
#define ACL_TRUNCATE
Definition: parsenodes.h:80

References AccessShareLock, ACL_DELETE, ACL_INSERT, ACL_MAINTAIN, 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 41 of file lockcmds.c.

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 }
static void RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid, void *arg)
Definition: lockcmds.c:71
static void LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views)
Definition: lockcmds.c:245
static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait)
Definition: lockcmds.c:117
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1981
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:426
@ RVR_NOWAIT
Definition: namespace.h:73
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
unsigned int Oid
Definition: postgres_ext.h:31
bool nowait
Definition: parsenodes.h:3873
List * relations
Definition: parsenodes.h:3871
bool inh
Definition: primnodes.h:85

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

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

References ConditionalLockRelationOid(), ereport, errcode(), errmsg(), ERROR, find_all_inheritors(), get_rel_name(), lfirst_oid, LockRelationOid(), NoLock, ObjectIdGetDatum(), relname, 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 245 of file lockcmds.c.

247 {
249  Relation view;
250  Query *viewquery;
251 
252  /* caller has already locked the view */
253  view = table_open(reloid, NoLock);
254  viewquery = get_view_query(view);
255 
256  /*
257  * If the view has the security_invoker property set, check permissions as
258  * the current user. Otherwise, check permissions as the view owner.
259  */
260  context.lockmode = lockmode;
261  context.nowait = nowait;
262  if (RelationHasSecurityInvoker(view))
263  context.check_as_user = GetUserId();
264  else
265  context.check_as_user = view->rd_rel->relowner;
266  context.viewoid = reloid;
267  context.ancestor_views = lappend_oid(ancestor_views, reloid);
268 
269  LockViewRecurse_walker((Node *) viewquery, &context);
270 
271  context.ancestor_views = list_delete_last(context.ancestor_views);
272 
273  table_close(view, NoLock);
274 }
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
List * list_delete_last(List *list)
Definition: list.c:957
static bool LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
Definition: lockcmds.c:177
Oid GetUserId(void)
Definition: miscinit.c:514
tree context
Definition: radixtree.h:1797
#define RelationHasSecurityInvoker(relation)
Definition: rel.h:438
Query * get_view_query(Relation view)
Definition: nodes.h:129
Form_pg_class rd_rel
Definition: rel.h:111
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References context, get_view_query(), GetUserId(), lappend_oid(), list_delete_last(), LockViewRecurse_walker(), NoLock, RelationData::rd_rel, RelationHasSecurityInvoker, table_close(), and table_open().

Referenced by LockTableCommand(), and LockViewRecurse_walker().

◆ LockViewRecurse_walker()

static bool LockViewRecurse_walker ( Node node,
LockViewRecurse_context context 
)
static

Definition at line 177 of file lockcmds.c.

178 {
179  if (node == NULL)
180  return false;
181 
182  if (IsA(node, Query))
183  {
184  Query *query = (Query *) node;
185  ListCell *rtable;
186 
187  foreach(rtable, query->rtable)
188  {
189  RangeTblEntry *rte = lfirst(rtable);
190  AclResult aclresult;
191 
192  Oid relid = rte->relid;
193  char relkind = rte->relkind;
194  char *relname = get_rel_name(relid);
195 
196  /* Currently, we only allow plain tables or views to be locked. */
197  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
198  relkind != RELKIND_VIEW)
199  continue;
200 
201  /*
202  * We might be dealing with a self-referential view. If so, we
203  * can just stop recursing, since we already locked it.
204  */
205  if (list_member_oid(context->ancestor_views, relid))
206  continue;
207 
208  /*
209  * Check permissions as the specified user. This will either be
210  * the view owner or the current user.
211  */
212  aclresult = LockTableAclCheck(relid, context->lockmode,
213  context->check_as_user);
214  if (aclresult != ACLCHECK_OK)
215  aclcheck_error(aclresult, get_relkind_objtype(relkind), relname);
216 
217  /* We have enough rights to lock the relation; do so. */
218  if (!context->nowait)
219  LockRelationOid(relid, context->lockmode);
220  else if (!ConditionalLockRelationOid(relid, context->lockmode))
221  ereport(ERROR,
222  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
223  errmsg("could not obtain lock on relation \"%s\"",
224  relname)));
225 
226  if (relkind == RELKIND_VIEW)
227  LockViewRecurse(relid, context->lockmode, context->nowait,
228  context->ancestor_views);
229  else if (rte->inh)
230  LockTableRecurse(relid, context->lockmode, context->nowait);
231  }
232 
233  return query_tree_walker(query,
235  context,
237  }
238 
239  return expression_tree_walker(node,
241  context);
242 }
@ ACLCHECK_OK
Definition: acl.h:183
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2688
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:722
static AclResult LockTableAclCheck(Oid reloid, LOCKMODE lockmode, Oid userid)
Definition: lockcmds.c:280
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:156
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:25
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
ObjectType get_relkind_objtype(char relkind)
List * rtable
Definition: parsenodes.h:168

References aclcheck_error(), ACLCHECK_OK, ConditionalLockRelationOid(), context, ereport, errcode(), errmsg(), ERROR, expression_tree_walker, get_rel_name(), get_relkind_objtype(), RangeTblEntry::inh, IsA, lfirst, list_member_oid(), LockRelationOid(), LockTableAclCheck(), LockTableRecurse(), LockViewRecurse(), QTW_IGNORE_JOINALIASES, query_tree_walker, RangeTblEntry::relid, relname, and Query::rtable.

Referenced by LockViewRecurse().

◆ RangeVarCallbackForLockTable()

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

Definition at line 71 of file lockcmds.c.

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("cannot lock relation \"%s\"",
92  rv->relname),
94 
95  /*
96  * Make note if a temporary relation has been accessed in this
97  * transaction.
98  */
99  relpersistence = get_rel_persistence(relid);
100  if (relpersistence == RELPERSISTENCE_TEMP)
102 
103  /* Check permissions. */
104  aclresult = LockTableAclCheck(relid, lockmode, GetUserId());
105  if (aclresult != ACLCHECK_OK)
107 }
#define OidIsValid(objectId)
Definition: c.h:762
int LOCKMODE
Definition: lockdefs.h:26
char get_rel_persistence(Oid relid)
Definition: lsyscache.c:2056
void * arg
int errdetail_relkind_not_supported(char relkind)
Definition: pg_class.c:24
char * relname
Definition: primnodes.h:82
int MyXactFlags
Definition: xact.c:134
#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().