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 "parser/parse_clause.h"
#include "storage/lmgr.h"
#include "utils/acl.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "rewrite/rewriteHandler.h"
#include "nodes/nodeFuncs.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, Oid userid)
 
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 297 of file lockcmds.c.

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

Referenced by LockTableRecurse(), LockViewRecurse_walker(), and RangeVarCallbackForLockTable().

298 {
299  AclResult aclresult;
301 
302  /* Verify adequate privilege */
303  if (lockmode == AccessShareLock)
304  aclmask = ACL_SELECT;
305  else if (lockmode == RowExclusiveLock)
307  else
308  aclmask = ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE;
309 
310  aclresult = pg_class_aclcheck(reloid, userid, aclmask);
311 
312  return aclresult;
313 }
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1321
#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:4631
#define ACL_TRUNCATE
Definition: parsenodes.h:78

◆ LockTableCommand()

void LockTableCommand ( LockStmt lockstmt)

Definition at line 41 of file lockcmds.c.

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

Referenced by standard_ProcessUtility().

42 {
43  ListCell *p;
44 
45  /*---------
46  * During recovery we only accept these variations:
47  * LOCK TABLE foo IN ACCESS SHARE MODE
48  * LOCK TABLE foo IN ROW SHARE MODE
49  * LOCK TABLE foo IN ROW EXCLUSIVE MODE
50  * This test must match the restrictions defined in LockAcquireExtended()
51  *---------
52  */
53  if (lockstmt->mode > RowExclusiveLock)
54  PreventCommandDuringRecovery("LOCK TABLE");
55 
56  /*
57  * Iterate over the list and process the named relations one at a time
58  */
59  foreach(p, lockstmt->relations)
60  {
61  RangeVar *rv = (RangeVar *) lfirst(p);
62  bool recurse = rv->inh;
63  Oid reloid;
64 
65  reloid = RangeVarGetRelidExtended(rv, lockstmt->mode,
66  lockstmt->nowait ? RVR_NOWAIT : 0,
68  (void *) &lockstmt->mode);
69 
70  if (get_rel_relkind(reloid) == RELKIND_VIEW)
71  LockViewRecurse(reloid, lockstmt->mode, lockstmt->nowait, NIL);
72  else if (recurse)
73  LockTableRecurse(reloid, lockstmt->mode, lockstmt->nowait, GetUserId());
74  }
75 }
#define NIL
Definition: pg_list.h:65
bool nowait
Definition: parsenodes.h:3295
Oid GetUserId(void)
Definition: miscinit.c:380
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1805
static void LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views)
Definition: lockcmds.c:270
unsigned int Oid
Definition: postgres_ext.h:31
void PreventCommandDuringRecovery(const char *cmdname)
Definition: utility.c:276
static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, Oid userid)
Definition: lockcmds.c:127
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:228
bool inh
Definition: primnodes.h:69
List * relations
Definition: parsenodes.h:3293
#define lfirst(lc)
Definition: pg_list.h:190
static void RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid, void *arg)
Definition: lockcmds.c:82

◆ LockTableRecurse()

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

Definition at line 127 of file lockcmds.c.

References aclcheck_error(), ACLCHECK_OK, ConditionalLockRelationOid(), ereport, errcode(), errmsg(), ERROR, find_inheritance_children(), get_rel_name(), get_rel_relkind(), get_relkind_objtype(), lfirst_oid, LockRelationOid(), LockTableAclCheck(), NoLock, ObjectIdGetDatum, relname, RELOID, SearchSysCacheExists1, and UnlockRelationOid().

Referenced by LockTableCommand(), and LockViewRecurse_walker().

128 {
129  List *children;
130  ListCell *lc;
131 
132  children = find_inheritance_children(reloid, NoLock);
133 
134  foreach(lc, children)
135  {
136  Oid childreloid = lfirst_oid(lc);
137  AclResult aclresult;
138 
139  /* Check permissions before acquiring the lock. */
140  aclresult = LockTableAclCheck(childreloid, lockmode, userid);
141  if (aclresult != ACLCHECK_OK)
142  {
143  char *relname = get_rel_name(childreloid);
144 
145  if (!relname)
146  continue; /* child concurrently dropped, just skip it */
147  aclcheck_error(aclresult, get_relkind_objtype(get_rel_relkind(childreloid)), relname);
148  }
149 
150  /* We have enough rights to lock the relation; do so. */
151  if (!nowait)
152  LockRelationOid(childreloid, lockmode);
153  else if (!ConditionalLockRelationOid(childreloid, lockmode))
154  {
155  /* try to throw error by name; relation could be deleted... */
156  char *relname = get_rel_name(childreloid);
157 
158  if (!relname)
159  continue; /* child concurrently dropped, just skip it */
160  ereport(ERROR,
161  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
162  errmsg("could not obtain lock on relation \"%s\"",
163  relname)));
164  }
165 
166  /*
167  * Even if we got the lock, child might have been concurrently
168  * dropped. If so, we can skip it.
169  */
170  if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(childreloid)))
171  {
172  /* Release useless lock */
173  UnlockRelationOid(childreloid, lockmode);
174  continue;
175  }
176 
177  LockTableRecurse(childreloid, lockmode, nowait, userid);
178  }
179 }
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:151
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:199
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1805
int errcode(int sqlerrcode)
Definition: elog.c:570
NameData relname
Definition: pg_class.h:35
unsigned int Oid
Definition: postgres_ext.h:31
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3353
static AclResult LockTableAclCheck(Oid relid, LOCKMODE lockmode, Oid userid)
Definition: lockcmds.c:297
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:183
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, Oid userid)
Definition: lockcmds.c:127
#define NoLock
Definition: lockdefs.h:34
#define ereport(elevel, rest)
Definition: elog.h:141
AclResult
Definition: acl.h:177
List * find_inheritance_children(Oid parentrelId, LOCKMODE lockmode)
Definition: pg_inherits.c:55
int errmsg(const char *fmt,...)
Definition: elog.c:784
ObjectType get_relkind_objtype(char relkind)
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108
Definition: pg_list.h:50
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730
#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 270 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().

271 {
272  LockViewRecurse_context context;
273 
274  Relation view;
275  Query *viewquery;
276 
277  view = table_open(reloid, NoLock);
278  viewquery = get_view_query(view);
279 
280  context.lockmode = lockmode;
281  context.nowait = nowait;
282  context.viewowner = view->rd_rel->relowner;
283  context.viewoid = reloid;
284  context.ancestor_views = lappend_oid(ancestor_views, reloid);
285 
286  LockViewRecurse_walker((Node *) viewquery, &context);
287 
288  (void) list_delete_last(context.ancestor_views);
289 
290  table_close(view, NoLock);
291 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
static bool LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
Definition: lockcmds.c:198
Definition: nodes.h:525
Form_pg_class rd_rel
Definition: rel.h:83
List * lappend_oid(List *list, Oid datum)
Definition: list.c:358
#define NoLock
Definition: lockdefs.h:34
List * list_delete_last(List *list)
Definition: list.c:878
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 198 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, relkind, RangeTblEntry::relkind, relname, Query::rtable, LockViewRecurse_context::viewoid, and LockViewRecurse_context::viewowner.

Referenced by LockViewRecurse().

199 {
200  if (node == NULL)
201  return false;
202 
203  if (IsA(node, Query))
204  {
205  Query *query = (Query *) node;
206  ListCell *rtable;
207 
208  foreach(rtable, query->rtable)
209  {
210  RangeTblEntry *rte = lfirst(rtable);
211  AclResult aclresult;
212 
213  Oid relid = rte->relid;
214  char relkind = rte->relkind;
215  char *relname = get_rel_name(relid);
216 
217  /*
218  * The OLD and NEW placeholder entries in the view's rtable are
219  * skipped.
220  */
221  if (relid == context->viewoid &&
222  (strcmp(rte->eref->aliasname, "old") == 0 ||
223  strcmp(rte->eref->aliasname, "new") == 0))
224  continue;
225 
226  /* Currently, we only allow plain tables or views to be locked. */
227  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
228  relkind != RELKIND_VIEW)
229  continue;
230 
231  /* Check infinite recursion in the view definition. */
232  if (list_member_oid(context->ancestor_views, relid))
233  ereport(ERROR,
234  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
235  errmsg("infinite recursion detected in rules for relation \"%s\"",
236  get_rel_name(relid))));
237 
238  /* Check permissions with the view owner's privilege. */
239  aclresult = LockTableAclCheck(relid, context->lockmode, context->viewowner);
240  if (aclresult != ACLCHECK_OK)
241  aclcheck_error(aclresult, get_relkind_objtype(relkind), relname);
242 
243  /* We have enough rights to lock the relation; do so. */
244  if (!context->nowait)
245  LockRelationOid(relid, context->lockmode);
246  else if (!ConditionalLockRelationOid(relid, context->lockmode))
247  ereport(ERROR,
248  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
249  errmsg("could not obtain lock on relation \"%s\"",
250  relname)));
251 
252  if (relkind == RELKIND_VIEW)
253  LockViewRecurse(relid, context->lockmode, context->nowait, context->ancestor_views);
254  else if (rte->inh)
255  LockTableRecurse(relid, context->lockmode, context->nowait, context->viewowner);
256  }
257 
258  return query_tree_walker(query,
260  context,
262  }
263 
264  return expression_tree_walker(node,
266  context);
267 }
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:2274
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
static bool LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
Definition: lockcmds.c:198
int errcode(int sqlerrcode)
Definition: elog.c:570
static void LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views)
Definition: lockcmds.c:270
NameData relname
Definition: pg_class.h:35
unsigned int Oid
Definition: postgres_ext.h:31
char relkind
Definition: pg_class.h:81
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3353
static AclResult LockTableAclCheck(Oid relid, LOCKMODE lockmode, Oid userid)
Definition: lockcmds.c:297
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:43
static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, Oid userid)
Definition: lockcmds.c:127
#define ereport(elevel, rest)
Definition: elog.h:141
AclResult
Definition: acl.h:177
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:675
#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:1840
int errmsg(const char *fmt,...)
Definition: elog.c:784
ObjectType get_relkind_objtype(char relkind)
Alias * eref
Definition: parsenodes.h:1092
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730
#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 82 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, relkind, RangeVar::relname, relpersistence, and XACT_FLAGS_ACCESSEDTEMPNAMESPACE.

Referenced by LockTableCommand().

84 {
85  LOCKMODE lockmode = *(LOCKMODE *) arg;
86  char relkind;
87  char relpersistence;
88  AclResult aclresult;
89 
90  if (!OidIsValid(relid))
91  return; /* doesn't exist, so no permissions check */
92  relkind = get_rel_relkind(relid);
93  if (!relkind)
94  return; /* woops, concurrently dropped; no permissions
95  * check */
96 
97  /* Currently, we only allow plain tables or views to be locked */
98  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
99  relkind != RELKIND_VIEW)
100  ereport(ERROR,
101  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
102  errmsg("\"%s\" is not a table or view",
103  rv->relname)));
104 
105  /*
106  * Make note if a temporary relation has been accessed in this
107  * transaction.
108  */
109  relpersistence = get_rel_persistence(relid);
110  if (relpersistence == RELPERSISTENCE_TEMP)
112 
113  /* Check permissions. */
114  aclresult = LockTableAclCheck(relid, lockmode, GetUserId());
115  if (aclresult != ACLCHECK_OK)
117 }
int LOCKMODE
Definition: lockdefs.h:26
Oid GetUserId(void)
Definition: miscinit.c:380
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1805
int errcode(int sqlerrcode)
Definition: elog.c:570
#define OidIsValid(objectId)
Definition: c.h:638
char relkind
Definition: pg_class.h:81
char * relname
Definition: primnodes.h:68
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3353
static AclResult LockTableAclCheck(Oid relid, LOCKMODE lockmode, Oid userid)
Definition: lockcmds.c:297
#define ERROR
Definition: elog.h:43
char relpersistence
Definition: pg_class.h:78
#define ereport(elevel, rest)
Definition: elog.h:141
int MyXactFlags
Definition: xact.c:119
#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE
Definition: xact.h:97
AclResult
Definition: acl.h:177
char get_rel_persistence(Oid relid)
Definition: lsyscache.c:1880
int errmsg(const char *fmt,...)
Definition: elog.c:784
ObjectType get_relkind_objtype(char relkind)
void * arg