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 "commands/tablecmds.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 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 282 of file lockcmds.c.

283 {
284  AclResult aclresult;
286 
287  /* any of these privileges permit any lock mode */
289 
290  /* SELECT privileges also permit ACCESS SHARE and below */
291  if (lockmode <= AccessShareLock)
292  aclmask |= ACL_SELECT;
293 
294  /* INSERT privileges also permit ROW EXCLUSIVE and below */
295  if (lockmode <= RowExclusiveLock)
296  aclmask |= ACL_INSERT;
297 
298  aclresult = pg_class_aclcheck(reloid, userid, aclmask);
299 
300  /*
301  * If this is a partition, check permissions of its ancestors if needed.
302  */
303  if (aclresult != ACLCHECK_OK &&
305  aclresult = ACLCHECK_OK;
306 
307  return aclresult;
308 }
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1359
AclResult
Definition: acl.h:182
@ ACLCHECK_OK
Definition: acl.h:183
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:3923
#define AccessShareLock
Definition: lockdefs.h:36
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ACL_DELETE
Definition: parsenodes.h:86
uint64 AclMode
Definition: parsenodes.h:81
#define ACL_MAINTAIN
Definition: parsenodes.h:97
#define ACL_INSERT
Definition: parsenodes.h:83
#define ACL_UPDATE
Definition: parsenodes.h:85
#define ACL_SELECT
Definition: parsenodes.h:84
#define ACL_TRUNCATE
Definition: parsenodes.h:87
bool has_partition_ancestor_privs(Oid relid, Oid userid, AclMode acl)
Definition: tablecmds.c:16993

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

Referenced by LockViewRecurse_walker(), and RangeVarCallbackForLockTable().

◆ LockTableCommand()

void LockTableCommand ( LockStmt lockstmt)

Definition at line 43 of file lockcmds.c.

44 {
45  ListCell *p;
46 
47  /*
48  * Iterate over the list and process the named relations one at a time
49  */
50  foreach(p, lockstmt->relations)
51  {
52  RangeVar *rv = (RangeVar *) lfirst(p);
53  bool recurse = rv->inh;
54  Oid reloid;
55 
56  reloid = RangeVarGetRelidExtended(rv, lockstmt->mode,
57  lockstmt->nowait ? RVR_NOWAIT : 0,
59  (void *) &lockstmt->mode);
60 
61  if (get_rel_relkind(reloid) == RELKIND_VIEW)
62  LockViewRecurse(reloid, lockstmt->mode, lockstmt->nowait, NIL);
63  else if (recurse)
64  LockTableRecurse(reloid, lockstmt->mode, lockstmt->nowait);
65  }
66 }
static void RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid, void *arg)
Definition: lockcmds.c:73
static void LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views)
Definition: lockcmds.c:247
static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait)
Definition: lockcmds.c:119
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1985
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:239
@ RVR_NOWAIT
Definition: namespace.h:72
#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:3775
List * relations
Definition: parsenodes.h:3773
bool inh
Definition: primnodes.h:77

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

120 {
121  List *children;
122  ListCell *lc;
123 
124  children = find_all_inheritors(reloid, NoLock, NULL);
125 
126  foreach(lc, children)
127  {
128  Oid childreloid = lfirst_oid(lc);
129 
130  /* Parent already locked. */
131  if (childreloid == reloid)
132  continue;
133 
134  if (!nowait)
135  LockRelationOid(childreloid, lockmode);
136  else if (!ConditionalLockRelationOid(childreloid, lockmode))
137  {
138  /* try to throw error by name; relation could be deleted... */
139  char *relname = get_rel_name(childreloid);
140 
141  if (!relname)
142  continue; /* child concurrently dropped, just skip it */
143  ereport(ERROR,
144  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
145  errmsg("could not obtain lock on relation \"%s\"",
146  relname)));
147  }
148 
149  /*
150  * Even if we got the lock, child might have been concurrently
151  * dropped. If so, we can skip it.
152  */
153  if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(childreloid)))
154  {
155  /* Release useless lock */
156  UnlockRelationOid(childreloid, lockmode);
157  continue;
158  }
159  }
160 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
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:1910
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:174
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
Definition: pg_list.h:54
@ RELOID
Definition: syscache.h:89
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:191

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

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

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

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

References aclcheck_error(), ACLCHECK_OK, LockViewRecurse_context::ancestor_views, LockViewRecurse_context::check_as_user, ConditionalLockRelationOid(), 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, and Query::rtable.

Referenced by LockViewRecurse().

◆ RangeVarCallbackForLockTable()

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

Definition at line 73 of file lockcmds.c.

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