@@ -270,6 +270,7 @@ static void dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
270
270
static void dumpEncoding(Archive *AH);
271
271
static void dumpStdStrings(Archive *AH);
272
272
static void dumpSearchPath(Archive *AH);
273
+ static void dumpToastCompression(Archive *AH);
273
274
static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
274
275
PQExpBuffer upgrade_buffer,
275
276
Oid pg_type_oid,
@@ -384,10 +385,10 @@ main(int argc, char **argv)
384
385
{"no-comments", no_argument, &dopt.no_comments, 1},
385
386
{"no-publications", no_argument, &dopt.no_publications, 1},
386
387
{"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},
389
388
{"no-subscriptions", no_argument, &dopt.no_subscriptions, 1},
389
+ {"no-synchronized-snapshots", no_argument, &dopt.no_synchronized_snapshots, 1},
390
390
{"no-toast-compression", no_argument, &dopt.no_toast_compression, 1},
391
+ {"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
391
392
{"no-sync", no_argument, NULL, 7},
392
393
{"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1},
393
394
{"rows-per-insert", required_argument, NULL, 10},
@@ -909,10 +910,14 @@ main(int argc, char **argv)
909
910
* order.
910
911
*/
911
912
912
- /* First the special ENCODING, STDSTRINGS, and SEARCHPATH entries. */
913
+ /*
914
+ * First the special entries for ENCODING, STDSTRINGS, SEARCHPATH and
915
+ * TOASTCOMPRESSION.
916
+ */
913
917
dumpEncoding(fout);
914
918
dumpStdStrings(fout);
915
919
dumpSearchPath(fout);
920
+ dumpToastCompression(fout);
916
921
917
922
/* The database items are always next, unless we don't want them at all */
918
923
if (dopt.outputCreateDB)
@@ -1048,9 +1053,9 @@ help(const char *progname)
1048
1053
printf(_(" --no-publications do not dump publications\n"));
1049
1054
printf(_(" --no-security-labels do not dump security label assignments\n"));
1050
1055
printf(_(" --no-subscriptions do not dump subscriptions\n"));
1051
- printf(_(" --no-toast-compression do not dump toast compression methods\n"));
1052
1056
printf(_(" --no-synchronized-snapshots do not use synchronized snapshots in parallel jobs\n"));
1053
1057
printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
1058
+ printf(_(" --no-toast-compression do not dump toast compression methods\n"));
1054
1059
printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
1055
1060
printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
1056
1061
printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
@@ -3321,6 +3326,49 @@ dumpSearchPath(Archive *AH)
3321
3326
destroyPQExpBuffer(path);
3322
3327
}
3323
3328
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
+
3324
3372
3325
3373
/*
3326
3374
* getBlobs:
@@ -8619,7 +8667,6 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
8619
8667
{
8620
8668
DumpOptions *dopt = fout->dopt;
8621
8669
PQExpBuffer q = createPQExpBuffer();
8622
- bool createWithCompression;
8623
8670
8624
8671
for (int i = 0; i < numTables; i++)
8625
8672
{
@@ -8686,6 +8733,13 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
8686
8733
appendPQExpBufferStr(q,
8687
8734
"0 AS attcollation,\n");
8688
8735
8736
+ if (fout->remoteVersion >= 140000)
8737
+ appendPQExpBuffer(q,
8738
+ "a.attcompression AS attcompression,\n");
8739
+ else
8740
+ appendPQExpBuffer(q,
8741
+ "'' AS attcompression,\n");
8742
+
8689
8743
if (fout->remoteVersion >= 90200)
8690
8744
appendPQExpBufferStr(q,
8691
8745
"pg_catalog.array_to_string(ARRAY("
@@ -8705,15 +8759,6 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
8705
8759
appendPQExpBufferStr(q,
8706
8760
"'' AS attidentity,\n");
8707
8761
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
-
8717
8762
if (fout->remoteVersion >= 110000)
8718
8763
appendPQExpBufferStr(q,
8719
8764
"CASE WHEN a.atthasmissing AND NOT a.attisdropped "
@@ -8757,9 +8802,9 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
8757
8802
tbinfo->attislocal = (bool *) pg_malloc(ntups * sizeof(bool));
8758
8803
tbinfo->attoptions = (char **) pg_malloc(ntups * sizeof(char *));
8759
8804
tbinfo->attcollation = (Oid *) pg_malloc(ntups * sizeof(Oid));
8805
+ tbinfo->attcompression = (char *) pg_malloc(ntups * sizeof(char));
8760
8806
tbinfo->attfdwoptions = (char **) pg_malloc(ntups * sizeof(char *));
8761
8807
tbinfo->attmissingval = (char **) pg_malloc(ntups * sizeof(char *));
8762
- tbinfo->attcompression = (char *) pg_malloc(ntups * sizeof(char *));
8763
8808
tbinfo->notnull = (bool *) pg_malloc(ntups * sizeof(bool));
8764
8809
tbinfo->inhNotNull = (bool *) pg_malloc(ntups * sizeof(bool));
8765
8810
tbinfo->attrdefs = (AttrDefInfo **) pg_malloc(ntups * sizeof(AttrDefInfo *));
@@ -8786,9 +8831,9 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
8786
8831
tbinfo->notnull[j] = (PQgetvalue(res, j, PQfnumber(res, "attnotnull"))[0] == 't');
8787
8832
tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attoptions")));
8788
8833
tbinfo->attcollation[j] = atooid(PQgetvalue(res, j, PQfnumber(res, "attcollation")));
8834
+ tbinfo->attcompression[j] = *(PQgetvalue(res, j, PQfnumber(res, "attcompression")));
8789
8835
tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attfdwoptions")));
8790
8836
tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, j, PQfnumber(res, "attmissingval")));
8791
- tbinfo->attcompression[j] = *(PQgetvalue(res, j, PQfnumber(res, "attcompression")));
8792
8837
tbinfo->attrdefs[j] = NULL; /* fix below */
8793
8838
if (PQgetvalue(res, j, PQfnumber(res, "atthasdef"))[0] == 't')
8794
8839
hasdefaults = true;
@@ -15905,31 +15950,6 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
15905
15950
tbinfo->atttypnames[j]);
15906
15951
}
15907
15952
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
-
15933
15953
if (print_default)
15934
15954
{
15935
15955
if (tbinfo->attgenerated[j] == ATTRIBUTE_GENERATED_STORED)
@@ -16348,7 +16368,36 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo)
16348
16368
qualrelname,
16349
16369
fmtId(tbinfo->attnames[j]),
16350
16370
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 */
16352
16401
16353
16402
if (ftoptions)
16354
16403
free(ftoptions);
0 commit comments