Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e3tuM-0005Zn-Ly for pgsql-general@arkaria.postgresql.org; Mon, 16 Oct 2017 01:09:30 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e3tuL-0003In-1Q for pgsql-general@arkaria.postgresql.org; Mon, 16 Oct 2017 01:09:29 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e3tuJ-0003Do-KK for pgsql-general@postgresql.org; Mon, 16 Oct 2017 01:09:27 +0000 Received: from mail-pf0-x230.google.com ([2607:f8b0:400e:c00::230]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1e3tuC-0005bK-Hg for pgsql-general@postgresql.org; Mon, 16 Oct 2017 01:09:26 +0000 Received: by mail-pf0-x230.google.com with SMTP id p87so14572411pfj.3 for ; Sun, 15 Oct 2017 18:09:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lucee-org.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=CJdCC43PlibN0MtgPmcdl4Lr3ym0BpKRAtSqL47Wl14=; b=sgBaGQjcPdbLT1lRtxB5NMTl0m/0y5J8mNTGwT2ImVdTFawvC8TWn1Mx4XrxyqSrjF McwDY4oQDNKCxov8iD1LkI/2nM8CQClaHdBSsjStZjdhk6x00nPfhd/AveEDSzjvYf+G INfrUSKsW6H5cxVI9arR8MckyhmFQjr9v7iWWBP53bAz/SqXHwceDOZIGDtFsi0UMXEY hlMxoRVrEAyOItNy1OJBN9ENxFWpo0m1E4Q5K36K1UVyPaL4HzyQdBfLHEu/SwY9DK4O a7XD50tiRcVhn+FoM5N5z6SluJ8ngG0U1/glpHA+OlEsIbd3MM/RLZQr5tnSY5lqA+B0 tiFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=CJdCC43PlibN0MtgPmcdl4Lr3ym0BpKRAtSqL47Wl14=; b=Ei4ZFBNJkH3BpatmlUduzYSePSDqC3kvWglpo/zvOFIgE9dI3SlF6cl05PnzhON48Z B78xlZ/Xu3SUrP3zHVl/1cyuxkFEn6HF9AzxdgyK2f60wPohSE6gWcPbM17AohzOr0i3 8W9ZJEnKU0jK8b0AiatY2cXCpFu4dJlhKWehLf8iNVqnqeISE9io2uJHe9gyruzMvICN oYSVF08BRFTdE3wNoi945vcOACmB1W86ScgB2RHzJ/z1Trfa2CJoeFJXbPiW3dQT8Oz+ jZgHhuzmrp6oC3FmPPY5gxU3vX68NtkG1sAET1Q4fKhpGv52Dfw0GdIx8dYptsnfP8+z ED4g== X-Gm-Message-State: AMCzsaVVEToKOaZYXF//b9UncPpPAehkXqJrBr4tUkuLJ+exDNc/z845 6dqk/nOaNloxGWM328LONNwWbW7Y X-Google-Smtp-Source: AOwi7QAE3xcBW8cRutZR5S3sSrNSY327Ns1Kl33cirbuAqGIz4Chx0Qu5zjHAKE9/IoA0kqAVBdW9A== X-Received: by 10.99.116.1 with SMTP id p1mr6885665pgc.247.1508116158586; Sun, 15 Oct 2017 18:09:18 -0700 (PDT) Received: from [192.168.1.15] (45-19-167-161.lightspeed.irvnca.sbcglobal.net. [45.19.167.161]) by smtp.googlemail.com with ESMTPSA id w9sm12519431pfk.16.2017.10.15.18.09.17 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 15 Oct 2017 18:09:17 -0700 (PDT) Subject: Re: Adding identity column to a non-empty table To: pgsql-general References: <37b3858b-0d61-0268-69cc-ff1868ac5255@lucee.org> <7435ef5f-f662-15f0-6ebc-b4b1b7cf1156@lucee.org> From: "Igal @ Lucee.org" Message-ID: <19fb9f81-ebb4-8e0a-24d3-c4aa1630eda3@lucee.org> Date: Sun, 15 Oct 2017 18:09:15 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------83240B9F83A20621338AD368" Content-Language: en-US List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org This is a multi-part message in MIME format. --------------83240B9F83A20621338AD368 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Melvin, On 10/15/2017 5:56 PM, Melvin Davidson wrote: > > On Sun, Oct 15, 2017 at 8:24 PM, Igal @ Lucee.org > wrote: > > On 10/15/2017 4:01 PM, Igal @ Lucee.org wrote: > > > Hello, > > I'm trying to add an identity column to a table that has > records (previously had a bigserial column which I removed): > > > There is probably a better solution, but the one I came up with is > to add the column as BIGSERIAL and DROP the SEQUENCE CASCADE, > SELECT the max(rid) + 1, and then convert the column to IDENTITY: > > > The correct way to make r_id the primary key would be: > > ALTER TABLE  event_log >   ADD COLUMN r_id SERIAL; > > ALTER TABLE  event_log >   ALTER COLUMN r_id TYPE BIGINT, >   ADD CONSTRAINT dummy_pk PRIMARY KEY (r_id); > > That automatically generates the column as > > r_id bigint NOT NULL DEFAULT nextval('dummy_r_id_seq'::regclass), >   CONSTRAINT dummy_pk PRIMARY KEY (r_id) > > and creates the appropriate sequence for you. > Does that use the new IDENTITY construct that was added in Postgres 10?  I do not really care for the PRIMARY KEY constraint.  I just want the sequence with the benefits of the new IDENTITY "type". Thanks, Igal --------------83240B9F83A20621338AD368 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit Melvin,

On 10/15/2017 5:56 PM, Melvin Davidson wrote:

On Sun, Oct 15, 2017 at 8:24 PM, Igal @ Lucee.org <igal@lucee.org> wrote:
On 10/15/2017 4:01 PM, Igal @ Lucee.org wrote:

Hello,

I'm trying to add an identity column to a table that has records (previously had a bigserial column which I removed):


There is probably a better solution, but the one I came up with is to add the column as BIGSERIAL and DROP the SEQUENCE CASCADE, SELECT the max(rid) + 1, and then convert the column to IDENTITY:


The correct way to make r_id the primary key would be:

ALTER TABLE  event_log
  ADD COLUMN r_id SERIAL;

ALTER TABLE  event_log
  ALTER COLUMN r_id TYPE BIGINT,
  ADD CONSTRAINT dummy_pk PRIMARY KEY (r_id);

That automatically generates the column as

r_id bigint NOT NULL DEFAULT nextval('dummy_r_id_seq'::regclass),
  CONSTRAINT dummy_pk PRIMARY KEY (r_id)

and creates the appropriate sequence for you.


Does that use the new IDENTITY construct that was added in Postgres 10?  I do not really care for the PRIMARY KEY constraint.  I just want the sequence with the benefits of the new IDENTITY "type".

Thanks,


Igal
--------------83240B9F83A20621338AD368--