| From: | Fujii Masao <fujii(at)postgresql(dot)org> |
|---|---|
| To: | pgsql-committers(at)lists(dot)postgresql(dot)org |
| Subject: | pgsql: Fix ADD PRIMARY KEY after dropping and re-adding a column |
| Date: | 2026-10-06 04:15:51 |
| Message-ID: | E1xDwal-00000000V5a-1i6n@gemulon.postgresql.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-committers |
Fix ADD PRIMARY KEY after dropping and re-adding a column
Previously, dropping a NOT NULL column, adding a new column with the same
name, and making it a primary key in a single ALTER TABLE command could
fail with "primary key column ... is not marked NOT NULL". This happened
because ALTER TABLE checked for an existing NOT NULL constraint before
dropping the old column. Finding the constraint caused it to skip adding
one for the new column, even though the old constraint would be removed
with the old column.
Defer this check until after column drops and additions have been
executed, so that it examines the columns that will actually form the
primary key. Add any required NOT NULL constraints before creating the
primary-key index.
Backpatch to v18, where this issue was introduced by commit a379061a22a.
Reported-by: Ludvig Janiuk <ludvig(dot)janiuk(at)proton(dot)me>
Author: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
Co-authored-by: Manuel Reyes Bravo <manuelreyesbravo(at)gmail(dot)com>
Discussion: https://postgr.es/m/0a9b4c44-cdff-4ec8-a8fb-b3a8b59e9ba9@proton.me
Backpatch-through: 18
Branch
------
REL_18_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/5659e549adeb9de1a89efa181b032044ada64b1f
Modified Files
--------------
src/backend/commands/tablecmds.c | 35 +++++++++++++++++++++----------
src/test/regress/expected/alter_table.out | 25 ++++++++++++++++++++++
src/test/regress/sql/alter_table.sql | 26 +++++++++++++++++++++++
3 files changed, 75 insertions(+), 11 deletions(-)
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Fujii Masao | 2026-10-06 04:18:21 | pgsql: Run repack_dropped with make and clean up its extension |
| Previous Message | Fujii Masao | 2026-10-06 04:15:27 | pgsql: Fix ADD PRIMARY KEY after dropping and re-adding a column |