Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gfaQE-0004nU-Vy for pgsql-general@arkaria.postgresql.org; Sat, 05 Jan 2019 01:06:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gfaPE-0004Y3-F5 for pgsql-general@arkaria.postgresql.org; Sat, 05 Jan 2019 01:05:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gfaPE-0004Xq-78; Sat, 05 Jan 2019 01:05:40 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gfaPB-0005P4-S0; Sat, 05 Jan 2019 01:05:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x0515YqW021257; Fri, 4 Jan 2019 20:05:34 -0500 From: Tom Lane To: Allison Kaptur cc: pgsql-general@postgresql.org, pgsql-hackers@lists.postgresql.org Subject: Re: ALTER TABLE with multiple SET NOT NULL In-reply-to: References: Comments: In-reply-to Allison Kaptur message dated "Fri, 04 Jan 2019 16:28:51 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <21255.1546650334.1@sss.pgh.pa.us> Date: Fri, 04 Jan 2019 20:05:34 -0500 Message-ID: <21256.1546650334@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Allison Kaptur writes: > I encountered a surprising error when writing a migration that both added a > primary key to a table and added a new NOT NULL column. It threw the error " > column "col_d" contains null values", even though I supplied a default. The > migration looks like this: > CREATE TABLE new_table AS SELECT col_a, col_b, col_c from existing_table; > ALTER TABLE new_table > ADD COLUMN col_d UUID UNIQUE NOT NULL DEFAULT uuid_generate_v4(), > ADD PRIMARY KEY (col_a, col_b, col_c); Hm, this can be made a good deal more self-contained: regression=# create table t1 (a int); CREATE TABLE regression=# insert into t1 values(1); INSERT 0 1 regression=# alter table t1 add column b float8 not null default random(), add primary key(a); ERROR: column "b" contains null values It fails like that as far back as I tried (8.4). I'm guessing that we're doing the ALTER steps in the wrong order, but haven't looked closer than that. Interestingly, in v11 and HEAD it works if you use a constant default, suggesting that the fast-default feature is at least adjacent to the problem. regards, tom lane