Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a table counter_registry that has a column priority_number with its default value from nextval('counter_registry_priority_number_seq'::regclass). My trigger function (which runs before inserting) has a snippet that resets the sequence when there is no existing transactions on the date inserted:

-- Restart the priority number sequence if current day is a new day
IF (SELECT CASE WHEN NOT EXISTS ( SELECT * FROM counter_registry WHERE transaction_date = now()::date ) THEN true ELSE false END) = true
    THEN PERFORM setval('counter_registry_priority_number_seq', 1);
END IF;

When I insert a new record to the table on a new day from a client application, the priority_number column gets its value from the nextval of the sequence before the reset, even though it had already been reset. The subsequent inserts follows the newly reset sequence, starting at 1.

How do you properly reset the sequence, that after such, when I insert the first time, I would get the nextval of the altered sequence?

share|improve this question
    
Sidenote: you do not need the CASE construct, EXISTS(...) already yields a boolean value. – wildplasser Jun 29 at 10:38
    
@wildplasser yeah, sorry my bad. – Earl Lapura Jun 30 at 2:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.