Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf295494

Browse files
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 NULLThis can only be fixed by marking the child column as NOT NULL beforethe upgrade, which could take an arbitrary amount of time (because ic'sdata must be scanned). Have pg_upgrade's check mode warn if thatcondition is found, so that users know what to adjust before running theupgrade for real.Author: Ali Akbar <the.apaan@gmail.com>Reviewed-by: Justin Pryzby <pryzby@telsasoft.com>Backpatch-through: 13Discussion:https://postgr.es/m/CACQjQLoMsE+1pyLe98pi0KvPG2jQQ94LWJ+PTiLgVRK4B=i_jg@mail.gmail.com
1 parentd64d68f commitf295494

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

‎src/bin/pg_upgrade/check.c‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
2323
staticvoidcheck_for_user_defined_postfix_ops(ClusterInfo*cluster);
2424
staticvoidcheck_for_incompatible_polymorphics(ClusterInfo*cluster);
2525
staticvoidcheck_for_tables_with_oids(ClusterInfo*cluster);
26+
staticvoidcheck_for_not_null_inheritance(ClusterInfo*cluster);
2627
staticvoidcheck_for_pg_role_prefix(ClusterInfo*cluster);
2728
staticvoidcheck_for_new_tablespace_dir(void);
2829
staticvoidcheck_for_user_defined_encoding_conversions(ClusterInfo*cluster);
@@ -672,6 +673,14 @@ check_and_dump_old_cluster(void)
672673
if (GET_MAJOR_VERSION(old_cluster.major_version) <=1100)
673674
check_for_tables_with_oids(&old_cluster);
674675

676+
/*
677+
* Pre-PG 18 allowed child tables to omit not-null constraints that their
678+
* parents columns have, but schema restore fails for them. Verify there
679+
* are none, iff applicable.
680+
*/
681+
if (GET_MAJOR_VERSION(old_cluster.major_version) <=1800)
682+
check_for_not_null_inheritance(&old_cluster);
683+
675684
/*
676685
* Pre-PG 10 allowed tables with 'unknown' type columns and non WAL logged
677686
* hash indexes
@@ -1624,6 +1633,92 @@ check_for_tables_with_oids(ClusterInfo *cluster)
16241633
check_ok();
16251634
}
16261635

1636+
/*
1637+
* Callback function for processing results of query for
1638+
* check_for_not_null_inheritance.
1639+
*/
1640+
staticvoid
1641+
process_inconsistent_notnull(DbInfo*dbinfo,PGresult*res,void*arg)
1642+
{
1643+
UpgradeTaskReport*report= (UpgradeTaskReport*)arg;
1644+
intntups=PQntuples(res);
1645+
inti_nspname=PQfnumber(res,"nspname");
1646+
inti_relname=PQfnumber(res,"relname");
1647+
inti_attname=PQfnumber(res,"attname");
1648+
1649+
AssertVariableIsOfType(&process_inconsistent_notnull,
1650+
UpgradeTaskProcessCB);
1651+
1652+
if (ntups==0)
1653+
return;
1654+
1655+
if (report->file==NULL&&
1656+
(report->file=fopen_priv(report->path,"w"))==NULL)
1657+
pg_fatal("could not open file \"%s\": %m",report->path);
1658+
1659+
fprintf(report->file,"In database: %s\n",dbinfo->db_name);
1660+
1661+
for (introwno=0;rowno<ntups;rowno++)
1662+
{
1663+
fprintf(report->file," %s.%s.%s\n",
1664+
PQgetvalue(res,rowno,i_nspname),
1665+
PQgetvalue(res,rowno,i_relname),
1666+
PQgetvalue(res,rowno,i_attname));
1667+
}
1668+
}
1669+
1670+
/*
1671+
* check_for_not_null_inheritance()
1672+
*
1673+
* An attempt to create child tables lacking not-null constraints that are
1674+
* present in their parents errors out. This can no longer occur since 18,
1675+
* but previously there were various ways for that to happen. Check that
1676+
* the cluster to be upgraded doesn't have any of those problems.
1677+
*/
1678+
staticvoid
1679+
check_for_not_null_inheritance(ClusterInfo*cluster)
1680+
{
1681+
UpgradeTaskReportreport;
1682+
UpgradeTask*task;
1683+
constchar*query;
1684+
1685+
prep_status("Checking for not-null constraint inconsistencies");
1686+
1687+
report.file=NULL;
1688+
snprintf(report.path,sizeof(report.path),"%s/%s",
1689+
log_opts.basedir,
1690+
"not_null_inconsistent_columns.txt");
1691+
1692+
query="SELECT cc.relnamespace::pg_catalog.regnamespace AS nspname, "
1693+
" cc.relname, ac.attname "
1694+
"FROM pg_catalog.pg_inherits i, pg_catalog.pg_attribute ac, "
1695+
" pg_catalog.pg_attribute ap, pg_catalog.pg_class cc "
1696+
"WHERE cc.oid = ac.attrelid AND i.inhrelid = ac.attrelid "
1697+
" AND i.inhparent = ap.attrelid AND ac.attname = ap.attname "
1698+
" AND ap.attnum > 0 and ap.attnotnull AND NOT ac.attnotnull";
1699+
1700+
task=upgrade_task_create();
1701+
upgrade_task_add_step(task,query,
1702+
process_inconsistent_notnull,
1703+
true,&report);
1704+
upgrade_task_run(task,cluster);
1705+
upgrade_task_free(task);
1706+
1707+
if (report.file)
1708+
{
1709+
fclose(report.file);
1710+
pg_log(PG_REPORT,"fatal");
1711+
pg_fatal("Your installation contains inconsistent NOT NULL constraints.\n"
1712+
"If the parent column(s) are NOT NULL, then the child column must\n"
1713+
"also be marked NOT NULL, or the upgrade will fail.\n"
1714+
"You can fix this by running\n"
1715+
" ALTER TABLE tablename ALTER column SET NOT NULL;\n"
1716+
"on each column listed in the file:\n"
1717+
" %s",report.path);
1718+
}
1719+
else
1720+
check_ok();
1721+
}
16271722

16281723
/*
16291724
* check_for_pg_role_prefix()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp