8000 pg_upgrade: check for inconsistencies in not-null constraints w/inher… · postgres/postgres@ea3386c · GitHub
[go: up one dir, main page]

Skip to content

Commit ea3386c

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 b61ddca commit ea3386c

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

src/bin/pg_upgrade/check.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
2525
static void check_for_user_defined_postfix_ops(ClusterInfo *cluster);
2626
static void check_for_incompatible_polymorphics(ClusterInfo *cluster);
2727
static void check_for_tables_with_oids(ClusterInfo *cluster);
28+
static void check_for_not_null_inheritance(ClusterInfo *cluster);
2829
static void check_for_composite_data_type_usage(ClusterInfo *cluster);
2930
static void check_for_reg_data_type_usage(ClusterInfo *cluster);
3031
static void check_for_removed_data_type_usage(ClusterInfo *cluster,
@@ -149,6 +150,13 @@ check_and_dump_old_cluster(bool live_check)
149150
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
150151
check_for_tables_with_oids(&old_cluster);
151152

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

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

12051290
/*
12061291
* check_for_composite_data_type_usage()

0 commit comments

Comments
 (0)
0