@@ -8103,6 +8103,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
81038103 int i_attoptions;
81048104 int i_attcollation;
81058105 int i_attfdwoptions;
8106+ int i_attmissingval;
81068107 PGresult *res;
81078108 int ntups;
81088109 bool hasdefaults;
@@ -8132,7 +8133,34 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
81328133
81338134 resetPQExpBuffer(q);
81348135
8135- if (fout->remoteVersion >= 100000)
8136+ if (fout->remoteVersion >= 110000)
8137+ {
8138+ /* atthasmissing and attmissingval are new in 11 */
8139+ appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
8140+ "a.attstattarget, a.attstorage, t.typstorage, "
8141+ "a.attnotnull, a.atthasdef, a.attisdropped, "
8142+ "a.attlen, a.attalign, a.attislocal, "
8143+ "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
8144+ "array_to_string(a.attoptions, ', ') AS attoptions, "
8145+ "CASE WHEN a.attcollation <> t.typcollation "
8146+ "THEN a.attcollation ELSE 0 END AS attcollation, "
8147+ "a.attidentity, "
8148+ "pg_catalog.array_to_string(ARRAY("
8149+ "SELECT pg_catalog.quote_ident(option_name) || "
8150+ "' ' || pg_catalog.quote_literal(option_value) "
8151+ "FROM pg_catalog.pg_options_to_table(attfdwoptions) "
8152+ "ORDER BY option_name"
8153+ "), E',\n ') AS attfdwoptions ,"
8154+ "CASE WHEN a.atthasmissing AND NOT a.attisdropped "
8155+ "THEN a.attmissingval ELSE null END AS attmissingval "
8156+ "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
8157+ "ON a.atttypid = t.oid "
8158+ "WHERE a.attrelid = '%u'::pg_catalog.oid "
8159+ "AND a.attnum > 0::pg_catalog.int2 "
8160+ "ORDER BY a.attnum",
8161+ tbinfo->dobj.catId.oid);
8162+ }
8163+ else if (fout->remoteVersion >= 100000)
81368164 {
81378165 /*
81388166 * attidentity is new in version 10.
@@ -8151,7 +8179,8 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
81518179 "' ' || pg_catalog.quote_literal(option_value) "
81528180 "FROM pg_catalog.pg_options_to_table(attfdwoptions) "
81538181 "ORDER BY option_name"
8154- "), E',\n ') AS attfdwoptions "
8182+ "), E',\n ') AS attfdwoptions ,"
8183+ "NULL as attmissingval "
81558184 "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
81568185 "ON a.atttypid = t.oid "
81578186 "WHERE a.attrelid = '%u'::pg_catalog.oid "
@@ -8177,7 +8206,8 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
81778206 "' ' || pg_catalog.quote_literal(option_value) "
81788207 "FROM pg_catalog.pg_options_to_table(attfdwoptions) "
81798208 "ORDER BY option_name"
8180- "), E',\n ') AS attfdwoptions "
8209+ "), E',\n ') AS attfdwoptions, "
8210+ "NULL as attmissingval "
81818211 "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
81828212 "ON a.atttypid = t.oid "
81838213 "WHERE a.attrelid = '%u'::pg_catalog.oid "
@@ -8201,7 +8231,8 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
82018231 "array_to_string(a.attoptions, ', ') AS attoptions, "
82028232 "CASE WHEN a.attcollation <> t.typcollation "
82038233 "THEN a.attcollation ELSE 0 END AS attcollation, "
8204- "NULL AS attfdwoptions "
8234+ "NULL AS attfdwoptions, "
8235+ "NULL as attmissingval "
82058236 "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
82068237 "ON a.atttypid = t.oid "
82078238 "WHERE a.attrelid = '%u'::pg_catalog.oid "
@@ -8219,7 +8250,8 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
82198250 "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
82208251 "array_to_string(a.attoptions, ', ') AS attoptions, "
82218252 "0 AS attcollation, "
8222- "NULL AS attfdwoptions "
8253+ "NULL AS attfdwoptions, "
8254+ "NULL as attmissingval "
82238255 "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
82248256 "ON a.atttypid = t.oid "
82258257 "WHERE a.attrelid = '%u'::pg_catalog.oid "
@@ -8236,7 +8268,8 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
82368268 "a.attlen, a.attalign, a.attislocal, "
82378269 "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
82388270 "'' AS attoptions, 0 AS attcollation, "
8239- "NULL AS attfdwoptions "
8271+ "NULL AS attfdwoptions, "
8272+ "NULL as attmissingval "
82408273 "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
82418274 "ON a.atttypid = t.oid "
82428275 "WHERE a.attrelid = '%u'::pg_catalog.oid "
@@ -8266,6 +8299,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
82668299 i_attoptions = PQfnumber(res, "attoptions");
82678300 i_attcollation = PQfnumber(res, "attcollation");
82688301 i_attfdwoptions = PQfnumber(res, "attfdwoptions");
8302+ i_attmissingval = PQfnumber(res, "attmissingval");
82698303
82708304 tbinfo->numatts = ntups;
82718305 tbinfo->attnames = (char **) pg_malloc(ntups * sizeof(char *));
@@ -8282,6 +8316,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
82828316 tbinfo->attoptions = (char **) pg_malloc(ntups * sizeof(char *));
82838317 tbinfo->attcollation = (Oid *) pg_malloc(ntups * sizeof(Oid));
82848318 tbinfo->attfdwoptions = (char **) pg_malloc(ntups * sizeof(char *));
8319+ tbinfo->attmissingval = (char **) pg_malloc(ntups * sizeof(char *));
82858320 tbinfo->notnull = (bool *) pg_malloc(ntups * sizeof(bool));
82868321 tbinfo->inhNotNull = (bool *) pg_malloc(ntups * sizeof(bool));
82878322 tbinfo->attrdefs = (AttrDefInfo **) pg_malloc(ntups * sizeof(AttrDefInfo *));
@@ -8309,6 +8344,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
83098344 tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, j, i_attoptions));
83108345 tbinfo->attcollation[j] = atooid(PQgetvalue(res, j, i_attcollation));
83118346 tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, j, i_attfdwoptions));
8347+ tbinfo->attmissingval[j] = pg_strdup(PQgetvalue(res, j, i_attmissingval));
83128348 tbinfo->attrdefs[j] = NULL; /* fix below */
83138349 if (PQgetvalue(res, j, i_atthasdef)[0] == 't')
83148350 hasdefaults = true;
@@ -15658,6 +15694,29 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1565815694 else
1565915695 appendPQExpBufferStr(q, ";\n");
1566015696
15697+ /*
15698+ * in binary upgrade mode, update the catalog with any missing values
15699+ * that might be present.
15700+ */
15701+ if (dopt->binary_upgrade)
15702+ {
15703+ for (j = 0; j < tbinfo->numatts; j++)
15704+ {
15705+ if (tbinfo->attmissingval[j][0] != '\0')
15706+ {
15707+ appendPQExpBufferStr(q, "\n-- set missing value.\n");
15708+ appendPQExpBufferStr(q,
15709+ "SELECT pg_catalog.binary_upgrade_set_missing_value(");
15710+ appendStringLiteralAH(q, qualrelname, fout);
15711+ appendPQExpBufferStr(q, "::pg_catalog.regclass,");
15712+ appendStringLiteralAH(q, tbinfo->attnames[j], fout);
15713+ appendPQExpBufferStr(q, ",");
15714+ appendStringLiteralAH(q, tbinfo->attmissingval[j], fout);
15715+ appendPQExpBufferStr(q, ");\n\n");
15716+ }
15717+ }
15718+ }
15719+
1566115720 /*
1566215721 * To create binary-compatible heap files, we have to ensure the same
1566315722 * physical column order, including dropped columns, as in the
0 commit comments