From 98e5fa73c440c010b4fb743cc8ae3e0464babf14 Mon Sep 17 00:00:00 2001 From: Denis Efimov <71937078+doefimov@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:42:35 +0300 Subject: [PATCH] Fix column encoding handling in pg_dump Allocate attencoding for the current table's columns. Sizing it by the full attribute result for each table can exhaust memory during schema-only dumps. Restore the pg_attribute_encoding join lost in the PostgreSQL 16 merge and read each encoding from the matching result row. Add a TAP case that dumps two AOCO tables with different compression levels. Refs #2330 --- src/bin/pg_dump/pg_dump.c | 12 +++++++----- src/bin/pg_dump/t/003_pg_dump_with_server.pl | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 06adfbcdb44..ffe7e1980e6 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -9556,6 +9556,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) "a.attalign,\n" "a.attislocal,\n" "pg_catalog.format_type(t.oid, a.atttypmod) AS atttypname,\n" + "pg_catalog.array_to_string(e.attoptions, ',') AS attencoding,\n" "array_to_string(a.attoptions, ', ') AS attoptions,\n" "CASE WHEN a.attcollation <> t.typcollation " "THEN a.attcollation ELSE 0 END AS attcollation,\n" @@ -9601,6 +9602,8 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) "JOIN pg_catalog.pg_attribute a ON (src.tbloid = a.attrelid) " "LEFT JOIN pg_catalog.pg_type t " "ON (a.atttypid = t.oid)\n" + "LEFT JOIN pg_catalog.pg_attribute_encoding e " + "ON (e.attrelid = a.attrelid AND e.attnum = a.attnum) " "WHERE a.attnum > 0::pg_catalog.int2\n" "ORDER BY a.attrelid, a.attnum", tbloids->data); @@ -9629,6 +9632,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) i_attfdwoptions = PQfnumber(res, "attfdwoptions"); i_attmissingval = PQfnumber(res, "attmissingval"); i_atthasdef = PQfnumber(res, "atthasdef"); + i_attencoding = PQfnumber(res, "attencoding"); /* Within the next loop, we'll accumulate OIDs of tables with defaults */ resetPQExpBuffer(tbloids); @@ -9669,9 +9673,6 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) pg_fatal("unexpected column data for table \"%s\"", tbinfo->dobj.name); - i_attencoding = PQfnumber(res, "attencoding"); - - tbinfo->attencoding = (char **) pg_malloc(ntups * sizeof(char *)); /* Save data for this table */ tbinfo->numatts = numatts; tbinfo->attnames = (char **) pg_malloc(numatts * sizeof(char *)); @@ -9693,6 +9694,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) tbinfo->notnull = (bool *) pg_malloc(numatts * sizeof(bool)); tbinfo->inhNotNull = (bool *) pg_malloc(numatts * sizeof(bool)); tbinfo->attrdefs = (AttrDefInfo **) pg_malloc(numatts * sizeof(AttrDefInfo *)); + tbinfo->attencoding = (char **) pg_malloc(numatts * sizeof(char *)); hasdefaults = false; for (int j = 0; j < numatts; j++, r++) @@ -9725,8 +9727,8 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) tbinfo->inhNotNull[j] = false; /* column storage attributes */ - if (!PQgetisnull(res, j, i_attencoding)) - tbinfo->attencoding[j] = pg_strdup(PQgetvalue(res, j, i_attencoding)); + if (!PQgetisnull(res, r, i_attencoding)) + tbinfo->attencoding[j] = pg_strdup(PQgetvalue(res, r, i_attencoding)); else tbinfo->attencoding[j] = NULL; } diff --git a/src/bin/pg_dump/t/003_pg_dump_with_server.pl b/src/bin/pg_dump/t/003_pg_dump_with_server.pl index 6c6bee4fe0c..22db25b7434 100644 --- a/src/bin/pg_dump/t/003_pg_dump_with_server.pl +++ b/src/bin/pg_dump/t/003_pg_dump_with_server.pl @@ -52,4 +52,21 @@ [ "pg_dump", '-p', $port, '-a', '--include-foreign-data=s2', 'postgres' ], "dump foreign server with no tables"); +# Dump both tables together so their column encodings must be read from +# different rows of the same result set. +$node->safe_psql( + 'postgres', q{ + CREATE TABLE aoco_first + (i int ENCODING (compresstype=zlib, compresslevel=1)) + USING ao_column DISTRIBUTED BY (i); + CREATE TABLE aoco_second + (i int ENCODING (compresstype=zlib, compresslevel=7)) + USING ao_column DISTRIBUTED BY (i); + }); + +command_like( + [ 'pg_dump', '-p', $port, '--schema-only', '--table=aoco_*', 'postgres' ], + qr/^CREATE TABLE public\.aoco_second \([^;]*\bCOLUMN i ENCODING \([^)\n]*\bcompresslevel=7\b/m, + 'schema dump preserves the second AOCO table column encoding'); + done_testing();