Doc fix for INSERT ... (DEFAULT, ...)
authorBruce Momjian
Wed, 24 Apr 2002 02:38:58 +0000 (02:38 +0000)
committerBruce Momjian
Wed, 24 Apr 2002 02:38:58 +0000 (02:38 +0000)
Appears I forgot to update the docs earlier.

Rod Taylor

src/backend/commands/tablecmds.c
src/bin/pg_dump/pg_dump.c

index c554bba4be31fa453c85d8f9db655273552041f1..952496fc1df9986bc169763c4dd6e3b55370bcee 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.7 2002/04/22 21:56:06 tgl Exp $
+ *   $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.8 2002/04/24 02:38:58 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -620,8 +620,14 @@ AlterTableAlterColumnDefault(Oid myrelid,
 
    rel = heap_open(myrelid, AccessExclusiveLock);
 
-   if (rel->rd_rel->relkind != RELKIND_RELATION)
-       elog(ERROR, "ALTER TABLE: relation \"%s\" is not a table",
+   /*
+    * We allow defaults on views so that INSERT into a view can have
+    * default-ish behavior.  This works because the rewriter substitutes
+    * default values into INSERTs before it expands rules.
+    */
+   if (rel->rd_rel->relkind != RELKIND_RELATION &&
+       rel->rd_rel->relkind != RELKIND_VIEW)
+       elog(ERROR, "ALTER TABLE: relation \"%s\" is not a table or view",
             RelationGetRelationName(rel));
 
    if (!allowSystemTableMods
index 9763fd54c817cb3f07eed477cc90dcf7e6b0f862..8eea64ca68abb7b2ae5b781627432f6fdf0db38d 100644 (file)
@@ -22,7 +22,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.251 2002/04/21 05:21:17 petere Exp $
+ *   $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.252 2002/04/24 02:38:58 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -4368,6 +4368,20 @@ dumpTables(Archive *fout, TableInfo *tblinfo, int numTables,
                objoid = tblinfo[i].viewoid;
                appendPQExpBuffer(delq, "DROP VIEW %s;\n", fmtId(tblinfo[i].relname, force_quotes));
                appendPQExpBuffer(q, "CREATE VIEW %s as %s\n", fmtId(tblinfo[i].relname, force_quotes), tblinfo[i].viewdef);
+
+               /*
+                * Views can have default values -- however, they must be
+                * specified in an ALTER TABLE command after the view has
+                * been created, not in the view definition itself.
+                */
+               for (j = 0; j < tblinfo[i].numatts; j++)
+               {
+                   if (tblinfo[i].adef_expr[j] != NULL && tblinfo[i].inhAttrDef[j] == 0)
+                       appendPQExpBuffer(q, "ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;\n",
+                                         tblinfo[i].relname, tblinfo[i].attnames[j],
+                                         tblinfo[i].adef_expr[j]);
+               }
+
                commentDeps = malloc(sizeof(char *) * 2);
                (*commentDeps)[0] = strdup(objoid);
                (*commentDeps)[1] = NULL;       /* end of list */