@@ -269,6 +269,7 @@ static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
269
269
const char *prefix, Archive *fout);
270
270
static char *get_synchronized_snapshot(Archive *fout);
271
271
static void setupDumpWorker(Archive *AHX);
272
+ static TableInfo *getRootTableInfo(TableInfo *tbinfo);
272
273
273
274
274
275
int
@@ -345,6 +346,7 @@ main(int argc, char **argv)
345
346
{"lock-wait-timeout", required_argument, NULL, 2},
346
347
{"no-tablespaces", no_argument, &dopt.outputNoTablespaces, 1},
347
348
{"quote-all-identifiers", no_argument, "e_all_identifiers, 1},
349
+ {"load-via-partition-root", no_argument, &dopt.load_via_partition_root, 1},
348
350
{"role", required_argument, NULL, 3},
349
351
{"section", required_argument, NULL, 5},
350
352
{"serializable-deferrable", no_argument, &dopt.serializable_deferrable, 1},
@@ -959,6 +961,7 @@ help(const char *progname)
959
961
printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
960
962
printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
961
963
printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
964
+ printf(_(" --load-via-partition-root load partitions via the root table\n"));
962
965
printf(_(" --section=SECTION dump named section (pre-data, data, or post-data)\n"));
963
966
printf(_(" --serializable-deferrable wait until the dump can run without anomalies\n"));
964
967
printf(_(" --snapshot=SNAPSHOT use given snapshot for the dump\n"));
@@ -1902,8 +1905,32 @@ dumpTableData_insert(Archive *fout, void *dcontext)
1902
1905
if (insertStmt == NULL)
1903
1906
{
1904
1907
insertStmt = createPQExpBuffer();
1908
+
1909
+ /*
1910
+ * When load-via-partition-root is set, get the root table
1911
+ * name for the partition table, so that we can reload data
1912
+ * through the root table.
1913
+ */
1914
+ if (dopt->load_via_partition_root && tbinfo->ispartition)
1915
+ {
1916
+ TableInfo *parentTbinfo;
1917
+
1918
+ parentTbinfo = getRootTableInfo(tbinfo);
1919
+
1920
+ /*
1921
+ * When we loading data through the root, we will qualify
1922
+ * the table name. This is needed because earlier
1923
+ * search_path will be set for the partition table.
1924
+ */
1925
+ classname = (char *) fmtQualifiedId(fout->remoteVersion,
1926
+ parentTbinfo->dobj.namespace->dobj.name,
1927
+ parentTbinfo->dobj.name);
1928
+ }
1929
+ else
1930
+ classname = fmtId(tbinfo->dobj.name);
1931
+
1905
1932
appendPQExpBuffer(insertStmt, "INSERT INTO %s ",
1906
- fmtId( classname) );
1933
+ classname);
1907
1934
1908
1935
/* corner case for zero-column table */
1909
1936
if (nfields == 0)
@@ -2025,6 +2052,27 @@ dumpTableData_insert(Archive *fout, void *dcontext)
2025
2052
return 1;
2026
2053
}
2027
2054
2055
+ /*
2056
+ * getRootTableInfo:
2057
+ * get the root TableInfo for the given partition table.
2058
+ */
2059
+ static TableInfo *
2060
+ getRootTableInfo(TableInfo *tbinfo)
2061
+ {
2062
+ TableInfo *parentTbinfo;
2063
+
2064
+ Assert(tbinfo->ispartition);
2065
+ Assert(tbinfo->numParents == 1);
2066
+
2067
+ parentTbinfo = tbinfo->parents[0];
2068
+ while (parentTbinfo->ispartition)
2069
+ {
2070
+ Assert(parentTbinfo->numParents == 1);
2071
+ parentTbinfo = parentTbinfo->parents[0];
2072
+ }
2073
+
2074
+ return parentTbinfo;
2075
+ }
2028
2076
2029
2077
/*
2030
2078
* dumpTableData -
@@ -2041,14 +2089,38 @@ dumpTableData(Archive *fout, TableDataInfo *tdinfo)
2041
2089
PQExpBuffer clistBuf = createPQExpBuffer();
2042
2090
DataDumperPtr dumpFn;
2043
2091
char *copyStmt;
2092
+ const char *copyFrom;
2044
2093
2045
2094
if (!dopt->dump_inserts)
2046
2095
{
2047
2096
/* Dump/restore using COPY */
2048
2097
dumpFn = dumpTableData_copy;
2098
+
2099
+ /*
2100
+ * When load-via-partition-root is set, get the root table name for
2101
+ * the partition table, so that we can reload data through the root
2102
+ * table.
2103
+ */
2104
+ if (dopt->load_via_partition_root && tbinfo->ispartition)
2105
+ {
2106
+ TableInfo *parentTbinfo;
2107
+
2108
+ parentTbinfo = getRootTableInfo(tbinfo);
2109
+
2110
+ /*
2111
+ * When we load data through the root, we will qualify the table
2112
+ * name, because search_path is set for the partition.
2113
+ */
2114
+ copyFrom = fmtQualifiedId(fout->remoteVersion,
2115
+ parentTbinfo->dobj.namespace->dobj.name,
2116
+ parentTbinfo->dobj.name);
2117
+ }
2118
+ else
2119
+ copyFrom = fmtId(tbinfo->dobj.name);
2120
+
2049
2121
/* must use 2 steps here 'cause fmtId is nonreentrant */
2050
2122
appendPQExpBuffer(copyBuf, "COPY %s ",
2051
- fmtId(tbinfo->dobj.name) );
2123
+ copyFrom );
2052
2124
appendPQExpBuffer(copyBuf, "%s %sFROM stdin;\n",
2053
2125
fmtCopyColumnList(tbinfo, clistBuf),
2054
2126
(tdinfo->oids && tbinfo->hasoids) ? "WITH OIDS " : "");
0 commit comments