X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 34B969FB256 for ; Wed, 2 Aug 2006 10:19:55 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 06605-06 for ; Wed, 2 Aug 2006 13:19:35 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 94C5A9FB252 for ; Wed, 2 Aug 2006 10:19:35 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k72DJUEx015042; Wed, 2 Aug 2006 09:19:30 -0400 (EDT) To: "Markus Bertheau" cc: pgsql-sql@postgresql.org Subject: Re: alter column type from boolean to char with default doesn't work In-reply-to: <684362e10608020546t16e6d3e8nf6756abd15a21a68@mail.gmail.com> References: <684362e10608020546t16e6d3e8nf6756abd15a21a68@mail.gmail.com> Comments: In-reply-to "Markus Bertheau" message dated "Wed, 02 Aug 2006 19:46:36 +0700" Date: Wed, 02 Aug 2006 09:19:30 -0400 Message-ID: <15041.1154524770@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=3.816 tagged_above=0 required=5 tests=SPF_HELO_SOFTFAIL, SPF_SOFTFAIL X-Spam-Level: *** X-Archive-Number: 200608/15 X-Sequence-Number: 25973 "Markus Bertheau" writes: > I basically want to change a boolean column to char. The boolean > column has a default of true. The char column should have 'f' for > false and 't' for true. I think that an SQL statement like the > following should work, but it doesn't: Hmm ... the way I would have expected to work is alter table posts alter column deleted drop default, alter column deleted type char(1) using (case when deleted then 't' else 'f' end), alter column deleted set default 'f'; but that does not work either --- you have to do it in more than one command: begin; alter table posts alter column deleted drop default; alter table posts alter column deleted type char(1) using (case when deleted then 't' else 'f' end), alter column deleted set default 'f'; commit; We could fix this by tweaking ATPrepCmd to schedule drop-default subcommands in an earlier pass than alter-type, and set-default subcommands afterwards. However I think the way it's done now was chosen to avoid surprising behavior in corner cases like alter table foo alter column bar set default ..., alter column bar drop default; You'd expect this to leave you with no default, but with the change the DROP part would be re-ordered to occur first. So maybe the cure is worse than the disease. OTOH that's a pretty silly example, whereas wanting to ALTER TYPE and fix the default in a single command is quite reasonable. Thoughts? regards, tom lane