pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Fujii Masao <fujii@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix ADD PRIMARY KEY after dropping and re-adding a column
Date: Tue, 06 Oct 2026 04:15:10 +0000
Message-ID: <E1xDwa6-00000000V33-41x9@gemulon.postgresql.org> (raw)

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.janiuk@proton.me>
Author: Fujii Masao <masao.fujii@gmail.com>
Co-authored-by: Manuel Reyes Bravo <manuelreyesbravo@gmail.com>
Discussion: https://postgr.es/m/0a9b4c44-cdff-4ec8-a8fb-b3a8b59e9ba9@proton.me
Backpatch-through: 18

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/dd97e0c8e3823690653cd555090e01fafbb33a85

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(-)



view thread (3+ messages)  latest in thread

Message-ID: <E1xDwa6-00000000V33-41x9@gemulon.postgresql.org>
Permalink:  ../E1xDwa6-00000000V33-41x9@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xDwa6-00000000V33-41x9@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: fujii@postgresql.org
  Subject: Re: pgsql: Fix ADD PRIMARY KEY after dropping and re-adding a column
  In-Reply-To: <E1xDwa6-00000000V33-41x9@gemulon.postgresql.org>
  References: <E1xDwa6-00000000V33-41x9@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox