PostgreSQL: How to compact/renumber id for all tables, and reset sequences to max(id)?

Assuming your ids are generated from a bignum sequence, just RESTART the sequence and update the table with idcolumn = DEFAULT CAVEAT : If this id column is used as a foreign key by other tables, make sure you have the on update cascade modifier turned on For example: Create the table, put some data in, and remove a middle value: db=# create sequence xseq; CREATE SEQUENCE db=# create table foo ( id bigint default nextval('xseq') not null, data text ); CREATE TABLE db=# insert into foo (data) values ('hello'), ('world'), ('how'), ('are'), ('you'); INSERT 0 5 db=# delete from foo where data = 'how'; DELETE 1 db=# select * from foo; id | data ----+------- 1 | hello 2 | world 4 | are 5 | you (4 rows) Reset your sequence: db=# ALTER SEQUENCE xseq RESTART ; ALTER SEQUENCE Update your data: db=# update foo set id = DEFAULT; UPDATE 4 db=# select * from foo; id | data ----+------- 1 | hello 2 | world 3 | are 4 | you (4 rows).

Assuming your ids are generated from a bignum sequence, just RESTART the sequence and update the table with idcolumn = DEFAULT. CAVEAT: If this id column is used as a foreign key by other tables, make sure you have the on update cascade modifier turned on. For example: Create the table, put some data in, and remove a middle value: db=# create sequence xseq; CREATE SEQUENCE db=# create table foo ( id bigint default nextval('xseq') not null, data text ); CREATE TABLE db=# insert into foo (data) values ('hello'), ('world'), ('how'), ('are'), ('you'); INSERT 0 5 db=# delete from foo where data = 'how'; DELETE 1 db=# select * from foo; id | data ----+------- 1 | hello 2 | world 4 | are 5 | you (4 rows) Reset your sequence: db=# ALTER SEQUENCE xseq RESTART ; ALTER SEQUENCE Update your data: db=# update foo set id = DEFAULT; UPDATE 4 db=# select * from foo; id | data ----+------- 1 | hello 2 | world 3 | are 4 | you (4 rows).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions