Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WVnPy-0003fR-Ay for pgsql-docs@arkaria.postgresql.org; Thu, 03 Apr 2014 19:35:18 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WVnPx-0006fE-CN for pgsql-docs@arkaria.postgresql.org; Thu, 03 Apr 2014 19:35:17 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WVnPs-0006Yu-Lg for pgsql-docs@postgresql.org; Thu, 03 Apr 2014 19:35:12 +0000 Received: from mail-oa0-x22b.google.com ([2607:f8b0:4003:c02::22b]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WVnPp-0001YG-Q3 for pgsql-docs@postgresql.org; Thu, 03 Apr 2014 19:35:11 +0000 Received: by mail-oa0-f43.google.com with SMTP id eb12so2514046oac.16 for ; Thu, 03 Apr 2014 12:35:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=m4x94sW+C9mk8RAz2ciHqZ8syzDrcagQspfe5M31SOs=; b=0TW9nC2s/R00YCI4W/VZHWkIc0aHsBdmzLesoHIVl6IikH1eTp/S7COR83yiDvYvFM hHzbUYDpOMS0chnuKt5ByGTTq35Emw8C0ahP0lzrrBtEjmR8juTOfKP3N9Tq8Thiea24 ZNPR6YnGaUJcukJUnyNDB2N/Ygr7b0WcFLTYgJNKgJXPv3/h117ov1GhyxH5Oi0BvIHg nIDh/92D7hiHHlajlTcEI1pDT4CWHBagA+shTYeUu3A/+WE3oZf60W62PXGuPjzmGGZX mP2UYQTylRgxTHFqSlHARRbVWCaKV8lwIzyMJ8KlPfI246Q6kzB3z5cep8O88aiSWyA4 pSZQ== X-Received: by 10.60.229.228 with SMTP id st4mr11353976oec.16.1396553709250; Thu, 03 Apr 2014 12:35:09 -0700 (PDT) MIME-Version: 1.0 Received: by 10.76.169.98 with HTTP; Thu, 3 Apr 2014 12:34:49 -0700 (PDT) In-Reply-To: <29415.1396543584@sss.pgh.pa.us> References: <3036.1396497272@sss.pgh.pa.us> <3704.1396498776@sss.pgh.pa.us> <29415.1396543584@sss.pgh.pa.us> From: Amit Langote Date: Fri, 4 Apr 2014 04:34:49 +0900 Message-ID: Subject: Re: Fwd: [GENERAL] Table Rewrite During ALTER TABLE ... ADD COLUMN ... DEFAULT NULL To: Tom Lane Cc: pgsql-docs@postgresql.org Content-Type: text/plain; charset=UTF-8 X-Pg-Spam-Score: -1.8 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-docs Precedence: bulk Sender: pgsql-docs-owner@postgresql.org On Fri, Apr 4, 2014 at 1:46 AM, Tom Lane wrote: > Amit Langote writes: >> On Thu, Apr 3, 2014 at 1:19 PM, Tom Lane wrote: >>> We could just rephrase the ALTER TABLE docs to say that the table >>> rewrite is avoided if you omit the DEFAULT clause, rather than >>> saying that a null default works. > >> How does the attached sound? >> Wonder if a rewrite-warning is necessary? > > I had in mind more like the attached. > > This is still not the full truth, as for example this case must do > a rewrite: > > regression=# create domain dnn as int check(value is not null); > CREATE DOMAIN > regression=# create table foo1 (f1 int); > CREATE TABLE > regression=# insert into foo1 values(42); > INSERT 0 1 > regression=# alter table foo1 add column ff dnn; > ERROR: value for domain dnn violates check constraint "dnn_check" > > But I think we can avoid getting into such complexities here. > > regards, tom lane > > > diff --git a/doc/src/sgml/ref/alter_table.sgml b/doc/src/sgml/ref/alter_table.sgml > index 4847d66..f0a8b86 100644 > *** a/doc/src/sgml/ref/alter_table.sgml > --- b/doc/src/sgml/ref/alter_table.sgml > *************** ALTER TABLE [ IF EXISTS ] *** 854,867 **** > When a column is added with ADD COLUMN, all existing > rows in the table are initialized with the column's default value > (NULL if no DEFAULT clause is specified). > > > > ! Adding a column with a non-null default or changing the type of an > ! existing column will require the entire table and indexes to be rewritten. > ! As an exception, if the USING clause does not change the column > contents and the old type is either binary coercible to the new type or > ! an unconstrained domain over the new type, a table rewrite is not needed, > but any indexes on the affected columns must still be rebuilt. Adding or > removing a system oid column also requires rewriting the entire > table. Table and/or index rebuilds may take a significant amount of time > --- 854,871 ---- > When a column is added with ADD COLUMN, all existing > rows in the table are initialized with the column's default value > (NULL if no DEFAULT clause is specified). > + If there is no DEFAULT clause, this is merely a metadata > + change and does not require any immediate update of the table's data; > + the added NULL values are supplied on readout, instead. > > > > ! Adding a column with a DEFAULT clause or changing the type of > ! an existing column will require the entire table and its indexes to be > ! rewritten. As an exception when changing the type of an existing column, > ! if the USING clause does not change the column > contents and the old type is either binary coercible to the new type or > ! an unconstrained domain over the new type, a table rewrite is not needed; > but any indexes on the affected columns must still be rebuilt. Adding or > removing a system oid column also requires rewriting the entire > table. Table and/or index rebuilds may take a significant amount of time Thanks for the fix. -- Amit -- Sent via pgsql-docs mailing list (pgsql-docs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-docs