X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (mx2.hub.org [200.46.204.254]) by postgresql.org (Postfix) with ESMTP id 652E811F6C80 for ; Thu, 20 Apr 2006 05:08:26 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: 0.4 X-Spam-Level: X-Spam-Status: No, score=0.4 required=5 tests=[BAYES_00=-2.599, DNS_FROM_RFC_ABUSE=0.2, DNS_FROM_RFC_WHOIS=1.447, HTML_10_20=1.351, HTML_MESSAGE=0.001] Received: from postgresql.org ([200.46.204.71]) by localhost (mx2.hub.org [200.46.204.254]) (amavisd-new, port 10024) with ESMTP id lTOJ4aDbEalJ for ; Thu, 20 Apr 2006 05:08:22 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from web30414.mail.mud.yahoo.com (web30414.mail.mud.yahoo.com [68.142.201.237]) by postgresql.org (Postfix) with SMTP id D990C11F6C39 for ; Thu, 20 Apr 2006 05:08:18 -0300 (ADT) Received: (qmail 55033 invoked by uid 60001); 20 Apr 2006 08:08:18 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=FZxhELTPSIWadzhjkiFPYH4u3Pvt9WQKFUsbxDAGnPNL7DPMHeHzxRumiwkmoMGw4tVpb+2KETAjN8QDwuu8NPvVRTyyFA+FUBIpTXVqkxMAsBnglPj/tmXyPJqdORlGi0bMfCiuskqHldP7mVNQEr0JsI09LrXHRWRy1EUWZk4= ; Message-ID: <20060420080818.55031.qmail@web30414.mail.mud.yahoo.com> Received: from [203.115.30.220] by web30414.mail.mud.yahoo.com via HTTP; Thu, 20 Apr 2006 01:08:18 PDT Date: Thu, 20 Apr 2006 01:08:18 -0700 (PDT) From: Thusitha Kodikara Subject: Re: Different results when specifying DEFAULT values through To: Stephan Szabo Cc: pgsql-admin@postgresql.org In-Reply-To: <20060419070851.F31019@megazone.bigpanda.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-747376931-1145520498=:10084" Content-Transfer-Encoding: 8bit X-Archive-Number: 200604/196 X-Sequence-Number: 21365 --0-747376931-1145520498=:10084 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Thanks Stephan. That appears to be the reason for the differences. -Thusitha Stephan Szabo wrote: On Wed, 19 Apr 2006, Thusitha Kodikara wrote: > Hi, > > I observed the following on PostgreSQL 8.1.3 (on Windows 2000). > > (1) alter table invoice add column active boolean default true; > > This will add a new column "active" with a default clause as "true" and > also sets the value "true" to all existing rows of the table. > > BUT > > (2) ALTER TABLE invoice ADD COLUMN active boolean; > ALTER TABLE invoice ALTER COLUMN active SET DEFAULT true; > > Will result in only adding the column "active" with default clause as > "true". It will NOT set the value "true" for existing rows. > > Is there a particular reason for this difference in behaviour? (Or am I > missing something in the syntax?) AFAICS that's what the spec requires (at least in SQL92) Add column says: 2) Let C be the column added to T. Every value in C is the default value for C. So, in the first case, every value in the new column is true. In the second, at the point of the add column every value is null (since there's no default). Set default doesn't appear to change the value of existing rows, so no further change is made when you set default (ie, it doesn't matter if something was set to the default in the past, changing the default doesn't change the value as stored). ---------------------------(end of broadcast)--------------------------- TIP 5: don't forget to increase your free space map settings --0-747376931-1145520498=:10084 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Thanks Stephan. That appears to be the reason for the differences.

-Thusitha

Stephan Szabo <sszabo@megazone.bigpanda.com> wrote:
On Wed, 19 Apr 2006, Thusitha Kodikara wrote:

> Hi,
>
> I observed the following on PostgreSQL 8.1.3 (on Windows 2000).
>
> (1) alter table invoice add column active boolean default true;
>
> This will add a new column "active" with a default clause as "true" and
> also sets the value "true" to all existing rows of the table.
>
> BUT
>
> (2) ALTER TABLE invoice ADD COLUMN active boolean;
> ALTER TABLE invoice ALTER COLUMN active SET DEFAULT true;
>
> Will result in only adding the column "active" with default clause as
> "true". It will NOT set the value "true" for existing rows.
>
> Is there a particular reason for this difference in behaviour? (Or am I
> missing something in the syntax?)

AFAICS that's what the spec requires (at least in SQL92)

Add column says:
2) Let C be the column added to T. Every value in C is the default
value for C.

So, in the first case, every value in the new column is true. In the
second, at the point of the add column every value is null (since there's
no default). Set default doesn't appear to change the value of existing
rows, so no further change is made when you set default (ie, it doesn't
matter if something was set to the default in the past, changing the
default doesn't change the value as stored).

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

--0-747376931-1145520498=:10084--