Skip to content

Commit 0f49586

Browse files
author
Álvaro Herrera
committed
pg_upgrade: check for inconsistencies in not-null constraints w/inheritance
With tables defined like this, CREATE TABLE ip (id int PRIMARY KEY); CREATE TABLE ic (id int) INHERITS (ip); ALTER TABLE ic ALTER id DROP NOT NULL; pg_upgrade fails during the schema restore phase due to this error: ERROR: column "id" in child table must be marked NOT NULL This can only be fixed by marking the child column as NOT NULL before the upgrade, which could take an arbitrary amount of time (because ic's data must be scanned). Have pg_upgrade's check mode warn if that condition is found, so that users know what to adjust before running the upgrade for real. Author: Ali Akbar <the.apaan@gmail.com> Reviewed-by: Justin Pryzby <pryzby@telsasoft.com> Backpatch-through: 13 Discussion: https://postgr.es/m/CACQjQLoMsE+1pyLe98pi0KvPG2jQQ94LWJ+PTiLgVRK4B=i_jg@mail.gmail.com
1 parent dcbbd43 commit 0f49586

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/bin/pg_upgrade/check.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
2626
static void check_for_user_defined_postfix_ops(ClusterInfo *cluster);
2727
static void check_for_incompatible_polymorphics(ClusterInfo *cluster);
2828
static void check_for_tables_with_oids(ClusterInfo *cluster);
29+
static void check_for_not_null_inheritance(ClusterInfo *cluster);
2930
static void check_for_composite_data_type_usage(ClusterInfo *cluster);
3031
static void check_for_reg_data_type_usage(ClusterInfo *cluster);
3132
static void check_for_removed_data_type_usage(ClusterInfo *cluster,
@@ -150,6 +151,13 @@ check_and_dump_old_cluster(bool live_check)
150151
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
151152
check_for_tables_with_oids(&old_cluster);
152153

154+
/*
155+
* Pre-PG 18 allowed child tables to omit not-null constraints that their
156+
* parents columns have, but schema restore fails for them. Verify there
157+
* are none.
158+
*/
159+
check_for_not_null_inheritance(&old_cluster);
160+
153161
/*
154162
* PG 12 changed the 'sql_identifier' type storage to be based on name,
155163
* not varchar, which breaks on-disk format for existing data. So we need
@@ -1227,6 +1235,84 @@ check_for_tables_with_oids(ClusterInfo *cluster)
12271235
check_ok();
12281236
}
12291237

1238+
/*
1239+
* check_for_not_null_inheritance()
1240+
*
1241+
* An attempt to create child tables lacking not-null constraints that are
1242+
* present in their parents errors out. This can no longer occur since 18,
1243+
* but previously there were various ways for that to happen. Check that
1244+
* the cluster to be upgraded doesn't have any of those problems.
1245+
*/
1246+
static void
1247+
check_for_not_null_inheritance(ClusterInfo *cluster)
1248+
{
1249+
FILE *script = NULL;
1250+
char output_path[MAXPGPATH];
1251+
int ntup;
1252+
1253+
prep_status("Checking for not-null constraint inconsistencies");
1254+
1255+
snprintf(output_path, sizeof(output_path), "%s/%s",
1256+
log_opts.basedir,
1257+
"not_null_inconsistent_columns.txt");
1258+
for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
1259+
{
1260+
PGresult *res;
1261+
bool db_used = false;
1262+
int i_nspname,
1263+
i_relname,
1264+
i_attname;
1265+
DbInfo *active_db = &old_cluster.dbarr.dbs[dbnum];
1266+
PGconn *conn = connectToServer(&old_cluster, active_db->db_name);
1267+
1268+
res = executeQueryOrDie(conn,
1269+
"SELECT cc.relnamespace::pg_catalog.regnamespace AS nspname, "
1270+
" cc.relname, ac.attname "
1271+
"FROM pg_catalog.pg_inherits i, pg_catalog.pg_attribute ac, "
1272+
" pg_catalog.pg_attribute ap, pg_catalog.pg_class cc "
1273+
"WHERE cc.oid = ac.attrelid AND i.inhrelid = ac.attrelid "
1274+
" AND i.inhparent = ap.attrelid AND ac.attname = ap.attname "
1275+
" AND ap.attnum > 0 and ap.attnotnull AND NOT ac.attnotnull");
1276+
1277+
ntup = PQntuples(res);
1278+
i_nspname = PQfnumber(res, "nspname");
1279+
i_relname = PQfnumber(res, "relname");
1280+
i_attname = PQfnumber(res, "attname");
1281+
for (int i = 0; i < ntup; i++)
1282+
{
1283+
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1284+
pg_fatal("could not open file \"%s\": %m", output_path);
1285+
if (!db_used)
1286+
{
1287+
fprintf(script, "In database: %s\n", active_db->db_name);
1288+
db_used = true;
1289+
}
1290+
1291+
fprintf(script, " %s.%s.%s\n",
1292+
PQgetvalue(res, i, i_nspname),
1293+
PQgetvalue(res, i, i_relname),
1294+
PQgetvalue(res, i, i_attname));
1295+
}
1296+
1297+
PQclear(res);
1298+
PQfinish(conn);
1299+
}
1300+
1301+
if (script)
1302+
{
1303+
fclose(script);
1304+
pg_log(PG_REPORT, "fatal");
1305+
pg_fatal("Your installation contains inconsistent NOT NULL constraints.\n"
1306+
"If the parent column(s) are NOT NULL, then the child column must\n"
1307+
"also be marked NOT NULL, or the upgrade will fail.\n"
1308+
"You can fix this by running\n"
1309+
" ALTER TABLE tablename ALTER column SET NOT NULL;\n"
1310+
"on each column listed in the file:\n"
1311+
" %s\n\n", output_path);
1312+
}
1313+
else
1314+
check_ok();
1315+
}
12301316

12311317
/*
12321318
* check_for_composite_data_type_usage()

0 commit comments

Comments
 (0)