]> granicus.if.org Git - postgresql/commitdiff
Disallow converting a table to a view if it has triggers, indexes, or
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 17 Sep 2003 17:19:17 +0000 (17:19 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 17 Sep 2003 17:19:17 +0000 (17:19 +0000)
child tables --- all cases that will trip various sanity checks elsewhere
in the system, as well as cases that should not occur in the only intended
use of this feature, namely coping with ancient pg_dump representation
of views.  Per bug report from Chris Pizzi.

src/backend/rewrite/rewriteDefine.c

index 6a0dd04568d842181844b914392d832d498caf8e..8abdd1bcb58d9beb514972da0e119f997ad585fd 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.86 2003/08/04 02:40:03 momjian Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteDefine.c,v 1.87 2003/09/17 17:19:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -392,7 +392,8 @@ DefineQueryRewrite(RuleStmt *stmt)
                 * Are we converting a relation to a view?
                 *
                 * If so, check that the relation is empty because the storage for
-                * the relation is going to be deleted.
+                * the relation is going to be deleted.  Also insist that the rel
+                * not have any triggers, indexes, or child tables.
                 */
                if (event_relation->rd_rel->relkind != RELKIND_VIEW)
                {
@@ -402,10 +403,29 @@ DefineQueryRewrite(RuleStmt *stmt)
                        if (heap_getnext(scanDesc, ForwardScanDirection) != NULL)
                                ereport(ERROR,
                                          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
-                               errmsg("cannot convert non-empty table \"%s\" to a view",
+                               errmsg("could not convert table \"%s\" to a view because it is not empty",
                                           event_obj->relname)));
                        heap_endscan(scanDesc);
 
+                       if (event_relation->rd_rel->reltriggers != 0)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                                errmsg("could not convert table \"%s\" to a view because it has triggers",
+                                                               event_obj->relname),
+                                                errhint("In particular, the table may not be involved in any foreign key relationships.")));
+
+                       if (event_relation->rd_rel->relhasindex)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                                errmsg("could not convert table \"%s\" to a view because it has indexes",
+                                                               event_obj->relname)));
+
+                       if (event_relation->rd_rel->relhassubclass)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                                errmsg("could not convert table \"%s\" to a view because it has child tables",
+                                                               event_obj->relname)));
+
                        RelisBecomingView = true;
                }
        }
@@ -456,6 +476,8 @@ DefineQueryRewrite(RuleStmt *stmt)
         * IF the relation is becoming a view, delete the storage files
         * associated with it.  NB: we had better have AccessExclusiveLock to
         * do this ...
+        *
+        * XXX what about getting rid of its TOAST table?  For now, we don't.
         */
        if (RelisBecomingView)
                smgrunlink(DEFAULT_SMGR, event_relation);