PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, 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 "catalog/namespace.h"
18 #include "catalog/pg_inherits_fn.h"
19 #include "commands/lockcmds.h"
20 #include "miscadmin.h"
21 #include "parser/parse_clause.h"
22 #include "storage/lmgr.h"
23 #include "utils/acl.h"
24 #include "utils/lsyscache.h"
25 #include "utils/syscache.h"
26 
27 static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait);
28 static AclResult LockTableAclCheck(Oid relid, LOCKMODE lockmode);
29 static void RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid,
30  Oid oldrelid, void *arg);
31 
32 /*
33  * LOCK TABLE
34  */
35 void
37 {
38  ListCell *p;
39 
40  /*---------
41  * During recovery we only accept these variations:
42  * LOCK TABLE foo IN ACCESS SHARE MODE
43  * LOCK TABLE foo IN ROW SHARE MODE
44  * LOCK TABLE foo IN ROW EXCLUSIVE MODE
45  * This test must match the restrictions defined in LockAcquireExtended()
46  *---------
47  */
48  if (lockstmt->mode > RowExclusiveLock)
49  PreventCommandDuringRecovery("LOCK TABLE");
50 
51  /*
52  * Iterate over the list and process the named relations one at a time
53  */
54  foreach(p, lockstmt->relations)
55  {
56  RangeVar *rv = (RangeVar *) lfirst(p);
57  bool recurse = rv->inh;
58  Oid reloid;
59 
60  reloid = RangeVarGetRelidExtended(rv, lockstmt->mode, false,
61  lockstmt->nowait,
63  (void *) &lockstmt->mode);
64 
65  if (recurse)
66  LockTableRecurse(reloid, lockstmt->mode, lockstmt->nowait);
67  }
68 }
69 
70 /*
71  * Before acquiring a table lock on the named table, check whether we have
72  * permission to do so.
73  */
74 static void
75 RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid,
76  void *arg)
77 {
78  LOCKMODE lockmode = *(LOCKMODE *) arg;
79  char relkind;
80  AclResult aclresult;
81 
82  if (!OidIsValid(relid))
83  return; /* doesn't exist, so no permissions check */
84  relkind = get_rel_relkind(relid);
85  if (!relkind)
86  return; /* woops, concurrently dropped; no permissions
87  * check */
88 
89  /* Currently, we only allow plain tables to be locked */
90  if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE)
91  ereport(ERROR,
92  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
93  errmsg("\"%s\" is not a table",
94  rv->relname)));
95 
96  /* Check permissions. */
97  aclresult = LockTableAclCheck(relid, lockmode);
98  if (aclresult != ACLCHECK_OK)
99  aclcheck_error(aclresult, ACL_KIND_CLASS, rv->relname);
100 }
101 
102 /*
103  * Apply LOCK TABLE recursively over an inheritance tree
104  *
105  * We use find_inheritance_children not find_all_inheritors to avoid taking
106  * locks far in advance of checking privileges. This means we'll visit
107  * multiply-inheriting children more than once, but that's no problem.
108  */
109 static void
110 LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait)
111 {
112  List *children;
113  ListCell *lc;
114 
115  children = find_inheritance_children(reloid, NoLock);
116 
117  foreach(lc, children)
118  {
119  Oid childreloid = lfirst_oid(lc);
120  AclResult aclresult;
121 
122  /* Check permissions before acquiring the lock. */
123  aclresult = LockTableAclCheck(childreloid, lockmode);
124  if (aclresult != ACLCHECK_OK)
125  {
126  char *relname = get_rel_name(childreloid);
127 
128  if (!relname)
129  continue; /* child concurrently dropped, just skip it */
130  aclcheck_error(aclresult, ACL_KIND_CLASS, relname);
131  }
132 
133  /* We have enough rights to lock the relation; do so. */
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  LockTableRecurse(childreloid, lockmode, nowait);
161  }
162 }
163 
164 /*
165  * Check whether the current user is permitted to lock this relation.
166  */
167 static AclResult
168 LockTableAclCheck(Oid reloid, LOCKMODE lockmode)
169 {
170  AclResult aclresult;
172 
173  /* Verify adequate privilege */
174  if (lockmode == AccessShareLock)
175  aclmask = ACL_SELECT;
176  else if (lockmode == RowExclusiveLock)
178  else
179  aclmask = ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE;
180 
181  aclresult = pg_class_aclcheck(reloid, GetUserId(), aclmask);
182 
183  return aclresult;
184 }
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:138
bool nowait
Definition: parsenodes.h:3160
int LOCKMODE
Definition: lockdefs.h:26
Oid GetUserId(void)
Definition: miscinit.c:283
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:182
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, bool missing_ok, bool nowait, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:218
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1801
AclMode aclmask(const Acl *acl, Oid roleid, Oid ownerId, AclMode mask, AclMaskHow how)
Definition: acl.c:1304
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ACL_DELETE
Definition: parsenodes.h:75
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
char * relname
Definition: primnodes.h:68
uint32 AclMode
Definition: parsenodes.h:70
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:174
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
void PreventCommandDuringRecovery(const char *cmdname)
Definition: utility.c:273
#define NoLock
Definition: lockdefs.h:34
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:122
bool inh
Definition: primnodes.h:69
#define ACL_UPDATE
Definition: parsenodes.h:74
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
static void LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait)
Definition: lockcmds.c:110
AclResult
Definition: acl.h:170
#define ACL_SELECT
Definition: parsenodes.h:73
List * relations
Definition: parsenodes.h:3158
List * find_inheritance_children(Oid parentrelId, LOCKMODE lockmode)
Definition: pg_inherits.c:57
#define lfirst(lc)
Definition: pg_list.h:106
#define ACL_INSERT
Definition: parsenodes.h:72
static void RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid, void *arg)
Definition: lockcmds.c:75
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4422
void LockTableCommand(LockStmt *lockstmt)
Definition: lockcmds.c:36
int errmsg(const char *fmt,...)
Definition: elog.c:797
static AclResult LockTableAclCheck(Oid relid, LOCKMODE lockmode)
Definition: lockcmds.c:168
void * arg
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:105
#define RELKIND_RELATION
Definition: pg_class.h:160
Definition: pg_list.h:45
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1726
#define lfirst_oid(lc)
Definition: pg_list.h:108
#define ACL_TRUNCATE
Definition: parsenodes.h:76