Header And Logo

PostgreSQL
| The world's most advanced open source database.

rewriteRemove.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * rewriteRemove.c
00004  *    routines for removing rewrite rules
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  *
00010  * IDENTIFICATION
00011  *    src/backend/rewrite/rewriteRemove.c
00012  *
00013  *-------------------------------------------------------------------------
00014  */
00015 #include "postgres.h"
00016 
00017 #include "access/genam.h"
00018 #include "access/heapam.h"
00019 #include "access/htup_details.h"
00020 #include "access/sysattr.h"
00021 #include "catalog/dependency.h"
00022 #include "catalog/indexing.h"
00023 #include "catalog/namespace.h"
00024 #include "catalog/pg_rewrite.h"
00025 #include "miscadmin.h"
00026 #include "rewrite/rewriteRemove.h"
00027 #include "utils/acl.h"
00028 #include "utils/fmgroids.h"
00029 #include "utils/inval.h"
00030 #include "utils/lsyscache.h"
00031 #include "utils/syscache.h"
00032 #include "utils/tqual.h"
00033 
00034 /*
00035  * Guts of rule deletion.
00036  */
00037 void
00038 RemoveRewriteRuleById(Oid ruleOid)
00039 {
00040     Relation    RewriteRelation;
00041     ScanKeyData skey[1];
00042     SysScanDesc rcscan;
00043     Relation    event_relation;
00044     HeapTuple   tuple;
00045     Oid         eventRelationOid;
00046 
00047     /*
00048      * Open the pg_rewrite relation.
00049      */
00050     RewriteRelation = heap_open(RewriteRelationId, RowExclusiveLock);
00051 
00052     /*
00053      * Find the tuple for the target rule.
00054      */
00055     ScanKeyInit(&skey[0],
00056                 ObjectIdAttributeNumber,
00057                 BTEqualStrategyNumber, F_OIDEQ,
00058                 ObjectIdGetDatum(ruleOid));
00059 
00060     rcscan = systable_beginscan(RewriteRelation, RewriteOidIndexId, true,
00061                                 SnapshotNow, 1, skey);
00062 
00063     tuple = systable_getnext(rcscan);
00064 
00065     if (!HeapTupleIsValid(tuple))
00066         elog(ERROR, "could not find tuple for rule %u", ruleOid);
00067 
00068     /*
00069      * We had better grab AccessExclusiveLock to ensure that no queries are
00070      * going on that might depend on this rule.  (Note: a weaker lock would
00071      * suffice if it's not an ON SELECT rule.)
00072      */
00073     eventRelationOid = ((Form_pg_rewrite) GETSTRUCT(tuple))->ev_class;
00074     event_relation = heap_open(eventRelationOid, AccessExclusiveLock);
00075 
00076     /*
00077      * Now delete the pg_rewrite tuple for the rule
00078      */
00079     simple_heap_delete(RewriteRelation, &tuple->t_self);
00080 
00081     systable_endscan(rcscan);
00082 
00083     heap_close(RewriteRelation, RowExclusiveLock);
00084 
00085     /*
00086      * Issue shared-inval notice to force all backends (including me!) to
00087      * update relcache entries with the new rule set.
00088      */
00089     CacheInvalidateRelcache(event_relation);
00090 
00091     /* Close rel, but keep lock till commit... */
00092     heap_close(event_relation, NoLock);
00093 }