pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feedNeed help to implement Primary Key.
3+ messages / 3 participants
[nested] [flat]
* Need help to implement Primary Key.
@ 2024-02-08 10:30 Gambhir Singh <gambhir.singh05@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Gambhir Singh @ 2024-02-08 10:30 UTC (permalink / raw)
To: pgsql-admin@lists.postgresql.org
Hi,
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.
Please help me to implement the same.
Thanks & Regards
Gambhir Singh
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Need help to implement Primary Key.
@ 2024-02-08 10:37 Thomas Kellerer <shammat@gmx.net>
parent: Gambhir Singh <gambhir.singh05@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Thomas Kellerer @ 2024-02-08 10:37 UTC (permalink / raw)
To: pgsql-admin@lists.postgresql.org
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);
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Need help to implement Primary Key.
@ 2024-02-08 11:25 Holger Jakobs <holger@jakobs.com>
parent: Thomas Kellerer <shammat@gmx.net>
0 siblings, 0 replies; 3+ messages in thread
From: Holger Jakobs @ 2024-02-08 11:25 UTC (permalink / raw)
To: pgsql-admin@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2024-02-08 11:25 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-02-08 10:30 Need help to implement Primary Key. Gambhir Singh <gambhir.singh05@gmail.com>
2024-02-08 10:37 ` Thomas Kellerer <shammat@gmx.net>
2024-02-08 11:25 ` Holger Jakobs <holger@jakobs.com>
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