1 /*-------------------------------------------------------------------------
2  *
3  * rewriteRemove.c
4  *	  routines for removing rewrite rules
5  *
6  * Portions Copyright (c) 1996-2021, 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
RemoveRewriteRuleById(Oid ruleOid)39 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 	RewriteRelation = table_open(RewriteRelationId, RowExclusiveLock);
52 
53 	/*
54 	 * Find the tuple for the target rule.
55 	 */
56 	ScanKeyInit(&skey[0],
57 				Anum_pg_rewrite_oid,
58 				BTEqualStrategyNumber, F_OIDEQ,
59 				ObjectIdGetDatum(ruleOid));
60 
61 	rcscan = systable_beginscan(RewriteRelation, RewriteOidIndexId, true,
62 								NULL, 1, skey);
63 
64 	tuple = systable_getnext(rcscan);
65 
66 	if (!HeapTupleIsValid(tuple))
67 		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 	eventRelationOid = ((Form_pg_rewrite) GETSTRUCT(tuple))->ev_class;
75 	event_relation = table_open(eventRelationOid, AccessExclusiveLock);
76 
77 	if (!allowSystemTableMods && IsSystemRelation(event_relation))
78 		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 	CatalogTupleDelete(RewriteRelation, &tuple->t_self);
87 
88 	systable_endscan(rcscan);
89 
90 	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 	CacheInvalidateRelcache(event_relation);
97 
98 	/* Close rel, but keep lock till commit... */
99 	table_close(event_relation, NoLock);
100 }
101