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

279 {
280  AclResult aclresult;
282 
283  /* Verify adequate privilege */
284  if (lockmode == AccessShareLock)
285  aclmask = ACL_SELECT;
286  else if (lockmode == RowExclusiveLock)
288  else
289  aclmask = ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE;
290 
291  aclresult = pg_class_aclcheck(reloid, userid, aclmask);
292 
293  return aclresult;
294 }
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 42 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().

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 }
#define NIL
Definition: pg_list.h:65
bool nowait
Definition: parsenodes.h:3331
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1920
static void LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views)
Definition: lockcmds.c:250
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:117
List * relations
Definition: parsenodes.h:3329
#define lfirst(lc)
Definition: pg_list.h:169
static void RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid, void *arg)
Definition: lockcmds.c:72

◆ LockTableRecurse()

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

Definition at line 117 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().

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 }
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:691
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:155
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:165
int errmsg(const char *fmt,...)
Definition: elog.c:902
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108
Definition: pg_list.h:50
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1845
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ LockViewRecurse()

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

Definition at line 250 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().

252 {
253  LockViewRecurse_context context;
254  Relation view;
255  Query *viewquery;
256 
257  /* caller has already locked the view */
258  view = table_open(reloid, NoLock);
259  viewquery = get_view_query(view);
260 
261  context.lockmode = lockmode;
262  context.nowait = nowait;
263  context.viewowner = view->rd_rel->relowner;
264  context.viewoid = reloid;
265  context.ancestor_views = lappend_oid(ancestor_views, reloid);
266 
267  LockViewRecurse_walker((Node *) viewquery, &context);
268 
270 
271  table_close(view, NoLock);
272 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static bool LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
Definition: lockcmds.c:177
Definition: nodes.h:527
Form_pg_class rd_rel
Definition: rel.h:110
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 177 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().

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  /*
197  * The OLD and NEW placeholder entries in the view's rtable are
198  * skipped.
199  */
200  if (relid == context->viewoid &&
201  (strcmp(rte->eref->aliasname, "old") == 0 ||
202  strcmp(rte->eref->aliasname, "new") == 0))
203  continue;
204 
205  /* Currently, we only allow plain tables or views to be locked. */
206  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
207  relkind != RELKIND_VIEW)
208  continue;
209 
210  /*
211  * We might be dealing with a self-referential view. If so, we
212  * can just stop recursing, since we already locked it.
213  */
214  if (list_member_oid(context->ancestor_views, relid))
215  continue;
216 
217  /* Check permissions with the view owner's privilege. */
218  aclresult = LockTableAclCheck(relid, context->lockmode, context->viewowner);
219  if (aclresult != ACLCHECK_OK)
220  aclcheck_error(aclresult, get_relkind_objtype(relkind), relname);
221 
222  /* We have enough rights to lock the relation; do so. */
223  if (!context->nowait)
224  LockRelationOid(relid, context->lockmode);
225  else if (!ConditionalLockRelationOid(relid, context->lockmode))
226  ereport(ERROR,
227  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
228  errmsg("could not obtain lock on relation \"%s\"",
229  relname)));
230 
231  if (relkind == RELKIND_VIEW)
232  LockViewRecurse(relid, context->lockmode, context->nowait,
233  context->ancestor_views);
234  else if (rte->inh)
235  LockTableRecurse(relid, context->lockmode, context->nowait);
236  }
237 
238  return query_tree_walker(query,
240  context,
242  }
243 
244  return expression_tree_walker(node,
246  context);
247 }
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:2322
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
static bool LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
Definition: lockcmds.c:177
int errcode(int sqlerrcode)
Definition: elog.c:691
static void LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views)
Definition: lockcmds.c:250
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:278
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:43
static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait)
Definition: lockcmds.c:117
AclResult
Definition: acl.h:177
#define ereport(elevel,...)
Definition: elog.h:155
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:674
#define lfirst(lc)
Definition: pg_list.h:169
char * aliasname
Definition: primnodes.h:42
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1888
int errmsg(const char *fmt,...)
Definition: elog.c:902
ObjectType get_relkind_objtype(char relkind)
Alias * eref
Definition: parsenodes.h:1121
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1845
#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 72 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().

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("\"%s\" is not a table or view",
93  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 }
int LOCKMODE
Definition: lockdefs.h:26
Oid GetUserId(void)
Definition: miscinit.c:476
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1920
int errcode(int sqlerrcode)
Definition: elog.c:691
#define OidIsValid(objectId)
Definition: c.h:706
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:278
#define ERROR
Definition: elog.h:43
int MyXactFlags
Definition: xact.c:132
#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE
Definition: xact.h:102
AclResult
Definition: acl.h:177
#define ereport(elevel,...)
Definition: elog.h:155
char get_rel_persistence(Oid relid)
Definition: lsyscache.c:1995
int errmsg(const char *fmt,...)
Definition: elog.c:902
ObjectType get_relkind_objtype(char relkind)
void * arg