PostgreSQL Source Code  git master
lockcmds.c File Reference
#include "postgres.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 "access/heapam.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 286 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().

287 {
288  AclResult aclresult;
290 
291  /* Verify adequate privilege */
292  if (lockmode == AccessShareLock)
293  aclmask = ACL_SELECT;
294  else if (lockmode == RowExclusiveLock)
296  else
297  aclmask = ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE;
298 
299  aclresult = pg_class_aclcheck(reloid, userid, aclmask);
300 
301  return aclresult;
302 }
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:178
#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:4627
#define ACL_TRUNCATE
Definition: parsenodes.h:78

◆ LockTableCommand()

void LockTableCommand ( LockStmt lockstmt)

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

41 {
42  ListCell *p;
43 
44  /*---------
45  * During recovery we only accept these variations:
46  * LOCK TABLE foo IN ACCESS SHARE MODE
47  * LOCK TABLE foo IN ROW SHARE MODE
48  * LOCK TABLE foo IN ROW EXCLUSIVE MODE
49  * This test must match the restrictions defined in LockAcquireExtended()
50  *---------
51  */
52  if (lockstmt->mode > RowExclusiveLock)
53  PreventCommandDuringRecovery("LOCK TABLE");
54 
55  /*
56  * Iterate over the list and process the named relations one at a time
57  */
58  foreach(p, lockstmt->relations)
59  {
60  RangeVar *rv = (RangeVar *) lfirst(p);
61  bool recurse = rv->inh;
62  Oid reloid;
63 
64  reloid = RangeVarGetRelidExtended(rv, lockstmt->mode,
65  lockstmt->nowait ? RVR_NOWAIT : 0,
67  (void *) &lockstmt->mode);
68 
69  if (get_rel_relkind(reloid) == RELKIND_VIEW)
70  LockViewRecurse(reloid, lockstmt->mode, lockstmt->nowait, NIL);
71  else if (recurse)
72  LockTableRecurse(reloid, lockstmt->mode, lockstmt->nowait, GetUserId());
73  }
74 }
#define NIL
Definition: pg_list.h:69
bool nowait
Definition: parsenodes.h:3252
Oid GetUserId(void)
Definition: miscinit.c:379
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:259
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:117
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:227
bool inh
Definition: primnodes.h:69
List * relations
Definition: parsenodes.h:3250
#define lfirst(lc)
Definition: pg_list.h:106
static void RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid, void *arg)
Definition: lockcmds.c:81

◆ LockTableRecurse()

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

Definition at line 117 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, RELOID, SearchSysCacheExists1, and UnlockRelationOid().

Referenced by LockTableCommand(), and LockViewRecurse_walker().

118 {
119  List *children;
120  ListCell *lc;
121 
122  children = find_inheritance_children(reloid, NoLock);
123 
124  foreach(lc, children)
125  {
126  Oid childreloid = lfirst_oid(lc);
127  AclResult aclresult;
128 
129  /* Check permissions before acquiring the lock. */
130  aclresult = LockTableAclCheck(childreloid, lockmode, userid);
131  if (aclresult != ACLCHECK_OK)
132  {
133  char *relname = get_rel_name(childreloid);
134 
135  if (!relname)
136  continue; /* child concurrently dropped, just skip it */
137  aclcheck_error(aclresult, get_relkind_objtype(get_rel_relkind(childreloid)), relname);
138  }
139 
140  /* We have enough rights to lock the relation; do so. */
141  if (!nowait)
142  LockRelationOid(childreloid, lockmode);
143  else if (!ConditionalLockRelationOid(childreloid, lockmode))
144  {
145  /* try to throw error by name; relation could be deleted... */
146  char *relname = get_rel_name(childreloid);
147 
148  if (!relname)
149  continue; /* child concurrently dropped, just skip it */
150  ereport(ERROR,
151  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
152  errmsg("could not obtain lock on relation \"%s\"",
153  relname)));
154  }
155 
156  /*
157  * Even if we got the lock, child might have been concurrently
158  * dropped. If so, we can skip it.
159  */
160  if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(childreloid)))
161  {
162  /* Release useless lock */
163  UnlockRelationOid(childreloid, lockmode);
164  continue;
165  }
166 
167  LockTableRecurse(childreloid, lockmode, nowait, userid);
168  }
169 }
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:138
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:182
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1805
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3349
static AclResult LockTableAclCheck(Oid relid, LOCKMODE lockmode, Oid userid)
Definition: lockcmds.c:286
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:182
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, Oid userid)
Definition: lockcmds.c:117
#define NoLock
Definition: lockdefs.h:34
#define ereport(elevel, rest)
Definition: elog.h:122
AclResult
Definition: acl.h:178
List * find_inheritance_children(Oid parentrelId, LOCKMODE lockmode)
Definition: pg_inherits.c:56
int errmsg(const char *fmt,...)
Definition: elog.c:797
ObjectType get_relkind_objtype(char relkind)
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:105
Definition: pg_list.h:45
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1730
#define lfirst_oid(lc)
Definition: pg_list.h:108

◆ LockViewRecurse()

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

Definition at line 259 of file lockcmds.c.

References LockViewRecurse_context::ancestor_views, get_view_query(), heap_close, heap_open(), lcons_oid(), list_delete_oid(), LockViewRecurse_context::lockmode, LockViewRecurse_walker(), NoLock, LockViewRecurse_context::nowait, RelationData::rd_rel, LockViewRecurse_context::viewoid, and LockViewRecurse_context::viewowner.

Referenced by LockTableCommand(), and LockViewRecurse_walker().

260 {
261  LockViewRecurse_context context;
262 
263  Relation view;
264  Query *viewquery;
265 
266  view = heap_open(reloid, NoLock);
267  viewquery = get_view_query(view);
268 
269  context.lockmode = lockmode;
270  context.nowait = nowait;
271  context.viewowner = view->rd_rel->relowner;
272  context.viewoid = reloid;
273  context.ancestor_views = lcons_oid(reloid, ancestor_views);
274 
275  LockViewRecurse_walker((Node *) viewquery, &context);
276 
277  ancestor_views = list_delete_oid(ancestor_views, reloid);
278 
279  heap_close(view, NoLock);
280 }
static bool LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
Definition: lockcmds.c:188
Definition: nodes.h:516
List * lcons_oid(Oid datum, List *list)
Definition: list.c:295
#define heap_close(r, l)
Definition: heapam.h:97
Form_pg_class rd_rel
Definition: rel.h:84
#define NoLock
Definition: lockdefs.h:34
List * list_delete_oid(List *list, Oid datum)
Definition: list.c:636
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
Query * get_view_query(Relation view)

◆ LockViewRecurse_walker()

static bool LockViewRecurse_walker ( Node node,
LockViewRecurse_context context 
)
static

Definition at line 188 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, Query::rtable, LockViewRecurse_context::viewoid, and LockViewRecurse_context::viewowner.

Referenced by LockViewRecurse().

189 {
190  if (node == NULL)
191  return false;
192 
193  if (IsA(node, Query))
194  {
195  Query *query = (Query *) node;
196  ListCell *rtable;
197 
198  foreach(rtable, query->rtable)
199  {
200  RangeTblEntry *rte = lfirst(rtable);
201  AclResult aclresult;
202 
203  Oid relid = rte->relid;
204  char relkind = rte->relkind;
205  char *relname = get_rel_name(relid);
206 
207  /*
208  * The OLD and NEW placeholder entries in the view's rtable are
209  * skipped.
210  */
211  if (relid == context->viewoid &&
212  (!strcmp(rte->eref->aliasname, "old") || !strcmp(rte->eref->aliasname, "new")))
213  continue;
214 
215  /* Currently, we only allow plain tables or views to be locked. */
216  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
217  relkind != RELKIND_VIEW)
218  continue;
219 
220  /* Check infinite recursion in the view definition. */
221  if (list_member_oid(context->ancestor_views, relid))
222  ereport(ERROR,
223  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
224  errmsg("infinite recursion detected in rules for relation \"%s\"",
225  get_rel_name(relid))));
226 
227  /* Check permissions with the view owner's privilege. */
228  aclresult = LockTableAclCheck(relid, context->lockmode, context->viewowner);
229  if (aclresult != ACLCHECK_OK)
230  aclcheck_error(aclresult, get_relkind_objtype(relkind), relname);
231 
232  /* We have enough rights to lock the relation; do so. */
233  if (!context->nowait)
234  LockRelationOid(relid, context->lockmode);
235  else if (!ConditionalLockRelationOid(relid, context->lockmode))
236  ereport(ERROR,
237  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
238  errmsg("could not obtain lock on relation \"%s\"",
239  relname)));
240 
241  if (relkind == RELKIND_VIEW)
242  LockViewRecurse(relid, context->lockmode, context->nowait, context->ancestor_views);
243  else if (rte->inh)
244  LockTableRecurse(relid, context->lockmode, context->nowait, context->viewowner);
245  }
246 
247  return query_tree_walker(query,
249  context,
251  }
252 
253  return expression_tree_walker(node,
255  context);
256 }
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:138
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2256
#define IsA(nodeptr, _type_)
Definition: nodes.h:567
static bool LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
Definition: lockcmds.c:188
int errcode(int sqlerrcode)
Definition: elog.c:575
static void LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views)
Definition: lockcmds.c:259
unsigned int Oid
Definition: postgres_ext.h:31
char relkind
Definition: pg_class.h:51
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3349
static AclResult LockTableAclCheck(Oid relid, LOCKMODE lockmode, Oid userid)
Definition: lockcmds.c:286
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:117
#define ereport(elevel, rest)
Definition: elog.h:122
AclResult
Definition: acl.h:178
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
#define lfirst(lc)
Definition: pg_list.h:106
char * aliasname
Definition: primnodes.h:42
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1834
int errmsg(const char *fmt,...)
Definition: elog.c:797
ObjectType get_relkind_objtype(char relkind)
Alias * eref
Definition: parsenodes.h:1066
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:105
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 81 of file lockcmds.c.

References aclcheck_error(), ACLCHECK_OK, ereport, errcode(), errmsg(), ERROR, get_rel_relkind(), get_relkind_objtype(), GetUserId(), LockTableAclCheck(), OidIsValid, relkind, and RangeVar::relname.

Referenced by LockTableCommand().

83 {
84  LOCKMODE lockmode = *(LOCKMODE *) arg;
85  char relkind;
86  AclResult aclresult;
87 
88  if (!OidIsValid(relid))
89  return; /* doesn't exist, so no permissions check */
90  relkind = get_rel_relkind(relid);
91  if (!relkind)
92  return; /* woops, concurrently dropped; no permissions
93  * check */
94 
95  /* Currently, we only allow plain tables or views to be locked */
96  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
97  relkind != RELKIND_VIEW)
98  ereport(ERROR,
99  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
100  errmsg("\"%s\" is not a table or a view",
101  rv->relname)));
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:379
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1805
int errcode(int sqlerrcode)
Definition: elog.c:575
#define OidIsValid(objectId)
Definition: c.h:605
char relkind
Definition: pg_class.h:51
char * relname
Definition: primnodes.h:68
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3349
static AclResult LockTableAclCheck(Oid relid, LOCKMODE lockmode, Oid userid)
Definition: lockcmds.c:286
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
AclResult
Definition: acl.h:178
int errmsg(const char *fmt,...)
Definition: elog.c:797
ObjectType get_relkind_objtype(char relkind)
void * arg