Skip to content

Commit b8b2e60

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 ae20c10 commit b8b2e60

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
@@ -24,6 +24,7 @@ static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
2424
static void check_for_user_defined_postfix_ops(ClusterInfo *cluster);
2525
static void check_for_incompatible_polymorphics(ClusterInfo *cluster);
2626
static void check_for_tables_with_oids(ClusterInfo *cluster);
27+
static void check_for_not_null_inheritance(ClusterInfo *cluster);
2728
static void check_for_pg_role_prefix(ClusterInfo *cluster);
2829
static void check_for_new_tablespace_dir(void);
2930
static void check_for_user_defined_encoding_conversions(ClusterInfo *cluster);
@@ -648,6 +649,13 @@ check_and_dump_old_cluster(bool live_check)
648649
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
649650
check_for_tables_with_oids(&old_cluster);
650651

652+
/*
653+
* Pre-PG 18 allowed child tables to omit not-null constraints that their
654+
* parents columns have, but schema restore fails for them. Verify there
655+
* are none.
656+
*/
657+
check_for_not_null_inheritance(&old_cluster);
658+
651659
/*
652660
* Pre-PG 10 allowed tables with 'unknown' type columns and non WAL logged
653661
* hash indexes
@@ -1576,6 +1584,84 @@ check_for_tables_with_oids(ClusterInfo *cluster)
15761584
check_ok();
15771585
}
15781586

1587+
/*
1588+
* check_for_not_null_inheritance()
1589+
*
1590+
* An attempt to create child tables lacking not-null constraints that are
1591+
* present in their parents errors out. This can no longer occur since 18,
1592+
* but previously there were various ways for that to happen. Check that
1593+
* the cluster to be upgraded doesn't have any of those problems.
1594+
*/
1595+
static void
1596+
check_for_not_null_inheritance(ClusterInfo *cluster)
1597+
{
1598+
FILE *script = NULL;
1599+
char output_path[MAXPGPATH];
1600+
int ntup;
1601+
1602+
prep_status("Checking for not-null constraint inconsistencies");
1603+
1604+
snprintf(output_path, sizeof(output_path), "%s/%s",
1605+
log_opts.basedir,
1606+
"not_null_inconsistent_columns.txt");
1607+
for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
1608+
{
1609+
PGresult *res;
1610+
bool db_used = false;
1611+
int i_nspname,
1612+
i_relname,
1613+
i_attname;
1614+
DbInfo *active_db = &old_cluster.dbarr.dbs[dbnum];
1615+
PGconn *conn = connectToServer(&old_cluster, active_db->db_name);
1616+
1617+
res = executeQueryOrDie(conn,
1618+
"SELECT cc.relnamespace::pg_catalog.regnamespace AS nspname, "
1619+
" cc.relname, ac.attname "
1620+
"FROM pg_catalog.pg_inherits i, pg_catalog.pg_attribute ac, "
1621+
" pg_catalog.pg_attribute ap, pg_catalog.pg_class cc "
1622+
"WHERE cc.oid = ac.attrelid AND i.inhrelid = ac.attrelid "
1623+
" AND i.inhparent = ap.attrelid AND ac.attname = ap.attname "
1624+
" AND ap.attnum > 0 and ap.attnotnull AND NOT ac.attnotnull");
1625+
1626+
ntup = PQntuples(res);
1627+
i_nspname = PQfnumber(res, "nspname");
1628+
i_relname = PQfnumber(res, "relname");
1629+
i_attname = PQfnumber(res, "attname");
1630+
for (int i = 0; i < ntup; i++)
1631+
{
1632+
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1633+
pg_fatal("could not open file \"%s\": %m", output_path);
1634+
if (!db_used)
1635+
{
1636+
fprintf(script, "In database: %s\n", active_db->db_name);
1637+
db_used = true;
1638+
}
1639+
1640+
fprintf(script, " %s.%s.%s\n",
1641+
PQgetvalue(res, i, i_nspname),
1642+
PQgetvalue(res, i, i_relname),
1643+
PQgetvalue(res, i, i_attname));
1644+
}
1645+
1646+
PQclear(res);
1647+
PQfinish(conn);
1648+
}
1649+
1650+
if (script)
1651+
{
1652+
fclose(script);
1653+
pg_log(PG_REPORT, "fatal");
1654+
pg_fatal("Your installation contains inconsistent NOT NULL constraints.\n"
1655+
"If the parent column(s) are NOT NULL, then the child column must\n"
1656+
"also be marked NOT NULL, or the upgrade will fail.\n"
1657+
"You can fix this by running\n"
1658+
" ALTER TABLE tablename ALTER column SET NOT NULL;\n"
1659+
"on each column listed in the file:\n"
1660+
" %s", output_path);
1661+
}
1662+
else
1663+
check_ok();
1664+
}
15791665

15801666
/*
15811667
* check_for_pg_role_prefix()

0 commit comments

Comments
 (0)