Aurora Postgres Sequences Skipping Values

0

I am seeing my Aurora Postgres sequence values skip by 33 on a semi-consistent basis. I am aware of a thread from the PG mailing list saying that when PG recovers it can cause a sequence skip. Also if a large transaction rolls back then any sequence values updated during that transaction will remain at their new value and won't be rolled back. I get that - its the whole point of a sequence. But in my case nothing is happening and boom tomorrow morning the sequences have skipped ahead by 33. This article discusses other reasons that can cause a sequence skip: https://www.cybertec-postgresql.com/en/gaps-in-sequences-postgresql/

But I am not seeing any of those events. This appears to happen randomly.

Anyone else seeing this?

I migrated from RDS/Postgres and never experienced this. This just started on the migration to Aurora Postgres.

2 Answers
1

Hi Naveen - thanks for the answer but it turned out that wasn't the issue.

For anyone else encountering this scenario; we created a support ticket and ultimately AWS Support provided us with this answer:

The root cause of the gap in sequence values is a behavior caused by an implementation difference between Aurora Postgres and community Postgres. In community Postgres, these gaps in sequence values can occur during cancelled transactions or database crashes, but in Aurora, this behavior can additionally occur when sequence pages are evicted from the buffer cache. Increasing the parameter shared_buffers, may decrease the frequency at which these gaps occur, however there is no way to guarantee a true "gapless" assignment of sequence numbers.

In our case we were also running our own pg_dump operations against the primary instance a couple times a day. This is safe to do on stock Postgresql. But it appears that during a pg_dump and experiencing large transactions / activity this can occur. We implicitly solved the problem by spinning up a replica and performing the pg_dump on that host and the problem went away.

cody
answered a year ago
0

Most likely the sequence is setup with a CACHE (CREATE SEQUENCE my_seq INCREMENT BY 1 CACHE 25). Sequence cache is stored in the database cache and it can age out, just like other information (data blocks, sql plans) from memory. If you set the CACHE to 1, you won't lose values, but you will incur a small cost every time the application needs to retrieve the sequence next value.

AWS
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions