Skip to content

Commit fdafd73

Browse files
committed
BARK: convert a versioned multikey index that becomes a constraint's
A multikey index on a table whose locator is versioned stores tags (BARK_META_MKTAGS) and lets updates keep a row's locator. Such an index can later become unique or an exclusion constraint's index (DocumentDB's prepareUnique does that with only AccessShareLock on the table), and an update whose statement began before the change can still keep its locator. While it is in progress a dirty snapshot sees two versions at one locator, each reached only through its own entries, and a check that kept one row per locator could miss the conflicting version and commit a duplicate. Until the conversion has settled, a non-MVCC scan of such an index judges every entry by itself and rechecks every row, keeping one row per locator only for the row its own transaction inserted last. The meta page records the conversion (CONVERTING, then SETTLED once a VACUUM has waited out every writer that could still keep a locator; an autovacuum worker does not block), inserts store untagged entries from SETTLED on, and a VACUUM that finds no tagged entry left clears the bits and MKTAGS, leaving the index a REINDEX would build. amcheck and the page validator check the bits. test_versioned_tableam gains real in-place updates on a HOT chain (keep_locator = inplace), and the isolation test bark_mk_convert shows the check waiting for the in-place update and then failing or succeeding by its outcome.
1 parent fa47b8a commit fdafd73

24 files changed

Lines changed: 2460 additions & 53 deletions

‎doc/src/sgml/amcheck.sgml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,11 @@ ORDER BY c.relpages DESC LIMIT 10;
229229
point to, and that each LIST, POSTING and OVERSIZED entry is well
230230
formed (an OVERSIZED entry's overflow chain is read in full). A
231231
unique index must have no LIST or POSTING entry: each of its entries
232-
has one heap TID. It
232+
has one heap TID. The meta page's flags must agree with each other
233+
and with the index's definition: version tags on a multikey column's
234+
keys only in an index built for version tags, and a conversion (see
235+
<xref linkend="bark-version-tags"/>) only of such an index, settled
236+
only once it has begun. It
233237
acquires an <literal>AccessShareLock</literal> on the index and its
234238
table, as <function>bt_index_check</function> does. The
235239
one-argument form <function>bark_index_check(index

‎doc/src/sgml/bark.sgml‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,24 @@ CREATE INDEX orders_customer_idx ON orders (customer);
12891289
columns, as for any index that stores none. A multikey index that
12901290
stores tags and later becomes unique or an exclusion constraint's keeps
12911291
its tags but no longer lets an update keep a locator
1292-
(<function>amindexmaykeeplocator</function>). An index built by a
1292+
(<function>amindexmaykeeplocator</function>); the first insert, check
1293+
or <command>VACUUM</command> to see the change records in the meta page
1294+
that the index is converting, and from then on no update keeps a
1295+
locator in it, even if the constraint is dropped. An update that began
1296+
before the change may still be keeping one, so the index's constraint
1297+
checks then judge each entry on its own and recheck every row they
1298+
find; a check of a row other than the one the statement inserted last
1299+
(a deferred exclusion check, or the check after an update that inserted
1300+
no entries) can then fail with <quote>found self tuple multiple
1301+
times</quote>. The next <command>VACUUM</command> of the table waits
1302+
for the transactions that were writing it when the conversion began,
1303+
as <command>CREATE INDEX CONCURRENTLY</command> waits for writers (an
1304+
autovacuum worker does not wait: it tries again next time); from then
1305+
on the index stores new entries untagged, and constraint checks work as
1306+
before. A <command>VACUUM</command> that finds no tagged entry left
1307+
completes the conversion: the index then stores no tags, as if rebuilt.
1308+
<xref linkend="pageinspect"/>'s <function>bark_metap</function> shows
1309+
each step in the meta page's flags. An index built by a
12931310
version of BARK without version tags stores none until it is rebuilt.
12941311
</para>
12951312

‎doc/src/sgml/pageinspect.sgml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,10 @@ test=# SELECT itemoffset, ctid, itemlen, nulls, vars, data, dead, htid, tids[0:2
546546
the root's level, whether all key columns are
547547
<quote>equalimage</quote> (which decides whether equal keys are
548548
coalesced into LIST and POSTING entries), its flags (bit 0, multikey:
549-
the index has held a row with more than one entry), and the number of
549+
the index has held a row with more than one entry; bit 1, built for
550+
version tags; bit 2, version tags on a multikey column's keys; bits 3
551+
and 4, a conversion begun and settled, see
552+
<xref linkend="bark-version-tags"/>), and the number of
550553
(key, heap TID) members as of the last <command>CREATE INDEX</command>
551554
or <command>VACUUM</command>. An index whose meta page predates the
552555
last two reads 0 in both. For example:

‎src/backend/access/bark/README‎

Lines changed: 139 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,12 @@ In an index with an extracted column one row may have several entries on
14141414
the leaf, but the table AM takes each TID at most once (heapam's sort of
14151415
the TIDs refuses two equal ones), so the TIDs are sorted and each is
14161416
offered 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

14191424
OVERSIZED entries are not offered to the table AM: deleting one has to free
14201425
its 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
21192124
compile away. t/015_page_validator.pl in src/test/modules/bark plants
21202125
faults through the injection points bark-verify-fault-count,
21212126
bark-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

21252132
Its cost, in a cassert build on c7i.4xlarge: a write-only workload of
21262133
long 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
26952702
POSTING 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+
26982718
Pivots. Suffix truncation keeps a tag only where it separates: when
26992719
lastleft 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
28532873
from rd_index at run time), so an update that needs new entries in it
28542874
moves the row, and a constraint check never meets two versions at one
28552875
locator 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

28592993
Updates. An update that keeps the locator inserts, into each index it
28602994
affects, the new version's entries, tagged. For a multikey index that is

0 commit comments

Comments
 (0)