Re: UNDO with FILEOPS, and the FLUX table AM

Поиск
Список
Период
Сортировка
Искать

Re: UNDO with FILEOPS, and the FLUX table AM

От:
Greg Burd <greg@burd.me>
Дата:
Вложения:
Attached is v4 which I hope fixes the various cf-bot build failures.

-greg

Re: UNDO with FILEOPS, and the FLUX table AM

От:
Manu <manuelreyesbravo@gmail.com>
Дата:
Вложения:
Hi Greg,

> Attached is v4 which I hope fixes the various cf-bot build failures.

It clears the ones I hit.  I measured v4 on master b28a2b2bfa5, against v2 for
the FILEOPS cost, and checked the per-prefix test claim.  Two findings, plus an
adversarial pass that came up clean.

1. ROLLBACK and crash recovery now pay two fsyncs per FILEOPS undo record

fileops_sync_inverse() fsyncs the file and its parent after every applied
inverse, and since FILEOPS can no longer be deferred that path runs inline at
ROLLBACK and in the crash-recovery undo phase.  N inverses on one file is 2N
fsyncs.  For N chmods in one transaction (ROLLBACK timed, median of 3), the
rollback goes from 2.5 ms on v2 to 660 ms on v4 at N=1000, and from 77 ms to
22.8 s at N=32000.  Crash-recovery undo of N in-flight records, from the
server's own log: N=8000 is about 7 ms on v2 and about 5.6 s on v4.  With
fsync=off, v4 at N=1000 drops from 638 ms to 1.4 ms, so the cost is entirely
the fsyncs, not asserts; the forward direction (doing the N chmods) is
unchanged from v2.

Durability of the inverse before the CLR is the point, but it can be batched:
fsync each distinct path (and its parent) once per chain, before the record
that marks the chain done, instead of once per record.  As it stands a
transaction that touched one file 10k times takes about 7 s to roll back and
adds about 7 s of undo before the cluster opens after a crash.

2. "every prefix builds and passes its own tests" does not quite hold

All of 0001-0008 build without warnings and pass make check.  Their own module
tests do not all run at their prefix:

- 0002: test_fileops/001 sets max_logical_revert_workers, a GUC added in 0005,
  so the server will not start -- the same shape as the split issue Shihao
  reported.  test_fileops is not listed in src/test/modules until 0005, so the
  build farm does not run it there.
- 0004: six recovery tests need the flux AM (0019) or test_idxundo (0006).
- 0005: eight test_idxundo TAP files ship here but the module arrives in 0006.
  And test_fileops/016 surfaces a real issue at this prefix: with an injected
  fsync failure on an inverse during abort, the backend raises ERROR inside
  abort, re-enters abort five times, and ends in PANIC: ERRORDATA_STACK_SIZE
  exceeded; the PANIC elevel that makes it one clean failure arrives in 0007.
  On the full series, 016 passes.
- 0008: recovery 094 and 121 need the flux AM.

The full list with the exact files is in the attached notes.

3. Adversarial pass: nothing to report

On an ASan+UBSan build I ran crash injection across plain ROLLBACK, savepoints,
postmaster and backend kills, a kill during ROLLBACK, a crash inside the
recovery undo phase, and 2PC.  Each time the file tree came back exactly to the
committed baseline, with no sanitizer report and no assert (a COMMIT control
diverges, so the check is live).

Repros and the full measurements are attached.

Regards,
Manu

Re: UNDO with FILEOPS, and the FLUX table AM

От:
Greg Burd <greg@burd.me>
Дата:
Вложения:
Hi Manu,

Thank you for this review, and for the repros.  Both findings were
right.  v6, attached, fixes both of them, along with the cfbot failures
from v4.  (I had a v5 ready for the cfbot failures but hadn't sent it
when your mail arrived, so its changes are in v6 too.)

> 1. ROLLBACK and crash recovery now pay two fsyncs per FILEOPS undo record

You were right, and batching was the right fix.  A rollback now notes
the path each inverse touches, plus its directory, in a backend-local
table.  It fsyncs each distinct path once, at the point where the
rollback is about to be recorded as complete:

 - online abort and ROLLBACK PREPARED: once, after all of the abort
   record's inverses have been applied, still before checkpoints are
   let through, and still a PANIC on failure (test_fileops/016);
 - abort redo: the same code, run by the standby's startup process;
 - the startup undo phase and the logical revert worker: at the end of
   the chain walk, before the walk's result is written to the ATM or
   the entry is forgotten.

The table is created when the transaction writes its first FILEOPS
UNDO, so the abort path never allocates it.  If an entry cannot be
added, that path is synced immediately instead.

test_fileops/017 counts the fsyncs through a new injection point that
reports each path it fsyncs.  For 200 chmods on one file, without the
fix it sees 400 fsyncs in each of online rollback, abort redo on a
standby, and startup undo of a crash loser.  With the fix it sees
exactly one fsync of the file and one of its directory in each case.
Your repro on an EC2 instance's NVMe volume (ext4, cassert build,
ROLLBACK only, median of 3):

    N chmods    before          v6 ROLLBACK
    200            521 ms          5.0 ms
    1000          2533 ms          6.6 ms
    8000         19365 ms         22.0 ms
    32000        75999 ms         72.7 ms

The startup undo phase for a crash loser of about 5,000 inverses went
from 12.6 s to 13 ms.  So your v2 point, "linear before open", still
holds, and the per-record cost is back near what you measured on v2.
What v6 adds over v2 is two fsyncs per distinct path per rollback.

> 2. "every prefix builds and passes its own tests" does not quite hold

Busted, apologies somehow I thought I'd kept that true but obviously
not.

 - 0002: test_fileops was not registered until 0005.  It is now
   registered in 0002.  t/001 there no longer sets
   max_logical_revert_workers, and no longer expects the ATM, which
   arrives in 0005.  t/007's DROP TABLESPACE case moves to 0003, where
   DROP TABLESPACE is routed through FILEOPS.

 - 0004: per-backend UNDO has no writer other than FLUX.  So the
   engine tests that roll back FLUX tables (recovery 073-075, 083-085,
   089, 096, 114, 117, 152, and test_checksums/025) now ship with FLUX
   in 0019.  0004 keeps recovery/071, which exercises the engine at
   that prefix.  An earlier version of recovery/146 shipped in 0004 as
   115, and 0019 deleted it; it no longer ships in 0004.

 - 0005 / 0007: test_fileops/016 was the real problem.  The hash
   commit carried the FILEOPS, ATM and abort-ordering fixes, including
   the PANIC for a failed inverse fsync during abort, and their tests.
   All of that belongs to 0005 and is now there, together with the
   fsync batching above.  0007 is hash only.  test_idxundo t/002-009
   ship with the module in 0006.  test_fileops/005 and recovery 126
   and 134 use the index_undo reloption, so they ship in 0006 as well.

 - 0005 also carried the old tuple sLog, which 0019 replaced.  It
   allocated a fixed 1.29 GB "sLog Flat Hash Partitions" area in
   shared memory at startup, with default settings.  That is why
   recovery/134 failed at 0005-0018.  0005 now adds
   the final, on-demand sLog, and the old one and slog_num_partitions
   are gone from the series.

 - 0008: recovery 094 and 121 need FLUX and ship with it.

 - test_idxundo/011 checks hash index UNDO, which arrives in 0007, so
   it ships there.

 - autoconf: src/test/recovery and test_fileops did not EXTRA_INSTALL
   every module their tests load (btree_gist, xid_wraparound,
   test_idxundo).  The meson build installs all of contrib, which is
   why only autoconf failed.

Running each prefix's tests and then load-testing the tip turned up a few
more test bugs, listed in the cover letter.  It also turned up one real
problem I have not fixed yet.  recovery/137 failed once under
EXEC_BACKEND with "FLUX undo redo: undo record pointer mismatch" while
replaying a FLUX DELETE after an immediate stop: the replayed undo insert
landed 136 bytes past the original.  It did not recur in 68 more runs.
My guess, not yet confirmed, is the undo-log insert-pointer side of
the checkpoint race that recovery/152 describes.  It is listed as a
known issue in the cover letter, and I'll chase it next.

> 3. Adversarial pass: nothing to report

Thank you for this one too.  The S0 negative control is a good idea,
and I'll add one to my own harness.  On the harness note: yes,
FileOpsRename is deferred to commit, so writing to the new name inside
the transaction fails.  storage/file/README says so ("DESTRUCTIVE
operations are DEFERRED"), but too quietly.  I'll make it more
prominent in the next version.


best.

-greg

Re: UNDO with FILEOPS, and the FLUX table AM

От:
Manu <manuelreyesbravo@gmail.com>
Дата:
Hi Greg,

> v6, attached, fixes both of them, along with the cfbot failures
> from v4.

Thanks, the fixes you describe are what I hoped for, and I was about to
measure them.  But I think the wrong tarball went out: undo-v6.tar.gz is v3
with a new version number.  Diffing each v6-00NN against v3-00NN, the only
line that differs in all 20 files is the "[PATCH v6 NN/20]" in the Subject;
the commit ids in the "From " lines are v3's (0001 is b7667c1ca7bf).
There is no cover letter, no test_fileops/t/017, slog_num_partitions is still
in 0005, and 0004 still adds 073-075 etc.  It also lacks v4's record
alignment fix, so cfbot will be testing v3 again.

Could you resend the real v6?  I'll run 017 on the three paths, the chmod
repro against v4 on the same machine, and the per-prefix check as soon as it
is out.

Regards,
Manu


Re: UNDO with FILEOPS, and the FLUX table AM

От:
Greg Burd <greg@burd.me>
Дата:
Вложения:

On Thursday, October 15th, 2026 at 8:26 PM, Manu  wrote:

> Hi Greg,

Hi Manu,

Apologies for the length of this email but there was a lot of churn and
this isn't a small set of changes.

> I think the wrong tarball went out: undo-v6.tar.gz is v3 with a new
> version number.

You're right, sorry about that, and thanks for checking before spending
time on it.  Not sure how I did that...

> > v6, attached, fixes both of them, along with the cfbot failures
> > from v4.

so, hopefully I've delivered v7 with:

 0001  cluster-wide UNDO engine (records in the WAL stream)
 0002  transactional filesystem operations (FILEOPS) backed by UNDO
 0003  route filesystem mutations through FILEOPS
 0004  per-backend UNDO logs
 0005  the ATM, sLog, and logical revert worker
 0006  nbtree: mark aborted index inserts dead via UNDO
 0007  hash: mark aborted index inserts dead via UNDO
 0008  nbtree: in-place delete-marking for stable-TID UPDATE
 0009  tableam: let an AM declare that UPDATE keeps a row's TID
 0010  executor: keep the pre-update row of an in-place UPDATE
 0011  EvalPlanQual: fetch an in-place AM's reference rows with the query snapshot
 0012  AFTER triggers: queue an in-place AM's row images with the event
 0013  logical replication: pre-check unique keys an in-place UPDATE changes
 0014  tableam: SO_RETURN_DELETED, and update_deleted detection for any AM
 0015  tableam: let a block AM's size estimate use its own default fillfactor
 0016  tableam: let REPACK (CONCURRENTLY) accept any AM whose changes are decoded
 0017  vacuum: add parallel_vacuum_set_index_stats() and a bulkdel injection point
 0018  pg_visibility: refuse tables of other table AMs in the tuple checks
 0019  the FLUX in-place-MVCC table access method
 0020  benchmarks: heap vs FLUX

The 0009-0018 commits are heap-neutral preparatory changes to the table-AM,
executor, trigger, EvalPlanQual, logical-replication, VACUUM and pg_visibility
interfaces; each is a no-op for heap and for any AM that does not opt in, and
all are ordered before 0019 (FLUX), which is the first commit that uses them.

As much as I want UNDO/FILEOPS/FLUX I really think the 0009-0018 series is
something we need to think over.  I've been running into other
table/index/executor issues over in my Tepid [1] thread, maybe the union of
these should be it's own thread?

The TL;DR for the benchmarks is that for OLTP workloads FLUX is at
0.83-0.98 of HEAP, row locking at parity, analytics 1.6-2x slower,
mostly because FLUX tables are 1.3-1.5x larger.  The main reason for
the size is a FLUX defect I'll fix next: it keeps varlenas' 4-byte
headers on the page where heap shortens them to one byte, that should
be easily adopted by FLUX code.

> Thanks, the fixes you describe are what I hoped for, and I was about to
> measure them.  But I think the wrong tarball went out: undo-v6.tar.gz is v3
> with a new version number.  Diffing each v6-00NN against v3-00NN, the only
> line that differs in all 20 files is the "[PATCH v6 NN/20]" in the Subject;
> the commit ids in the "From " lines are v3's (0001 is b7667c1ca7bf).
> There is no cover letter, no test_fileops/t/017, slog_num_partitions is still
> in 0005, and 0004 still adds 073-075 etc.  It also lacks v4's record
> alignment fix, so cfbot will be testing v3 again.
> 
> Could you resend the real v6?  I'll run 017 on the three paths, the chmod
> repro against v4 on the same machine, and the per-prefix check as soon as it
> is out.
> 
> Regards,
> Manu

So, v7 has everything I'd promised in v6's email plus it also fixes the two
things I said I'd do next, and a few bugs that a benchmark on bare-metal
hosts turned up since.

 - recovery/137's "undo record pointer mismatch" is fixed.  It was the
   insert-pointer side of the checkpoint race, as I suspected, but not
   only that.  A FLUX writer reserves its undo space before its critical
   section and inserts the forward WAL record after.  A checkpoint whose
   redo point falls in between, and whose undo-log snapshot is taken
   after the reservation, leaves recovery with a log that is already past
   the record.  Whether a META record puts it back depends on which redo
   point the writer saw.  The forward record now carries the record's
   whole placement, and redo installs it before re-running the insert.
   recovery/154 forces the window with an injection point: on v6 it fails
   in all four shapes it tries, and a heap control passes.  Under
   EXEC_BACKEND with CPU load, 137 failed 3 times in 192 runs on v6 and
   0 times in 384 with the fix.

 - FileOpsRename is now immediate and rolled back, like the other
   constructive operations, rather than deferred to commit.  The new name
   exists inside the transaction, which is what your harness expected,
   and a crash right after commit no longer loses it.  It refuses to
   replace an existing destination.  storage/file/README now opens with a
   table of when each operation takes effect and what undoes it.

 - The  pgbench row-lock table was described as "scale 100".  Its custom
   scripts used :scale without passing it, so they only drew rows from the
   first 100,000 accounts.  The letter says so now, and the new metal
    benchmarks use the whole table.

 - The metal benchmark (HammerDB TPROC-C and TPROC-H, pgbench, the
   ledger benchmark; x86_64 and aarch64) found a lost update in FLUX's
   same-size UPDATE path: it asked CLOG before the proc array whether the
   row's last writer had committed, so a writer that committed between
   the two questions was overwritten.  It showed up as a duplicate TPROC-C
   order id at 256 users on 128 cores.  Fixed, with a deterministic test
   (recovery/160).  It also found a checkpoint that failed on an undo
   segment the discard worker had just removed (fixed as md.c does,
   recovery/161), and two memory problems in an earlier candidate.

 - Rollbacks in the undo workers and the logical revert worker can now be
   interrupted, so a fast shutdown no longer waits for a large rollback,
   and the logical revert worker no longer PANICs on an ordinary error
   for an entry older than the last checkpoint.


Shihao, Matthias,

Thanks for the careful reviews.  All three of your reports were real
bugs that led to a much larger round of stabilization than I expected.
Hence the delay responding.


Your reports:

1. Patch-split mistakes (059/061 setting a GUC that only 0005 added; test
   modules only built in 0005).  Fixed: tests live with the feature they
   exercise, and every prefix of the series builds and passes its own tests.

2. CREATE DATABASE crash leaves the directory, for both wal_log and
   file_copy.  Fixed; the destination tree is now owned by FILEOPS and removed
   on abort or crash (recovery/090).  Following it up also found that a loser
   whose UNDO predated the last checkpoint was never rolled back; the
   checkpoint now records in-flight UNDO chains and recovery starts from them
   (test_fileops/005).

3. An ordinary filesystem error turning into a PANIC inside a critical
   section.  Your diagnosis was exact and applied to every immediate
   operation.  The syscall now runs before the critical section, so a failure
   is an ordinary ERROR with the usual message (test_fileops/004 / 016).


Correcting my earlier CTR claim... 

Earlier in this thread I said the ATM lets recovery accept connections without
replaying UNDO.  That is not entirely accurate.

 - FLUX table and index UNDO: a live rollback applies the UNDO inline before
   ROLLBACK returns, unless the chain exceeds undo_rollback_overflow_size
   (16 MB) and an undo apply worker has room.  After a crash, recovery queues
   the losers and the per-backend undo workers revert them AFTER the database
   is open, not the ATM.
 - FILEOPS: cluster-wide UNDO for a crash loser is applied BEFORE the database
   opens, linear in the loser's UNDO.

So "no UNDO replay before open" is true only for FLUX table/index UNDO,
via the per-backend workers, and not for FILEOPS.  The cluster-wide
engine keeps UNDO in the WAL stream, so a long transaction pins WAL and
recovery walks loser chains.

"Constant time recovery" (CTR) in the SQL Server sense (a persistent
version store, O(1) abort, bounded recovery) was inspirational, but
recovery as of this version doesn't match that implementation or
claim as yet.


FILEOPS: what it is and is not

FILEOPS makes the filesystem mutations the server does outside the
buffer manager (CREATE/DROP DATABASE and TABLESPACE directories,
copydir) participate in transactions and in crash recovery, at wal_level
>= replica.  It is NOT a replacement for smgr, and it is NOT a
self-contained crash-atomic resource manager yet, and I'm not sure that
should ever be the goal, but feel free to disagree with me on that.

Right now here are the honest limitations (also in the README and the
commit message):

 - a commit-time operation (delete/rmdir at commit) is logged after the
   commit record, so a crash in between can lose it (test_fileops/007 TODO);
   rename is no longer one of them (it is immediate since v7);
 - the syscall-before-WAL window;
 - per-transaction, not per-operation, ownership of a crash loser's inverses;
 - nothing for wal_level = minimal.

I've considered integration into the resource manager (self-contained
redo/undo from common WAL, durable intent before commit, per-operation
ownership) but I'm not sure where I'm going to take that idea next.  I'm
open to ideas here.


FLUX: a new table AM that uses UNDO (not a HEAP replacement)

FLUX is an in-place-MVCC table access method here not as competition
with HEAP but more an illustration/research/proof-of-concept for an
UNDO-based table AM, not meant for production use (at this time).

Attempts to "make HEAP better by using UNDO" were all unsuccessful; HEAP
represents a set of trade-offs that are proven, well optimized, and
understood by all who use Postgres.  The first rule of Database Club
is: "Never talk about Database Club", the second is "Don't break the
database." (or don't break what works). :-)

In FLUX, a row keeps its TID for its lifetime; the header carries
heap-shaped xmin/xmax/cid; older versions are reconstructed from the
per-backend UNDO log; row locks live in a shared, on-demand sLog (never
on the tuple).

Two FLUX notes worth stating up front:
 - index-only scans are not used on a FLUX table (TODO). A delete-marked 
   index entry can carry a key that no longer matches the row until VACUUM;
 - FLUX must never/will never depend on VACUUM for correctness; VACUUM
   reclaims space and freezes for wraparound.

The sLog that holds row locks allocates nothing at startup (a cluster that
never uses FLUX pays almost nothing) and grows on demand.

Measured on a quiet 64-vCPU metal host, release builds, a pgbench scale-100
database, all at 64 concurrent clients; each number is the median of five
interleaved 60-second runs, in thousands of transactions per second (higher
is better).  The custom scripts (all rows but -N) draw their rows from the
first 100,000 accounts only, a 1% hot set: they use :scale without passing
it, and pgbench reads the scale from the database only for its built-in
scripts.  The comparison is like for like, but it is a hot-set measurement;
the metal results below use the whole table.  Three FLUX builds are compared
against each other, with heap on the same v3 build as a reference:

 - FLUX/v2    : FLUX as posted in v2 (the old fixed-size row-lock table)
 - FLUX/v7    : FLUX in this series (the on-demand row-lock sLog)
 - heap/v3    : the heap AM on the v3 build, for reference

workload (what each pgbench transaction does)   FLUX/v2  FLUX/v7  heap/v3   v7 vs v2
---------------------------------------------   -------  -------  -------   --------
SELECT FOR UPDATE one row, then UPDATE it         293.0    306.5    359.0     +4.6%
INSERT a child row (foreign-key check on parent)  497.1    505.3    529.9     +1.6%
plain UPDATE (pgbench -N, no row locks)           229.3    243.6    291.7     +6.2%
SELECT 10 rows FOR UPDATE in one transaction      273.8    306.6    285.7    +12.0%
SELECT 100 rows FOR UPDATE in one transaction      20.4     33.0     35.1    +62.1%

So FLUX's row-lock path is at or above the v2 fixed-table store
everywhere, and now beats it by 12% (10-row) and 62% (100-row) at 64
clients.

What's in each patch?

0001  Add a cluster-wide UNDO engine with records in the WAL stream
      The UNDO substrate: records live in the common WAL stream; crash recovery
      reverts loser transactions' structural changes through a recovery pass.
      Changes since v2: 6 items (unused AM prepare hook removed; headerscheck
      skips undormgrlist.h; recovery-model docs made accurate; buffer content
      locks released before inline abort UNDO; failed-abort WAL-hold claim
      corrected; UNDO docs promoted to a chapter).

0002  Add transactional filesystem operations (FILEOPS) backed by UNDO
      WAL resource manager (RM_FILEOPS_ID) making structural filesystem
      mutations crash-safe and rollback-safe; AtPrepare path for 2PC.
      Changes since v2: report apply failures as failures; remove obsolete UNDO
      file cleanup. Tests: drop logical_revert_naptime in 063/061; show a crash
      right after commit loses deferred ops.

0003  Route filesystem mutations through FILEOPS
      Wire in-tree DDL/storage callers (CREATE/DROP DATABASE, TABLESPACE,
      copydir) through the FILEOPS API.  (No "Changes since v2" block.)

0004  Add per-backend UNDO logs
      Per-backend UNDO logs and the undo apply/discard workers; the engine FLUX
      table/index UNDO reverts crash losers from, after the database opens.
      Changes since v2: 38 items (2PC rollback chains; subxact undo dispatch;
      non-permanent/temp undo handling; 3-page record spanning; FPIs on undo
      pages; snapshot-limited recovery start; project formatting + pgperltidy).

0005  Add the ATM, sLog, and logical revert worker
      The aborted-transaction map, the on-demand sLog row-lock store, and the
      logical revert worker.
      Changes since v2: 24 items + 3 tests (retention report; inline UNDO always
      for non-index; new-database dir removed on abort/crash; roll back
      in-flight UNDO preceding the checkpoint; prepared row locks restored
      without clobbering owners; "a cluster that never uses UNDO costs what
      master does").

0006  nbtree: mark aborted index inserts dead via UNDO
      Changes since v2: 22 items + 2 tests (flush index UNDO before commit
      record; newest-first apply in crash recovery; CLR redo consistency-checked;
      index UNDO applied inline only).

0007  hash: mark aborted index inserts dead via UNDO
      Changes since v2: 14 items + 1 test (restore UNDO abort ordering; persist
      FILEOPS ownership for deferred crash losers; dead-tuples hint on hash
      rollback; PANIC only when an inverse cannot be made durable).

0008  nbtree: support in-place delete-marking for stable-TID UPDATE
      Changes since v2: 11 items + 1 test (uniqueness check asks the AM about a
      delete-marked entry; atomic delete-mark of a full leaf via replace split;
      amcheck handles delete-marked entries; tombstone trailer room).

0009  [prep] tableam: let an AM declare that UPDATE keeps a row's TID
0010  [prep] executor: keep the pre-update row of an in-place UPDATE
0011  [prep] EvalPlanQual: fetch an in-place AM's reference rows with the query
      snapshot
0012  [prep] AFTER triggers: queue an in-place AM's row images with the event
0013  [prep] logical replication: pre-check unique keys an in-place UPDATE changes
0014  [prep] tableam: SO_RETURN_DELETED, and update_deleted detection for any AM
0015  [prep] tableam: let a block AM's size estimate use its own default fillfactor
0016  [prep] tableam: let REPACK (CONCURRENTLY) accept any AM whose changes are
      decoded
      (adds TableAmRoutine.am_logical_decoding; REPACK tests it instead of
       naming heap.)
0017  [prep] vacuum: add parallel_vacuum_set_index_stats() and a bulkdel injection
      point
0018  [prep] pg_visibility: refuse tables of other table AMs in the tuple checks
      (0009-0018 have no "Changes since v2" block except 0016; they are the new
       heap-neutral interface preparation split out of the FLUX commit.)

0019  Add the FLUX in-place-MVCC table access method
      In-place MVCC table AM: a row keeps its TID; heap-shaped xmin/xmax/cid in
      the header; older versions from the per-backend UNDO log; row locks in the
      shared on-demand sLog, never on the tuple.
      Changes since v2: 60+ items (invisible aborted writers; idempotent UNDO
      keyed on version; helping; TOAST supersede; VACUUM reclaims aborted-INSERT
      rows and truncates the whole empty tail; tombstone-removal horizon
      reasoning; scan slots own a copy under a writer-excluding lock; VACUUM
      never moves a live row; row locks not held per-row until commit;
      user_catalog_table refused; project formatting + pgperltidy).

0020  Benchmarks: HEAP vs FLUX
      Benchmark scaffolding comparing heap and FLUX. (No "Changes since v2".)


Benchmarks: HEAP vs FLUX on metal

x86_64 c6id.metal (128 vCPU, 2 NUMA nodes) and aarch64 m7gd.metal (64
vCPU), 251 GiB, NVMe RAID0; external load drivers in the same placement
group; release builds; shared_buffers = 128 GB (50% of RAM); datasets ~2x
shared_buffers on HEAP.  HEAP and FLUX on the same binary (only
default_table_access_method differs).  Runs interleaved, starting AM
alternating by round, >= 10 minutes measured each, one golden restore per
run.  Ratio of medians FLUX/HEAP with a 95% bootstrap CI; above 1 is
better for FLUX, except TPROC-H power, which is time.  These were measured
on the v7 candidate BEFORE the lost-update and checkpoint fixes above;
TPROC-C was re-measured on the final tree ().

                                  x86_64                aarch64
 HammerDB TPROC-C, 2680 WH
   0.25x vCPU users               0.956 (0.928-0.984)   0.863 (0.853-0.873)
   0.5x                           0.952 (0.902-1.005)   0.890 (0.863-0.918)
   1x                             0.941 (0.872-1.017)   0.828 (0.811-0.845)
   2x                             0.896 (one run)       0.836 (0.806-0.866)
 HammerDB TPROC-H SF100
   power, geomean query time      1.60x slower          2.08x slower
   throughput (vCPU/8 streams)    0.230                 0.192
 pgbench row-lock set, whole
   table (scale 17000), 64/128
   clients                        0.89-0.98             (x86 only)
 pgbench row-lock set, 1% hot
   set (scale 100), 64 clients    0.90-1.03             (x86 only)
 ledger benchmark, 50M accounts
   mixed, 1x vCPU clients         0.852                 0.854
   hot                            -                     1.053

The series costs HEAP little net new overhead, on the whole-table row-lock 
et upstream runs 1.006-1.032x the series' HEAP (0.6-3.2%, all ten points;
the series adds the per-backend UNDO workers and the hooks of 0009-0018).

What this says about FLUX:

 - OLTP that fits in memory runs at 0.83-0.98 of heap.  Row locking on a
   hot set is at parity or ahead (FLUX writes no lock into the tuple:
   locking 100 rows writes 16.5 kB of WAL per commit vs heap's 37.7 kB).
   Plain updates write more WAL (pgbench -N 1,047 vs 894 bytes/commit).

 - FLUX scales with connections a little worse than heap: from 32 to 256
   TPROC-C users heap gains 1.37x and FLUX 1.28x on x86; pgbench -N from
   64 to 128 clients heap 1.51x, FLUX 1.44x.

 - Analytics is where FLUX loses, and mostly because its tables are
   1.3-1.5x larger for the same rows (indexes are the same size).  At
   SF100 heap's 226 GB nearly fits in shared_buffers plus the page cache
   and FLUX's 304 GB does not, so the same plans read from the device:
   Q14 reads 138M blocks vs heap's 16M, and with read I/O taken out runs
   1.5x heap.  The larger relpages also change three plans (Q7, Q17, Q18).
   The size has three causes, in order: FLUX stores varlenas with their
   4-byte header where heap converts them to 1-byte short headers (a
   FLUX defect, to be fixed); a 36-byte tuple header against heap's 23
   (the version-chain pointer and FLUX's own cmin/cmax); and a default
   fillfactor of 90, kept so that a row can grow in place.

The full report (per-query TPROC-H times, latency percentiles, WAL and
undo bytes per transaction, the plans of every query > 2x heap, settings
and method) is attached as v7-benchmark-results.md in the .tgz with the
patches.  It's early days for FLUX (and soon also RECNO) so much more
optimization to be had, and don't forget the missing bloat vs HEAP.

best.

-greg

[1] https://postgr.es/m/10eD4qtjEu2S7fepnPrBqxg73zuMbBCHN1MOEsbrwlPgOoGVzpzOchAF1GqaU8tKrIVCykGm7HVl6EUUonIheJNr9OQ2C5uajOLd1WLThlI=@burd.me

Re: UNDO with FILEOPS, and the FLUX table AM

От:
Greg Burd <greg@burd.me>
Дата:
Вложения:
Hello Manu, Matthias, Shihao, hackers,

v8 is attached, rebased onto current master.  Some significant changes and the
last series with the FLUX table AM and core changes required to enable it to
work.  The series is too long/large and I'd like to get some/any momentum for
the idea and FILEOPS in place or put the effort aside.

One interesting point for those working on TOAST alternatives is that FLUX in
this version has its own overflow management that doesn't use TOAST at all
(maybe interesting to you Hannu).

This is experimental, draft stage posted for design review of:

0001-0003 the UNDO and FILEOPS layers
0004-0008 are only useful to an UNDO-based table AM
0009-0018 are small heap-neutral hooks, and
0019-0020 FLUX exists to exercise all of it, not proposed for core.

Fixed in v7's UNDO and FILEOPS code (0001, 0004, 0005):

 - The undo launcher could hang for good.

 - The undo workers' round robin could skip the xid queue.

 - An abort that defers its UNDO to the ATM in some cases leaked
   its "Inline UNDO Apply" memory context.

 - The FILEOPS WRITE inverse treated a failed stat() of its file like a
   missing file and reported success, so a crash loser's overwritten
   bytes were never restored.

 - The logical revert launcher gave up on a database whose worker could
   not be registered (no free background-worker slot): it recorded the
   database as served anyway, so its ATM entries were never reverted and
   their WAL was kept for good.

 - When the revert worker's 64-slot retry table was full, a chain that
   failed to apply was given up on its first failure, marked reverted
   and forgotten.

and more.

More fixes in FLUX code (0019):

 - Relation extension race.

 - An in-place UPDATE's WAL diff was taken against the wrong old image.

 - The FSM can return a block at or past the end of the relation, which a
   plain ReadBuffer() rejects.

 - A DELETE or UPDATE reserved its UNDO record before pinning the
   visibility-map page, and that read can fail.

 - FLUX row locks keyed their lock tag on the relation's relfilenumber,
   where heap uses its OID.

and more.

FLUX-native overflow (0020, new):

In v7, FLUX pushed every wide value to an ordinary heap TOAST table; the
overflow pointer type and its redo existed but were never wired up.  0020
stores a wide value on FLUX's own pages, so it goes through the relation's own
buffers, WAL and UNDO.  In outline (the commit message has the details):

 - A value over a quarter page becomes data chunks on ordinary FLUX pages plus
   a root directory chunk listing every data chunk; one level of indirect
   directories covers values up to the varlena limit.  The tuple keeps a
   20-byte pointer to the root.  Values of 2 MB and more are read through a
   read_stream, since the directory names every block up front.

 - Each chunk is written under its own buffer lock and its own WAL record, so a
   writer never holds two chunk-page locks.  Holding several, acquired in FSM
   order by many backends, deadlocked at the buffer-lock level, where there is
   no deadlock detector (recovery/164).

 - Chunks carry no MVCC state.  A chunk lives while any version that names it
   is visible to someone, on the page or in UNDO.  VACUUM's orphan sweep marks
   what is reachable, walking each row's UNDO chain for superseded values, and
   frees the rest; it fails safe, abandoning reclaim for the relation rather
   than freeing on uncertainty.  Freeing is offset-stable and carries a standby
   conflict horizon.  Every reader checks a chunk's recorded lengths before
   following them, so a damaged page is reported as data corruption
   (recovery/171) instead of overrunning a buffer.  The same goes for a
   superseded value's image rebuilt from UNDO for that walk: one shorter than a
   tuple header is reported, where an earlier version of this commit read its
   xmin from past its end and gave an old snapshot a wrong answer
   (recovery/173), and a directory entry's offset is checked against its page's
   line pointers before it is followed (recovery/171).  Redo of a chunk write
   also sets the page commit timestamp the primary set, which a write to an
   existing chunk page, logged without a full-page image, missed
   (recovery/175).

 - Logically logged relations keep heap TOAST for wide values: reorderbuffer
   needs an oversized value as an external heap TOAST pointer, and the decoder
   has no FLUX relation to reassemble chunks from.

Measured against heap TOAST on a c6id.metal (128 vCPU, 4x NVMe RAID0), release
build of this series, fsync and full_page_writes on.  Values are uncompressible
md5-hex text, about 6 GB per size, 3 reps, a fresh table per rep.  In every rep
the md5 of every value matched between FLUX and heap, after the load and after
a 10% update.

Medians, FLUX/heap (below 1 is better for FLUX):

                          4 KB    32 KB   256 KB    1 MB
     total size           1.47    1.00    0.97     0.97
     load time            0.99    0.94    0.98     0.98
     load WAL             1.00    1.02    0.96     0.96
     full scan, cold      1.04    0.35    0.39     0.37
     full scan, warm      0.95    0.30    0.34     0.35
     10% UPDATE time      0.58    0.85    1.03     1.04
     10% UPDATE WAL       0.71    0.59    0.50     0.48
     VACUUM time          1.83    1.35    1.31     1.30
     point read time, 32 clients, warm

- Full scans of wide values are about three times faster: the chunks
  sit in the relation's own pages instead of being reached through a
  TOAST index.

- An in-place UPDATE writes about half the WAL.

- Point reads of very wide single values are slower.  A 1 MB value is
  about 129 chunks, read one at a time because that is below the 2 MB
  read_stream threshold, while TOAST fetches in index order.

- At 4 KB, FLUX is 1.47x larger.  Each chunk carries per-record and
  per-page overhead that is large next to a 4 KB value.  Heap also
  compressed about 23% of the 4 KB values, md5 text though they are,
  and FLUX compressed none, so that column favours heap.

- VACUUM is 1.3-1.8x slower.  Part of that is the orphan sweep, which
  reads the whole relation again; an earlier, separate measurement on
  a narrow-row table put it at about 3-4 s per 4 GB of cold reads.

- Next steps: make the sweep skip pages that hold no chunks, and add a
  per-chunk point-read path.

Under wal_level = logical, wide FLUX values go to heap TOAST as described
above; they decode through test_decoding, and scans match heap's (the
native-overflow advantage disappears, as expected).


V8 Series
---------

 0001  cluster-wide UNDO engine (records in the WAL stream)
 0002  transactional filesystem operations (FILEOPS) backed by UNDO
 0003  route filesystem mutations through FILEOPS
 0004  per-backend UNDO logs
 0005  the ATM, sLog, and logical revert worker
 0006  nbtree: mark aborted index inserts dead via UNDO
 0007  hash: mark aborted index inserts dead via UNDO
 0008  nbtree: in-place delete-marking for stable-TID UPDATE
 0009  tableam: let an AM declare that UPDATE keeps a row's TID
 0010  executor: keep the pre-update row of an in-place UPDATE
 0011  EvalPlanQual: fetch an in-place AM's reference rows with the query snapshot
 0012  AFTER triggers: queue an in-place AM's row images with the event
 0013  logical replication: pre-check unique keys an in-place UPDATE changes
 0014  tableam: SO_RETURN_DELETED, and update_deleted detection for any AM
 0015  tableam: let a block AM's size estimate use its own default fillfactor
 0016  tableam: let REPACK (CONCURRENTLY) accept any AM whose changes are decoded
 0017  vacuum: add parallel_vacuum_set_index_stats() and a bulkdel injection point
 0018  pg_visibility: refuse tables of other table AMs in the tuple checks
 0019  the FLUX in-place-MVCC table access method
 0020  flux: store wide values in FLUX-native overflow          (new)
 0021  benchmarks: heap vs FLUX

Next post will be 0001, 0002, and 0005 only.  The rest are interesting
only if the first three, and the idea of adding UNDO, gain traction.

best.

-greg

Re: UNDO with FILEOPS, and the FLUX table AM

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi Greg,

Thanks for v7. It is a lot of careful work. I would really like to see UNDO get in.

Matthias said the series does too much at once, and I agree. Maybe
it could be split into a few smaller pieces. Each one would come
with its own user.

I think crash cleanup of orphaned relation files would be a good first
user. It only needs undo at the end of recovery, so the first piece
could be much smaller than 0001. I think it only needs these parts:

  - one WAL record type that adds an undo record for a transaction
  - replay keeps these records in memory per xid, and drops them at
    commit or abort
  - checkpoint records carry the undo of open transactions
  - at the end of recovery, run the undo of transactions that did not
    finish, in reverse order
  - a small table of undo callbacks, and pg_waldump support

It needs no undo log files and no worker. Per backend logs and FILEOPS
could wait. It does not change ROLLBACK either, which already works.

The user itself would be a new hook in RelationCreateStorage(), and it
also has to handle relfilenode reuse.

I would guess that is under 1,000 lines of C. It could be two patches.

The first is the undo core and the second is the orphaned file user.
Today 0001 to 0005 add about 18,500 lines in 110 files. Both numbers
count C code outside tests, without comments or blank lines.

I also did some research and found [1] which has more information.
 
In addition, I think it will be super helpful if we have a commitfeast for
that patch. CF automatically generates github links and makes patches easy
to be reviewed in a UI tool.

Re: UNDO with FILEOPS, and the FLUX table AM

От:
Manu <manuelreyesbravo@gmail.com>
Дата:
Вложения:
Hi Greg,

On Sun, Oct 25, 2026 at 7:12 PM Greg Burd  wrote:
>  - The FILEOPS WRITE inverse treated a failed stat() of its file like a
>    missing file and reported success, so a crash loser's overwritten
>    bytes were never restored.

I tested this with a real EACCES instead of the injection point. A
crash loser overwrites the first 10 bytes of a file, and before the
restart the file's directory loses its search permission (the server
does not run as root).
v7: no ATM entry, the bytes stay overwritten (3/3 runs).
v8: the loser goes to the ATM, and once the directory is readable
again the worker restores the bytes (3/3).

That only holds while the error is short. If the directory is still
unreadable when the revert worker starts (I held it for 10 s), the
worker walks the chain 3 times, about 1 s and 2 s apart, logs
"giving up on 1 UNDO record(s) of transaction N that failed to apply
3 times" and forgets the entry. The bytes stay overwritten, as in v7
(3/3). So at the default logical_revert_naptime, an EACCES or EIO
that lasts more than about 3 s ends the way it did before, now with
a WARNING. Is that intended for a record that failed to apply? From
reading process_revert_entry(), LOGICAL_REVERT_MAX_RECORD_RETRIES
gives up on it the same way as on a malformed chain.

>  - The logical revert launcher gave up on a database whose worker could
>    not be registered (no free background-worker slot): it recorded the
>    database as served anyway, so its ATM entries were never reverted and
>    their WAL was kept for good.

I tested this with real slot exhaustion. worker_spi workers take
every free slot, an ATM entry is queued the way test_fileops/019
does it, and the launcher is woken and fails to register. Then the
worker_spi workers are terminated and the launcher is woken again.
v7: the entry is not drained after 90 s (past one 60 s scan), no
revert worker runs, and its WAL segment is still kept after 4
segment switches and a CHECKPOINT (3/3).
v8: the launcher retries and the entry is drained (3/3).

>  - When the revert worker's 64-slot retry table was full, a chain that
>    failed to apply was given up on its first failure, marked reverted
>    and forgotten.

Your test_idxundo/020 leaves 64 of 65 entries after one pass on v7
and fails (3/3). On v8 it leaves 65 of 65 and passes (3/3).

Each check also fails on v8 with only its fix reverted (3/3 each):
my two tests, your test_fileops/020 and your test_idxundo/020.

One thing for the 0001/0002/0005 post: the tests for the launcher fix
and the retry-table fix (test_idxundo/021 and 020) are in 0006, and
they need idxundo_atm_add() or an index_undo table. Without 0006
those two fixes have no test. The attached nocfbot diff adds my two
tests to test_fileops as t/021 and t/022. They only need
test_fileops, injection_points and worker_spi, so they apply on top
of 0005. I have not ported test_idxundo/020. t/021 takes HOLD= in
the environment (default 0) for the 10 s case, which fails on v8.

On master dfd7ede529f all 21 patches apply. 0001-0005 together
build with no warnings (cassert), and these pass: make check (239),
test_fileops (19 files, including 019, 020 and the two new ones),
and recovery 061-064, 071, 072 and 090.

Limits: cassert only, Linux, one machine, no timings. v7 here is the
full v7 series. fileops_undo.c and logical_revert_worker.c are the
same in v7-0005, except for one whitespace line.

Regards,
Manu

Re: UNDO with FILEOPS, and the FLUX table AM

От:
Greg Burd <greg@burd.me>
Дата:
On Sun, Oct 25, 2026 at 6:56 PM shihao zhong  wrote:
> Matthias said the series does too much at once, and I agree. Maybe
> it could be split into a few smaller pieces. Each one would come
> with its own user.

Hi Shihao,

Agreed, and that is what I'll do next.  The next version will be only
the first three commits, the cluster-wide UNDO engine and FILEOPS:

  0001  cluster-wide UNDO engine (records in the WAL stream)
  0002  transactional filesystem operations (FILEOPS) backed by UNDO
  0003  route filesystem mutations through FILEOPS

The thread will cover just those from then on.  The per-backend UNDO
logs, the ATM and logical revert worker, the index AM changes, the
table AM hooks and FLUX are set aside for now; they can come back later
as separate threads, each with its own user, if this part goes
anywhere.

FILEOPS is the user of the UNDO engine in that set, so it has the
shape you describe, an engine plus one consumer, though it is larger
than your sketch: 0001-0003 add about 17,000 lines, 12,500 of them C
outside the tests.

The series is already registered as CF 7389, so it has the cfbot GitHub
branch; I'll keep using that entry for the reduced series.

Thanks for the review, and for pushing on the scope.

best.

-greg




Re: UNDO with FILEOPS, and the FLUX table AM

От:
Greg Burd <greg@burd.me>
Дата:
Вложения:
Hi Shihao, Manu, Matthias, Hannu,

v9 is attached, and it is smaller and more focused on purpose and as
requested.  As I said on Oct 29, it is only the first three commits of
v8:

 0001  cluster-wide UNDO engine (records in the WAL stream)
 0002  transactional filesystem operations (FILEOPS) backed by UNDO
 0003  route filesystem mutations through FILEOPS

The per-backend UNDO logs, the ATM and logical revert worker, the index
AM changes, the table AM hooks and FLUX are no longer in the series.
I'd like to go slowly: get agreement on the engine and on one user of
it, and only then bring the other pieces back, one at a time and each
with its own user, if the foundation is accepted and committed.  If it
isn't, none of the rest should go in either.

On Sun, Oct 25, 2026 at 6:56 PM shihao zhong  wrote:
> I think crash cleanup of orphaned relation files would be a good first
> user. It only needs undo at the end of recovery, so the first piece
> could be much smaller than 0001.
...
> I would guess that is under 1,000 lines of C. It could be two patches.

v9 is not that small, I wish it were.  0001-0003 add about 15,800 lines;
counting as you did (C outside the tests, no comments or blank lines)
that is about 5,800, 3,300 of them in 0001.

The user is FILEOPS rather than relation files: it is what makes CREATE
DATABASE, ALTER DATABASE SET TABLESPACE and CREATE/DROP TABLESPACE leave
nothing behind when they roll back or crash. Orphaned relation files
would be a second user of the same engine, and a natural next patch, but
I'd rather have the engine reviewed against one user before writing
another.  If the consensus is that the first piece should be the smaller
design you sketched, I'm open to that; tell me and I'll cut 0001 down to
it.


What changed from v8's 0001-0003

 - Rebased onto current master.

 - Removed what only the set-aside commits used: an UNDO buffer manager
   with no caller, two bufmgr.c functions, the index AMs' batching entry
   points, per-backend fields in TransactionStateData, the nbtree/hash
   UNDO rmgr IDs, a revert worker LWLock tranche, and stubs and getters
   that had no caller even in v8.  About 1,700 lines.

 - Comments, log messages, the READMEs and the docs no longer describe the
   ATM, a revert worker, per-backend UNDO, index_undo or FLUX.  The UNDO
   chapter (doc/src/sgml/undo.sgml) is rewritten around FILEOPS; v8's
   version documented pg_stat_undo and index_undo, which do not exist in
   0001-0003.

 - Manu's Oct 26 review, as it applies without the revert worker.  With no
   ATM, a FILEOPS inverse that fails (a real EACCES, say) is tried once and
   not retried.  The startup undo phase used to count such a record as
   applied and log "1 transactions rolled back"; it now counts failures and
   ends with a WARNING saying how many records could not be applied
   (0001, test_fileops/024).  The inline-abort memory context fix, whose
   test was in v8's nbtree commit, has test_fileops/023 instead.  Manu, your
   t/021 and t/022 test the revert worker, so they wait for it.

 - Two fixes for Windows that v8 only had in later commits: undostats.c
   no longer marks its built-in SQL functions with PG_FUNCTION_INFO_V1,
   which MSVC rejected as a second declaration, and recovery/062 and 065
   skip their file-mode checks there.

 - recovery/065 now crashes the server before it closes the client.  It
   closed the client first, so the backend sometimes rolled the
   transaction back online before the crash, which runs into the gap
   below instead of testing the crash path.

Each commit message has a "Changes since v7" section with the details.


A known gap

An online ROLLBACK reverses FILEOPS changes in the backend, and no WAL
records that it did; redo of the abort record only drops the transaction
from UNDO tracking.  So a physical standby keeps a change the primary
rolled back, and so does the primary after a crash, once the abort record
has reached disk.  A crash before that is fine: the transaction is then a
loser and the startup undo phase reverses it.  v8 closed this in 0005 by
carrying the inverses in the abort record, and that left with 0005.  I'll
bring that into 0002 in the next version.

The CF entry is still 7389; I should probably re-title it.

best.

-greg

Re: UNDO with FILEOPS, and the FLUX table AM

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi Greg,

Thanks for v9,

I had a few thoughs here.

1. If a checkpoint comes after a transaction writes its undo, a crash
does not undo it. Replay starts at the redo point.  CREATE
DATABASE can hit this.

2. A crash in the middle of CREATE DATABASE still seems to leave the 
new directory behind. With wal_log the undo only calls rmdir(). 
That fails because the relation files are still inside. With file_copy 
the cleanup is only kept in memory, so the crash loses it.

3. These commands cannot run in a transaction block, so the
subtransaction and 2PC code is never used. Could it wait for a user?

Thanks,
Shihao

Re: UNDO with FILEOPS, and the FLUX table AM

От:
Greg Burd <greg@burd.me>
Дата:

On Tue, Nov 10, 2026 at 3:56 AM shihao zhong  wrote:

> Hi Greg,

Hi Shihao,

> Thanks for v9,
>
> I had a few thoughs here.

Most welcome as always.

> 1. If a checkpoint comes after a transaction writes its undo, a crash
> does not undo it. Replay starts at the redo point.  CREATE
> DATABASE can hit this.

You're right.  A loser whose UNDO batches all come before the redo point
is never seen by the startup undo phase, so nothing reverses it.  I
reproduced it with a FileOpsCreate() in an open transaction, a
CHECKPOINT, and an immediate stop: the file survives the restart.
Without the CHECKPOINT it is removed.

> 2. A crash in the middle of CREATE DATABASE still seems to leave the
> new directory behind. With wal_log the undo only calls rmdir().
> That fails because the relation files are still inside. With file_copy
> the cleanup is only kept in memory, so the crash loses it.

Also right, for both strategies, with or without a checkpoint in
between:

  WARNING:  FILEOPS UNDO MKDIR: could not rmdir "base/16399": Directory not empty

> 3. These commands cannot run in a transaction block, so the
> subtransaction and 2PC code is never used. Could it wait for a user?

That is true of the in-tree callers today; only test_fileops reaches
those paths.  I'd still like to keep them.  The engine's subtransaction
and PREPARE handling is the part of 0001 that any later user that does
run inside a transaction block will depend on, and it is easier to
review it now, against FILEOPS and its tests, than to bring it back
later as a change to an engine that has already gone in.  If others
would rather see 0001 without it, I'll take it out.

I found a few more oversights myself.  An ordinary filesystem error in a
FileOps* call is a PANIC again, as in your v2 review.  CREATE TABLESPACE
t LOCATION '/' takes the server down with "PANIC: could not chmod file
"/"".  v9 runs the system call inside the critical section, after the
WAL flush.

All four were fixed in v8, in the commit that v9 left out: the system
call before the WAL record, the in-flight UNDO chains carried in the
checkpoint, FileOpsMkdirTree() for the new database directory, and the
abort record carrying the inverses (the gap I described in the v9 mail).

v10 will bring those fixes into 0001 and 0002.

best.

-greg




Re: UNDO with FILEOPS, and the FLUX table AM

От:
Greg Burd <greg@burd.me>
Дата:
Вложения:
Hello again. 

v10 is attached.  It is the same three commits as v9, with the fixes for
the four problems in my last message.

 0001  cluster-wide UNDO engine (records in the WAL stream)
 0002  transactional filesystem operations (FILEOPS) backed by UNDO
 0003  route filesystem mutations through FILEOPS

 - A loser whose UNDO all precedes the checkpoint is now rolled back
   (Shihao's point 1; 0001, test_fileops/005).  Each online checkpoint
   writes an XLOG_UNDO_INFLIGHT record with the UNDO chain heads of the
   transactions in flight, and redo starts tracking them from it.
   InsertXactUndoData() holds off the checkpoint's redo-pointer phase
   while it inserts a batch and publishes the head, so a batch cannot
   fall between the two.

 - A crash or error part way through CREATE DATABASE leaves no directory
   behind, for either strategy (point 2; 0002 and 0003, recovery/066).
   The new database directory, and the destination of copydir(), are
   created with FileOpsMkdirTree(), whose UNDO removes the whole tree.

 - An ordinary filesystem error is an ERROR again, not a PANIC (0002,
   test_fileops/004).  An immediate operation now makes its system call
   first and WAL-logs it only once the call has succeeded.  That leaves a
   short window, between the call and its WAL record, in which a crash
   leaves the change in place with nothing to reverse it, as without
   FILEOPS; fileops.c and the README say so.  The other order closes the
   window only by making every filesystem error a PANIC.

 - The abort record carries the transaction's FILEOPS inverses, and abort
   redo applies them, so a standby, and the primary after a crash, end up
   with the files the primary had after ROLLBACK (0002, test_fileops/008
   and /010).  This is the gap I described with v9.  ROLLBACK PREPARED
   carries them too, from the 2PC state.  It applies them before it clears
   DELAY_CHKPT_START, which RecordTransactionAbortPrepared() sets since
   cea96cdfecb, so a checkpoint cannot move past the abort record while
   they are not yet durable.

All of these were in v8, in the commit that v9 left out, and come from
there.  Bringing them back also brought a few smaller FILEOPS changes v8
had made in the same place: FileOpsRename() renames at once and is
undone by renaming back (test_fileops/018); an inverse reverts only an
object that still shows the operation's effect, so a name reused since
is left alone; a rollback makes its inverses durable once per path
rather than once per inverse; and the abort data a transaction can
accumulate is capped, so the abort record itself cannot fail.

As I said, the subtransaction and two-phase paths stay.  They support
future behavior, so strictly not necessary right now.  Perhaps I should
separate them into a 4th patch to make that explicit.

Size

0001-0003 now add about 17,800 lines; counted as Shihao did (C outside
the tests, no comments or blank lines) that is about 6,800, of which
3,400 are in 0001.  v9 was about 5,800.  Most of the difference is in
fileops.c and fileops_undo.c but what this shows me is that I should
review 0001 to see if there are ways to break it down into morsel sized
bits easier to chew on.

Also, I wonder if there is a smaller table AM example I could come up
with that uses UNDO to bring that back in, or something else adjacent to
that so as to bring a few more layers back into the cake.  Bad idea?
Too early?  I feel that without that the series is less interesting but
with it too large.  I'll think on this a bit.

Thanks for the continued interest, this is not a small feature to
consider.

best.

-greg

Re: UNDO with FILEOPS, and the FLUX table AM

От:
Manu <manuelreyesbravo@gmail.com>
Дата:
Вложения:
Hi Greg,

The four fixes hold: your tests pass and each fails when I revert its
fix.  Two cases outside the window you document still end with files
that differ from what the committed transactions imply; the attached
oracle runs 186 scenarios.

On Wed, Nov 11, 2026 at 1:09 PM Greg Burd  wrote:
> The abort record carries the transaction's FILEOPS inverses, and abort
> redo applies them, so a standby, and the primary after a crash, end up
> with the files the primary had after ROLLBACK

Not for a crash loser: the primary's startup undo phase removes its
files, but a crash writes no abort record, so a streaming standby keeps
them until it is promoted.  I see it for create, mkdir, link, rename and
write, and on v9 for the two I tried there.  After a link, if another
transaction then creates that name, the standby's source file ends up
with that transaction's bytes.

FileOpsCreate() opens without O_EXCL and writes the create UNDO even if
the file existed, so a rollback unlinks a committed file, on the
primary, the standby and after a crash.  No in-core caller does that;
the first diff makes it an ERROR, as FileOpsMkdir() already does.

The hold-off in InsertXactUndoData() and ROLLBACK PREPARED keeping
DELAY_CHKPT_START have no test that fails without them.  The second
diff adds one for each (the hold-off one drives gdb, so take it as a
repro rather than a test for the tree), the crash-loser repro, and a
copy of 024 with three failures.

> Perhaps I should separate them into a 4th patch to make that explicit.

In 0001 the subtransaction and 2PC functions are about 140 of the 1,626
lines gcov counts; at most 58 lines run only under the tests using them.

Manu
FAQ