Is there a simple (ie. non-hacky) and race-condition free way to create a partitioned sequence in PostgreSQL. Example:
Using a normal sequence in Issue:
I do not believe there is a simple way that is as easy as regular sequences, because:
nextval('myseq')
; but it cannot refer to other columns to inform the function which stream the value should come from.You can make something that works, but you probably won't think it simple. Addressing the above problems in turn:
multiseq (partition_id, next_val)
.Write a multinextval(seq_table, partition_id)
function that does something like the following:
seq_table
.partition_id
, with an incremented value. (Or insert a new row with value 2 if there is no existing one.)Create an insert trigger on your projects table that uses a call to multinextval('projects_table', NEW.Project_ID)
for insertions.
I have not used this entire plan myself, but I have tried something similar to each step individually. Examples of the multinextval
function and the trigger can be provided if you want to attempt this...