pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christophe Pettus <xof@thebuild.com>
To: yudhi s <learnerdatabase99@gmail.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: How to add columns faster
Date: Sun, 3 Mar 2024 11:13:11 -0800
Message-ID: <017A6342-5001-4F43-9C3B-FD4C2CC866BE@thebuild.com> (raw)
In-Reply-To: <CAEzWdqe4_j9fMcgjguCCvrtV=4Sy3beOKkhuL8yMy1KZU9g+VA@mail.gmail.com>
References: <CAEzWdqe4_j9fMcgjguCCvrtV=4Sy3beOKkhuL8yMy1KZU9g+VA@mail.gmail.com>



> On Mar 3, 2024, at 11:06, yudhi s <learnerdatabase99@gmail.com> wrote:
> as the column addition using the traditional "Alter table" command in postgres looks to be a full table rewrite

That's not always (or, really, usually) true.  Adding a new column in any recent version of PostgreSQL just alters the system catalogs; it does not rewrite the table.  Make sure the new column is either NULL-able, or has a simple DEFAULT expression (specifically, not using a VOLATILE function).  Per the documentation:

> When a column is added with ADD COLUMN and a non-volatile DEFAULT is specified, the default is evaluated at the time of the statement and the result stored in the table's metadata. That value will be used for the column for all existing rows. If no DEFAULT is specified, NULL is used. In neither case is a rewrite of the table required.
> 
> Adding a column with a volatile DEFAULT or changing the type of an existing column will require the entire table and its indexes to be rewritten.

https://www.postgresql.org/docs/current/sql-altertable.html




view thread (6+ messages)  latest in thread

Message-ID: <017A6342-5001-4F43-9C3B-FD4C2CC866BE@thebuild.com>
Permalink:  ../017A6342-5001-4F43-9C3B-FD4C2CC866BE@thebuild.com/
Also on:    postgresql.org/message-id/017A6342-5001-4F43-9C3B-FD4C2CC866BE@thebuild.com

 · 

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-general@postgresql.org
  Cc: xof@thebuild.com, learnerdatabase99@gmail.com
  Subject: Re: How to add columns faster
  In-Reply-To: <017A6342-5001-4F43-9C3B-FD4C2CC866BE@thebuild.com>
  References: <CAEzWdqe4_j9fMcgjguCCvrtV=4Sy3beOKkhuL8yMy1KZU9g+VA@mail.gmail.com> <017A6342-5001-4F43-9C3B-FD4C2CC866BE@thebuild.com>

* 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