pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Holger Jakobs <holger@jakobs.com>
To: pgsql-admin@lists.postgresql.org
Subject: Re: Need help to implement Primary Key.
Date: Thu, 8 Feb 2024 12:25:08 +0100
Message-ID: <d1b63b5e-3fc4-3eb7-615d-da97d9b2ea51@jakobs.com> (raw)
In-Reply-To: <aab0c4e7-4c05-4245-9956-c1584aadf132@gmx.net>
References: <CAHOGQfWo-2vZMQN1U8dKF=AZtrbJrCT8pGBAY554P4jW2Z6JEw@mail.gmail.com>
	<aab0c4e7-4c05-4245-9956-c1584aadf132@gmx.net>

Am 08.02.24 um 11:37 schrieb Thomas Kellerer:
> Gambhir Singh schrieb am 08.02.2024 um 11:30:
>
>> I have a table with a record count of ~500 million (496,098,390).
>> There is no primary key in this table. Now I need to set up a primary
>> key on this table. I can't find a way to set up a primary key. I
>> tried to add a new column with IDENTITY but it is not allowing null
>> value for existing rows and I don't know how to update the unique
>> value for existing records.
> If you add an identity column, it will automatically be populated
>
>
> alter table foo
>     add id bigint generated always as identity not null;
>
> Once that has finished (which will take some time given the size of the table),
> you can add the primary key:
>
> alter table foo
>     add constraint pk_foo primary key (id);
>
You can even do this in one step:

alter table table1 add column id bigint primary key generated always as 
identity;

-- 
Holger Jakobs, Bergisch Gladbach, Tel. +49-178-9759012



Attachments:

  [application/pgp-signature] OpenPGP_signature (202B, ../d1b63b5e-3fc4-3eb7-615d-da97d9b2ea51@jakobs.com/2-OpenPGP_signature)
  download

view thread (3+ messages)

Message-ID: <d1b63b5e-3fc4-3eb7-615d-da97d9b2ea51@jakobs.com>
Permalink:  ../d1b63b5e-3fc4-3eb7-615d-da97d9b2ea51@jakobs.com/
Also on:    postgresql.org/message-id/d1b63b5e-3fc4-3eb7-615d-da97d9b2ea51@jakobs.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: holger@jakobs.com, pgsql-admin@lists.postgresql.org
  Subject: Re: Need help to implement Primary Key.
  In-Reply-To: <d1b63b5e-3fc4-3eb7-615d-da97d9b2ea51@jakobs.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox