From c273e8b13c30832954490f88b64c704d5c841c8a Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Tue, 17 Apr 2001 00:32:58 +0000
Subject: [PATCH] Add some defenses to guard against case where a rule refers
 to a table or view that's been dropped and then recreated with the same name
 (but, perhaps, different columns).  Eventually we'd like to support this but
 for now all we can do is fail cleanly, rather than possibly coredumping if we
 proceed using the obsolete rule.

---
 src/backend/rewrite/rewriteHandler.c | 32 +++++++++++++++++++++++-----
 1 file changed, 27 insertions(+), 5 deletions(-)

diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c
index 889351067db..9bc112de200 100644
--- a/src/backend/rewrite/rewriteHandler.c
+++ b/src/backend/rewrite/rewriteHandler.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.91 2001/03/22 06:16:16 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.92 2001/04/17 00:32:58 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -486,16 +486,25 @@ fireRIRrules(Query *parsetree)
 
 		rel = heap_openr(rte->relname, lockmode);
 
+		/*
+		 * Check to see if relation's OID matches the RTE.  If not, the RTE
+		 * actually refers to an older relation that had the same name.
+		 * Eventually we might want to reparse the referencing rule, but
+		 * for now all we can do is punt.
+		 */
+		if (RelationGetRelid(rel) != rte->relid)
+			elog(ERROR, "Relation \"%s\" with OID %u no longer exists",
+				 rte->relname, rte->relid);
+
+		/*
+		 * Collect the RIR rules that we must apply
+		 */
 		rules = rel->rd_rules;
 		if (rules == NULL)
 		{
 			heap_close(rel, NoLock);
 			continue;
 		}
-
-		/*
-		 * Collect the RIR rules that we must apply
-		 */
 		locks = NIL;
 		for (i = 0; i < rules->numLocks; i++)
 		{
@@ -776,6 +785,19 @@ RewriteQuery(Query *parsetree, bool *instead_flag, List **qual_products)
 	 */
 	rt_entry_relation = heap_openr(rt_entry->relname, RowExclusiveLock);
 
+	/*
+	 * Check to see if relation's OID matches the RTE.  If not, the RTE
+	 * actually refers to an older relation that had the same name.
+	 * Eventually we might want to reparse the referencing rule, but
+	 * for now all we can do is punt.
+	 */
+	if (RelationGetRelid(rt_entry_relation) != rt_entry->relid)
+		elog(ERROR, "Relation \"%s\" with OID %u no longer exists",
+			 rt_entry->relname, rt_entry->relid);
+
+	/*
+	 * Collect and apply the appropriate rules.
+	 */
 	rt_entry_locks = rt_entry_relation->rd_rules;
 
 	if (rt_entry_locks != NULL)
-- 
GitLab