LCOV - code coverage report
Current view: top level - src/backend/commands - lockcmds.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 83 95 87.4 %
Date: 2020-06-01 09:07:10 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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
      41        4184 : LockTableCommand(LockStmt *lockstmt)
      42             : {
      43             :     ListCell   *p;
      44             : 
      45             :     /*
      46             :      * Iterate over the list and process the named relations one at a time
      47             :      */
      48        8302 :     foreach(p, lockstmt->relations)
      49             :     {
      50        4184 :         RangeVar   *rv = (RangeVar *) lfirst(p);
      51        4184 :         bool        recurse = rv->inh;
      52             :         Oid         reloid;
      53             : 
      54        4184 :         reloid = RangeVarGetRelidExtended(rv, lockstmt->mode,
      55        4184 :                                           lockstmt->nowait ? RVR_NOWAIT : 0,
      56             :                                           RangeVarCallbackForLockTable,
      57        4184 :                                           (void *) &lockstmt->mode);
      58             : 
      59        4126 :         if (get_rel_relkind(reloid) == RELKIND_VIEW)
      60          32 :             LockViewRecurse(reloid, lockstmt->mode, lockstmt->nowait, NIL);
      61        4094 :         else if (recurse)
      62        4090 :             LockTableRecurse(reloid, lockstmt->mode, lockstmt->nowait);
      63             :     }
      64        4118 : }
      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        4212 : RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid,
      72             :                              void *arg)
      73             : {
      74        4212 :     LOCKMODE    lockmode = *(LOCKMODE *) arg;
      75             :     char        relkind;
      76             :     char        relpersistence;
      77             :     AclResult   aclresult;
      78             : 
      79        4212 :     if (!OidIsValid(relid))
      80           0 :         return;                 /* doesn't exist, so no permissions check */
      81        4212 :     relkind = get_rel_relkind(relid);
      82        4212 :     if (!relkind)
      83           0 :         return;                 /* woops, concurrently dropped; no permissions
      84             :                                  * check */
      85             : 
      86             :     /* Currently, we only allow plain tables or views to be locked */
      87        4212 :     if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
      88             :         relkind != RELKIND_VIEW)
      89           0 :         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        4212 :     relpersistence = get_rel_persistence(relid);
      99        4212 :     if (relpersistence == RELPERSISTENCE_TEMP)
     100           4 :         MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPNAMESPACE;
     101             : 
     102             :     /* Check permissions. */
     103        4212 :     aclresult = LockTableAclCheck(relid, lockmode, GetUserId());
     104        4212 :     if (aclresult != ACLCHECK_OK)
     105          40 :         aclcheck_error(aclresult, get_relkind_objtype(get_rel_relkind(relid)), rv->relname);
     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        4130 : LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait)
     117             : {
     118             :     List       *children;
     119             :     ListCell   *lc;
     120             : 
     121        4130 :     children = find_all_inheritors(reloid, NoLock, NULL);
     122             : 
     123        8618 :     foreach(lc, children)
     124             :     {
     125        4488 :         Oid         childreloid = lfirst_oid(lc);
     126             : 
     127             :         /* Parent already locked. */
     128        4488 :         if (childreloid == reloid)
     129        4130 :             continue;
     130             : 
     131         358 :         if (!nowait)
     132         356 :             LockRelationOid(childreloid, lockmode);
     133           2 :         else if (!ConditionalLockRelationOid(childreloid, lockmode))
     134             :         {
     135             :             /* try to throw error by name; relation could be deleted... */
     136           0 :             char       *relname = get_rel_name(childreloid);
     137             : 
     138           0 :             if (!relname)
     139           0 :                 continue;       /* child concurrently dropped, just skip it */
     140           0 :             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         358 :         if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(childreloid)))
     151             :         {
     152             :             /* Release useless lock */
     153           0 :             UnlockRelationOid(childreloid, lockmode);
     154           0 :             continue;
     155             :         }
     156             :     }
     157        4130 : }
     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 */
     173             : } LockViewRecurse_context;
     174             : 
     175             : static bool
     176         916 : LockViewRecurse_walker(Node *node, LockViewRecurse_context *context)
     177             : {
     178         916 :     if (node == NULL)
     179         560 :         return false;
     180             : 
     181         356 :     if (IsA(node, Query))
     182             :     {
     183          60 :         Query      *query = (Query *) node;
     184             :         ListCell   *rtable;
     185             : 
     186         204 :         foreach(rtable, query->rtable)
     187             :         {
     188         164 :             RangeTblEntry *rte = lfirst(rtable);
     189             :             AclResult   aclresult;
     190             : 
     191         164 :             Oid         relid = rte->relid;
     192         164 :             char        relkind = rte->relkind;
     193         164 :             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         164 :             if (relid == context->viewoid &&
     200          96 :                 (strcmp(rte->eref->aliasname, "old") == 0 ||
     201          48 :                  strcmp(rte->eref->aliasname, "new") == 0))
     202          96 :                 continue;
     203             : 
     204             :             /* Currently, we only allow plain tables or views to be locked. */
     205          68 :             if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE &&
     206             :                 relkind != RELKIND_VIEW)
     207           4 :                 continue;
     208             : 
     209             :             /* Check infinite recursion in the view definition. */
     210          64 :             if (list_member_oid(context->ancestor_views, relid))
     211           8 :                 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          56 :             aclresult = LockTableAclCheck(relid, context->lockmode, context->viewowner);
     218          56 :             if (aclresult != ACLCHECK_OK)
     219           0 :                 aclcheck_error(aclresult, get_relkind_objtype(relkind), relname);
     220             : 
     221             :             /* We have enough rights to lock the relation; do so. */
     222          56 :             if (!context->nowait)
     223          56 :                 LockRelationOid(relid, context->lockmode);
     224           0 :             else if (!ConditionalLockRelationOid(relid, context->lockmode))
     225           0 :                 ereport(ERROR,
     226             :                         (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
     227             :                          errmsg("could not obtain lock on relation \"%s\"",
     228             :                                 relname)));
     229             : 
     230          56 :             if (relkind == RELKIND_VIEW)
     231          16 :                 LockViewRecurse(relid, context->lockmode, context->nowait, context->ancestor_views);
     232          40 :             else if (rte->inh)
     233          40 :                 LockTableRecurse(relid, context->lockmode, context->nowait);
     234             :         }
     235             : 
     236          40 :         return query_tree_walker(query,
     237             :                                  LockViewRecurse_walker,
     238             :                                  context,
     239             :                                  QTW_IGNORE_JOINALIASES);
     240             :     }
     241             : 
     242         296 :     return expression_tree_walker(node,
     243             :                                   LockViewRecurse_walker,
     244             :                                   context);
     245             : }
     246             : 
     247             : static void
     248          48 : LockViewRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, List *ancestor_views)
     249             : {
     250             :     LockViewRecurse_context context;
     251             : 
     252             :     Relation    view;
     253             :     Query      *viewquery;
     254             : 
     255          48 :     view = table_open(reloid, NoLock);
     256          48 :     viewquery = get_view_query(view);
     257             : 
     258          48 :     context.lockmode = lockmode;
     259          48 :     context.nowait = nowait;
     260          48 :     context.viewowner = view->rd_rel->relowner;
     261          48 :     context.viewoid = reloid;
     262          48 :     context.ancestor_views = lappend_oid(ancestor_views, reloid);
     263             : 
     264          48 :     LockViewRecurse_walker((Node *) viewquery, &context);
     265             : 
     266          28 :     (void) list_delete_last(context.ancestor_views);
     267             : 
     268          28 :     table_close(view, NoLock);
     269          28 : }
     270             : 
     271             : /*
     272             :  * Check whether the current user is permitted to lock this relation.
     273             :  */
     274             : static AclResult
     275        4268 : LockTableAclCheck(Oid reloid, LOCKMODE lockmode, Oid userid)
     276             : {
     277             :     AclResult   aclresult;
     278             :     AclMode     aclmask;
     279             : 
     280             :     /* Verify adequate privilege */
     281        4268 :     if (lockmode == AccessShareLock)
     282        3878 :         aclmask = ACL_SELECT;
     283         390 :     else if (lockmode == RowExclusiveLock)
     284          28 :         aclmask = ACL_INSERT | ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE;
     285             :     else
     286         362 :         aclmask = ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE;
     287             : 
     288        4268 :     aclresult = pg_class_aclcheck(reloid, userid, aclmask);
     289             : 
     290        4268 :     return aclresult;
     291             : }

Generated by: LCOV version 1.13