From 825206f2b06e8474172c4c310d540f985da6ea7b Mon Sep 17 00:00:00 2001 From: Shigeru Hanada Date: Wed, 15 Jun 2011 10:05:33 +0900 Subject: [PATCH] Add pg_dump support for per-column FDW options. --- src/bin/pg_dump/pg_dump.c | 46 ++++++++++++++++++++++++++++++++++++++- src/bin/pg_dump/pg_dump.h | 1 + 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 9e69b0fc52..04ca7f6cb2 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -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]); + } } } diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h index c95614b16f..0a65401150 100644 --- a/src/bin/pg_dump/pg_dump.h +++ b/src/bin/pg_dump/pg_dump.h @@ -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 -- 2.39.5