-- What a reorg overwrote. -- -- `block` is keyed on (chain, height) precisely so a replacement overwrites -- rather than accumulates — a schema keyed on the hash would inflate the losing -- fork's author forever. The cost of that choice is that the losing block is -- gone without a trace, and with it the only record that the height was ever -- contested. This table is that trace: one row per displacement, holding -- exactly what was about to be overwritten. -- -- It is not an attempt to keep the orphan itself. The node will not serve one -- for long — `blocks-pruning` defaults to `archive-canonical`, which discards -- non-canonical bodies once finality passes them, and Planck finalises about a -- hundred blocks behind the tip. Difficulty is worse: it is a state read, and -- state defaults to 256 blocks. So a few minutes after the reorg the chain -- itself can no longer answer for the block, and what is written here is all -- there will ever be. -- -- Which is why it holds the whole displaced row rather than just its hash. The -- author is the interesting field — it says who lost the race — and it costs -- eight columns of a table that gains a row only when the chain forks. create table block_displacement ( chain text not null references chain (id) on delete cascade, height bigint not null, -- The displaced block's hash. Addressable: a block route keyed on the hash -- can serve this even though `block` no longer mentions it. hash text not null, miner text not null, authored_at timestamptz, observed_at timestamptz not null, difficulty numeric, at_tip boolean not null, -- When the replacement arrived. On the same hash being displaced twice — -- a chain that reorgs back and forth across the same pair — this is the -- most recent time, and every other column is immutable for a given hash. displaced_at timestamptz not null default now(), primary key (chain, height, hash) ); -- "What else has held this height?", for the block page's replaced-by banner. create index block_displacement_chain_height_idx on block_displacement (chain, height desc); -- "Is this hash a block we once recorded?" — the lookup a hash-addressed route -- makes, which the primary key cannot serve without knowing the height first. create index block_displacement_chain_hash_idx on block_displacement (chain, hash);