Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rgrH4-008Ipn-T3 for pgsql-general@arkaria.postgresql.org; Sun, 03 Mar 2024 19:13:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rgrH2-001mU5-2o for pgsql-general@arkaria.postgresql.org; Sun, 03 Mar 2024 19:13:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rgrH1-001mTx-Oe for pgsql-general@lists.postgresql.org; Sun, 03 Mar 2024 19:13:24 +0000 Received: from smtp92.ord1d.emailsrvr.com ([184.106.54.92]) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rgrGw-002b36-DN for pgsql-general@lists.postgresql.org; Sun, 03 Mar 2024 19:13:20 +0000 X-Auth-ID: xof@thebuild.com Received: by smtp20.relay.ord1d.emailsrvr.com (Authenticated sender: xof-AT-thebuild.com) with ESMTPSA id 6A13FC0084; Sun, 3 Mar 2024 14:13:16 -0500 (EST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Subject: Re: How to add columns faster From: Christophe Pettus In-Reply-To: Date: Sun, 3 Mar 2024 11:13:11 -0800 Cc: pgsql-general Content-Transfer-Encoding: quoted-printable Message-Id: <017A6342-5001-4F43-9C3B-FD4C2CC866BE@thebuild.com> References: To: yudhi s X-Mailer: Apple Mail (2.3608.120.23.2.7) X-Classification-ID: 2b95dcf4-246a-48a6-ae0d-c3b0c37a78be-1-1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Mar 3, 2024, at 11:06, yudhi s 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. >=20 > 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=