Search code examples
databasepostgresqlsequences

Postgresql: keep 2 sequences synchronized


Is there a way to keep 2 sequences synchronized in Postgres?

I mean if I have:

table_A_id_seq = 1
table_B_id_seq = 1

if I execute SELECT nextval('table_A_id_seq'::regclass)

I want that table_B_id_seq takes the same value of table_A_id_seq

and obviously it must be the same on the other side.

I need 2 different sequences because I have to hack some constraints I have in Django (and that I cannot solve there).


Solution

  • The two tables must be related in some way? I would encapsulate that relationship in a lookup table containing the sequence and then replace the two tables you expect to be handling with views that use the lookup table.