---
title: "One, Two, Skip a Few..."
description: "Postgres sequences pre-log 32 values to the WAL (SEQ_LOG_VALS), so replicas see sequence state as it would be after a primary crash."
url: "https://swe.quest/articles/one-two-skip-a-few-b1af2f9386afbc09"
canonical: "https://incident.io/blog/one-two-skip-a-few"
source: "incident.io"
published: "2021-07-12"
difficulty: "medium"
kind: "Incident"
reading_minutes: 8
tags: ["postgres","replication","incident-response","databases"]
---

# One, Two, Skip a Few...

A post on [incident.io](https://swe.quest/sources/incident-io.md), published 2021-07-12, 8 min read, Incident. Original: [https://incident.io/blog/one-two-skip-a-few](https://incident.io/blog/one-two-skip-a-few)

## Summary

Postgres sequences pre-log 32 values to the WAL (SEQ\_LOG\_VALS), so replicas see sequence state as it would be after a primary crash. When incident.io promoted a follower during a database upgrade, every per-organisation incident ID sequence jumped forward by up to 32, producing gaps like #INC-7 to #INC-39. Sequences guarantee monotonic values but not gap-free ones, and rollbacks can also skip numbers. The fix replaced them with a BEFORE INSERT trigger computing MAX(external\_id)+1 per organisation, backed by an index, and the post recommends keeping external IDs separate from internal primary keys.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Replication](https://swe.quest/tags/replication.md) (Distributed systems)
- [Incidents & postmortems](https://swe.quest/tags/incident-response.md) (Reliability & operations)
- [Databases](https://swe.quest/tags/databases.md) (Data & databases)

## Prerequisites

- PostgreSQL sequences ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- Postgres triggers ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- database replication basics ([easy posts on Databases](https://swe.quest/tags/databases.md?difficulty=easy))

## Keywords

PostgreSQL, nextval, SEQ\_LOG\_VALS, Write Ahead Log, German Tank Problem

## Related posts

- [Patching the PostgreSQL JDBC Driver](https://swe.quest/articles/patching-the-postgresql-jdbc-driver-47e9400eaa77f0d2.md): Zalando Engineering · 2023-11-08 · Medium · 10 min read · PostgreSQL, Replication, Event-driven architecture, Java & JVM, Open source
  Postgres logical replication slots for clients watching low-traffic tables never advance, so the server retains all WAL generated by other tables until disk fills and the node goes read-only.
- [Sequences and other volatiles in Neki](https://swe.quest/articles/sequences-and-other-volatiles-in-neki-867c9857ecacbacb.md): PlanetScale · 2026-10-14 · Hard · 14 min read · Query engines, PostgreSQL, Distributed databases, SQL
  Neki, PlanetScale's sharded Postgres, implements Postgres sequences by classifying sequence operations like nextval() as router-only, with an authoritative shard hosting all sequences so values are never duplicated across shards.
- [Why Your Staging Database Never Matches Production](https://swe.quest/articles/why-your-staging-database-never-matches-production-0082e7428bf22c68.md): Neon · 2025-12-12 · Medium · 8 min read · PostgreSQL, Testing, Schema migrations, Privacy
  Staging databases drift from production continuously in three ways: data drift (stale dumps, sequence values that diverge so the same ID points to different records), schema drift (hotfixes, skipped migrations, mismatched indexes and extensions causing query plan divergence), and transformation drift (anonymization that breaks uniqueness, referential integrity and determinism).
- [Mitigating Deadlocks in high concurrency environments](https://swe.quest/articles/mitigating-deadlocks-in-high-concurrency-environments-f958e9ca77901412.md): Shopify Engineering · 2024-08-14 · Medium · 3 min read · MySQL, Concurrency, Storage engines, Databases
  Concurrent batch upserts into an InnoDB table keyed by a sequential id can deadlock even when processes touch different rows, because the gap locks taken on unique-key collisions span neighbouring index entries that other processes also need.
- [Production Postgres on Railway: From First Deploy to High Availability](https://swe.quest/articles/production-postgres-on-railway-from-first-deploy-to-high-availability-70a3bcba84354fc9.md): Railway · 2026-10-15 · Medium · 13 min read · PostgreSQL, Replication, Resilience, Cloud platforms
  Railway Postgres can grow from a single private-networked database to a production setup with volume backups, point-in-time recovery, PgBouncer connection pooling and Patroni-based high availability, demonstrated with an orders API that uses idempotency keys so retries stay safe.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
