@@ -1414,7 +1414,12 @@ In an index with an extracted column one row may have several entries on
14141414the leaf, but the table AM takes each TID at most once (heapam's sort of
14151415the TIDs refuses two equal ones), so the TIDs are sorted and each is
14161416offered once, promising if any of its entries is, with the free space of
1417- all of them; its answer applies to every entry of that TID.
1417+ all of them; its answer applies to every entry of that TID. In an index
1418+ that stores version tags the answer is per entry, (TID, tag): a row's
1419+ stale entries are dead while its current ones live, so a TID is offered
1420+ once per tag, and only entries of one TID and one tag share an answer.
1421+ (Merging by TID alone made the dead stale entry's answer delete the
1422+ row's live entries with it, and an index scan then lost the row.)
14181423
14191424OVERSIZED entries are not offered to the table AM: deleting one has to free
14201425its overflow chain once the leaf no longer references it, which VACUUM does.
@@ -2119,8 +2124,10 @@ builds only, on by default there) turns both off; elsewhere the calls
21192124compile away. t/015_page_validator.pl in src/test/modules/bark plants
21202125faults through the injection points bark-verify-fault-count,
21212126bark-verify-fault-order, bark-verify-fault-far, bark-verify-fault-meta
2122- and bark-verify-fault-overflow, and bark-verify-force-full makes the
2123- next check of a few entries a whole-page one.
2127+ and bark-verify-fault-overflow (t/008_bark_mktags.pl of
2128+ test_versioned_tableam through bark-verify-fault-mktags, -converting and
2129+ -settled), and bark-verify-force-full makes the next check of a few
2130+ entries a whole-page one.
21242131
21252132Its cost, in a cassert build on c7i.4xlarge: a write-only workload of
21262133long equal-key LISTs (60,000 inserts into a table with three BARK
@@ -2695,6 +2702,19 @@ first; only the table AM reads tags modularly, if it likes. A LIST or
26952702POSTING entry's members are untagged. bark_compare_itups_tid compares a
26962703(key, TID, tag) with an entry or a pivot.
26972704
2705+ The order is strict: no two entries share a (key, TID, tag), as no two
2706+ share a (key, TID) in an index of heap's. A table AM may still hand an
2707+ insert a (key, TID, tag) the index has: it may give several versions of a
2708+ row one tag (heap_versioned tags each with its transaction ID), and an
2709+ update that keeps the locator inserts the row's full key set, so A -> B ->
2710+ A in one transaction, or a key two updates of a multikey column both keep,
2711+ inserts an entry twice. The second insert finds its copy just before its
2712+ insert offset and stores nothing (bark_insert_key); if a scan marked the
2713+ copy LP_DEAD, it clears the mark (an OVERWRITE of the same entry), since
2714+ the insert says that version is live. A second copy would break the order
2715+ that searches, splits and amcheck rely on, and an index scan would return
2716+ the row through both.
2717+
26982718Pivots. Suffix truncation keeps a tag only where it separates: when
26992719lastleft and firstright are equal on every key attribute and on heap TID
27002720(two entries of one row), the high key keeps firstright's TID and tag;
@@ -2853,8 +2873,122 @@ answers false to amindexmaykeeplocator (bark_indexmaykeeplocator, read
28532873from rd_index at run time), so an update that needs new entries in it
28542874moves the row, and a constraint check never meets two versions at one
28552875locator through an update that began after the change. An update whose
2856- statement began before it can still keep the locator; waiting those out
2857- is the conversion barrier, left to a later change.
2876+ statement began before it can still keep the locator: DocumentDB's
2877+ prepareUnique holds only AccessShareLock on the table, so nothing waits
2878+ for it. While that update is in progress, a dirty snapshot sees two
2879+ versions at its locator, v0 (being replaced) and v1, each reached only
2880+ through its own entries. Rule D's R, keyed by the locator, would return
2881+ the first the scan reaches and skip the rest: with v0 = {1,2}, v1 =
2882+ {2,3} and a check for {2,3} under EXCLUDE (doc WITH =), the stale
2883+ (2,L,0) returns v0, its recheck fails, and v1 is never compared, so a
2884+ duplicate commits. So a scan under a non-MVCC snapshot of a multikey
2885+ index with MKTAGS whose rd_index says unique or exclusion
2886+ (BarkMkScanState.unsettled, set in bark_mk_rescan) puts no row in R but
2887+ one: the row this backend's transaction inserted last into the index
2888+ (bark_is_last_insert), which is the row a check right after its insert is
2889+ about, and which core must not meet twice ("found self tuple multiple
2890+ times"). That row has one version a dirty snapshot sees, since nobody
2891+ else can update a row whose insert is in progress. Every other entry is
2892+ judged by itself, the table AM returns the version that entry is valid
2893+ for, and every row comes back with xs_recheck, since a version need not
2894+ hold the key that reached it. A check of a row other than the last
2895+ inserted (a deferred exclusion recheck, or the check after a HOT or
2896+ kept-locator update, which inserted nothing) can then meet its own row
2897+ twice and raise that error: a refused statement, not a wrong answer. The
2898+ rule holds until VACUUM's barrier has passed (BARK_META_SETTLED, below),
2899+ read from the meta page at each rescan.
2900+
2901+ The meta page records the conversion in two bits beside MKTAGS.
2902+ BARK_META_CONVERTING says the conversion has begun: the first BARK call
2903+ that sees MKTAGS and an rd_index unique or exclusion sets it
2904+ (bark_note_conversion: the first insert of a statement, through
2905+ bark_insert_state; the non-MVCC rescan of a constraint check; VACUUM's
2906+ bulkdelete, or its cleanup when bulkdelete did not run), under the meta
2907+ page's exclusive lock, logged whole, as BARK_META_MULTIKEY is. The
2908+ caller read the bits unlocked, so they are checked again under the lock
2909+ (bark_change_conversion_flags; the injection point
2910+ bark-conversion-flags-before-lock is just before it): a backend that finds
2911+ CONVERTING set by another, or MKTAGS gone because a VACUUM completed the
2912+ conversion meanwhile (only a scan under AccessShareLock can be outlasted
2913+ so), changes nothing. Nothing in recovery sets it. From then on
2914+ bark_indexmaykeeplocator answers false whatever rd_index says, so a
2915+ constraint dropped mid-conversion does not let updates keep locators
2916+ again: the barrier must see every writer that could still add a tagged
2917+ entry. amindexcanversion stays MKTAGS: an update
2918+ that began before the conversion and keeps its locator still needs its
2919+ entries tagged. BARK_META_SETTLED says VACUUM's barrier has passed, so
2920+ no update that began under the old rd_index is still in progress.
2921+ CONVERTING without MKTAGS, and SETTLED without CONVERTING, are corruption,
2922+ which amcheck and the page validator report (bark_verify_meta; the
2923+ injection points bark-verify-fault-converting and -settled plant them, on
2924+ the primary and in redo).
2925+
2926+ bark_index_converting's answer is read again in each transaction
2927+ (BarkAmCache.convlxid), never kept longer: the bit is set outside any
2928+ catalog change, so no invalidation tells other backends. That is enough
2929+ for the barrier. A transaction that read the bit clear may keep locators
2930+ until it ends, but it took its lock on the table first, and the bit was
2931+ set after; a later transaction reads it set. A backend that set the bit
2932+ for a catalog change that then aborted leaves it set: the conversion runs
2933+ to its end anyway, as after a constraint dropped mid-way.
2934+
2935+ The barrier (bark_convert_barrier) runs in VACUUM: in bulkdelete, and in
2936+ cleanup when this transaction has not tried it (no bulkdelete ran, or a
2937+ parallel worker ran it; workers do not run it, since they would wait for
2938+ the leader's own lock). It waits out every transaction holding a lock on
2939+ the table that conflicts with ShareLock, WaitForLockers as CREATE INDEX
2940+ CONCURRENTLY waits out writers (bark_convert_wait; the injection point
2941+ bark-convert-barrier-wait is before the wait). Every update that may keep
2942+ a locator under the old rd_index holds RowExclusiveLock until its
2943+ transaction ends; VACUUM's own ShareUpdateExclusiveLock is not counted
2944+ (GetLockConflicts skips the caller). An autovacuum worker does not wait,
2945+ since an idle-in-transaction writer could hold it indefinitely: it asks
2946+ GetLockConflicts and passes only when the answer is empty
2947+ (bark-convert-autovac-busy marks the other case); the next VACUUM tries
2948+ again. A barrier that passes for the first time sets SETTLED, then waits
2949+ once more: an insert stores entries untagged only once it has read
2950+ SETTLED (BarkInsertState.untag, read once per statement), so a statement
2951+ that read the meta page just before may still add a tagged one, and the
2952+ second wait outlasts it. Every later VACUUM waits again before it counts.
2953+ The barrier runs once per index and transaction, which a VACUUM of one
2954+ table is.
2955+
2956+ From SETTLED on, inserts store their entries untagged, even when the table
2957+ AM hands a tag: every update moves the rows it changes (CONVERTING), and a
2958+ new locator has no history, so an untagged entry is valid for it. The
2959+ simpler rule "no tagged entry can be added after SETTLED" would
2960+ not hold: canversion stays MKTAGS, and the executor tags every entry
2961+ of an index that can store tags, moved row or not.
2962+
2963+ Completion (bark_convert_complete). A VACUUM pass that passed the
2964+ barrier counts the tagged leaf entries it leaves (BarkVacState.ntagged:
2965+ those it neither deleted nor untagged, and minus those bark_lower_bound
2966+ then untagged). The pass deletes (cleanup locks, splits followed by cycle
2967+ ID), so the count is exact; a cleanup with nothing to delete uses a
2968+ callback that keeps every entry, with the table AM's version callback if
2969+ it gave one. With none left it clears MKTAGS, CONVERTING and SETTLED in
2970+ one meta page write, and invalidates the index's relcache entry, since
2971+ every backend caches MKTAGS. The index is then one that stores no tags:
2972+ amindexcanversion answers false, the executor moves rows, and amcheck
2973+ expects no tagged leaf entry. Tagged pivots may remain, where
2974+ bark_lower_bound could not lower a parent's first child's bound; they
2975+ still separate the entries below them correctly, so amcheck and the
2976+ validator accept a pivot tag in any index with BARK_META_VERSIONED. An
2977+ entry kept tagged because its page's lower bound has a tag, or because the
2978+ table AM keeps answering LIVE for it (a row with history the table AM
2979+ cannot drop), keeps the conversion open; until it ends the index works
2980+ as a converting one, the constraint-check rule above included until
2981+ SETTLED, and rule D
2982+ after.
2983+
2984+ Tests: bark_mk_versioned (CONVERTING, SETTLED, untagged inserts,
2985+ completion, the constraint after it; the bits through each caller of
2986+ bark_note_conversion); isolation bark_mk_convert (VACUUM waits for
2987+ the in-place update, then SETTLED); t/009_bark_convert.pl (autovacuum
2988+ does not wait, a manual VACUUM waits twice, a row the table AM will not
2989+ untag holds the conversion open, completion, and a standby that sees each
2990+ bit and returns the same rows, under wal_consistency_checking);
2991+ t/008_bark_mktags.pl (the bits' corruption checks).
28582992
28592993Updates. An update that keeps the locator inserts, into each index it
28602994affects, the new version's entries, tagged. For a multikey index that is
0 commit comments