Tip box on Adding a Column - Mailing list pgsql-docs

From Marcelo Fernandes
Subject Tip box on Adding a Column
Date
Msg-id CAM2F1VNAP2bKEtxymaX=j+aV3hTfcZjH7p2jyCDGc_329rUiPQ@mail.gmail.com
Whole thread Raw
Responses Re: Tip box on Adding a Column
Re: Tip box on Adding a Column
List pgsql-docs
Hi folks,

We have this Tip box under the "Adding a Column" header here:

- https://www.postgresql.org/docs/current/ddl-alter.html#DDL-ALTER-ADDING-A-COLUMN

That says:

> From PostgreSQL 11, adding a column with a constant default value no longer
> means that each row of the table needs to be updated when the ALTER TABLE
> statement is executed. Instead, the default value will be returned the next
> time the row is accessed, and applied when the table is rewritten, making the
> ALTER TABLE very fast even on large tables.

I'm just seeking clarification if this advice is true **even for** new columns
declared with NOT NULL?

Historically, I've had to add new fields on existing big tables with a NULL to
avoid downtime, but it may be different when a DEFAULT is provided?

I have used perf to profile the call-chain for adding a NOT NULL column with
a default versus just an ordinary NULL with a default, and they are fairly
similar.

However, I see these functions being called in both cases:

- ATRewriteTables
     find_composite_type_dependencies
     systable_beginscan
     index_rescan
     btrescan

And the names raised my eyebrow... I don't have a deep understanding of the
internals here, so it would be great if someone could clarify this for me.

Thanks,
Marcelo.



pgsql-docs by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Logical replication - initial data synchronization
Next
From: PG Doc comments form
Date:
Subject: PostgreSQL limits