Add pg_dump support for per-column FDW options.
authorShigeru Hanada <hanada@metrosystems.co.jp>
Wed, 15 Jun 2011 01:05:33 +0000 (10:05 +0900)
committerShigeru Hanada <shigeru.hanada@gmail.com>
Mon, 20 Jun 2011 04:32:04 +0000 (13:32 +0900)
src/bin/pg_dump/pg_dump.c
src/bin/pg_dump/pg_dump.h

index 9e69b0fc5241e0ceedb304a9f62b9f56095f76da..04ca7f6cb2621608681e2b5369ed7b3533a48e0d 100644 (file)
@@ -5556,6 +5556,7 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
    int         i_attislocal;
    int         i_attoptions;
    int         i_attcollation;
+   int         i_attfdwoptions;
    PGresult   *res;
    int         ntups;
    bool        hasdefaults;
@@ -5593,7 +5594,31 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
 
        resetPQExpBuffer(q);
 
-       if (g_fout->remoteVersion >= 90100)
+       if (g_fout->remoteVersion >= 90200)
+       {
+           /*
+            * attfdwoptions is new in 9.2.
+            */
+           appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
+                             "a.attstattarget, a.attstorage, t.typstorage, "
+                             "a.attnotnull, a.atthasdef, a.attisdropped, "
+                             "a.attlen, a.attalign, a.attislocal, "
+                 "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
+                       "array_to_string(a.attoptions, ', ') AS attoptions, "
+                             "CASE WHEN a.attcollation <> t.typcollation "
+                           "THEN a.attcollation ELSE 0 END AS attcollation, "
+                 "array_to_string(ARRAY("
+                 "  SELECT option_name || ' ' || quote_literal(option_value) "
+                 "  FROM pg_options_to_table(attfdwoptions)), ', ') "
+                 " AS attfdwoptions "
+            "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
+                             "ON a.atttypid = t.oid "
+                             "WHERE a.attrelid = '%u'::pg_catalog.oid "
+                             "AND a.attnum > 0::pg_catalog.int2 "
+                             "ORDER BY a.attrelid, a.attnum",
+                             tbinfo->dobj.catId.oid);
+       }
+       else if (g_fout->remoteVersion >= 90100)
        {
            /*
             * attcollation is new in 9.1.  Since we only want to dump COLLATE
@@ -5616,6 +5641,7 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
                              "ORDER BY a.attrelid, a.attnum",
                              tbinfo->dobj.catId.oid);
        }
+
        else if (g_fout->remoteVersion >= 90000)
        {
            /* attoptions is new in 9.0 */
@@ -5708,6 +5734,7 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
        i_attislocal = PQfnumber(res, "attislocal");
        i_attoptions = PQfnumber(res, "attoptions");
        i_attcollation = PQfnumber(res, "attcollation");
+       i_attfdwoptions = PQfnumber(res, "attfdwoptions");
 
        tbinfo->numatts = ntups;
        tbinfo->attnames = (char **) malloc(ntups * sizeof(char *));
@@ -5724,6 +5751,7 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
        tbinfo->attrdefs = (AttrDefInfo **) malloc(ntups * sizeof(AttrDefInfo *));
        tbinfo->attoptions = (char **) malloc(ntups * sizeof(char *));
        tbinfo->attcollation = (Oid *) malloc(ntups * sizeof(Oid));
+       tbinfo->attfdwoptions = (char **) malloc(ntups * sizeof(char *));
        tbinfo->inhAttrs = (bool *) malloc(ntups * sizeof(bool));
        tbinfo->inhAttrDef = (bool *) malloc(ntups * sizeof(bool));
        tbinfo->inhNotNull = (bool *) malloc(ntups * sizeof(bool));
@@ -5750,6 +5778,7 @@ getTableAttrs(TableInfo *tblinfo, int numTables)
            tbinfo->notnull[j] = (PQgetvalue(res, j, i_attnotnull)[0] == 't');
            tbinfo->attoptions[j] = strdup(PQgetvalue(res, j, i_attoptions));
            tbinfo->attcollation[j] = atooid(PQgetvalue(res, j, i_attcollation));
+           tbinfo->attfdwoptions[j] = strdup(PQgetvalue(res, j, i_attfdwoptions));
            tbinfo->attrdefs[j] = NULL; /* fix below */
            if (PQgetvalue(res, j, i_atthasdef)[0] == 't')
                hasdefaults = true;
@@ -12451,6 +12480,21 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
                appendPQExpBuffer(q, "SET (%s);\n",
                                  tbinfo->attoptions[j]);
            }
+
+           /*
+            * Dump per-column generic options.
+            */
+           if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
+               tbinfo->attfdwoptions[j] &&
+               tbinfo->attfdwoptions[j][0] != '\0')
+           {
+               appendPQExpBuffer(q, "ALTER FOREIGN TABLE %s ",
+                                 fmtId(tbinfo->dobj.name));
+               appendPQExpBuffer(q, "ALTER COLUMN %s ",
+                                 fmtId(tbinfo->attnames[j]));
+               appendPQExpBuffer(q, "OPTIONS (%s);\n",
+                                 tbinfo->attfdwoptions[j]);
+           }
        }
    }
 
index c95614b16fa6e1499177bf44f0d5300a2dbe4cb8..0a65401150c1f342a5b849f807634f8c288b3055 100644 (file)
@@ -275,6 +275,7 @@ typedef struct _tableInfo
    bool       *attislocal;     /* true if attr has local definition */
    char      **attoptions;     /* per-attribute options */
    Oid        *attcollation;   /* per-attribute collation selection */
+   char      **attfdwoptions;  /* per-attribute generic options */
 
    /*
     * Note: we need to store per-attribute notnull, default, and constraint