Databases
Advisory locks, migration tools, and deploy coordination
A Kubernetes deployment scales from zero to three replicas simultaneously. All three pods boot and each tries to run the pending migration. Without coordination, all three apply it in parallel — one succeeds, two fail with duplicate-constraint errors, the app half-starts in an unknown state.
Postgres DDL transaction semantics
Most DDL in Postgres is transactional: BEGIN; CREATE TABLE; ALTER TABLE; COMMIT; either all succeeds or all rolls back atomically. Concurrent sessions see the change only after COMMIT (MVCC snapshot isolation).
Exceptions that cannot run inside a transaction block:
CREATE INDEX CONCURRENTLYREINDEX CONCURRENTLYALTER TYPE ... ADD VALUE(in PG 11 and earlier; transactional from PG 12+)
Migration tools wrap each migration in a transaction by default. Any migration containing a CONCURRENT operation needs a non-transactional mode annotation. Most tools support this:
- Atlas:
-- atlas:txmode none - golang-migrate:
-- migrate:notransaction - Prisma: not natively supported; use
prisma db executewith a raw SQL file
If you mix a CONCURRENT operation with other DDL in one migration file, split them into two files.
The advisory lock pattern for serialised migration runners
Multiple application servers booting simultaneously can each try to run pending migrations. Without coordination, two processes might apply the same migration twice or conflict on the schema_migrations table.
The standard fix is a Postgres advisory lock at migration start:
-- Acquired before any migrations run:
SELECT pg_advisory_lock(12345); -- 12345 = stable integer key for this app
-- Migrations run here ...
-- Released after all migrations complete:
SELECT pg_advisory_unlock(12345);pg_advisory_lock blocks until it acquires the lock. The first server gets it and runs migrations; subsequent servers wait. When the first finishes and releases, the next server acquires — but finds all migrations already applied and does nothing.
Most migration tools implement this automatically:
- golang-migrate: acquires
pg_advisory_lock(hashInt64(databaseName))at start. - Prisma: uses a
_prisma_migrationstable with row-level locking. - Atlas: uses
pg_advisory_lockwith a configurable key.
For non-blocking startup behaviour, use pg_try_advisory_lock with a retry loop:
-- Returns true if acquired, false if already held
SELECT pg_try_advisory_lock(12345);| Tool | lock_timeout support | Retries | DDL lint | Non-transactional mode |
|---|---|---|---|---|
| Atlas | Yes (config) | Yes (exponential backoff) | Yes (built-in + Squawk) | Yes (txmode none) |
| pgroll | Yes | Yes | Via view-layer safety | N/A (handles CONCURRENT internally) |
| Prisma Migrate | Manual (SET in SQL) | Manual | Squawk (separate CI step) | Limited |
| golang-migrate | Manual (SET in SQL) | External | Squawk (separate CI step) | Yes (notransaction) |
| Flyway | Manual (SET in SQL) | Manual | Pro edition only | Yes |
Deploy coordination: migration before pod rollout
The three common patterns:
-
Pre-deploy job (recommended): a k8s Job or CI step runs
migrate upagainst production before the rolling deploy. Code deploy is gated on migration success. Old pods handle traffic during migration; schema is additive (expand-only). This is the cleanest pattern — clear separation of concerns. -
Init container on each pod: each new pod runs
migrate upon boot; advisory lock serialises them. Simple but slow — every pod waits for the migration check, even if nothing is pending. -
Separate migration pipeline: migration is run manually or on its own cadence, fully decoupled from code deploy. Requires human coordination; error-prone at scale.
2026 senior default: separate pre-deploy job (pattern 1), gated on migration success. The migration job emits metrics; any failure pages on-call and blocks the code rollout.
- Atlas (recommended for Postgres teams)
- Declarative + lint + retries + dry-run
- pgroll (zero-downtime via virtual schemas)
- Views + triggers, PG 14+
- Prisma Migrate (TS/Node stacks)
- Schema-first, generates migration SQL
- Drizzle (TS/Node, lightweight)
- Schema-first, similar to Prisma
- golang-migrate (language-agnostic)
- Simple up/down SQL files
- Squawk (linter)
- CI: catches unsafe DDL in PRs
- Advisory lock acquired at startup
- Standard in golang-migrate, Atlas
Why this works
Why does pgroll use views instead of the direct schema? pgroll’s goal is to make a declarative migration that renames a column feel like a single-step operation to the developer. Underneath, it has to maintain compatibility for both old and new code simultaneously — views with triggers are the only Postgres-native mechanism to present two different column names backed by the same storage, without application changes to manage dual-write manually. The cost is operational complexity: debugging a migration incident requires understanding the view layer.
Three k8s pods boot simultaneously and each tries to run pending migrations. Without an advisory lock, what is the most likely failure mode?
Which migration tool offers built-in DDL linting, lock_timeout configuration, retries, and non-transactional mode for Postgres in 2026?
- 01How does pg_advisory_lock prevent duplicate migration execution across concurrent pods, and how do tools implement it?
- 02What is the recommended deploy coordination pattern for a k8s app, and why is it preferred over running migrations on pod startup?
- 03Why must CREATE INDEX CONCURRENTLY run outside a transaction, and how do you configure this in migration tools?
Postgres DDL is transactional — BEGIN; ALTER TABLE; COMMIT; rolls back atomically on failure — except for CREATE INDEX CONCURRENTLY and a few other operations that must run outside a transaction block and need a tool-level annotation. When multiple application pods boot simultaneously, all may try to run pending migrations; pg_advisory_lock serialises them by letting only one session run at a time. Atlas (declarative schema, built-in lint, lock_timeout, retries, dry-run) is the most complete Postgres migration tool in 2026; pgroll adds virtual-schema zero-downtime for teams with frequent breaking changes; Prisma/Drizzle provide schema-first ergonomics for TS stacks. The recommended deploy pattern is a pre-deploy migration job gated before the rolling pod deploy, with Squawk CI lint blocking unsafe PRs before merge.
appears again in140
- Why GraphQL gets N+1junior
- DataLoader mechanics: tick-boundary batchingmiddle
- Batch function contracts: ordering, shapes, errorsmiddle
- Federation and lookahead: batching beyond DataLoadermiddle
- Query complexity defences: depth, cost, persisted queriesmiddle
- Senior GraphQL API: scheduling contract, tenant isolation, observabilitysenior
- Why idempotency: making retries safejunior
- Server-side state machine: four states of an idempotency keymiddle
- Outbox and inbox: effectively-once across the dual-write boundarymiddle
- Concurrency and cache architecture for idempotency at scalesenior
- Observability, production failures, and global-scale designsenior
- The event loop: one thread, three queuesjunior
- Tasks, microtasks, and scheduler.yield()middle
- Microtask starvation, Long Tasks, and LoAFsenior
- Node.js event loop: phases, nextTick, and loop lagsenior
- React, Vue, and INP observability in productionsenior
- The render pipeline: six stages from bytes to pixelsjunior
- Stage costs and the renderer process modelmiddle
- Invalidation, dirty bits, and containmiddle
- Compositor layers: promotion, overlap, and GPU memorymiddle
- DevTools flame strip and the frame lifecyclemiddle
- Layout thrash: forced synchronous layoutsenior
- BeginMainFrame, compositor-driven animations, and GPU memorysenior
- Production observability: LoAF, INP, and the full attack surfacesenior
- What V8 is and why performance varies 100×junior
- V8''''s four-tier JIT pipeline and profile-guided tieringmiddle
- Hidden classes, transition trees, and memory layoutmiddle
- Inline caches, IC states, and deoptimizationmiddle
- Orinoco GC: parallel scavenger, concurrent marking, and write barriersmiddle
- TurboFan''''s speculative engine and the deopt-loop trapsenior
- V8 in production: isolates, pointer compression, and real failuressenior
- Service worker lifecycle and cache strategiesmiddle
- Service worker edge cases: version skew, durability, and navigation trapssenior
- What the reconciler does: render vs commitjunior
- The fiber object and the double-buffer treemiddle
- Render phase purity and commit phase sub-stepsmiddle
- Reconciliation: diffing heuristics and the key trapmiddle
- Priority lanes, time-slicing, and useTransitionmiddle
- Bailout, memoisation, and tearingsenior
- React Profiler, the Compiler, and production observabilitysenior
- Rendering strategies: SSG, SSR, ISR, streaming, and hydrationjunior
- SSG, SSR, ISR, streaming, and RSC — how each worksmiddle
- Hydration cost: selective, progressive, islands, resumabilitymiddle
- Hydration mismatch: causes, detection, and the determinism rulesenior
- RSC, per-route strategy, and production observabilitysenior
- Core Web Vitals: what LCP, INP, and CLS measurejunior
- CLS: why layout shifts happen and how to stop themmiddle
- Metric tradeoffs, RUM attribution, and the CI+field loopsenior
- The full picture: URL to LCP to INP as a relay racejunior
- Eight layers traced: from the service worker to the second navigationmiddle
- Five canonical breaks: where production reliably diessenior
- The three-track method: reading traces and building a monitored systemsenior
- What is a cache stampede and why it makes things worsejunior
- Lock and single-flight: bounding concurrent rebuildsmiddle
- XFetch: coordination-free probabilistic early expirationmiddle
- Stale-while-revalidate and CDN request coalescingmiddle
- Detecting stampedes and designing TTL for productionmiddle
- Metastable failure, fencing tokens, and production postmortemssenior
- Raft roles, terms, and why majority quorums prevent split brainjunior
- How Raft replicates a log entry and decides it is safe to commitmiddle
- Raft leader election: timeouts, voting rules, and the four safety propertiesmiddle
- Raft in the real world: partitions, slow disks, and client routingmiddle
- Raft extensions: pre-vote, learners, snapshots, and linearizable readssenior
- Raft in production: membership changes, Multi-Raft, and observabilitysenior
- Where data fetching happens — and why it decides LCPjunior
- Fetch waterfalls — diagnosis and the Promise.all curemiddle
- React Server Components and Suspense streamingmiddle
- Client-side cache: TanStack Query, SWR, and stale-while-revalidatemiddle
- LCP, prefetch, and race conditions in interactive fetchingmiddle
- Senior internals: RSC payload, caching layers, and production failure modessenior
- The three-way handshakejunior
- Sequence numbers and connection statemiddle
- DNS: what it does and why it existsjunior
- The resolver walk: referrals, record types, and gluemiddle
- TTL, caching, and DNS propagationmiddle
- The 1-RTT handshake: key shares and ECDHEmiddle
- Session resumption and 0-RTTmiddle
- WebSocket: the HTTP upgrade handshakejunior
- WebSocket frame format: opcodes, masking, fragmentationmiddle
- WebSocket backpressure: when clients can''''t keep upmiddle
- Reconnection: jittered backoff, thundering herd, message resumptionsenior
- WebSocket at scale: HTTP/2 multiplexing, permessage-deflate, C10Msenior
- WebSocket in production: proxies, security, and distributed architecturesenior
- What reverse proxies dojunior
- Health checks, connection draining, and slow startmiddle
- Session affinity, consistent hashing, and the right fixmiddle
- Retry storms, circuit breakers, and load sheddingsenior
- Resilient LB architecture: anycast, zone-aware routing, and observabilitysenior
- Why QUIC and not TCP+TLSjunior
- Connection IDs and network migrationmiddle
- 0-RTT resumption and packet encryptionsenior
- DDoS: what it is and why it worksjunior
- Amplification attacks and state exhaustionmiddle
- Rate limiting: algorithms and architecturemiddle
- WAFs, firewalls, mTLS, and HSTSmiddle
- DNS cache poisoning and BGP hijackingsenior
- Defense-in-depth architecture and attack economicssenior
- DNS, TCP, TLS in sequence: where the milliseconds gomiddle
- Proxy intercepts and security gates: rate limiters, WAF, mTLSmiddle
- Alternate paths: QUIC 0-RTT, WebSocket upgrade, connection migrationmiddle
- Observability: distributed traces, USE/RED, and samplingsenior
- Resilience: cascading retries, circuit breakers, and error budgetssenior
- What the three signals are: logs, metrics, and tracesjunior
- Why structured logs exist: the diary vs the spreadsheetjunior
- The production log schema: fields every line must carrymiddle
- PII redaction and log injectionsenior
- OTel Logs Data Model and audit logs as a subsystemsenior
- SLI, SLO, and the error budget: reliability by the numbersjunior
- Error budget policy, latency SLOs, and composite journeysmiddle
- Production SLO failures, self-observability, security, and the big picturesenior
- The incident loop: from pager to postmortem to preventionmiddle
- Cache lines, struct layout, and false sharingmiddle
- SIMD, SoA vs AoS, and memory bandwidthmiddle
- Cache-oblivious algorithms, PGO, and production failuressenior
- GC in production: observability, security, edge cases, and fleet governancesenior
- Batching: amortize fixed cost per operationjunior
- The batching window: size and wait timemiddle
- Batching in Kafka and Postgresmiddle
- io_uring and observability of batchingmiddle
- From Nagle to io_uring: evolution of batchingmiddle
- Backpressure, failure isolation, and batch security in productionsenior
- CI enforcement and RUM: making budgets stickmiddle
- V8 JIT pipeline, HTTP priorities, and bundle securitysenior
- The performance loop: discipline, not a projectjunior
- Classify and fix: matching bottleneck families to remediesmiddle
- Observability stack and CI gates: catching regressions before they shipmiddle
- Incident to enforcement: SLO burn to verified fix in 35 minutesmiddle
- Culture, economics, and org-scale performancesenior
- At-most-once, at-least-once, exactly-once: the three delivery contractsjunior
- The three failure legs — where duplicates and losses actually happenmiddle
- Consumer-side dedup: the cheapest path to exactly-once processingmiddle
- Kafka exactly-once semantics: idempotent producer and transactionsmiddle
- SQS visibility timeout, DLQ, and the outbox patternmiddle
- Exactly-once in production: impossibility proof, hybrid patterns, and real incidentssenior
- What OAuth is and why passwords are not the answerjunior
- Authorization code flow with PKCEmiddle
- ID token validation and JWKS cache managementmiddle
- Refresh token rotation and scope-based least privilegemiddle
- Sender-constrained tokens: DPoP and mTLSsenior
- OAuth in production: audience attacks, observability, and real failuressenior