public inbox for [email protected]
help / color / mirror / Atom feedFrom: Adrian Klaver <[email protected]>
To: Laurenz Albe <[email protected]>
To: Marcelo Fernandes <[email protected]>
To: [email protected]
Subject: Re: Default Value Retention After Dropping Default
Date: Mon, 24 Feb 2025 08:37:06 -0800
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <CAM2F1VNZNeruO2N+_-Fq3JJ7bto35kOzomoN5A2dmstAWLi5tQ@mail.gmail.com>
<[email protected]>
On 2/24/25 03:50, Laurenz Albe wrote:
> On Mon, 2025-02-24 at 20:56 +1300, Marcelo Fernandes wrote:
>> I am experiencing an interesting behavior in PostgreSQL and would like to seek
>> some clarification.
>>
>> Can anyone explain how PostgreSQL "knows about" the default value that has just
>> been dropped and what is happened under the scenes? I am keen on a deep
>> understanding on how Postgres achieves this.
>
> The "missing value" is stored in pg_attribute.admissingval:
>
> SELECT attmissingval
> FROM pg_attribute
> WHERE attrelid = 'foo'::regclass
> AND attname = 'bar';
>
> attmissingval
> ═══════════════
> {default}
> (1 row)
>
> That value is used for all rows that don't yet physically have the column.
That answers this part of the process:
ALTER TABLE foo ADD COLUMN bar varchar(255) NOT NULL DEFAULT 'default';
I believe the OP is asking about this:
ALTER TABLE foo ALTER COLUMN bar DROP DEFAULT;
Because if after dropping the DEFAULT you do this:
INSERT INTO foo (id) SELECT generate_series(1001, 1010);
You get:
ERROR: null value in column "bar" of relation "foo" violates not-null
constraint
DETAIL: Failing row contains (1001, null).
The DEFAULT is no longer in use, but the values still exist in the
previously entered rows:
SELECT * from foo order by id desc limit 5;
id | bar
-------+---------
10000 | default
9999 | default
9998 | default
9997 | default
9996 | default
>
> Yours,
> Laurenz Albe
>
--
Adrian Klaver
[email protected]
view thread (5+ messages) latest in thread
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 using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected]
Subject: Re: Default Value Retention After Dropping Default
In-Reply-To: <[email protected]>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox