PostgreSQL Source Code  git master
lockcmds.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * lockcmds.c
4  * LOCK command support code
5  *
6  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/commands/lockcmds.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/table.h"
18 #include "access/xact.h"
19 #include "catalog/namespace.h"
20 #include "catalog/pg_inherits.h"
21 #include "commands/lockcmds.h"
22 #include "miscadmin.h"
23 #include "nodes/nodeFuncs.h"
24 #include "parser/parse_clause.h"
25 #include "rewrite/rewriteHandler.h"
26 #include "storage/lmgr.h"
27 #include "utils/acl.h"
28 #include "utils/lsyscache.h"
29 #include "utils/syscache.h"
30 
31 static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait);
32 static AclResult LockTableAclCheck(Oid relid, LOCKMODE lockmode, Oid userid);
33 static void RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid,
34  Oid oldrelid, void *arg);
35 static void LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views);
36 
37 /*
38  * LOCK TABLE
39  */
40 void
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 }
65 
66 /*
67  * Before acquiring a table lock on the named table, check whether we have
68  * permission to do so.
69  */
70 static void
71 RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid,
72  void *arg)
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("\"%s\" is not a table or view",
92  rv->relname)));
93 
94  /*
95  * Make note if a temporary relation has been accessed in this
96  * transaction.
97  */
98  relpersistence = get_rel_persistence(relid);
99  if (relpersistence == RELPERSISTENCE_TEMP)
101 
102  /* Check permissions. */
103  aclresult = LockTableAclCheck(relid, lockmode, GetUserId());
104  if (aclresult != ACLCHECK_OK)
106 }
107 
108 /*
109  * Apply LOCK TABLE recursively over an inheritance tree
110  *
111  * This doesn't check permission to perform LOCK TABLE on the child tables,
112  * because getting here means that the user has permission to lock the
113  * parent which is enough.
114  */
115 static void
116 LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait)
117 {
118  List *children;
119  ListCell *lc;
120 
121  children = find_all_inheritors(reloid, NoLock, NULL);
122 
123  foreach(lc, children)
124  {
125  Oid childreloid = lfirst_oid(lc);
126 
127  /* Parent already locked. */
128  if (childreloid == reloid)
129  continue;
130 
131  if (!nowait)
132  LockRelationOid(childreloid, lockmode);
133  else if (!ConditionalLockRelationOid(childreloid, lockmode))
134  {
135  /* try to throw error by name; relation could be deleted... */
136  char *relname = get_rel_name(childreloid);
137 
138  if (!relname)
139  continue; /* child concurrently dropped, just skip it */
140  ereport(ERROR,
141  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
142  errmsg("could not obtain lock on relation \"%s\"",
143  relname)));
144  }
145 
146  /*
147  * Even if we got the lock, child might have been concurrently
148  * dropped. If so, we can skip it.
149  */
150  if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(childreloid)))
151  {
152  /* Release useless lock */
153  UnlockRelationOid(childreloid, lockmode);
154  continue;
155  }
156  }
157 }
158 
159 /*
160  * Apply LOCK TABLE recursively over a view
161  *
162  * All tables and views appearing in the view definition query are locked
163  * recursively with the same lock mode.
164  */
165 
166 typedef struct
167 {
168  LOCKMODE lockmode; /* lock mode to use */
169  bool nowait; /* no wait mode */
170  Oid viewowner; /* view owner for checking the privilege */
171  Oid viewoid; /* OID of the view to be locked */
172  List *ancestor_views; /* OIDs of ancestor views */
174 
175 static bool
177 {
178  if (node == NULL)
179  return false;
180 
181  if (IsA(node, Query))
182  {
183  Query *query = (Query *) node;
184  ListCell *rtable;
185 
186  foreach(rtable, query->rtable)
187  {
188  RangeTblEntry *rte = lfirst(rtable);
189  AclResult aclresult;
190 
191  Oid relid = rte->relid;
192  char relkind = rte->relkind;
193  char *relname = get_rel_name(relid);
194 
195  /*
196  * The OLD and NEW placeholder entries in the view's rtable are
197  * skipped.
198  */
199  if (relid == context->viewoid &&
200  (strcmp(rte->eref->aliasname, "old") == 0 ||
201  strcmp(rte->eref->aliasname, "new") == 0))
202  continue;
203 
204  /* Currently, we only allow plain tables or views to be locked. */
205  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
206  relkind != RELKIND_VIEW)
207  continue;
208 
209  /* Check infinite recursion in the view definition. */
210  if (list_member_oid(context->ancestor_views, relid))
211  ereport(ERROR,
212  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
213  errmsg("infinite recursion detected in rules for relation \"%s\"",
214  get_rel_name(relid))));
215 
216  /* Check permissions with the view owner's privilege. */
217  aclresult = LockTableAclCheck(relid, context->lockmode, context->viewowner);
218  if (aclresult != ACLCHECK_OK)
219  aclcheck_error(aclresult, get_relkind_objtype(relkind), relname);
220 
221  /* We have enough rights to lock the relation; do so. */
222  if (!context->nowait)
223  LockRelationOid(relid, context->lockmode);
224  else if (!ConditionalLockRelationOid(relid, context->lockmode))
225  ereport(ERROR,
226  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
227  errmsg("could not obtain lock on relation \"%s\"",
228  relname)));
229 
230  if (relkind == RELKIND_VIEW)
231  LockViewRecurse(relid, context->lockmode, context->nowait, context->ancestor_views);
232  else if (rte->inh)
233  LockTableRecurse(relid, context->lockmode, context->nowait);
234  }
235 
236  return query_tree_walker(query,
238  context,
240  }
241 
242  return expression_tree_walker(node,
244  context);
245 }
246 
247 static void
248 LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views)
249 {
250  LockViewRecurse_context context;
251 
252  Relation view;
253  Query *viewquery;
254 
255  view = table_open(reloid, NoLock);
256  viewquery = get_view_query(view);
257 
258  context.lockmode = lockmode;
259  context.nowait = nowait;
260  context.viewowner = view->rd_rel->relowner;
261  context.viewoid = reloid;
262  context.ancestor_views = lappend_oid(ancestor_views, reloid);
263 
264  LockViewRecurse_walker((Node *) viewquery, &context);
265 
266  (void) list_delete_last(context.ancestor_views);
267 
268  table_close(view, NoLock);
269 }
270 
271 /*
272  * Check whether the current user is permitted to lock this relation.
273  */
274 static AclResult
275 LockTableAclCheck(Oid reloid, LOCKMODE lockmode, Oid userid)
276 {
277  AclResult aclresult;
279 
280  /* Verify adequate privilege */
281  if (lockmode == AccessShareLock)
282  aclmask = ACL_SELECT;
283  else if (lockmode == RowExclusiveLock)
285  else
286  aclmask = ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE;
287 
288  aclresult = pg_class_aclcheck(reloid, userid, aclmask);
289 
290  return aclresult;
291 }
#define NIL
Definition: pg_list.h:65
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
bool nowait
Definition: parsenodes.h:3334
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static bool LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
Definition: lockcmds.c:176
int LOCKMODE
Definition: lockdefs.h:26
Oid GetUserId(void)
Definition: miscinit.c:476
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:199
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1915
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1313
#define AccessShareLock
Definition: lockdefs.h:36
Definition: nodes.h:528
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ACL_DELETE
Definition: parsenodes.h:77
static void LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views)
Definition: lockcmds.c:248
Form_pg_class rd_rel
Definition: rel.h:109
NameData relname
Definition: pg_class.h:38
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
#define OidIsValid(objectId)
Definition: c.h:652
char * relname
Definition: primnodes.h:68
uint32 AclMode
Definition: parsenodes.h:72
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:275
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:183
List * rtable
Definition: parsenodes.h:137
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
List * list_delete_last(List *list)
Definition: list.c:877
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:236
bool inh
Definition: primnodes.h:69
int MyXactFlags
Definition: xact.c:132
#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE
Definition: xact.h:102
#define ACL_UPDATE
Definition: parsenodes.h:76
static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait)
Definition: lockcmds.c:116
AclResult
Definition: acl.h:177
#define ACL_SELECT
Definition: parsenodes.h:75
List * relations
Definition: parsenodes.h:3332
#define ereport(elevel,...)
Definition: elog.h:144
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
#define ACL_INSERT
Definition: parsenodes.h:74
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1888
static void RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid, void *arg)
Definition: lockcmds.c:71
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4563
char get_rel_persistence(Oid relid)
Definition: lsyscache.c:1990
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:165
void LockTableCommand(LockStmt *lockstmt)
Definition: lockcmds.c:41
int errmsg(const char *fmt,...)
Definition: elog.c:821
Query * get_view_query(Relation view)
ObjectType get_relkind_objtype(char relkind)
void * arg
Alias * eref
Definition: parsenodes.h:1116
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1840
#define lfirst_oid(lc)
Definition: pg_list.h:171
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23
#define ACL_TRUNCATE
Definition: parsenodes.h:78