@@ -270,6 +270,7 @@ static void dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
270270static void dumpEncoding(Archive *AH);
271271static void dumpStdStrings(Archive *AH);
272272static void dumpSearchPath(Archive *AH);
273+ static void dumpToastCompression(Archive *AH);
273274static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
274275 PQExpBuffer upgrade_buffer,
275276 Oid pg_type_oid,
@@ -384,10 +385,10 @@ main(int argc, char **argv)
384385 {"no-comments", no_argument, &dopt.no_comments, 1},
385386 {"no-publications", no_argument, &dopt.no_publications, 1},
386387 {"no-security-labels", no_argument, &dopt.no_security_labels, 1},
387- {"no-synchronized-snapshots", no_argument, &dopt.no_synchronized_snapshots, 1},
388- {"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
389388 {"no-subscriptions", no_argument, &dopt.no_subscriptions, 1},
389+ {"no-synchronized-snapshots", no_argument, &dopt.no_synchronized_snapshots, 1},
390390 {"no-toast-compression", no_argument, &dopt.no_toast_compression, 1},
391+ {"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
391392 {"no-sync", no_argument, NULL, 7},
392393 {"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1},
393394 {"rows-per-insert", required_argument, NULL, 10},
@@ -909,10 +910,14 @@ main(int argc, char **argv)
909910 * order.
910911 */
911912
912- /* First the special ENCODING, STDSTRINGS, and SEARCHPATH entries. */
913+ /*
914+ * First the special entries for ENCODING, STDSTRINGS, SEARCHPATH and
915+ * TOASTCOMPRESSION.
916+ */
913917 dumpEncoding(fout);
914918 dumpStdStrings(fout);
915919 dumpSearchPath(fout);
920+ dumpToastCompression(fout);
916921
917922 /* The database items are always next, unless we don't want them at all */
918923 if (dopt.outputCreateDB)
@@ -1048,9 +1053,9 @@ help(const char *progname)
10481053 printf(_(" --no-publications do not dump publications\n"));
10491054 printf(_(" --no-security-labels do not dump security label assignments\n"));
10501055 printf(_(" --no-subscriptions do not dump subscriptions\n"));
1051- printf(_(" --no-toast-compression do not dump toast compression methods\n"));
10521056 printf(_(" --no-synchronized-snapshots do not use synchronized snapshots in parallel jobs\n"));
10531057 printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
1058+ printf(_(" --no-toast-compression do not dump toast compression methods\n"));
10541059 printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
10551060 printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
10561061 printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
@@ -3321,6 +3326,49 @@ dumpSearchPath(Archive *AH)
33213326 destroyPQExpBuffer(path);
33223327}
33233328
3329+ /*
3330+ * dumpToastCompression: save the dump-time default TOAST compression in the
3331+ * archive
3332+ */
3333+ static void
3334+ dumpToastCompression(Archive *AH)
3335+ {
3336+ const char *toast_compression;
3337+ PQExpBuffer qry;
3338+ PGresult *res;
3339+
3340+ if (AH->remoteVersion < 140000 || AH->dopt->no_toast_compression)
3341+ {
3342+ /* server doesn't support compression, or we don't care */
3343+ return;
3344+ }
3345+
3346+ res = ExecuteSqlQueryForSingleRow(AH, "SHOW default_toast_compression");
3347+ toast_compression = PQgetvalue(res, 0, 0);
3348+
3349+ qry = createPQExpBuffer();
3350+ appendPQExpBufferStr(qry, "SET default_toast_compression = ");
3351+ appendStringLiteralAH(qry, toast_compression, AH);
3352+ appendPQExpBufferStr(qry, ";\n");
3353+
3354+ pg_log_info("saving default_toast_compression = %s", toast_compression);
3355+
3356+ ArchiveEntry(AH, nilCatalogId, createDumpId(),
3357+ ARCHIVE_OPTS(.tag = "TOASTCOMPRESSION",
3358+ .description = "TOASTCOMPRESSION",
3359+ .section = SECTION_PRE_DATA,
3360+ .createStmt = qry->data));
3361+
3362+ /*
3363+ * Also save it in AH->default_toast_compression, in case we're doing
3364+ * plain text dump.
3365+ */
3366+ AH->default_toast_compression = pg_strdup(toast_compression);
3367+
3368+ PQclear(res);
3369+ destroyPQExpBuffer(qry);
3370+ }
3371+
33243372
33253373/*
33263374 * getBlobs:
@@ -8619,7 +8667,6 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
86198667{
86208668 DumpOptions *dopt = fout->dopt;
86218669 PQExpBuffer q = createPQExpBuffer();
8622- bool createWithCompression;
86238670
86248671 for (int i = 0; i < numTables; i++)
86258672 {
@@ -8686,6 +8733,13 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
86868733 appendPQExpBufferStr(q,
86878734 "0 AS attcollation,\n");
86888735
8736+ if (fout->remoteVersion >= 140000)
8737+ appendPQExpBuffer(q,
8738+ "a.attcompression AS attcompression,\n");
8739+ else
8740+ appendPQExpBuffer(q,
8741+ "'' AS attcompression,\n");
8742+
86898743 if (fout->remoteVersion >= 90200)
86908744 appendPQExpBufferStr(q,
86918745 "pg_catalog.array_to_string(ARRAY("
@@ -8705,15 +8759,6 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
87058759 appendPQExpBufferStr(q,
87068760 "'' AS attidentity,\n");
87078761
8708- createWithCompression = (fout->remoteVersion >= 140000);
8709-
8710- if (createWithCompression)
8711- appendPQExpBuffer(q,
8712- "a.attcompression AS attcompression,\n");
8713- else
8714- appendPQExpBuffer(q,
8715- "NULL AS attcompression,\n");
8716-
87178762 if (fout->remoteVersion >= 110000)
87188763 appendPQExpBufferStr(q,
87198764 "CASE WHEN a.atthasmissing AND NOT a.attisdropped "
@@ -8757,9 +8802,9 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
87578802 tbinfo->attislocal = (bool *) pg_malloc(ntups * sizeof(bool));
87588803 tbinfo->attoptions = (char **) pg_malloc(ntups * sizeof(char *));
87598804 tbinfo->attcollation = (Oid *) pg_malloc(ntups * sizeof(Oid));
8805+ tbinfo->attcompression = (char *) pg_malloc(ntups * sizeof(char));
87608806 tbinfo->attfdwoptions = (char **) pg_malloc(ntups * sizeof(char *));
87618807 tbinfo->attmissingval = (char **) pg_malloc(ntups * sizeof(char *));
8762- tbinfo->attcompression = (char *) pg_malloc(ntups * sizeof(char *));
87638808 tbinfo->notnull = (bool *) pg_malloc(ntups * sizeof(bool));
87648809 tbinfo->inhNotNull = (bool *) pg_malloc(ntups * sizeof(bool));
87658810 tbinfo->attrdefs = (AttrDefInfo **) pg_malloc(ntups * sizeof(AttrDefInfo *));
@@ -8786,9 +8831,9 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
87868831 tbinfo->notnull[j] = (PQgetvalue(res, j, PQfnumber(res, "attnotnull"))[0] == 't');
87878832 tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attoptions")));
87888833 tbinfo->attcollation[j] = atooid(PQgetvalue(res, j, PQfnumber(res, "attcollation")));
8834+ tbinfo->attcompression[j] = *(PQgetvalue(res, j, PQfnumber(res, "attcompression")));
87898835 tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attfdwoptions")));
87908836 tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attmissingval")));
8791- tbinfo->attcompression[j] = *(PQgetvalue(res, j, PQfnumber(res, "attcompression")));
87928837 tbinfo->attrdefs[j] = NULL; /* fix below */
87938838 if (PQgetvalue(res, j, PQfnumber(res, "atthasdef"))[0] == 't')
87948839 hasdefaults = true;
@@ -15905,31 +15950,6 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
1590515950 tbinfo->atttypnames[j]);
1590615951 }
1590715952
15908- /*
15909- * Attribute compression
15910- */
15911- if (!dopt->no_toast_compression &&
15912- tbinfo->attcompression != NULL)
15913- {
15914- char *cmname;
15915-
15916- switch (tbinfo->attcompression[j])
15917- {
15918- case 'p':
15919- cmname = "pglz";
15920- break;
15921- case 'l':
15922- cmname = "lz4";
15923- break;
15924- default:
15925- cmname = NULL;
15926- break;
15927- }
15928-
15929- if (cmname != NULL)
15930- appendPQExpBuffer(q, " COMPRESSION %s", cmname);
15931- }
15932-
1593315953 if (print_default)
1593415954 {
1593515955 if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_STORED)
@@ -16348,7 +16368,36 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
1634816368 qualrelname,
1634916369 fmtId(tbinfo->attnames[j]),
1635016370 tbinfo->attfdwoptions[j]);
16351- }
16371+
16372+ /*
16373+ * Dump per-column compression, if different from default.
16374+ */
16375+ if (!dopt->no_toast_compression)
16376+ {
16377+ const char *cmname;
16378+
16379+ switch (tbinfo->attcompression[j])
16380+ {
16381+ case 'p':
16382+ cmname = "pglz";
16383+ break;
16384+ case 'l':
16385+ cmname = "lz4";
16386+ break;
16387+ default:
16388+ cmname = NULL;
16389+ break;
16390+ }
16391+
16392+ if (cmname != NULL &&
16393+ (fout->default_toast_compression == NULL ||
16394+ strcmp(cmname, fout->default_toast_compression) != 0))
16395+ appendPQExpBuffer(q, "ALTER %sTABLE ONLY %s ALTER COLUMN %s SET COMPRESSION %s;\n",
16396+ foreign, qualrelname,
16397+ fmtId(tbinfo->attnames[j]),
16398+ cmname);
16399+ }
16400+ } /* end loop over columns */
1635216401
1635316402 if (ftoptions)
1635416403 free(ftoptions);
0 commit comments