pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Clarify behavior of adding and altering a column in same ALTER c
Date: Tue, 21 Jan 2020 21:17:29 +0000
Message-ID: <E1iu0tt-0003xc-Th@gemulon.postgresql.org> (raw)

Clarify behavior of adding and altering a column in same ALTER command.

The behavior of something like

ALTER TABLE transactions
  ADD COLUMN status varchar(30) DEFAULT 'old',
  ALTER COLUMN status SET default 'current';

is to fill existing table rows with 'old', not 'current'.  That's
intentional and desirable for a couple of reasons:

* It makes the behavior the same whether you merge the sub-commands
into one ALTER command or give them separately;

* If we applied the new default while filling the table, there would
be no way to get the existing behavior in one SQL command.

The same reasoning applies in cases that add a column and then
manipulate its GENERATED/IDENTITY status in a second sub-command,
since the generation expression is really just a kind of default.
However, that wasn't very obvious (at least not to me; earlier in
the referenced discussion thread I'd thought it was a bug to be
fixed).  And it certainly wasn't documented.

Hence, add documentation, code comments, and a test case to clarify
that this behavior is all intentional.

In passing, adjust ATExecAddColumn's defaults-related relkind check
so that it matches up exactly with ATRewriteTables, instead of being
effectively (though not literally) the negated inverse condition.
The reasoning can be explained a lot more concisely that way, too
(not to mention that the comment now matches the code, which it
did not before).

Discussion: https://postgr.es/m/10365.1558909428@sss.pgh.pa.us

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/9b9c5f279e8261ab90dc64559911d2578288b7e9

Modified Files
--------------
doc/src/sgml/ref/alter_table.sgml      | 39 ++++++++++++++++++++++++++++++----
src/backend/commands/tablecmds.c       | 20 ++++++++++-------
src/test/regress/expected/identity.out |  6 ++++++
src/test/regress/sql/identity.sql      |  6 ++++++
4 files changed, 59 insertions(+), 12 deletions(-)



Message-ID: <E1iu0tt-0003xc-Th@gemulon.postgresql.org>
Permalink:  ../E1iu0tt-0003xc-Th@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1iu0tt-0003xc-Th@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: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Clarify behavior of adding and altering a column in same ALTER c
  In-Reply-To: <E1iu0tt-0003xc-Th@gemulon.postgresql.org>
  References: <E1iu0tt-0003xc-Th@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