pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <shammat@gmx.net>
To: pgsql-admin@lists.postgresql.org
Subject: Re: Need help to implement Primary Key.
Date: Thu, 8 Feb 2024 11:37:45 +0100
Message-ID: <aab0c4e7-4c05-4245-9956-c1584aadf132@gmx.net> (raw)
In-Reply-To: <CAHOGQfWo-2vZMQN1U8dKF=AZtrbJrCT8pGBAY554P4jW2Z6JEw@mail.gmail.com>
References: <CAHOGQfWo-2vZMQN1U8dKF=AZtrbJrCT8pGBAY554P4jW2Z6JEw@mail.gmail.com>


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);







view thread (3+ messages)  latest in thread

Message-ID: <aab0c4e7-4c05-4245-9956-c1584aadf132@gmx.net>
Permalink:  ../aab0c4e7-4c05-4245-9956-c1584aadf132@gmx.net/
Also on:    postgresql.org/message-id/aab0c4e7-4c05-4245-9956-c1584aadf132@gmx.net

 · 

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: shammat@gmx.net, pgsql-admin@lists.postgresql.org
  Subject: Re: Need help to implement Primary Key.
  In-Reply-To: <aab0c4e7-4c05-4245-9956-c1584aadf132@gmx.net>

* 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