LCOV - code coverage report
Current view: top level - src/backend/rewrite - rewriteRemove.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 16 17 94.1 %
Date: 2020-06-01 08:06:25 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * rewriteRemove.c
       4             :  *    routines for removing rewrite rules
       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/rewrite/rewriteRemove.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include "access/genam.h"
      18             : #include "access/htup_details.h"
      19             : #include "access/sysattr.h"
      20             : #include "access/table.h"
      21             : #include "catalog/catalog.h"
      22             : #include "catalog/dependency.h"
      23             : #include "catalog/indexing.h"
      24             : #include "catalog/namespace.h"
      25             : #include "catalog/pg_rewrite.h"
      26             : #include "miscadmin.h"
      27             : #include "rewrite/rewriteRemove.h"
      28             : #include "utils/acl.h"
      29             : #include "utils/fmgroids.h"
      30             : #include "utils/inval.h"
      31             : #include "utils/lsyscache.h"
      32             : #include "utils/rel.h"
      33             : #include "utils/syscache.h"
      34             : 
      35             : /*
      36             :  * Guts of rule deletion.
      37             :  */
      38             : void
      39        1484 : RemoveRewriteRuleById(Oid ruleOid)
      40             : {
      41             :     Relation    RewriteRelation;
      42             :     ScanKeyData skey[1];
      43             :     SysScanDesc rcscan;
      44             :     Relation    event_relation;
      45             :     HeapTuple   tuple;
      46             :     Oid         eventRelationOid;
      47             : 
      48             :     /*
      49             :      * Open the pg_rewrite relation.
      50             :      */
      51        1484 :     RewriteRelation = table_open(RewriteRelationId, RowExclusiveLock);
      52             : 
      53             :     /*
      54             :      * Find the tuple for the target rule.
      55             :      */
      56        1484 :     ScanKeyInit(&skey[0],
      57             :                 Anum_pg_rewrite_oid,
      58             :                 BTEqualStrategyNumber, F_OIDEQ,
      59             :                 ObjectIdGetDatum(ruleOid));
      60             : 
      61        1484 :     rcscan = systable_beginscan(RewriteRelation, RewriteOidIndexId, true,
      62             :                                 NULL, 1, skey);
      63             : 
      64        1484 :     tuple = systable_getnext(rcscan);
      65             : 
      66        1484 :     if (!HeapTupleIsValid(tuple))
      67           0 :         elog(ERROR, "could not find tuple for rule %u", ruleOid);
      68             : 
      69             :     /*
      70             :      * We had better grab AccessExclusiveLock to ensure that no queries are
      71             :      * going on that might depend on this rule.  (Note: a weaker lock would
      72             :      * suffice if it's not an ON SELECT rule.)
      73             :      */
      74        1484 :     eventRelationOid = ((Form_pg_rewrite) GETSTRUCT(tuple))->ev_class;
      75        1484 :     event_relation = table_open(eventRelationOid, AccessExclusiveLock);
      76             : 
      77        1484 :     if (!allowSystemTableMods && IsSystemRelation(event_relation))
      78           2 :         ereport(ERROR,
      79             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
      80             :                  errmsg("permission denied: \"%s\" is a system catalog",
      81             :                         RelationGetRelationName(event_relation))));
      82             : 
      83             :     /*
      84             :      * Now delete the pg_rewrite tuple for the rule
      85             :      */
      86        1482 :     CatalogTupleDelete(RewriteRelation, &tuple->t_self);
      87             : 
      88        1482 :     systable_endscan(rcscan);
      89             : 
      90        1482 :     table_close(RewriteRelation, RowExclusiveLock);
      91             : 
      92             :     /*
      93             :      * Issue shared-inval notice to force all backends (including me!) to
      94             :      * update relcache entries with the new rule set.
      95             :      */
      96        1482 :     CacheInvalidateRelcache(event_relation);
      97             : 
      98             :     /* Close rel, but keep lock till commit... */
      99        1482 :     table_close(event_relation, NoLock);
     100        1482 : }

Generated by: LCOV version 1.13