Skip to content
atom®technologiesBook a call
Platform modernization

Choosing the first strangler slice off a legacy PostgreSQL core: plain read, locking read, or write

Pick a plain read slice when it delivers the learning you need, a locking read only when row-lock behavior is the learning and its evidence is in hand, and a write only when writes are required. Route production traffic after the lock-mode inventory and the tests that apply to that slice class.

A software team at a whiteboard

The direct answer

Make the first strangler slice a plain read path when a plain read delivers the learning you need. Make it a locking read only when the learning you specifically need is row-lock behavior, and only with its ROW SHARE conflict evidence and visibility tests in hand. Make it a write slice only when the learning can come from writes, and route production traffic only after the lock-mode inventory and the class-appropriate tests are complete.

Why the first slice is a sequencing decision

Martin Fowler's Strangler Fig essay, in a revision dated 22 August 2024 of a piece first posted in 2004, describes gradual modernization as building small additions on top of, yet separate from, the legacy code base, then moving bits of behavior into the new code base. He lists four high-level activities for this approach: understand the outcomes you want, decide how to break the problem into smaller parts, deliver the parts, and change the organization so it can continue; he notes the ordering does not imply a sequence. He also states that legacy systems rarely have clear components that can be replaced independently, so teams must identify seams to split the system, and that systems where the seams already exist are unicorns. Once seams isolate a few components, Fowler argues, replacing small components carries less risk, the business can reap value earlier, and the team learns more about replacing the legacy system and its business consequences. That learning argument is the crux of sequencing: the first slice exists to teach you how to replace the rest, so choose it for evidence value per unit of cutover risk, not for feature value.

Classify the candidate before committing

We propose, as a simplification rather than a documented fact, that you classify each candidate slice by its riskiest statement: plain read, locking read, or write. The simplification is useful because the PostgreSQL documentation on explicit locking gives each class a different table-lock footprint, but it is not exhaustive. A real slice can mix plain reads, locking reads, and writes in one transaction, and forcing it into a single class can skip the lock-conflict or visibility test it needs. When a candidate mixes classes, either split it along the mix or classify it by its riskiest statement and run the tests for every class present in that statement set. Under the proposed classification, a plain read runs SELECT, which acquires ACCESS SHARE on referenced tables, and ACCESS SHARE conflicts only with ACCESS EXCLUSIVE. A locking read runs SELECT with a row-locking clause, meaning FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE, or FOR KEY SHARE, which acquires ROW SHARE on those tables, in addition to ACCESS SHARE locks on any other tables referenced without an explicit locking clause; ROW SHARE conflicts with EXCLUSIVE and ACCESS EXCLUSIVE. A write runs UPDATE, DELETE, INSERT, or MERGE, which acquires ROW EXCLUSIVE on the target table, again in addition to ACCESS SHARE locks on other referenced tables; ROW EXCLUSIVE conflicts with SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, and ACCESS EXCLUSIVE. The documentation states the general rule: two transactions cannot hold locks of conflicting modes on the same table at the same time, while non-conflicting modes can be held concurrently by many transactions. From those conflict sets, a reasoned inference follows: a plain read slice has the narrowest table-lock conflict surface, a locking read sits in the middle because ROW SHARE conflicts with EXCLUSIVE and ACCESS EXCLUSIVE but not with ROW EXCLUSIVE or SHARE, and a write slice has the widest surface because ROW EXCLUSIVE conflicts with four modes. This ordering is our inference from the documented conflict sets, not a statement in the PostgreSQL documentation.

The visibility contract under Read Committed

Lock footprint is half the evidence; the other half is what the slice is allowed to see. The PostgreSQL transaction isolation documentation states that Read Committed is the default isolation level, and that a SELECT without a row-locking clause sees only data committed before the query began, in effect a snapshot as of the instant the query starts. It also states that two successive SELECT commands can see different data within a single transaction if other transactions commit changes between them. For row-targeting commands, the documentation warns that UPDATE, DELETE, SELECT FOR UPDATE, and SELECT FOR SHARE find target rows committed as of command start, but such a target row might already have been updated, deleted, or locked by another concurrent transaction. Two inferences follow, and we label them as such. First, because successive SELECTs can observe different committed data under Read Committed, a read-only slice that issues multiple statements per transaction has a visibility contract that a single-read test cannot prove; the test must exercise the multi-statement pattern the slice actually uses. Second, because a target row found at command start may already have been concurrently updated, deleted, or locked, a pre-cutover test for a write slice must include concurrent updates to the same target rows, not just single-threaded inserts. For a locking read slice, both inferences apply at once: the row-lock semantics are part of what you are learning, so the test must cover both the ROW SHARE conflict behavior and the visibility of the rows it locks.

Schema changes and maintenance during the cutover window

The lock-conflict question is not only about your own traffic. The PostgreSQL explicit-locking documentation states that most PostgreSQL commands automatically acquire locks of appropriate modes to ensure referenced tables are not dropped or modified in incompatible ways while the command executes, and that certain ALTER TABLE variants acquire SHARE UPDATE EXCLUSIVE, a mode that conflicts with SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, and ACCESS EXCLUSIVE. The ALTER TABLE documentation lists many schema-change subforms, but the excerpt we worked from is truncated before the per-subform lock levels, so we do not assign lock levels to specific subforms beyond that statement. The documentation describes PostgreSQL commands, not your cutover runbook, so verify each operation you expect to run during the window against the full ALTER TABLE and explicit-locking documentation rather than assuming its lock level. Then test your slice's statements concurrently with the operations you actually expect, and check whether the modes conflict.

The go/no-go evidence record

We propose, as a diligence step rather than a documented PostgreSQL behavior, a narrow test matrix with one row per slice class, and the tests differ by class. Every slice gets a lock-mode inventory: record the modes its statements acquire on every table they touch, including the ACCESS SHARE locks that locking reads and writes also take on other referenced tables, confirmed against the pg_locks system view, which the PostgreSQL documentation describes as the way to examine currently outstanding locks in a database server. Every slice also gets a lock-conflict test that runs its statements concurrently with the schema-change and maintenance operations expected during cutover. On top of those two, a plain read slice gets a read-visibility test of successive SELECTs under the chosen isolation level, run with concurrent commits in flight, shaped around the multi-statement pattern the slice actually uses. A locking read slice gets that same read-visibility test plus a row-lock test that exercises its row-locking-clause statements, whichever of FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE, or FOR KEY SHARE it uses, against concurrent writers to the same rows. A write slice instead gets a concurrent-write test against the same target rows. The go/no-go record is the filled-in matrix plus the isolation level and visibility assertions the slice depends on, signed by whoever owns the cutover.

Ownership and the conditions that change the call

We propose this split of ownership: the engineering lead classifies the slice, including any mixed-statement handling, and writes its visibility contract; the platform or database owner runs the lock inventory and the tests that apply to each class present in the slice; the CTO or VP Engineering reads the evidence record and makes the routing call. The recommendation changes under three conditions. If the learning you need is row-lock behavior specifically, a locking read slice goes first, but only with its ROW SHARE conflict evidence and both its visibility and row-lock tests complete. If the learning can only come from writes, a write slice goes first, but only with its full evidence record in hand: the lock-conflict test and the concurrent-write test. If your database runs at Repeatable Read or Serializable rather than the Read Committed default, the visibility contract differs; the PostgreSQL documentation notes its Repeatable Read implementation does not allow phantom reads, so re-derive the read-visibility test from the isolation level you actually use. And if a slice's visibility contract cannot be tested before cutover, that is a reason to shrink the slice or pick a different seam, not to route traffic and observe.