diff --git a/contrib/amcheck/verify_common.c b/contrib/amcheck/verify_common.c
index 46e53b3f99ab6..2c31664b1bbc5 100644
--- a/contrib/amcheck/verify_common.c
+++ b/contrib/amcheck/verify_common.c
@@ -20,6 +20,7 @@
#include "catalog/pg_am.h"
#include "commands/defrem.h"
#include "commands/tablecmds.h"
+#include "doltgres/hooks.h"
#include "utils/guc.h"
#include "utils/syscache.h"
@@ -167,7 +168,7 @@ index_checkable(Relation rel, Oid am_id)
errmsg("expected index as targets for verification"),
errdetail_relkind_not_supported(rel->rd_rel->relkind)));
- if (rel->rd_rel->relam != am_id)
+ if (rel->rd_rel->relam != am_id || doltgres_heap_only_refused(rel))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("expected \"%s\" index as targets for verification", get_am_name(am_id)),
diff --git a/contrib/amcheck/verify_heapam.c b/contrib/amcheck/verify_heapam.c
index 4ecadc88d16cc..8d61bf7696950 100644
--- a/contrib/amcheck/verify_heapam.c
+++ b/contrib/amcheck/verify_heapam.c
@@ -21,6 +21,7 @@
#include "access/xact.h"
#include "catalog/pg_am.h"
#include "catalog/pg_class.h"
+#include "doltgres/hooks.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -344,8 +345,8 @@ verify_heapam(PG_FUNCTION_ARGS)
* Sequences always use heap AM, but they don't show that in the catalogs.
* Other relkinds might be using a different AM, so check.
*/
- if (ctx.rel->rd_rel->relkind != RELKIND_SEQUENCE &&
- ctx.rel->rd_rel->relam != HEAP_TABLE_AM_OID)
+ if ((ctx.rel->rd_rel->relkind != RELKIND_SEQUENCE &&
+ ctx.rel->rd_rel->relam != HEAP_TABLE_AM_OID) || doltgres_heap_only_refused(ctx.rel))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("only heap AM is supported")));
diff --git a/contrib/pageinspect/brinfuncs.c b/contrib/pageinspect/brinfuncs.c
index 309b9522f9022..8beeb46c9e7cc 100644
--- a/contrib/pageinspect/brinfuncs.c
+++ b/contrib/pageinspect/brinfuncs.c
@@ -15,6 +15,7 @@
#include "access/htup_details.h"
#include "catalog/pg_am_d.h"
#include "catalog/pg_type.h"
+#include "doltgres/hooks.h"
#include "funcapi.h"
#include "lib/stringinfo.h"
#include "miscadmin.h"
@@ -165,7 +166,7 @@ brin_page_items(PG_FUNCTION_ARGS)
indexRel = index_open(indexRelid, AccessShareLock);
- if (!IS_BRIN(indexRel))
+ if (!IS_BRIN(indexRel) || doltgres_heap_only_refused(indexRel))
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is not a %s index",
diff --git a/contrib/pageinspect/btreefuncs.c b/contrib/pageinspect/btreefuncs.c
index 62c905c6e7c2c..222a8677eb663 100644
--- a/contrib/pageinspect/btreefuncs.c
+++ b/contrib/pageinspect/btreefuncs.c
@@ -33,6 +33,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_am.h"
#include "catalog/pg_type.h"
+#include "doltgres/hooks.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pageinspect.h"
@@ -225,7 +226,7 @@ check_relation_block_range(Relation rel, int64 blkno)
static void
bt_index_block_validate(Relation rel, int64 blkno)
{
- if (!IS_INDEX(rel) || !IS_BTREE(rel))
+ if (!IS_INDEX(rel) || !IS_BTREE(rel) || doltgres_heap_only_refused(rel))
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is not a %s index",
@@ -858,7 +859,7 @@ bt_metap(PG_FUNCTION_ARGS)
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
rel = relation_openrv(relrv, AccessShareLock);
- if (!IS_INDEX(rel) || !IS_BTREE(rel))
+ if (!IS_INDEX(rel) || !IS_BTREE(rel) || doltgres_heap_only_refused(rel))
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is not a %s index",
diff --git a/contrib/pageinspect/gistfuncs.c b/contrib/pageinspect/gistfuncs.c
index 23a4b49771d45..b3a8b13cab8db 100644
--- a/contrib/pageinspect/gistfuncs.c
+++ b/contrib/pageinspect/gistfuncs.c
@@ -15,6 +15,7 @@
#include "access/htup_details.h"
#include "access/relation.h"
#include "catalog/pg_am_d.h"
+#include "doltgres/hooks.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pageinspect.h"
@@ -218,7 +219,7 @@ gist_page_items(PG_FUNCTION_ARGS)
/* Open the relation */
indexRel = index_open(indexRelid, AccessShareLock);
- if (!IS_GIST(indexRel))
+ if (!IS_GIST(indexRel) || doltgres_heap_only_refused(indexRel))
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is not a %s index",
diff --git a/contrib/pageinspect/hashfuncs.c b/contrib/pageinspect/hashfuncs.c
index 7fc97d043ce13..959a2493ffc45 100644
--- a/contrib/pageinspect/hashfuncs.c
+++ b/contrib/pageinspect/hashfuncs.c
@@ -15,6 +15,7 @@
#include "access/relation.h"
#include "catalog/pg_am.h"
#include "catalog/pg_type.h"
+#include "doltgres/hooks.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pageinspect.h"
@@ -421,7 +422,7 @@ hash_bitmap_info(PG_FUNCTION_ARGS)
*/
indexRel = relation_open(indexRelid, AccessShareLock);
- if (!IS_INDEX(indexRel) || !IS_HASH(indexRel))
+ if (!IS_INDEX(indexRel) || !IS_HASH(indexRel) || doltgres_heap_only_refused(indexRel))
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("\"%s\" is not a %s index",
diff --git a/contrib/pageinspect/heapfuncs.c b/contrib/pageinspect/heapfuncs.c
index 4f0f3bd53e743..f2a883b8ebb6e 100644
--- a/contrib/pageinspect/heapfuncs.c
+++ b/contrib/pageinspect/heapfuncs.c
@@ -29,6 +29,7 @@
#include "access/relation.h"
#include "catalog/pg_am_d.h"
#include "catalog/pg_type.h"
+#include "doltgres/hooks.h"
#include "funcapi.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
@@ -325,8 +326,8 @@ tuple_data_split_internal(Oid relid, char *tupdata,
/*
* Sequences always use heap AM, but they don't show that in the catalogs.
*/
- if (rel->rd_rel->relkind != RELKIND_SEQUENCE &&
- rel->rd_rel->relam != HEAP_TABLE_AM_OID)
+ if ((rel->rd_rel->relkind != RELKIND_SEQUENCE &&
+ rel->rd_rel->relam != HEAP_TABLE_AM_OID) || doltgres_heap_only_refused(rel))
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("only heap AM is supported")));
diff --git a/contrib/pageinspect/rawpage.c b/contrib/pageinspect/rawpage.c
index f3996dc39fcde..9cc140f813967 100644
--- a/contrib/pageinspect/rawpage.c
+++ b/contrib/pageinspect/rawpage.c
@@ -19,6 +19,7 @@
#include "access/relation.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
+#include "doltgres/hooks.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pageinspect.h"
@@ -158,7 +159,8 @@ get_raw_page_internal(text *relname, ForkNumber forknum, BlockNumber blkno)
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
rel = relation_openrv(relrv, AccessShareLock);
- if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
+ if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind) ||
+ (forknum == MAIN_FORKNUM && doltgres_heap_only_refused(rel)))
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("cannot get raw page from relation \"%s\"",
diff --git a/contrib/pg_surgery/heap_surgery.c b/contrib/pg_surgery/heap_surgery.c
index 50a39d98ff301..3cff53865ef01 100644
--- a/contrib/pg_surgery/heap_surgery.c
+++ b/contrib/pg_surgery/heap_surgery.c
@@ -18,6 +18,7 @@
#include "access/xloginsert.h"
#include "catalog/pg_am_d.h"
#include "catalog/pg_control.h"
+#include "doltgres/hooks.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "utils/acl.h"
@@ -116,7 +117,7 @@ heap_force_common(FunctionCallInfo fcinfo, HeapTupleForceOption heap_force_opt)
RelationGetRelationName(rel)),
errdetail_relkind_not_supported(rel->rd_rel->relkind)));
- if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
+ if (rel->rd_rel->relam != HEAP_TABLE_AM_OID || doltgres_heap_only_refused(rel))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("only heap AM is supported")));
diff --git a/contrib/pg_visibility/pg_visibility.c b/contrib/pg_visibility/pg_visibility.c
index dfab0b64cf50b..f0bf8e0bb4937 100644
--- a/contrib/pg_visibility/pg_visibility.c
+++ b/contrib/pg_visibility/pg_visibility.c
@@ -16,6 +16,7 @@
#include "access/xloginsert.h"
#include "catalog/pg_type.h"
#include "catalog/storage_xlog.h"
+#include "doltgres/hooks.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -105,6 +106,7 @@ pg_visibility_map(PG_FUNCTION_ARGS)
tupdesc = pg_visibility_tupdesc(false, false);
mapbits = (int32) visibilitymap_get_status(rel, blkno, &vmbuffer);
+ mapbits = doltgres_visibility_bits(rel, blkno, mapbits);
if (vmbuffer != InvalidBuffer)
ReleaseBuffer(vmbuffer);
values[0] = BoolGetDatum((mapbits & VISIBILITYMAP_ALL_VISIBLE) != 0);
@@ -146,13 +148,15 @@ pg_visibility(PG_FUNCTION_ARGS)
tupdesc = pg_visibility_tupdesc(false, true);
mapbits = (int32) visibilitymap_get_status(rel, blkno, &vmbuffer);
+ mapbits = doltgres_visibility_bits(rel, blkno, mapbits);
if (vmbuffer != InvalidBuffer)
ReleaseBuffer(vmbuffer);
values[0] = BoolGetDatum((mapbits & VISIBILITYMAP_ALL_VISIBLE) != 0);
values[1] = BoolGetDatum((mapbits & VISIBILITYMAP_ALL_FROZEN) != 0);
/* Here we have to explicitly check rel size ... */
- if (blkno < RelationGetNumberOfBlocks(rel))
+ if (blkno < RelationGetNumberOfBlocks(rel) &&
+ !doltgres_visibility_kept(rel))
{
buffer = ReadBuffer(rel, blkno);
LockBuffer(buffer, BUFFER_LOCK_SHARE);
@@ -165,7 +169,10 @@ pg_visibility(PG_FUNCTION_ARGS)
else
{
/* As with the vismap, silently return 0 for pages past EOF */
- values[2] = BoolGetDatum(false);
+ if (!doltgres_visibility_page(rel, blkno, &values[2]))
+ {
+ values[2] = BoolGetDatum(false);
+ }
}
relation_close(rel, AccessShareLock);
@@ -173,6 +180,21 @@ pg_visibility(PG_FUNCTION_ARGS)
PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
}
+/*
+ * doltgres_visibility_page gives pg_visibility's page-level all-visible flag
+ * of a fork table's block, set for each block within the table since the
+ * fork has no pages and keeps every block all-visible, answering whether the
+ * fork stores the table.
+ */
+bool
+doltgres_visibility_page(Relation rel, BlockNumber blkno, Datum *all_visible)
+{
+ if (!doltgres_visibility_kept(rel))
+ return false;
+ *all_visible = BoolGetDatum(blkno < RelationGetNumberOfBlocks(rel));
+ return true;
+}
+
/*
* Visibility map information for every block in a relation.
*/
@@ -503,7 +525,8 @@ collect_visibility_data(Oid relid, bool include_pd)
info->count = nblocks;
/* Create a stream if reading main fork. */
- if (include_pd)
+ if (include_pd &&
+ !doltgres_visibility_kept(rel))
{
p.current_blocknum = 0;
p.last_exclusive = nblocks;
@@ -541,7 +564,8 @@ collect_visibility_data(Oid relid, bool include_pd)
* caller needs it. Use a buffer access strategy, too, to prevent
* cache-trashing.
*/
- if (include_pd)
+ if (include_pd &&
+ !doltgres_visibility_kept(rel))
{
Buffer buffer;
Page page;
@@ -557,11 +581,13 @@ collect_visibility_data(Oid relid, bool include_pd)
}
}
- if (include_pd)
+ if (include_pd &&
+ !doltgres_visibility_kept(rel))
{
Assert(read_stream_next_buffer(stream, NULL) == InvalidBuffer);
read_stream_end(stream);
}
+ doltgres_visibility_collected(rel, include_pd, info);
/* Clean up. */
if (vmbuffer != InvalidBuffer)
@@ -571,6 +597,19 @@ collect_visibility_data(Oid relid, bool include_pd)
return info;
}
+/*
+ * doltgres_visibility_collected sets each block of a fork table all-visible
+ * and all-frozen in collect_visibility_data's bits, and all-visible on its
+ * page too when the caller asked for pages, since the fork keeps no
+ * visibility map and has no pages to read.
+ */
+void
+doltgres_visibility_collected(Relation rel, bool include_pd, vbits *info)
+{
+ if (doltgres_visibility_kept(rel))
+ memset(info->bits, include_pd ? (1 << 0) | (1 << 1) | (1 << 2) : (1 << 0) | (1 << 1), info->count);
+}
+
/*
* The "strict" version of GetOldestNonRemovableTransactionId(). The
* pg_visibility check can tolerate false positives (don't report some of the
diff --git a/contrib/pgstattuple/pgstatapprox.c b/contrib/pgstattuple/pgstatapprox.c
index 21e0b50fb4bd4..85de651351c9b 100644
--- a/contrib/pgstattuple/pgstatapprox.c
+++ b/contrib/pgstattuple/pgstatapprox.c
@@ -18,6 +18,7 @@
#include "access/visibilitymap.h"
#include "catalog/pg_am_d.h"
#include "commands/vacuum.h"
+#include "doltgres/hooks.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -345,7 +346,7 @@ pgstattuple_approx_internal(Oid relid, FunctionCallInfo fcinfo)
RelationGetRelationName(rel)),
errdetail_relkind_not_supported(rel->rd_rel->relkind)));
- if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
+ if (rel->rd_rel->relam != HEAP_TABLE_AM_OID || doltgres_heap_only_refused(rel))
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("only heap AM is supported")));
diff --git a/contrib/pgstattuple/pgstatindex.c b/contrib/pgstattuple/pgstatindex.c
index 3a3f2637bd956..efb7054267dfc 100644
--- a/contrib/pgstattuple/pgstatindex.c
+++ b/contrib/pgstattuple/pgstatindex.c
@@ -34,6 +34,7 @@
#include "access/relation.h"
#include "catalog/namespace.h"
#include "catalog/pg_am.h"
+#include "doltgres/hooks.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -222,7 +223,7 @@ pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
ReadStream *stream;
BlockNumber startblk;
- if (!IS_INDEX(rel) || !IS_BTREE(rel))
+ if (!IS_INDEX(rel) || !IS_BTREE(rel) || doltgres_heap_only_refused(rel))
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("relation \"%s\" is not a btree index",
@@ -541,7 +542,7 @@ pgstatginindex_internal(Oid relid, FunctionCallInfo fcinfo)
*/
rel = relation_open(relid, AccessShareLock);
- if (!IS_INDEX(rel) || !IS_GIN(rel))
+ if (!IS_INDEX(rel) || !IS_GIN(rel) || doltgres_heap_only_refused(rel))
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("relation \"%s\" is not a GIN index",
@@ -631,7 +632,7 @@ pgstathashindex(PG_FUNCTION_ARGS)
*/
rel = relation_open(relid, AccessShareLock);
- if (!IS_INDEX(rel) || !IS_HASH(rel))
+ if (!IS_INDEX(rel) || !IS_HASH(rel) || doltgres_heap_only_refused(rel))
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("relation \"%s\" is not a hash index",
diff --git a/contrib/pgstattuple/pgstattuple.c b/contrib/pgstattuple/pgstattuple.c
index 6a7f8cb4a7ca5..eba2c352e5580 100644
--- a/contrib/pgstattuple/pgstattuple.c
+++ b/contrib/pgstattuple/pgstattuple.c
@@ -32,6 +32,7 @@
#include "access/tableam.h"
#include "catalog/namespace.h"
#include "catalog/pg_am_d.h"
+#include "doltgres/hooks.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -268,7 +269,7 @@ pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
errmsg("index \"%s\" is not valid",
RelationGetRelationName(rel))));
- switch (rel->rd_rel->relam)
+ switch (doltgres_heap_only_refused(rel) ? InvalidOid : rel->rd_rel->relam)
{
case BTREE_AM_OID:
return pgstat_index(rel, BTREE_METAPAGE + 1,
@@ -328,8 +329,8 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
/*
* Sequences always use heap AM, but they don't show that in the catalogs.
*/
- if (rel->rd_rel->relkind != RELKIND_SEQUENCE &&
- rel->rd_rel->relam != HEAP_TABLE_AM_OID)
+ if ((rel->rd_rel->relkind != RELKIND_SEQUENCE &&
+ rel->rd_rel->relam != HEAP_TABLE_AM_OID) || doltgres_heap_only_refused(rel))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("only heap AM is supported")));
diff --git a/src/backend/access/heap/visibilitymap.c b/src/backend/access/heap/visibilitymap.c
index 7eab3cd645050..595f76e2dead3 100644
--- a/src/backend/access/heap/visibilitymap.c
+++ b/src/backend/access/heap/visibilitymap.c
@@ -101,6 +101,7 @@
#include "access/visibilitymap.h"
#include "access/xloginsert.h"
#include "access/xlogutils.h"
+#include "doltgres/hooks.h"
#include "miscadmin.h"
#include "port/pg_bitutils.h"
#include "storage/bufmgr.h"
@@ -379,6 +380,9 @@ visibilitymap_count(Relation rel, BlockNumber *all_visible, BlockNumber *all_fro
/* all_visible must be specified */
Assert(all_visible);
+ if (doltgres_visibility_counted(rel, all_visible, all_frozen))
+ return;
+
for (mapBlock = 0;; mapBlock++)
{
Buffer mapBuffer;
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index 0317ead49bf6a..591e451a0eada 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -67,6 +67,7 @@
#include "commands/seclabel.h"
#include "commands/tablespace.h"
#include "common/file_perm.h"
+#include "doltgres/hooks.h"
#include "miscadmin.h"
#include "postmaster/bgwriter.h"
#include "storage/fd.h"
@@ -493,6 +494,8 @@ DropTableSpace(DropTableSpaceStmt *stmt)
*/
deleteSharedDependencyRecordsFor(TableSpaceRelationId, tablespaceoid, 0);
+ doltgres_tablespace_emptied(tablespaceoid, false);
+
/*
* Acquire TablespaceCreateLock to ensure that no TablespaceCreateDbspace
* is running concurrently.
@@ -1555,6 +1558,8 @@ tblspc_redo(XLogReaderState *record)
/* Close all smgr fds in all backends. */
WaitForProcSignalBarrier(EmitProcSignalBarrier(PROCSIGNAL_BARRIER_SMGRRELEASE));
+ doltgres_tablespace_emptied(xlrec->ts_id, true);
+
/*
* If we issued a WAL record for a drop tablespace it implies that
* there were no files in it at all when the DROP was done. That means
@@ -1574,6 +1579,8 @@ tblspc_redo(XLogReaderState *record)
{
ResolveRecoveryConflictWithTablespace(xlrec->ts_id);
+ doltgres_tablespace_emptied(xlrec->ts_id, true);
+
/*
* If we did recovery processing then hopefully the backends who
* wrote temp files should have cleaned up and exited by now. So
diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index 1c575e56ff607..4ef371b7de6a6 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -88,6 +88,7 @@
#include "access/amapi.h"
#include "access/htup_details.h"
#include "access/tsmapi.h"
+#include "doltgres/hooks.h"
#include "executor/executor.h"
#include "executor/nodeAgg.h"
#include "executor/nodeHash.h"
@@ -745,6 +746,10 @@ cost_index(IndexPath *path, PlannerInfo *root, double loop_count,
min_IO_cost = 0;
}
+ doltgres_index_scan_io(index->indexoid, indexonly, tuples_fetched, indexSelectivity, loop_count,
+ spc_random_page_cost, spc_seq_page_cost, cpu_operator_cost, &max_IO_cost, &min_IO_cost,
+ &rand_heap_pages);
+
if (partial_path)
{
/*
@@ -1063,6 +1068,9 @@ cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel,
* sqrt(pages_fetched / T);
else
cost_per_page = spc_random_page_cost;
+ doltgres_bitmap_page_cost(planner_rt_fetch(baserel->relid, root)->relid, tuples_fetched, pages_fetched,
+ baserel->pages, tbm_calculate_entries(work_mem * (Size) 1024), spc_random_page_cost,
+ cpu_operator_cost, &cost_per_page);
run_cost += pages_fetched * cost_per_page;
diff --git a/src/doltgres/HOOKS.md b/src/doltgres/HOOKS.md
index 53d3a58493398..c9002cccca586 100644
--- a/src/doltgres/HOOKS.md
+++ b/src/doltgres/HOOKS.md
@@ -117,11 +117,12 @@ queue's include-tree check lists.
| src/backend/utils/adt/pg_locale_libc.c | `get_collation_actual_version_libc` | call site | libc-named collations report the vendored ICU's collator version (design item 12) |
| src/backend/utils/adt/pg_locale_icu.c | `create_pg_locale_icu` | call sites | A libc-named locale's ICU tag comes from the fork, mapped from its stored name, in place of the catalog's locale name, which a libc row leaves null; the two guards skip only the two lines that read it (design item 12) |
| src/backend/utils/adt/pg_locale_builtin.c | `create_pg_locale_builtin` | call sites | A libc locale of the C family, but `C` and `POSIX` alone, gets the builtin provider's locale of the name its ctype name stands for (`C.UTF-8` in a UTF-8 database, else `C`), which the fork gives in place of the catalog's locale name, which a libc row leaves null; the two guards skip only the two lines that read it (design item 12) |
-| contrib/pageinspect/heapfuncs.c, contrib/pageinspect/rawpage.c, contrib/pageinspect/btreefuncs.c, contrib/pageinspect/brinfuncs.c, contrib/pageinspect/gistfuncs.c, contrib/pageinspect/hashfuncs.c, contrib/pageinspect/ginfuncs.c | each function's access method check | call sites | Refuse fork relations with that check's own SQLSTATE and message (design item 5) |
-| contrib/pg_visibility/pg_visibility.c | `check_relation_relkind` | call site | As above (design item 5) |
+| contrib/pageinspect/heapfuncs.c, contrib/pageinspect/rawpage.c, contrib/pageinspect/btreefuncs.c, contrib/pageinspect/brinfuncs.c, contrib/pageinspect/gistfuncs.c, contrib/pageinspect/hashfuncs.c | each function's access method check (`rawpage.c`'s is `get_raw_page`'s relkind check; `ginfuncs.c` has none, since its functions take raw pages, which `get_raw_page` refuses) | call sites | Refuse fork relations with that check's own SQLSTATE and message (design item 5) |
+| contrib/pg_visibility/pg_visibility.c | `pg_visibility_map`, `pg_visibility`, `collect_visibility_data` | call sites | A fork table keeps no visibility map and has no pages: each block within it reads all-visible and all-frozen, in the map and on its page, and none past it, and its pages are never read (design item 5; bugs row 348) |
+| contrib/pg_visibility/pg_visibility.c | `doltgres_visibility_page`, `doltgres_visibility_collected` | helper function | The fork's answers for a fork table that write pg_visibility.c's result values and its private per-block data, `vbits` (design item 5; bugs row 348) |
| contrib/pg_surgery/heap_surgery.c | the access method check | call site | As above (design item 5) |
| contrib/pgstattuple/pgstattuple.c, contrib/pgstattuple/pgstatapprox.c, contrib/pgstattuple/pgstatindex.c | the access method checks | call sites | As above (design item 5) |
-| contrib/amcheck/verify_heapam.c, contrib/amcheck/verify_nbtree.c | the access method checks | call sites | As above (design item 5) |
+| contrib/amcheck/verify_heapam.c, contrib/amcheck/verify_common.c | the access method checks (an index's in `verify_common.c`'s `index_checkable`) | call sites | As above (design item 5) |
| contrib/pg_prewarm/pg_prewarm.c | `pg_prewarm` | call site | Read a fork relation's trees into the node cache (design item 5) |
| src/bin/initdb/initdb.c | `subdirs` | registration entry | Create `pg_versioned` (design item 5) |
| src/bin/pg_checksums/pg_checksums.c | `scan_directory` | call site | Skip `pg_versioned` in a tablespace's version directory (design item 5) |
@@ -153,7 +154,7 @@ queue's include-tree check lists.
| (existing hook) | `RegisterXactCallback`, `XACT_EVENT_ABORT`, `XACT_EVENT_PARALLEL_ABORT`; `RegisterSubXactCallback`, `SUBXACT_EVENT_ABORT_SUB` | existing hook | Take an aborted transaction's or subtransaction's versions back out of the memtable after its abort record (design item 7) |
| (existing hook) | `ProcessUtility_hook` | existing hook | After the standard function, settle the tables whose primary key a cascaded drop changed outside ALTER TABLE (design item 5) |
| src/backend/optimizer/path/costsize.c | `cost_index` | call site | A fork table's part of an index scan: primary key lookups, none for an index-only scan (design item 16) |
-| src/backend/optimizer/path/costsize.c | `cost_bitmap_heap_scan`, `compute_bitmap_pages` | call sites | A fork table's part of a bitmap scan: tid resolutions and primary key lookups (design item 16) |
+| src/backend/optimizer/path/costsize.c | `cost_bitmap_heap_scan` | call site | A fork table's cost per page fetched of a bitmap heap scan: a tuple id resolution and a primary key lookup a row, and a tuple id range scan a lossy block (design items 2 and 16) |
| src/backend/access/transam/xlogrecovery.c | `PerformWalRecovery` | call site | Refuse a replay that stopped below the WAL position the fork's durable bases and runs prove (design item 8) |
| src/backend/utils/adt/float.c | `float4in_internal`, `float8in_internal`, `float4recv`, `float8recv`, `float4send`, `float8send` | call sites | The correctly rounded parser and the quiet positive NaN (design item 15) |
| src/port/snprintf.c | `fmtfloat` | call site | Float formatting with a precision through Ryu (design item 15) |
diff --git a/src/doltgres/chunks/c/packs.c b/src/doltgres/chunks/c/packs.c
index 04dca9bb180b2..91d80d6b830a9 100644
--- a/src/doltgres/chunks/c/packs.c
+++ b/src/doltgres/chunks/c/packs.c
@@ -434,6 +434,17 @@ chunks_packs_table_runs_insert(StdVec *runs, uint32 space, ChunksPacksTail tail)
298, 27);
}
+/* chunks_packs_table_runs_remove is BTreeMap::remove of a table's runs tails: a tablespace's taken out when they hold one. */
+void
+chunks_packs_table_runs_remove(StdVec *runs, uint32 space)
+{
+ size_t at;
+ chunks_TupleU32Tail_t removed;
+
+ if (chunks_packs_table_runs_find(runs, space, &at))
+ std_vec_remove(runs, at, &removed, sizeof(removed), _Alignof(chunks_TupleU32Tail_t), RUST_FILE, 0, 0);
+}
+
/*
* chunks_packs_Table_whole returns the record that begins a pack table's
* file: the generation, the highest number a pack was added at, the bound of
diff --git a/src/doltgres/chunks/c/packs/backend.c b/src/doltgres/chunks/c/packs/backend.c
index 87692a7af75ac..50e39c60199aa 100644
--- a/src/doltgres/chunks/c/packs/backend.c
+++ b/src/doltgres/chunks/c/packs/backend.c
@@ -358,7 +358,8 @@ chunks_packs_backend_Closing_body_0(void *frame_)
if (!host_shared_read(frame->io, &frame->reading, frame->error))
goto done;
frame->reading_live = true;
- if (!chunks_packs_backend_Packs_flush(&frame->packs, frame->io, &frame->reading, &failed))
+ if (!chunks_packs_backend_Packs_flush(&frame->packs, HOST_IO_IO_TAKES_SHORT(frame->io), &frame->reading,
+ &failed))
{
*frame->error = chunks_packs_shared_raised(&failed);
goto done;
@@ -567,7 +568,8 @@ chunks_packs_backend_Packs_viewed_body_0(void *frame_)
HOST_IO_IO_TAKES_SHORT(frame->io));
frame->view_live = true;
view = host_lock_Lending_DerefMut_deref_mut(&frame->view);
- frame->ok = chunks_packs_view_View_refresh(view, frame->io, host_shared_Held_record(&frame->self->held),
+ frame->ok = chunks_packs_view_View_refresh(view, HOST_IO_IO_TAKES_SHORT(frame->io),
+ host_shared_Held_record(&frame->self->held),
frame->error) &&
frame->read(frame->captures, view, frame->io, frame->error);
chunks_packs_backend_Packs_viewed_pad_0(frame);
@@ -675,13 +677,16 @@ chunks_packs_backend_Packs_changed(const ChunksPacksBackendPacks *self, HostIoTa
/*
* chunks_packs_backend_Packs_compact_frame_0_t is compact's state across its
- * pad: the view's guard, the list files' flag, and the rewrite under way.
+ * pad, and remove_space's: the view's guard, the list files' flag, and the
+ * rewrite under way.
*/
typedef struct chunks_packs_backend_Packs_compact_frame_0_t
{
const ChunksPacksBackendPacks *self;
HostIoTakesShort io;
bool all;
+ bool removing;
+ uint32 space;
ChunksErrorError *error;
bool ok;
HostLockLending view;
@@ -715,7 +720,7 @@ chunks_packs_backend_Packs_compact_pad_0(void *frame_)
}
}
-/* chunks_packs_backend_Packs_compact_body_0 is compact's body: the list files due a whole write written whole. */
+/* chunks_packs_backend_Packs_compact_body_0 is compact's body, the list files due a whole write written whole, and remove_space's, its tablespace taken out of them. */
static void
chunks_packs_backend_Packs_compact_body_0(void *frame_)
{
@@ -730,7 +735,7 @@ chunks_packs_backend_Packs_compact_body_0(void *frame_)
frame->io);
frame->view_live = true;
view = host_lock_Lending_DerefMut_deref_mut(&frame->view);
- if (!frame->all && !chunks_packs_lists_past_slack(view))
+ if (!frame->removing && !frame->all && !chunks_packs_lists_past_slack(view))
{
frame->ok = true;
goto done;
@@ -746,7 +751,8 @@ chunks_packs_backend_Packs_compact_body_0(void *frame_)
if (!chunks_packs_lists_Rewrite_begin(view, record, &flag_io, &frame->rewrite, frame->error))
goto done;
frame->rewrite_live = true;
- if (!chunks_packs_lists_Rewrite_compact(&frame->rewrite, frame->all, frame->error))
+ if (frame->removing ? !chunks_packs_lists_Rewrite_remove_space(&frame->rewrite, frame->space, frame->error) :
+ !chunks_packs_lists_Rewrite_compact(&frame->rewrite, frame->all, frame->error))
goto done;
frame->rewrite_live = false;
frame->ok = chunks_packs_lists_Change_install(&frame->rewrite, frame->error);
@@ -770,14 +776,65 @@ chunks_packs_backend_Packs_compact(const ChunksPacksBackendPacks *self, HostIoTa
return frame.ok;
}
-/* chunks_packs_backend_none_deferred is flush's read: whether the view defers no add. */
-static bool
-chunks_packs_backend_none_deferred(void *captures, ChunksPacksViewView *view, HostIoIo *io, ChunksErrorError *error)
+/*
+ * chunks_packs_backend_Packs_remove_space takes a tablespace out of the
+ * repository's list files under their flag once none of its packs is listed,
+ * refusing while one is (bugs row 99).
+ */
+bool
+chunks_packs_backend_Packs_remove_space(const ChunksPacksBackendPacks *self, HostIoTakesShort io,
+ const HostSharedReader *reading, uint32 space, ChunksErrorError *error)
{
- (void) io;
- (void) error;
- *(bool *) captures = view->deferred.len == 0;
- return true;
+ chunks_packs_backend_Packs_compact_frame_0_t frame = {.self = self, .io = io, .removing = true, .space = space,
+ .error = error};
+
+ (void) reading;
+ std_landing(&frame, chunks_packs_backend_Packs_compact_body_0, chunks_packs_backend_Packs_compact_pad_0);
+ return frame.ok;
+}
+
+/*
+ * chunks_packs_backend_Packs_flush_frame_0_t is flush's state across its
+ * pad: the view's guard, and whether the view defers no add.
+ */
+typedef struct chunks_packs_backend_Packs_flush_frame_0_t
+{
+ const ChunksPacksBackendPacks *self;
+ HostIoTakesShort io;
+ ChunksErrorError *error;
+ bool ok;
+ bool none;
+ HostLockLending view;
+ bool view_live;
+} chunks_packs_backend_Packs_flush_frame_0_t;
+
+/* chunks_packs_backend_Packs_flush_pad_0 is flush's pad: the view's guard. */
+static void
+chunks_packs_backend_Packs_flush_pad_0(void *frame_)
+{
+ chunks_packs_backend_Packs_flush_frame_0_t *frame = frame_;
+
+ if (frame->view_live)
+ {
+ frame->view_live = false;
+ host_lock_Lending_drop(&frame->view);
+ }
+}
+
+/* chunks_packs_backend_Packs_flush_body_0 is flush's body: the view refreshed, then whether it defers no add. */
+static void
+chunks_packs_backend_Packs_flush_body_0(void *frame_)
+{
+ chunks_packs_backend_Packs_flush_frame_0_t *frame = frame_;
+ ChunksPacksViewView *value;
+
+ frame->view = chunks_packs_backend_Packs_view_lock(frame->self, frame->io);
+ frame->view_live = true;
+ value = host_lock_Lending_DerefMut_deref_mut(&frame->view);
+ frame->ok = chunks_packs_view_View_refresh(value, frame->io, host_shared_Held_record(&frame->self->held),
+ frame->error);
+ frame->none = value->deferred.len == 0;
+ chunks_packs_backend_Packs_flush_pad_0(frame);
}
/* chunks_packs_backend_flushed is flush's change: every deferred add appended. */
@@ -794,15 +851,16 @@ chunks_packs_backend_flushed(void *captures, ChunksPacksListsChange *change, Chu
* its base rests on, so a crash's recovery finds them listed.
*/
bool
-chunks_packs_backend_Packs_flush(const ChunksPacksBackendPacks *self, HostIoIo *io, const HostSharedReader *reading,
- ChunksErrorError *error)
+chunks_packs_backend_Packs_flush(const ChunksPacksBackendPacks *self, HostIoTakesShort io,
+ const HostSharedReader *reading, ChunksErrorError *error)
{
- bool none;
+ chunks_packs_backend_Packs_flush_frame_0_t frame = {.self = self, .io = io, .error = error};
- if (!chunks_packs_backend_Packs_viewed(self, io, chunks_packs_backend_none_deferred, &none, error))
+ std_landing(&frame, chunks_packs_backend_Packs_flush_body_0, chunks_packs_backend_Packs_flush_pad_0);
+ if (!frame.ok)
return false;
- return none || chunks_packs_backend_Packs_changed(self, HOST_IO_IO_TAKES_SHORT(io), reading,
- chunks_packs_backend_flushed, NULL, error);
+ return frame.none || chunks_packs_backend_Packs_changed(self, io, reading, chunks_packs_backend_flushed, NULL,
+ error);
}
/*
@@ -814,7 +872,7 @@ bool
chunks_packs_backend_Packs_close(ChunksPacksBackendPacks *self, HostIoIo *io, const HostSharedReader *reading,
ChunksErrorError *error)
{
- bool ok = chunks_packs_backend_Packs_flush(self, io, reading, error);
+ bool ok = chunks_packs_backend_Packs_flush(self, HOST_IO_IO_TAKES_SHORT(io), reading, error);
chunks_packs_backend_Packs_drop(self);
return ok;
@@ -1062,7 +1120,7 @@ chunks_packs_backend_Packs_copied_body_0(void *frame_)
frame->ok = false;
if (!chunks_packs_backend_Packs_unreserve(self, frame->io, frame->reading, frame->error) ||
!chunks_packs_backend_Packs_even_manifest(self, frame->io, frame->error) ||
- !chunks_packs_backend_Packs_flush(self, frame->io, frame->reading, frame->error) ||
+ !chunks_packs_backend_Packs_flush(self, HOST_IO_IO_TAKES_SHORT(frame->io), frame->reading, frame->error) ||
!chunks_packs_open_made_dir(frame->io, frame->into, frame->into_len, frame->error))
goto done;
frame->dir = host_tablespace_dir((uint32) self->repository, self->own);
@@ -2775,7 +2833,7 @@ chunks_packs_backend_Packs_merge_runs_body_0(void *frame_)
frame->view = chunks_packs_backend_Packs_view_lock(self, HOST_IO_IO_TAKES_SHORT(frame->io));
frame->view_live = true;
value = host_lock_Lending_DerefMut_deref_mut(&frame->view);
- if (!chunks_packs_view_View_refresh(value, frame->io, record, frame->error))
+ if (!chunks_packs_view_View_refresh(value, HOST_IO_IO_TAKES_SHORT(frame->io), record, frame->error))
goto done;
if (!chunks_packs_merge_due(value, &space, &from, &to))
{
diff --git a/src/doltgres/chunks/c/packs/fixtures.c b/src/doltgres/chunks/c/packs/fixtures.c
index fd23777c4ced5..9e579a345e1bb 100644
--- a/src/doltgres/chunks/c/packs/fixtures.c
+++ b/src/doltgres/chunks/c/packs/fixtures.c
@@ -613,7 +613,7 @@ chunks_packs_tests_refreshed(const char *file, ChunksPacksViewView *view, HostIo
{
ChunksErrorError error;
- if (!chunks_packs_view_View_refresh(view, io, record, &error))
+ if (!chunks_packs_view_View_refresh(view, HOST_IO_IO_TAKES_SHORT(io), record, &error))
chunks_packs_tests_unwrapped(file, &error, line, col);
}
diff --git a/src/doltgres/chunks/c/packs/lists.c b/src/doltgres/chunks/c/packs/lists.c
index 3e35ff0f0947b..4a857d13f2bb3 100644
--- a/src/doltgres/chunks/c/packs/lists.c
+++ b/src/doltgres/chunks/c/packs/lists.c
@@ -1071,6 +1071,68 @@ chunks_packs_lists_Rewrite_compact(ChunksPacksListsChange *self, bool all, Chunk
return chunks_packs_lists_run(&frame, chunks_packs_lists_Rewrite_compact_body_0);
}
+/*
+ * chunks_packs_lists_Rewrite_remove_space_body_0 is remove_space's body: the
+ * deferred adds appended, nothing more when the table holds no runs tail of
+ * the tablespace, a refusal while the table lists a pack in it, then the
+ * table written whole without its runs tail, so no record after it names the
+ * tablespace, and the changer's view's read of its runs file dropped, so its
+ * next add writes the file whole again.
+ */
+static void
+chunks_packs_lists_Rewrite_remove_space_body_0(void *frame_)
+{
+ chunks_packs_lists_frame_0_t *frame = frame_;
+ ChunksPacksListsChange *self = frame->self;
+ ChunksPacksViewView *view = self->view;
+
+ frame->ok = false;
+ if (!chunks_packs_lists_flush(frame))
+ goto done;
+ if (chunks_packs_table_runs_get(&view->table.list.runs, frame->space) == NULL)
+ {
+ frame->ok = true;
+ goto done;
+ }
+ for (size_t k = 0, k_len = view->table.list.packs.len; k < k_len; k++)
+ if (STD_VEC_AT(&view->table.list.packs, chunks_TupleU64Listed_t, k).f1.space == frame->space)
+ {
+ StdFmtPiece pieces[] = {STD_FMT_LIT("tablespace "), STD_FMT_ARG(0), STD_FMT_LIT(" still holds packs")};
+ StdFmtArg args[] = {{&frame->space, std_u32_Display_fmt}};
+ StdFmtArguments arguments = {pieces, lengthof(pieces), args};
+ StdString message;
+
+ std_fmt_format_Checked(&message, &arguments);
+ *frame->error = chunks_error_Error_refused(&message);
+ goto done;
+ }
+ chunks_packs_table_runs_remove(&view->table.list.runs, frame->space);
+ chunks_packs_lists_path_into(frame, &view->dir, CHUNKS_PACKS_TABLE);
+ if (!chunks_packs_lists_Rewrite_whole(frame, &CHUNKS_PACKS_VIEW_TABLE_LIST_OPS, &view->table.list,
+ &view->table.read, &frame->taken_table, sizeof(frame->taken_table)))
+ goto done;
+ self->snapshot.table = chunks_packs_lists_tail_of(&CHUNKS_PACKS_VIEW_TABLE_LIST_OPS, &view->table.read,
+ &view->table.list);
+ chunks_packs_view_runs_remove(&view->runs, frame->space);
+ frame->ok = true;
+done:
+ chunks_packs_lists_pad_0(frame);
+}
+
+/*
+ * chunks_packs_lists_Rewrite_remove_space takes a tablespace out of the
+ * repository's lists once none of its packs is listed, as DROP TABLESPACE
+ * needs before it removes the tablespace's directory (bugs row 99), and
+ * refuses while one is.
+ */
+bool
+chunks_packs_lists_Rewrite_remove_space(ChunksPacksListsChange *self, uint32 space, ChunksErrorError *error)
+{
+ chunks_packs_lists_frame_0_t frame = {.self = self, .space = space, .error = error};
+
+ return chunks_packs_lists_run(&frame, chunks_packs_lists_Rewrite_remove_space_body_0);
+}
+
#ifdef DOLTGRES_TEST_CHUNKS
/* chunks_packs_lists_tests_TupleU64U64_Debug_fmt is a (u64, u64)'s Debug, in a tuple. */
@@ -1977,7 +2039,7 @@ chunks_packs_lists_tests_reader(void *captures, HostIoIo *io, HostError *error)
bool has_two;
bool whole;
- if (!chunks_packs_view_View_refresh(&view, io, host_shared_Held_record(&held), &failed))
+ if (!chunks_packs_view_View_refresh(&view, HOST_IO_IO_TAKES_SHORT(io), host_shared_Held_record(&held), &failed))
{
*error = chunks_packs_tests_raised(&failed);
ok = false;
@@ -2242,4 +2304,114 @@ test_chunks_packs_lists_tests_a_whole_write_that_fails_before_its_renames_leaves
simc_TestIo_drop(&test_io);
}
+/* chunks_packs_lists_tests_first_retired retires pack 1 in a change. */
+static void
+chunks_packs_lists_tests_first_retired(ChunksPacksListsChange *change, void *captures)
+{
+ static const uint64 numbers[] = {1};
+
+ (void) captures;
+ chunks_packs_lists_tests_retired(change, numbers, lengthof(numbers), 0, 0);
+}
+
+/* chunks_packs_lists_tests_space_removed takes a tablespace out of a view's repository's list files under their flag, answering whether it could. */
+static bool
+chunks_packs_lists_tests_space_removed(HostIoIo *io, HostSharedRecord record, ChunksPacksViewView *view, uint32 space,
+ ChunksErrorError *error)
+{
+ HostSharedFlagGuard rewriting;
+ HostSharedFlagIo flag_io;
+ ChunksPacksListsChange rewrite;
+ HostError host;
+ bool ok;
+
+ if (!host_shared_Record_flag(record, HOST_IO_IO_TAKES_SHORT(io), CHUNKS_PACKS_SHARED_REWRITING_INDEX, &rewriting,
+ &host))
+ chunks_packs_tests_host_unwrapped(RUST_FILE, &host, 0, 0);
+ flag_io = host_shared_FlagGuard_io(&rewriting);
+ if (!chunks_packs_lists_Rewrite_begin(view, record, &flag_io, &rewrite, error))
+ chunks_packs_tests_unwrapped(RUST_FILE, error, 0, 0);
+ if (!chunks_packs_lists_Rewrite_remove_space(&rewrite, space, error))
+ {
+ chunks_packs_lists_Change_drop(&rewrite);
+ ok = false;
+ }
+ else
+ ok = chunks_packs_lists_Change_install(&rewrite, error);
+ host_shared_FlagGuard_drop(&rewriting);
+ return ok;
+}
+
+/*
+ * test_chunks_packs_lists_tests_a_removed_tablespace_reads_a_runs_file_made_again_from_its_start:
+ * a tablespace's removal is refused while the table lists a pack in it, and
+ * once its pack is retired it takes the tablespace's runs tail out of the
+ * table, so a pack the tablespace gets again after its directory went, as a
+ * tablespace made again under a dropped one's id does, writes a fresh runs
+ * file every view reads from its start (bugs row 99).
+ */
+void
+test_chunks_packs_lists_tests_a_removed_tablespace_reads_a_runs_file_made_again_from_its_start(void)
+{
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ StdPathBuf dir;
+ StdPathBuf space;
+ StdIoError io_error;
+ HostSharedOwned owned;
+ HostSharedRecord record;
+ ChunksPacksFixturesPacked packed;
+ ChunksPacksFixturesPacked again;
+ ChunksPacksTableEntry spaced;
+ ChunksPacksViewView mine;
+ ChunksPacksViewView other;
+ ChunksPacksViewView *views[2] = {&mine, &other};
+ ChunksErrorError error;
+ static const uint32 got_cols[] = {0, 0, 0};
+
+ chunks_packs_fixtures_home(&dir);
+ space = host_tablespace_dir(CHUNKS_PACKS_FIXTURES_REPOSITORY, 5);
+ if (!std_fs_create_dir_all(space.bytes.ptr, space.bytes.len, &io_error))
+ STD_RESULT_EXPECT_FAILED(0, 0, CHUNKS_PACKS_FIXTURES_UNWRAP, &io_error, std_io_Error_Debug_fmt);
+ owned = chunks_packs_lists_tests_repository(io, &dir);
+ record = host_shared_Owned_record(&owned);
+ packed = chunks_packs_fixtures_pack(io, &space, 1);
+ chunks_packs_fixtures_view(&mine);
+ chunks_packs_fixtures_view(&other);
+ spaced = packed.entry;
+ spaced.f1 = 5;
+ chunks_packs_lists_tests_one(io, record, &mine, &spaced, false, 0, 0);
+ STD_ASSERT_MSG(0, 0, !chunks_packs_lists_tests_space_removed(io, record, &mine, 5, &error) &&
+ error.tag == CHUNKS_ERROR_ERROR_REFUSED, "a tablespace that lists a pack was taken out");
+ chunks_error_Error_drop(&error);
+ chunks_packs_lists_tests_changed(io, record, &mine, chunks_packs_lists_tests_first_retired, NULL);
+ if (!chunks_packs_lists_tests_space_removed(io, record, &mine, 5, &error))
+ chunks_packs_tests_unwrapped(RUST_FILE, &error, 0, 0);
+ for (size_t k = 0; k < lengthof(views); k++)
+ {
+ chunks_packs_tests_refreshed(RUST_FILE, views[k], io, record, 0, 0);
+ STD_ASSERT(0, 0, chunks_packs_table_runs_get(&chunks_packs_view_View_table(views[k])->runs, 5) == NULL,
+ "!view.table().runs.contains_key(&5)");
+ }
+ if (!std_fs_remove_dir_all(space.bytes.ptr, space.bytes.len, &io_error) ||
+ !std_fs_create_dir_all(space.bytes.ptr, space.bytes.len, &io_error))
+ STD_RESULT_EXPECT_FAILED(0, 0, CHUNKS_PACKS_FIXTURES_UNWRAP, &io_error, std_io_Error_Debug_fmt);
+ again = chunks_packs_fixtures_pack(io, &space, 2);
+ spaced = again.entry;
+ spaced.f1 = 5;
+ chunks_packs_lists_tests_one(io, record, &mine, &spaced, false, 0, 0);
+ for (size_t k = 0; k < lengthof(views); k++)
+ {
+ chunks_packs_tests_refreshed(RUST_FILE, views[k], io, record, 0, 0);
+ chunks_packs_tests_listed(RUST_FILE, views[k], &spaced, 1, 0, 0);
+ chunks_packs_tests_got(RUST_FILE, views[k], io, 5, &again.address, &again.at, 0, got_cols);
+ }
+ chunks_packs_view_View_drop(&other);
+ chunks_packs_view_View_drop(&mine);
+ host_shared_Owned_drop(&owned);
+ std_vec_free_Checked(&space.bytes, 1, 1);
+ std_vec_free_Checked(&dir.bytes, 1, 1);
+ simc_TestIo_drop(&test_io);
+}
+
#endif /* DOLTGRES_TEST_CHUNKS */
diff --git a/src/doltgres/chunks/c/packs/marking.c b/src/doltgres/chunks/c/packs/marking.c
index f97740b076bfd..4bbf42a1fef89 100644
--- a/src/doltgres/chunks/c/packs/marking.c
+++ b/src/doltgres/chunks/c/packs/marking.c
@@ -977,7 +977,7 @@ chunks_packs_marking_noted_body_0(void *frame_)
bool dead;
bool recorded;
- if (!chunks_packs_view_View_refresh(view, frame->io, record, frame->error))
+ if (!chunks_packs_view_View_refresh(view, HOST_IO_IO_TAKES_SHORT(frame->io), record, frame->error))
goto done;
if (!host_shared_Record_lock_long_shared(record, frame->io, CHUNKS_PACKS_SHARED_BARRIER_INDEX, &frame->barrier,
&host))
diff --git a/src/doltgres/chunks/c/packs/merge.c b/src/doltgres/chunks/c/packs/merge.c
index b233d1cd32da0..ca4841033baf4 100644
--- a/src/doltgres/chunks/c/packs/merge.c
+++ b/src/doltgres/chunks/c/packs/merge.c
@@ -536,7 +536,7 @@ chunks_packs_merge_tests_merged(HostIoIo *io, HostSharedRecord record, ChunksPac
ChunksPacksMergeTestsMerged *result, ChunksErrorError *error)
{
result->has = false;
- if (!chunks_packs_view_View_refresh(view, io, record, error))
+ if (!chunks_packs_view_View_refresh(view, HOST_IO_IO_TAKES_SHORT(io), record, error))
return false;
return chunks_packs_merge_tests_merge_stale(io, record, view, result, error);
}
diff --git a/src/doltgres/chunks/c/packs/view.c b/src/doltgres/chunks/c/packs/view.c
index 240c060dea3de..d9bddd6726859 100644
--- a/src/doltgres/chunks/c/packs/view.c
+++ b/src/doltgres/chunks/c/packs/view.c
@@ -496,7 +496,7 @@ chunks_packs_view_runs_entry(StdVec *runs, uint32 space)
}
/* chunks_packs_view_runs_remove drops a tablespace's runs file as the view read it, if it has one. */
-static void
+void
chunks_packs_view_runs_remove(StdVec *runs, uint32 space)
{
size_t at;
@@ -799,7 +799,7 @@ chunks_packs_view_View_read_lists(ChunksPacksViewView *self, HostIoIoDoors io, c
typedef struct chunks_packs_view_View_refresh_frame_0_t
{
ChunksPacksViewView *self;
- HostIoIo *io;
+ HostIoTakesShort io;
HostSharedRecord record;
ChunksErrorError *error;
bool ok;
@@ -863,7 +863,7 @@ chunks_packs_view_View_refresh_body_0(void *frame_)
{
chunks_packs_view_View_refresh_frame_0_t *frame = frame_;
ChunksPacksViewView *self = frame->self;
- HostIoIoDoors io = HOST_IO_IO_IO_DOORS(frame->io);
+ HostIoIoDoors io = HOST_IO_TAKES_SHORT_IO_DOORS(frame->io);
uint64 rotations;
uint64 generation;
bool read;
@@ -882,7 +882,7 @@ chunks_packs_view_View_refresh_body_0(void *frame_)
}
self->has_current = false;
rotations = chunks_packs_shared_load(frame->record, CHUNKS_PACKS_SHARED_ROTATIONS_INDEX);
- if (!host_shared_Record_lock_shared(frame->record, HOST_IO_IO_TAKES_SHORT(frame->io),
+ if (!host_shared_Record_lock_shared(frame->record, frame->io,
CHUNKS_PACKS_SHARED_STATE_LOCK_INDEX, &frame->state, &host))
{
*frame->error = chunks_error_Error_FromError_from(&host);
@@ -918,7 +918,7 @@ chunks_packs_view_View_refresh_body_0(void *frame_)
#ifdef DOLTGRES_TEST_CHUNKS
chunks_packs_view_count_reread();
#endif
- if (!host_shared_Record_flag(frame->record, HOST_IO_IO_TAKES_SHORT(frame->io),
+ if (!host_shared_Record_flag(frame->record, frame->io,
CHUNKS_PACKS_SHARED_REWRITING_INDEX, &frame->rewriting, &host))
{
*frame->error = chunks_error_Error_FromError_from(&host);
@@ -946,7 +946,7 @@ chunks_packs_view_View_refresh_body_0(void *frame_)
frame->rewriting_live = false;
host_shared_FlagGuard_drop(&frame->rewriting);
}
- if (!chunks_packs_view_View_open(self, HOST_IO_IO_READ_DOORS(frame->io), frame->snapshot.deferred.ptr,
+ if (!chunks_packs_view_View_open(self, HOST_IO_IO_DOORS_READ_DOORS(io), frame->snapshot.deferred.ptr,
frame->snapshot.deferred.len, frame->error))
goto done;
std_vec_free_Checked(&self->deferred, sizeof(ChunksPacksTableEntry), _Alignof(ChunksPacksTableEntry));
@@ -966,7 +966,7 @@ chunks_packs_view_View_refresh_body_0(void *frame_)
* a read found a list file missing or not as its tail says.
*/
bool
-chunks_packs_view_View_refresh(ChunksPacksViewView *self, HostIoIo *io, HostSharedRecord record,
+chunks_packs_view_View_refresh(ChunksPacksViewView *self, HostIoTakesShort io, HostSharedRecord record,
ChunksErrorError *error)
{
chunks_packs_view_View_refresh_frame_0_t frame = {.self = self, .io = io, .record = record, .error = error};
diff --git a/src/doltgres/chunks/c/trailer.c b/src/doltgres/chunks/c/trailer.c
index e1cb0e54eac82..4389a6e38c867 100644
--- a/src/doltgres/chunks/c/trailer.c
+++ b/src/doltgres/chunks/c/trailer.c
@@ -852,12 +852,13 @@ chunks_trailer_Stamps_holds_closure_3(void *captures, const void *elem)
}
/*
- * chunks_trailer_Stamps_holds reports whether an entry has a stamp of a tuple
- * id, by a search of its stamps, which are in tuple id order as a fold writes
- * them.
+ * chunks_trailer_Stamps_stamp_of finds an entry's stamp of a tuple id, by a
+ * search of its stamps, which are in tuple id order as a fold writes them,
+ * and answers whether it has one: a run's stamp of a tuple id is its first
+ * stamp's transaction with that tuple id.
*/
bool
-chunks_trailer_Stamps_holds(const ChunksTrailerStamps *self, size_t i, uint64 tuple)
+chunks_trailer_Stamps_stamp_of(const ChunksTrailerStamps *self, size_t i, uint64 tuple, ChunksTrailerStamp *result)
{
uint64 start;
uint64 end;
@@ -873,8 +874,12 @@ chunks_trailer_Stamps_holds(const ChunksTrailerStamps *self, size_t i, uint64 tu
uint64 at_2;
if (!chunks_trailer_Stamps_span(self, i, &start, &end))
- return !self->has_ends && i < self->single.len &&
- STD_VEC_AT(&self->single, ChunksTrailerStamp, i).tuple == tuple;
+ {
+ if (self->has_ends || i >= self->single.len || STD_VEC_AT(&self->single, ChunksTrailerStamp, i).tuple != tuple)
+ return false;
+ *result = STD_VEC_AT(&self->single, ChunksTrailerStamp, i);
+ return true;
+ }
from = std_slice_partition_point(self->runs.ptr, self->runs.len, sizeof(chunks_trailer_Stamps_runs_t),
chunks_trailer_Stamps_holds_closure_1, &start, NULL);
to = std_slice_partition_point(self->runs.ptr, self->runs.len, sizeof(chunks_trailer_Stamps_runs_t),
@@ -898,7 +903,20 @@ chunks_trailer_Stamps_holds(const ChunksTrailerStamps *self, size_t i, uint64 tu
else
run_start = 0;
at_2 = std_saturating_add_u64(run_start, STD_SUB(u64, tuple, first.tuple, 312, 43));
- return at_2 >= std_max_u64(start, run_start) && at_2 < std_min_u64(end, run_end);
+ if (at_2 < std_max_u64(start, run_start) || at_2 >= std_min_u64(end, run_end))
+ return false;
+ result->txn = first.txn;
+ result->tuple = tuple;
+ return true;
+}
+
+/* chunks_trailer_Stamps_holds reports whether an entry has a stamp of a tuple id, as stamp_of finds it. */
+bool
+chunks_trailer_Stamps_holds(const ChunksTrailerStamps *self, size_t i, uint64 tuple)
+{
+ ChunksTrailerStamp stamp;
+
+ return chunks_trailer_Stamps_stamp_of(self, i, tuple, &stamp);
}
/*
@@ -3813,4 +3831,76 @@ test_chunks_trailer_tests_xxh3_digests_are_the_committed_table(void)
std_vec_free_Checked(&table, 1, 1);
}
+/* CHUNKS_TRAILER_TESTS_SPREAD is how many copies the stamp_of case's spread entry holds. */
+#define CHUNKS_TRAILER_TESTS_SPREAD 600
+
+/*
+ * test_chunks_trailer_tests_stamp_of_finds_each_copys_stamp_among_its_runs:
+ * an entry of copies whose tuple ids are 4 apart, so each is its own run, of
+ * three transactions, between two other entries, written and read back,
+ * gives each copy's own stamp by stamp_of, none for a tuple id between,
+ * past, or of another entry, and the single form gives its one stamp.
+ */
+void
+test_chunks_trailer_tests_stamp_of_finds_each_copys_stamp_among_its_runs(void)
+{
+ ChunksTrailerStamps stamps = chunks_trailer_Stamps_Default_default();
+ ChunksTrailerStamps read;
+ ChunksTrailerStamps single;
+ ChunksTrailerStamp before = {1, 1};
+ ChunksTrailerStamp after[3] = {{7, 5000}, {7, 5001}, {7, 5002}};
+ ChunksTrailerStamp spread[CHUNKS_TRAILER_TESTS_SPREAD];
+ ChunksTrailerStamp stamp;
+ ChunksErrorError error;
+ StdVec out;
+ StdVec one;
+
+ for (size_t i = 0; i < CHUNKS_TRAILER_TESTS_SPREAD; i++)
+ {
+ spread[i].txn = 100 + i / 200;
+ spread[i].tuple = 3 + 4 * i;
+ }
+ chunks_trailer_Stamps_push(&stamps, &before, 1);
+ chunks_trailer_Stamps_push(&stamps, spread, CHUNKS_TRAILER_TESTS_SPREAD);
+ chunks_trailer_Stamps_push(&stamps, after, lengthof(after));
+ std_vec_new(&out, 1, 1);
+ chunks_trailer_Stamps_write(&stamps, &out);
+ if (!chunks_trailer_Stamps_read(out.ptr, out.len, true, false, &read, &error))
+ std_result_unwrap_failed("called `Result::unwrap()` on an `Err` value",
+ sizeof("called `Result::unwrap()` on an `Err` value") - 1, &error,
+ chunks_error_Error_Debug_fmt, RUST_FILE, __LINE__, 9);
+ STD_ASSERT(__LINE__, 9, chunks_trailer_Stamps_entry_len(&read, 1) == CHUNKS_TRAILER_TESTS_SPREAD,
+ "read.entry_len(1) == SPREAD");
+ for (size_t i = 0; i < CHUNKS_TRAILER_TESTS_SPREAD; i++)
+ STD_ASSERT(__LINE__, 13,
+ chunks_trailer_Stamps_stamp_of(&read, 1, spread[i].tuple, &stamp) &&
+ chunks_trailer_Stamp_PartialEq_eq(&stamp, &spread[i]),
+ "read.stamp_of(1, spread[i].tuple) == Some(spread[i])");
+ STD_ASSERT(__LINE__, 9,
+ !chunks_trailer_Stamps_stamp_of(&read, 1, 4, &stamp) &&
+ !chunks_trailer_Stamps_stamp_of(&read, 1, 3 + 4 * CHUNKS_TRAILER_TESTS_SPREAD, &stamp) &&
+ !chunks_trailer_Stamps_stamp_of(&read, 1, 1, &stamp) &&
+ !chunks_trailer_Stamps_stamp_of(&read, 0, 3, &stamp) &&
+ !chunks_trailer_Stamps_stamp_of(&read, 2, 3, &stamp),
+ "no stamp for a tuple id between, past, or of another entry");
+ STD_ASSERT(__LINE__, 9,
+ chunks_trailer_Stamps_stamp_of(&read, 0, 1, &stamp) && chunks_trailer_Stamp_PartialEq_eq(&stamp, &before) &&
+ chunks_trailer_Stamps_stamp_of(&read, 2, 5001, &stamp) &&
+ chunks_trailer_Stamp_PartialEq_eq(&stamp, &after[1]),
+ "read.stamp_of(0, 1) == Some(before) && read.stamp_of(2, 5001) == Some(after[1])");
+ std_vec_new(&one, sizeof(ChunksTrailerStamp), _Alignof(ChunksTrailerStamp));
+ std_vec_push_Checked(&one, &spread[5], sizeof(ChunksTrailerStamp), _Alignof(ChunksTrailerStamp));
+ single = chunks_trailer_Stamps_FromVecStamp_from(&one);
+ STD_ASSERT(__LINE__, 9,
+ chunks_trailer_Stamps_stamp_of(&single, 0, spread[5].tuple, &stamp) &&
+ chunks_trailer_Stamp_PartialEq_eq(&stamp, &spread[5]) &&
+ !chunks_trailer_Stamps_stamp_of(&single, 0, spread[6].tuple, &stamp) &&
+ !chunks_trailer_Stamps_stamp_of(&single, 1, spread[5].tuple, &stamp),
+ "the single form's stamp_of gives its one stamp and none else");
+ chunks_trailer_Stamps_drop(&single);
+ chunks_trailer_Stamps_drop(&read);
+ std_vec_free_Checked(&out, 1, 1);
+ chunks_trailer_Stamps_drop(&stamps);
+}
+
#endif /* DOLTGRES_TEST_CHUNKS */
diff --git a/src/doltgres/doltgres/c/entries.c b/src/doltgres/doltgres/c/entries.c
index 8b24483580cdf..96d2c494c593d 100644
--- a/src/doltgres/doltgres/c/entries.c
+++ b/src/doltgres/doltgres/c/entries.c
@@ -1574,7 +1574,8 @@ doltgres_entries_rows_closure_0_body(void *frame_)
DoltgresScanUnder under = {.state = host_shared_Held_record(frame->held), .snapshot = c->read,
.has_serial = c->has_check};
TreesStoreReader store = repo_Repo_reader(frame->repo, HOST_CACHE_PLACEMENT_SERVER, c->reading);
- DoltgresScanTrees trees = {0};
+ uint64 generation = txn_state_generation(under.state);
+ DoltgresScanTrees trees;
uint32 epoch;
bool has_chain;
TreesStoreTree chain;
@@ -1585,14 +1586,7 @@ doltgres_entries_rows_closure_0_body(void *frame_)
if (!txn_state_readable(frame->io, frame->held, c->read, &some, &frame->read_base, frame->error))
return;
frame->read_base_live = some;
- if (some)
- {
- trees.has_root = frame->read_base.has_root;
- trees.root = frame->read_base.root;
- trees.has_tuple_ids = frame->read_base.has_tuple_ids;
- trees.tuple_ids = frame->read_base.tuple_ids;
- trees.folded = frame->read_base.folded;
- }
+ trees = doltgres_scan_Trees_of(some ? &frame->read_base : NULL, generation);
doltgres_scan_Epochs_based(some ? &frame->read_base : NULL, &epoch, &has_chain, &chain);
if (!doltgres_scan_Epochs_of(frame->io, TREES_STORE_READER_NODE_STORE(&store), epoch, has_chain, chain,
&frame->epochs, frame->error))
@@ -1603,9 +1597,13 @@ doltgres_entries_rows_closure_0_body(void *frame_)
{
DoltgresScanRow row;
bool has;
+ DoltgresScanResolved resolved = {0};
+ bool fetched;
- if (!doltgres_scan_fetch(frame->io, TREES_STORE_READER_NODE_STORE(&store), &under, &frame->epochs, trees,
- &c->layout, c->by.tuple_id, &has, &row, frame->error))
+ fetched = doltgres_scan_fetch(frame->io, TREES_STORE_READER_NODE_STORE(&store), &under, &frame->epochs,
+ &resolved, trees, &c->layout, c->by.tuple_id, &has, &row, frame->error);
+ doltgres_scan_Resolved_drop(&resolved);
+ if (!fetched)
return;
std_vec_new(frame->result, sizeof(DoltgresScanRow), _Alignof(DoltgresScanRow));
if (has)
@@ -1960,7 +1958,9 @@ doltgres_entries_raised(bool ok, HostError *failed, PgPgError *error)
* subtransaction's start marks where its keys begin, its commit leaves them
* to its parent, and its abort, which Postgres records before it calls this,
* takes back its versions and its children's by pruning the keys since its
- * mark. It marks and ends each subtransaction in the settle's list too.
+ * mark. It marks and ends each subtransaction in the settle's list too. An
+ * abort then settles the process's pending spill whose take-out of versions
+ * from the memtable its error stopped, so the take-out ends there.
*/
bool
doltgres_entries_doltgres_subxact_ended(const PgCaps *caps, int32 event, uint32 mySubid, uint32 parentSubid,
@@ -1981,7 +1981,7 @@ doltgres_entries_doltgres_subxact_ended(const PgCaps *caps, int32 event, uint32
{
doltgres_layouts_sub_aborted(&io, mySubid);
doltgres_settle_sub_ended(&io, mySubid, true);
- ok = txn_written_sub_aborted(&io, mySubid, &failed);
+ ok = txn_written_sub_aborted(&io, mySubid, &failed) && txn_spill_take_settled(&io, &failed);
}
return doltgres_entries_raised(ok, &failed, error);
}
@@ -2305,6 +2305,73 @@ doltgres_entries_databases(const PgCatalogCaps *caps, StdVec *result, PgPgError
return frame.ok;
}
+/*
+ * doltgres_entries_swept empties the fork's packs out of each tablespace whose
+ * directory of repositories holds the dropped mark, one a standby's redo of
+ * DROP TABLESPACE couldn't empty, through the collection that empties a
+ * tablespace, and removes that directory once no repository's is left in it
+ * (bugs row 99).
+ */
+static bool
+doltgres_entries_swept(HostIoIo *io, StoreCollectSchedule *schedule, const StdVec *databases,
+ const StoreCollectSettings *settings, PgPgError *error)
+{
+ StdVec spaces;
+ HostError failed;
+ bool ok = true;
+
+ if (!host_tablespaces(HOST_IO_IO_READ_DOORS(io), &spaces, &failed))
+ {
+ *error = pghost_raised(&failed);
+ return false;
+ }
+ for (size_t s = 0, s_len = spaces.len; s < s_len && ok; s++)
+ {
+ uint32 space = STD_VEC_AT(&spaces, uint32, s);
+ StdPathBuf versioned;
+ StdVec names;
+ bool has = false;
+ bool marked = false;
+ bool left = false;
+ bool removed;
+
+ versioned = host_versioned_dir(space);
+ ok = host_list_dir_if_there(HOST_IO_IO_READ_DOORS(io), (const char *) versioned.bytes.ptr,
+ versioned.bytes.len, &has, &names, &failed);
+ has = ok && has;
+ for (size_t k = 0, k_len = has ? names.len : 0; k < k_len; k++)
+ {
+ const StdString *name = &STD_VEC_AT(&names, StdString, k);
+
+ marked |= name->len == strlen(DOLTGRES_DROPPED_MARK) &&
+ memcmp(name->ptr, DOLTGRES_DROPPED_MARK, name->len) == 0;
+ }
+ for (size_t k = 0, k_len = marked ? names.len : 0; k < k_len && ok; k++)
+ {
+ const StdString *name = &STD_VEC_AT(&names, StdString, k);
+ uint32 repository;
+ uint64 counts[3];
+ StdParseIntError parsed;
+
+ if (!std_str_parse_u32(name->ptr, name->len, &repository, &parsed) || repository == 0)
+ continue;
+ left = true;
+ ok = store_collect_collect_asked(io, schedule, databases->ptr, databases->len, repository, space,
+ settings, counts, &failed);
+ }
+ if (has)
+ std_vec_drop_Checked(&names, sizeof(StdString), _Alignof(StdString), std_String_drop);
+ if (ok && marked && !left)
+ ok = host_remove_tree(HOST_IO_IO_IO_DOORS(io), (const char *) versioned.bytes.ptr, versioned.bytes.len,
+ &removed, &failed);
+ std_vec_free_Checked(&versioned.bytes, 1, 1);
+ if (!ok)
+ *error = pghost_raised(&failed);
+ }
+ std_vec_free_Checked(&spaces, sizeof(uint32), _Alignof(uint32));
+ return ok;
+}
+
/*
* doltgres_entries_collect_frame_t is collect's state across its pad: the
* collector's schedule and the databases of a check, each while live.
@@ -2353,6 +2420,7 @@ doltgres_entries_collect_body(void *frame_)
HostError failed;
uint64 word;
uint32 database;
+ uint32 emptied;
if (!pg_calls_latch_sleep(raise, (int64) pg_calls_settings_collector_naptime(), frame->error) ||
!pg_calls_config_reload_if_pending(raise, catalog, frame->error))
@@ -2362,12 +2430,13 @@ doltgres_entries_collect_body(void *frame_)
return;
frame->databases_live = true;
io = pghost_io(raise);
- if (host_collection_asked(&word, &database))
+ if (host_collection_asked(&word, &database, &emptied))
{
uint64 counts[3] = {PG_UINT64_MAX, PG_UINT64_MAX, PG_UINT64_MAX};
uint64 done[3];
bool ok = store_collect_collect_asked(&io, &frame->schedule, frame->databases.ptr,
- frame->databases.len, database, &settings, done, &failed);
+ frame->databases.len, database, emptied, &settings, done,
+ &failed);
host_collection_answer(word, ok ? done : counts);
if (!ok)
@@ -2376,6 +2445,8 @@ doltgres_entries_collect_body(void *frame_)
return;
}
}
+ if (!doltgres_entries_swept(&io, &frame->schedule, &frame->databases, &settings, frame->error))
+ return;
if (!store_collect_collect_due(&io, &frame->schedule, frame->databases.ptr, frame->databases.len, &settings,
&failed))
{
@@ -2739,12 +2810,21 @@ doltgres_entries_doltgres_fold_work(const PgCatalogCaps *caps, PgDatum main_arg,
* doltgres_entries_doltgres_fold_relation folds a relation's storage now,
* inside a fold worker's transaction as its owner, and answers whether it
* installed a base; heap is the relation, or an index's table, whose horizon
- * bounds the fold.
+ * bounds the fold. It first pauses at doltgres-fold-relation, with the
+ * transaction's snapshot taken and the relation locked.
*/
bool
doltgres_entries_doltgres_fold_relation(const PgCatalogCaps *caps, const PgRel *rel, const PgRel *heap, bool *result,
PgPgError *error)
{
+ HostIoIo io = pghost_io(pg_CatalogCaps_may_raise(caps));
+ HostError failed;
+
+ if (!host_pause(HOST_SHARED_IO_PAUSES(&io), "doltgres-fold-relation", &failed))
+ {
+ *error = pghost_raised(&failed);
+ return false;
+ }
return doltgres_entries_folded_now(caps, rel, heap, result, error);
}
@@ -3087,9 +3167,23 @@ doltgres_entries_redo_aborted_body(void *frame_)
{
doltgres_entries_redo_aborted_frame_t *frame = frame_;
doltgres_entries_redo_aborted_captures_t captures = {&frame->rows};
+ uint32 aborted = pg_calls_aborted_xid(frame->record);
HostIoIo io;
HostError failed;
+ if (aborted != 0)
+ {
+ HostXid xid;
+
+ if (!pg_calls_widened_xid(pg_Caps_may_raise(frame->caps), aborted, &xid.f0, frame->error))
+ return;
+ io = pghost_io(pg_Caps_may_raise(frame->caps));
+ if (!txn_spill_redo_aborted(&io, xid.f0, pg_calls_record_end(frame->record), &failed))
+ {
+ doltgres_entries_raised(false, &failed, frame->error);
+ return;
+ }
+ }
doltgres_entries_filled(DOLTGRES_ENTRIES_RELATION_ROW, pg_calls_aborted_relations, frame->record, &frame->rows);
frame->rows_live = true;
if (frame->rows.len < DOLTGRES_ENTRIES_RELATION_ROW)
@@ -3109,7 +3203,9 @@ doltgres_entries_redo_aborted_body(void *frame_)
* deltas the built nodes of every storage a replayed abort drops, at the
* start of xact_redo, before any branch's DropRelationFiles, so an aborted
* rewrite's Chunk records leave nothing on a standby that never promotes; a
- * record of another kind drops none.
+ * record of another kind drops none. It first marks the replayed aborted
+ * transaction's free space map spills with the record's end, as its abort
+ * marked them on its primary.
*/
bool
doltgres_entries_doltgres_redo_aborted(const PgCaps *caps, const PgWalRecord *record, PgPgError *error)
@@ -3958,6 +4054,17 @@ doltgres_entries_doltgres_rmgr_redo_closure_11(void *captures, HostIoIo *io, Tre
return txn_rewrite_spaced(io, held, c->record, true, c->page.most, true, c->end, error);
}
+/* doltgres_entries_doltgres_rmgr_redo_closure_12 is a carry's unlist's redo: the carry's versions taken back out. */
+static bool
+doltgres_entries_doltgres_rmgr_redo_closure_12(void *captures, HostIoIo *io, TreesStoreNodeStore store,
+ const HostSharedHeld *held, const HostSharedHeld *toast,
+ HostError *error)
+{
+ const doltgres_entries_redone_t *c = captures;
+
+ return txn_spill_redo_unlisted(io, held, c->record, error);
+}
+
/*
* doltgres_entries_doltgres_rmgr_redo_frame_t is rmgr_redo's state across its
* pad: the record decoded, each while live, a row change's out-of-line
@@ -3987,6 +4094,8 @@ typedef struct doltgres_entries_doltgres_rmgr_redo_frame_t
bool freed_live;
TxnRecordMapCarried carried;
bool carried_live;
+ TxnRecordCarryUnlisted unlisted;
+ bool unlisted_live;
StdArcInner *repo;
bool repo_live;
HostSharedReader reading;
@@ -4023,9 +4132,11 @@ doltgres_entries_doltgres_rmgr_redo_pad(void *frame_)
txn_record_TidsFreed_drop(&frame->freed);
if (frame->carried_live)
txn_record_MapCarried_drop(&frame->carried);
+ if (frame->unlisted_live)
+ txn_record_CarryUnlisted_drop(&frame->unlisted);
frame->held_live = frame->reading_live = frame->repo_live = frame->entries_live = frame->completion_live =
frame->change_live = frame->advanced_live = frame->chunk_live = frame->database_change_live =
- frame->freed_live = frame->carried_live = false;
+ frame->freed_live = frame->carried_live = frame->unlisted_live = false;
}
/* doltgres_entries_redo_raised raises a redo's host error as Postgres', into the frame's error, answering false. */
@@ -4121,17 +4232,17 @@ doltgres_entries_base_advanced(doltgres_entries_doltgres_rmgr_redo_frame_t *fram
if (advanced->horizon != 0 &&
!pg_calls_resolve_conflict(raise, advanced->horizon, s->tablespace, s->database, s->relation, frame->error))
return false;
+ end = pg_calls_record_end(frame->record);
if (advanced->has_chain)
{
TxnBaseBase recorded = advanced->base;
advanced->base.key.tag = TXN_BASE_KEY_UNKNOWN;
if (!txn_fold_redo_chained(&frame->io, repo, &frame->reading, &frame->held, advanced->chain.ptr,
- advanced->chain.len, &recorded, &failed))
+ advanced->chain.len, &recorded, end, &failed))
return doltgres_entries_redo_raised(frame, &failed);
return true;
}
- end = pg_calls_record_end(frame->record);
if (!txn_fold_redo(&frame->io, repo, &frame->reading, &frame->held, advanced, end, &kept, &failed))
return doltgres_entries_redo_raised(frame, &failed);
if ((!kept || !shmem_delta_launcher_running()) &&
@@ -4349,6 +4460,17 @@ doltgres_entries_doltgres_rmgr_redo_body(void *frame_)
frame->ok = doltgres_entries_replayed(frame->caps, &frame->carried.storage, NULL, NULL,
doltgres_entries_doltgres_rmgr_redo_closure_11, &c, frame->error);
return;
+ case HOST_WAL_KIND_CARRY_UNLISTED:
+ if (!txn_record_CarryUnlisted_decode(body, body_len, &frame->unlisted, &failed))
+ {
+ doltgres_entries_redo_raised(frame, &failed);
+ return;
+ }
+ frame->unlisted_live = true;
+ c.record = &frame->unlisted;
+ frame->ok = doltgres_entries_replayed(frame->caps, &frame->unlisted.storage, NULL, NULL,
+ doltgres_entries_doltgres_rmgr_redo_closure_12, &c, frame->error);
+ return;
case HOST_WAL_KIND_REPOSITORY_CREATED:
frame->ok = doltgres_entries_store_redone(frame, store_databases_redo_created, false, body, body_len);
return;
@@ -4410,6 +4532,8 @@ typedef struct doltgres_entries_doltgres_rmgr_desc_frame_t
bool freed_live;
TxnRecordMapCarried carried;
bool carried_live;
+ TxnRecordCarryUnlisted unlisted;
+ bool unlisted_live;
StdString text;
bool text_live;
} doltgres_entries_doltgres_rmgr_desc_frame_t;
@@ -4434,8 +4558,10 @@ doltgres_entries_doltgres_rmgr_desc_pad(void *frame_)
txn_record_TidsFreed_drop(&frame->freed);
if (frame->carried_live)
txn_record_MapCarried_drop(&frame->carried);
+ if (frame->unlisted_live)
+ txn_record_CarryUnlisted_drop(&frame->unlisted);
frame->text_live = frame->change_live = frame->advanced_live = frame->chunk_live = false;
- frame->database_change_live = frame->freed_live = frame->carried_live = false;
+ frame->database_change_live = frame->freed_live = frame->carried_live = frame->unlisted_live = false;
}
/* doltgres_entries_described formats a description into the frame's text. */
@@ -4678,6 +4804,30 @@ doltgres_entries_rmgr_desc_carried(doltgres_entries_doltgres_rmgr_desc_frame_t *
return true;
}
+/* doltgres_entries_rmgr_desc_unlisted describes a carry's unlist: the storage and how many of the carry's records' ends it names. */
+static bool
+doltgres_entries_rmgr_desc_unlisted(doltgres_entries_doltgres_rmgr_desc_frame_t *frame, const uint8 *body,
+ size_t body_len, HostError *failed)
+{
+ StdFmtPiece pieces[] = {STD_FMT_LIT("rel "), STD_FMT_ARG(0), STD_FMT_LIT("/"), STD_FMT_ARG(1), STD_FMT_LIT("/"),
+ STD_FMT_ARG(2), STD_FMT_LIT("; "), STD_FMT_ARG(3), STD_FMT_LIT(" ends")};
+ const HostStorage *s;
+ size_t ends;
+
+ if (!txn_record_CarryUnlisted_decode(body, body_len, &frame->unlisted, failed))
+ return false;
+ frame->unlisted_live = true;
+ s = &frame->unlisted.storage;
+ ends = frame->unlisted.ends.len;
+ {
+ StdFmtArg args[] = {{&s->tablespace, std_u32_Display_fmt}, {&s->database, std_u32_Display_fmt},
+ {&s->relation, std_u32_Display_fmt}, {&ends, std_usize_Display_fmt}};
+
+ doltgres_entries_described(frame, pieces, lengthof(pieces), args);
+ }
+ return true;
+}
+
/* doltgres_entries_doltgres_rmgr_desc_body describes the record by its kind, then appends the description. */
static void
doltgres_entries_doltgres_rmgr_desc_body(void *frame_)
@@ -4723,6 +4873,9 @@ doltgres_entries_doltgres_rmgr_desc_body(void *frame_)
case HOST_WAL_KIND_MAP_CARRIED:
ok = doltgres_entries_rmgr_desc_carried(frame, body, body_len, &failed);
break;
+ case HOST_WAL_KIND_CARRY_UNLISTED:
+ ok = doltgres_entries_rmgr_desc_unlisted(frame, body, body_len, &failed);
+ break;
case HOST_WAL_KIND_REPOSITORY_CREATED:
ok = store_databases_describe_created(body, body_len, &frame->text, &failed);
frame->text_live = ok;
@@ -4867,44 +5020,57 @@ typedef struct doltgres_entries_committing_t
/*
* doltgres_entries_committing_closure_0 is committing's lent call over a
* value map: each written value's entry with the transaction's versions
- * against without, recorded for the commit to add.
+ * against without, recorded for the commit to add, read again from the
+ * newest base when an install moved the base before the entries were read
+ * (bugs rows 240 and 316). It pauses at doltgres-commit-valued after each
+ * read of the base.
*/
static bool
doltgres_entries_committing_closure_0(void *captures, HostIoIo *io, const RepoRepo *repo, const HostSharedHeld *held,
HostError *error)
{
const doltgres_entries_committing_t *c = captures;
- TreesStoreReader store;
- TxnBaseSizes adds = {0};
- TxnBaseSizes takes = {0};
- TxnBaseBase base;
- TreesStoreTree root = {0};
- bool has_root = false;
- bool some;
+ HostSharedRecord record = host_shared_Held_record(held);
+ TreesStoreReader store = repo_Repo_reader(repo, HOST_CACHE_PLACEMENT_SERVER, c->reading);
- if (!txn_state_base_record(io, held, &some, &base, error))
- return false;
- if (some)
- {
- has_root = base.has_root;
- root = base.root;
- txn_base_Base_drop(&base);
- }
- store = repo_Repo_reader(repo, HOST_CACHE_PLACEMENT_SERVER, c->reading);
- for (size_t i = 0; i < c->keys->len; i++)
+ for (;;)
{
- const StdVec *key = &STD_VEC_AT(c->keys, StdVec, i);
- TxnBaseSizes added;
- TxnBaseSizes taken;
+ uint64 generation = txn_state_generation(record);
+ TxnBaseSizes adds = {0};
+ TxnBaseSizes takes = {0};
+ TxnBaseBase base;
+ TreesStoreTree root = {0};
+ bool has_root = false;
+ bool some;
- if (!txn_fold_committed_value_moves(io, TREES_STORE_READER_NODE_STORE(&store), held, has_root, root, c->own,
- key->ptr, key->len, &added, &taken, error))
+ if (!txn_state_base_record(io, held, &some, &base, error))
+ return false;
+ if (some)
+ {
+ has_root = base.has_root;
+ root = base.root;
+ txn_base_Base_drop(&base);
+ }
+ if (!host_pause(HOST_SHARED_IO_PAUSES(io), "doltgres-commit-valued", error))
return false;
- adds = txn_base_Sizes_summed(adds, added);
- takes = txn_base_Sizes_summed(takes, taken);
+ for (size_t i = 0; i < c->keys->len; i++)
+ {
+ const StdVec *key = &STD_VEC_AT(c->keys, StdVec, i);
+ TxnBaseSizes added;
+ TxnBaseSizes taken;
+
+ if (!txn_fold_committed_value_moves(io, TREES_STORE_READER_NODE_STORE(&store), held, has_root, root,
+ c->own, key->ptr, key->len, &added, &taken, error))
+ return false;
+ adds = txn_base_Sizes_summed(adds, added);
+ takes = txn_base_Sizes_summed(takes, taken);
+ }
+ if (!txn_state_moved_past(record, generation))
+ {
+ txn_state_committing(held, adds, takes);
+ return true;
+ }
}
- txn_state_committing(held, adds, takes);
- return true;
}
/* doltgres_entries_committing_closure_1 is committing's based: the table and index its storage's base names. */
@@ -4939,48 +5105,63 @@ doltgres_entries_committing_closure_2(void *captures, HostIoIo *io, const RepoRe
/*
* doltgres_entries_committing_closure_3 is committing's lent call over a
* table's or an index's rows: each written key's fold over the base with the
- * transaction's versions against without, recorded for the commit to add.
+ * transaction's versions against without, recorded for the commit to add,
+ * folded again over the newest base when an install moved the base before
+ * the keys' versions were read (bugs rows 240 and 316). It pauses at
+ * doltgres-commit-based after each read of the base.
*/
static bool
doltgres_entries_committing_closure_3(void *captures, HostIoIo *io, const RepoRepo *repo, const HostSharedHeld *held,
HostError *error)
{
const doltgres_entries_committing_t *c = captures;
- TreesStoreReader store;
- TxnBaseSizes adds = {0};
- TxnBaseSizes takes = {0};
- TxnBaseBase base;
- TreesStoreTree root;
- TreesStoreTree tuple_ids;
- bool has_root;
- bool has_tuple_ids;
- bool some;
+ HostSharedRecord record = host_shared_Held_record(held);
+ TreesStoreReader store = repo_Repo_reader(repo, HOST_CACHE_PLACEMENT_SERVER, c->reading);
- if (!txn_state_base_record(io, held, &some, &base, error))
- return false;
- if (!some)
- return true;
- has_root = base.has_root;
- root = base.root;
- has_tuple_ids = base.has_tuple_ids;
- tuple_ids = base.tuple_ids;
- txn_base_Base_drop(&base);
- store = repo_Repo_reader(repo, HOST_CACHE_PLACEMENT_SERVER, c->reading);
- for (size_t i = 0; i < c->keys->len; i++)
+ for (;;)
{
- const StdVec *key = &STD_VEC_AT(c->keys, StdVec, i);
- TxnBaseSizes added;
- TxnBaseSizes taken;
-
- if (!txn_fold_committed_moves(io, TREES_STORE_READER_NODE_STORE(&store), held, has_root, root, has_tuple_ids,
- tuple_ids, c->order, c->keyless, c->index, c->own, key->ptr, key->len, &added,
- &taken, error))
+ uint64 generation = txn_state_generation(record);
+ TxnBaseSizes adds = {0};
+ TxnBaseSizes takes = {0};
+ TxnBaseBase base;
+ TreesStoreTree root;
+ TreesStoreTree tuple_ids;
+ bool has_root;
+ bool has_tuple_ids;
+ HostLsn folded;
+ bool some;
+
+ if (!txn_state_base_record(io, held, &some, &base, error))
return false;
- adds = txn_base_Sizes_summed(adds, added);
- takes = txn_base_Sizes_summed(takes, taken);
+ if (!some)
+ return true;
+ has_root = base.has_root;
+ root = base.root;
+ has_tuple_ids = base.has_tuple_ids;
+ tuple_ids = base.tuple_ids;
+ folded = base.folded;
+ txn_base_Base_drop(&base);
+ if (!host_pause(HOST_SHARED_IO_PAUSES(io), "doltgres-commit-based", error))
+ return false;
+ for (size_t i = 0; i < c->keys->len; i++)
+ {
+ const StdVec *key = &STD_VEC_AT(c->keys, StdVec, i);
+ TxnBaseSizes added;
+ TxnBaseSizes taken;
+
+ if (!txn_fold_committed_moves(io, TREES_STORE_READER_NODE_STORE(&store), held, has_root, root,
+ has_tuple_ids, tuple_ids, c->order, c->keyless, c->index, folded, c->own,
+ key->ptr, key->len, &added, &taken, error))
+ return false;
+ adds = txn_base_Sizes_summed(adds, added);
+ takes = txn_base_Sizes_summed(takes, taken);
+ }
+ if (!txn_state_moved_past(record, generation))
+ {
+ txn_state_committing(held, adds, takes);
+ return true;
+ }
}
- txn_state_committing(held, adds, takes);
- return true;
}
/* doltgres_entries_TupleStorageOfVecVecU8_drop is a written tree's drop glue: its keys. */
@@ -5234,7 +5415,9 @@ doltgres_entries_committing(const PgCatalogCaps *caps, PgPgError *error)
* which Postgres records before it calls this, it takes back the
* transaction's versions by pruning those keys (2g's take-back). Each end
* clears the moves of the totals. It ends the settle's list at a commit, a
- * prepare, or an abort (the rewrite text's section 6).
+ * prepare, or an abort (the rewrite text's section 6). An abort first
+ * settles the process's pending spill whose take-out of versions from the
+ * memtable its error stopped, so the take-out ends there.
*/
bool
doltgres_entries_doltgres_xact_ended(const PgCatalogCaps *caps, int32 event, void *arg, PgPgError *error)
@@ -5274,7 +5457,7 @@ doltgres_entries_doltgres_xact_ended(const PgCatalogCaps *caps, int32 event, voi
}
else if (abort)
{
- if (!txn_written_aborted(&io, &failed))
+ if (!txn_spill_take_settled(&io, &failed) || !txn_written_aborted(&io, pg_calls_last_record_end(), &failed))
return doltgres_entries_raised(false, &failed, error);
doltgres_settle_ended(&io, true);
}
@@ -5448,25 +5631,31 @@ doltgres_entries_handle(const PgScan *scan)
#define TXN_STATE_RESULT_ERROR error
#include "doltgres/txn/state_template.h"
-/* doltgres_entries_doltgres_scan_begin_captures_t is scan_begin's lent call's captures: the relation and the read. */
+/*
+ * doltgres_entries_doltgres_scan_begin_captures_t is scan_begin's lent
+ * call's captures: the relation, the read, and the base generation its base
+ * was read at.
+ */
typedef struct doltgres_entries_doltgres_scan_begin_captures_t
{
const PgRel *relation;
const HostSnapshot *read;
+ uint64 generation;
} doltgres_entries_doltgres_scan_begin_captures_t;
/*
* doltgres_entries_doltgres_scan_begin_closure_0 is scan_begin's lent call:
* the storage's block count and the base its read sees, or its newest when
- * it has no snapshot.
+ * it has no snapshot, with the generation read before it.
*/
static bool
doltgres_entries_doltgres_scan_begin_closure_0(void *captures, HostIoIo *io, const RepoRepo *repo,
const HostSharedHeld *held, uint64 *result_0, bool *some,
TxnBaseBase *result_1, HostError *error)
{
- const doltgres_entries_doltgres_scan_begin_captures_t *c = captures;
+ doltgres_entries_doltgres_scan_begin_captures_t *c = captures;
+ c->generation = txn_state_generation(host_shared_Held_record(held));
if (c->read != NULL ? !txn_state_readable(io, held, c->read, some, result_1, error) :
!txn_state_newest(io, held, some, result_1, error))
return false;
@@ -5543,10 +5732,48 @@ doltgres_entries_doltgres_scan_begin_pad(void *frame_)
frame->columns_live = frame->key_recorded_live = frame->reading_live = frame->repo_live = false;
}
+/* txn::state::held at u64, which entries_tail.c defines and scan_begin and analyze_next_tuple take. */
+#define TXN_STATE_TS U64
+#define TXN_STATE_RETURNS bool
+#define TXN_STATE_RESULT_PARAMS , uint64 *result, HostError *error
+#define TXN_STATE_DECLARE
+#include "doltgres/txn/state_template.h"
+
+/*
+ * doltgres_entries_doltgres_scan_begin_closure_1 is a parallel scan's lent
+ * call: the base generation its worker reads at, one before the state's when
+ * the base the leader shared is no longer the storage's newest, so the
+ * worker's first read moves to the newest (rebased).
+ */
+static bool
+doltgres_entries_doltgres_scan_begin_closure_1(void *captures, HostIoIo *io, const RepoRepo *repo,
+ const HostSharedHeld *held, uint64 *result, HostError *error)
+{
+ const DoltgresScanShared *shared = captures;
+ uint64 generation = txn_state_generation(host_shared_Held_record(held));
+ TxnBaseBase newest;
+ bool some;
+ bool same;
+
+ if (!txn_state_newest(io, held, &some, &newest, error))
+ return false;
+ same = !some ||
+ (newest.has_root == shared->has_tree &&
+ (!newest.has_root || trees_store_Tree_PartialEq_eq(&newest.root, &shared->tree)) &&
+ newest.has_tuple_ids == shared->has_tuple_ids &&
+ (!newest.has_tuple_ids || trees_store_Tree_PartialEq_eq(&newest.tuple_ids, &shared->tuple_ids)) &&
+ newest.read == shared->folded);
+ if (some)
+ txn_base_Base_drop(&newest);
+ *result = same ? generation : generation - 1;
+ return true;
+}
+
/*
* doltgres_entries_doltgres_scan_begin_parallel makes a parallel scan's
* record over the base the leader shared: its trees, its split keys, its
- * epochs, and its count of entries and units.
+ * epochs, its count of entries and units, and the base generation its
+ * worker reads at.
*/
static bool
doltgres_entries_doltgres_scan_begin_parallel(doltgres_entries_doltgres_scan_begin_frame_t *frame,
@@ -5561,9 +5788,20 @@ doltgres_entries_doltgres_scan_begin_parallel(doltgres_entries_doltgres_scan_beg
bool has_chain;
TreesStoreReader store;
HostError failed;
+ uint64 generation;
frame->shared.has_tree = doltgres_entries_root(&parts.table, &frame->shared.tree);
frame->shared.has_tuple_ids = doltgres_entries_root(&parts.tuples, &frame->shared.tuple_ids);
+ frame->shared.folded = parts.folded;
+ if (!doltgres_entries_making(frame->caps, &frame->io, frame->relation, &frame->found, frame->error))
+ return false;
+ if (!txn_state_held_U64(&frame->io, repo, &frame->reading, frame->found,
+ doltgres_entries_doltgres_scan_begin_closure_1, &frame->shared, NULL, &generation,
+ &failed))
+ {
+ *frame->error = pghost_raised(&failed);
+ return false;
+ }
if (!doltgres_scan_split_keys(splits, splits_len, &frame->splits, &failed))
{
*frame->error = pghost_raised(&failed);
@@ -5572,7 +5810,6 @@ doltgres_entries_doltgres_scan_begin_parallel(doltgres_entries_doltgres_scan_beg
frame->splits_live = true;
frame->shared.fresh_end = parts.fresh;
frame->shared.entries = parts.entries;
- frame->shared.folded = parts.folded;
frame->shared.units = units;
store = repo_Repo_reader(repo, HOST_CACHE_PLACEMENT_SERVER, &frame->reading);
has_chain = doltgres_entries_root(&parts.chain, &chain);
@@ -5584,8 +5821,8 @@ doltgres_entries_doltgres_scan_begin_parallel(doltgres_entries_doltgres_scan_beg
}
frame->shared.splits = frame->splits;
frame->splits_live = frame->keyed_live = frame->order_live = false;
- if (!doltgres_scan_begin_parallel(&frame->io, &frame->shared, &frame->keyed, pg_calls_rel_id(frame->relation),
- &frame->order, owned, &failed))
+ if (!doltgres_scan_begin_parallel(&frame->io, &frame->shared, generation, &frame->keyed,
+ pg_calls_rel_id(frame->relation), &frame->order, owned, &failed))
{
*frame->error = pghost_raised(&failed);
return false;
@@ -5630,7 +5867,7 @@ doltgres_entries_doltgres_scan_begin_serial(doltgres_entries_doltgres_scan_begin
store = repo_Repo_reader(repo, HOST_CACHE_PLACEMENT_SERVER, &frame->reading);
frame->keyed_live = frame->order_live = false;
if (!doltgres_scan_scan_begin(&frame->io, TREES_STORE_READER_NODE_STORE(&store), blocks,
- has_base ? &frame->read_base : NULL, &frame->keyed,
+ has_base ? &frame->read_base : NULL, c.generation, &frame->keyed,
pg_calls_rel_id(frame->relation), &frame->order, owned, &failed))
{
*frame->error = pghost_raised(&failed);
@@ -6413,13 +6650,6 @@ doltgres_entries_sampled(HostIoIo *io, const DoltgresScanRow *row, DoltgresEntri
return true;
}
-/* txn::state::held at u64, which entries_tail.c defines and analyze_next_tuple takes. */
-#define TXN_STATE_TS U64
-#define TXN_STATE_RETURNS bool
-#define TXN_STATE_RESULT_PARAMS , uint64 *result, HostError *error
-#define TXN_STATE_DECLARE
-#include "doltgres/txn/state_template.h"
-
/* doltgres_entries_spent_t is analyze_next_tuple's lent call's captures: the reader and the block's first tid. */
typedef struct doltgres_entries_spent_t
{
diff --git a/src/doltgres/doltgres/c/entries/searches.c b/src/doltgres/doltgres/c/entries/searches.c
index 649d366aa2a2d..7470f2f41392e 100644
--- a/src/doltgres/doltgres/c/entries/searches.c
+++ b/src/doltgres/doltgres/c/entries/searches.c
@@ -2661,12 +2661,26 @@ doltgres_entries_searches_read_closure_5(void *captures, HostIoIo *io, DoltgresE
return true;
}
-/* doltgres_entries_searches_read_closure_3 is read's lent call: the base the scan's snapshot reads, as splits' is. */
+/* doltgres_entries_searches_read_captures_t is read's lent call's captures: the snapshot and the generation it reads. */
+typedef struct doltgres_entries_searches_read_captures_t
+{
+ const HostSnapshot *read;
+ uint64 generation;
+} doltgres_entries_searches_read_captures_t;
+
+/*
+ * doltgres_entries_searches_read_closure_3 is read's lent call: the base the
+ * scan's snapshot reads, as splits' is, with the base generation read
+ * before it.
+ */
static bool
doltgres_entries_searches_read_closure_3(void *captures, HostIoIo *io, const RepoRepo *repo, const HostSharedHeld *held, bool *some,
TxnBaseBase *result, HostError *error)
{
- return doltgres_entries_searches_Entries_splits_closure_0(captures, io, repo, held, some, result, error);
+ doltgres_entries_searches_read_captures_t *c = captures;
+
+ c->generation = txn_state_generation(host_shared_Held_record(held));
+ return doltgres_entries_searches_Entries_splits_closure_0((void *) c->read, io, repo, held, some, result, error);
}
/* doltgres_entries_searches_unsnapshotted returns the error of an index scan asked to read with no snapshot. */
@@ -2828,6 +2842,7 @@ doltgres_entries_searches_read_body_0(void *frame_)
TreesStoreReader store;
TreesKeyOrder order;
bool ok;
+ doltgres_entries_searches_read_captures_t c = {&frame->read};
if (!host_owned_TableDoltgresEntriesSearchesSearch_withVecU8(&DOLTGRES_ENTRIES_SEARCHES_SEARCHES, &frame->io,
frame->owned,
@@ -2843,8 +2858,8 @@ doltgres_entries_searches_read_body_0(void *frame_)
doltgres_entries_index_making(&frame->io, index, &found, frame->error)))
return;
if (!txn_state_held_OptionBase(&frame->io, STD_ARC_VALUE(frame->repo, _Alignof(RepoRepo)), &frame->reading,
- found, doltgres_entries_searches_read_closure_3, &frame->read, NULL, &some,
- &base, &failed))
+ found, doltgres_entries_searches_read_closure_3, &c, NULL, &some, &base,
+ &failed))
{
*frame->error = pghost_raised(&failed);
return;
@@ -2854,7 +2869,8 @@ doltgres_entries_searches_read_body_0(void *frame_)
trees_KeyOrder_Clone_clone(&frame->order, &order);
frame->keyed_live = false;
ok = doltgres_scan_scan_begin(&frame->io, TREES_STORE_READER_NODE_STORE(&store), 0, some ? &base : NULL,
- &frame->keyed, pg_calls_index_table(index), &order, &reader, &failed);
+ c.generation, &frame->keyed, pg_calls_index_table(index), &order, &reader,
+ &failed);
if (some)
txn_base_Base_drop(&base);
if (!ok ||
diff --git a/src/doltgres/doltgres/c/entries/testing.c b/src/doltgres/doltgres/c/entries/testing.c
index 57ba3e3ce8f09..c1734a4c83640 100644
--- a/src/doltgres/doltgres/c/entries/testing.c
+++ b/src/doltgres/doltgres/c/entries/testing.c
@@ -32,10 +32,12 @@
#include "doltgres/chunks/error.h"
#include "doltgres/chunks/file.h"
#include "doltgres/chunks/pack.h"
+#include "doltgres/chunks/packs.h"
#include "doltgres/chunks/packs/roots.h"
#include "doltgres/chunks/packs/shared.h"
#include "doltgres/chunks/trailer.h"
#include "doltgres/doltgres/entries.h"
+#include "doltgres/doltgres/entries_tail.h"
#include "doltgres/doltgres/entries/testing.h"
#include "doltgres/doltgres/index.h"
#include "doltgres/doltgres/kinds.h"
@@ -93,6 +95,7 @@
#include "doltgres/txn/memtable/table.h"
#include "doltgres/txn/memtable/version.h"
#include "doltgres/txn/rewrite.h"
+#include "doltgres/txn/runs.h"
#include "doltgres/txn/spill.h"
#include "doltgres/txn/state.h"
#include "doltgres/txn/status.h"
@@ -1346,19 +1349,23 @@ doltgres_entries_testing_doltgres_test_wal(const PgCaps *caps, int64 value, int6
/*
* doltgres_entries_testing_doltgres_test_wal_unknown inserts and flushes a
- * record of a kind no crate owns, of len zero bytes, whose redo stops
- * recovery.
+ * record of a kind no crate owns, the lowest value no kind of the registry
+ * has, of len zero bytes, whose redo stops recovery.
*/
bool
doltgres_entries_testing_doltgres_test_wal_unknown(const PgCaps *caps, int32 len, PgPgError *error)
{
const PgMayRaise *raise = pg_Caps_may_raise(caps);
+ uint8 unknown = 0;
+ HostWalKind kind;
StdVec body;
uint64 lsn;
bool ok;
+ while (host_WalKind_of(unknown, &kind))
+ unknown++;
std_vec_u8_from_elem(&body, 0, len > 0 ? (size_t) len : 0);
- ok = pg_calls_wal_insert(raise, 14, (const uint8 *) body.ptr, body.len, &lsn, error) &&
+ ok = pg_calls_wal_insert(raise, unknown, (const uint8 *) body.ptr, body.len, &lsn, error) &&
pg_calls_wal_flush(raise, lsn, error);
std_vec_free_Checked(&body, 1, 1);
return ok;
@@ -7112,7 +7119,7 @@ doltgres_entries_testing_doltgres_test_collect(const PgCatalogCaps *caps, PgDatu
HostError failed;
if (!host_session_database(&database, &tablespace, &failed) ||
- !host_collection_ask(&io, database, counts, &failed))
+ !host_collection_ask(&io, database, CHUNKS_PACKS_DEFAULT_SPACE, counts, &failed))
{
*error = pghost_raised(&failed);
return false;
@@ -7316,7 +7323,7 @@ doltgres_entries_testing_identity_body_0(void *frame_)
uint64 id;
StdString shown;
- frame->io = pghost_io(raise);
+ frame->io = pghost_io_catalog(frame->caps);
if (!doltgres_entries_repository(&frame->io, &frame->repo, frame->error))
return;
frame->repo_live = true;
@@ -7402,6 +7409,169 @@ doltgres_entries_testing_doltgres_test_identity(const PgCatalogCaps *caps, int32
return frame.ok;
}
+/*
+ * doltgres_entries_testing_identity_versions_frame_0_t is
+ * doltgres_test_identity_versions's state across its pad: the working
+ * state's delta, a read's hold, the key, and the versions read, each while
+ * live.
+ */
+typedef struct doltgres_entries_testing_identity_versions_frame_0_t
+{
+ const PgCaps *caps;
+ int32 kind;
+ uint32 number;
+ HostIoIo io;
+ HostSharedHeld deltas;
+ bool deltas_live;
+ HostDeltaHold hold;
+ bool hold_live;
+ StdVec key;
+ bool key_live;
+ StdVec versions;
+ bool versions_live;
+ StdVec spilled;
+ bool spilled_live;
+ PgDatum *result;
+ PgPgError *error;
+ bool ok;
+} doltgres_entries_testing_identity_versions_frame_0_t;
+
+/* doltgres_entries_testing_identity_versions_body_0 counts the key's versions in the delta and its identity spills. */
+static void
+doltgres_entries_testing_identity_versions_body_0(void *frame_)
+{
+ doltgres_entries_testing_identity_versions_frame_0_t *frame = frame_;
+ const PgMayRaise *raise = pg_Caps_may_raise(frame->caps);
+ HostError failed;
+ uint32 database = pg_calls_my_database_id();
+ bool some;
+ uint64 count = 0;
+ StdString shown;
+
+ frame->io = pghost_io(raise);
+ if (database == 0)
+ {
+ *frame->error = doltgres_entries_testing_pg_fork("a backend has no database");
+ return;
+ }
+ if (!txn_memtable_branch_find(HOST_SHARED_IO_WITHIN(&frame->io), database, &some, &frame->deltas, &failed))
+ {
+ *frame->error = pghost_raised(&failed);
+ return;
+ }
+ if (some)
+ {
+ HostSharedRecord deltas = host_shared_Held_record(&frame->deltas);
+
+ frame->deltas_live = true;
+ if (!host_delta_hold(&frame->io, &frame->hold, &failed))
+ {
+ *frame->error = pghost_raised(&failed);
+ return;
+ }
+ frame->hold_live = true;
+ txn_identity_by_oid(frame->kind == 1 ? TXN_IDENTITY_KIND_SCHEMA : TXN_IDENTITY_KIND_RELATION, frame->number,
+ &frame->key);
+ frame->key_live = true;
+ if (!txn_memtable_branch_identity_versions(&frame->io, &frame->hold, deltas, frame->key.ptr, frame->key.len,
+ &frame->versions, &failed))
+ {
+ *frame->error = pghost_raised(&failed);
+ return;
+ }
+ frame->versions_live = true;
+ if (!txn_spill_versions(&TXN_SPILL_IDENTITY_HOME_OPS, &frame->io, &frame->hold, deltas,
+ TXN_RUNS_KIND_SPILLED_IDENTITY, frame->key.ptr, frame->key.len, NULL, &frame->spilled,
+ &failed))
+ {
+ *frame->error = pghost_raised(&failed);
+ return;
+ }
+ frame->spilled_live = true;
+ count = (uint64) (frame->versions.len + frame->spilled.len);
+ }
+ {
+ StdFmtPiece pieces[] = {STD_FMT_ARG(0)};
+ StdFmtArg args[] = {{&count, std_u64_Display_fmt}};
+ StdFmtArguments arguments = {pieces, lengthof(pieces), args};
+
+ std_fmt_format_Checked(&shown, &arguments);
+ }
+ frame->ok = doltgres_entries_testing_text_datum(raise, &shown, "a count's text holds no NUL", __LINE__, 47,
+ frame->result, frame->error);
+}
+
+/* doltgres_entries_testing_identity_versions_pad_0 drops the versions, the key, the hold, and the delta. */
+static void
+doltgres_entries_testing_identity_versions_pad_0(void *frame_)
+{
+ doltgres_entries_testing_identity_versions_frame_0_t *frame = frame_;
+
+ if (frame->spilled_live)
+ txn_memtable_version_versions_drop(&frame->spilled, 0);
+ if (frame->versions_live)
+ txn_memtable_version_versions_drop(&frame->versions, 0);
+ if (frame->key_live)
+ std_vec_free_Checked(&frame->key, 1, 1);
+ if (frame->hold_live)
+ host_delta_Hold_drop(&frame->hold);
+ if (frame->deltas_live)
+ host_shared_Held_drop(&frame->deltas);
+ frame->spilled_live = frame->versions_live = frame->key_live = frame->hold_live = frame->deltas_live = false;
+}
+
+/*
+ * doltgres_entries_testing_doltgres_test_identity_versions returns, as text,
+ * how many versions the database's working state's delta and its identity
+ * spills hold of the identity tree's key of a schema's OID for kind 1 or a
+ * relation's for kind 2 (bugs row 291).
+ */
+bool
+doltgres_entries_testing_doltgres_test_identity_versions(const PgCaps *caps, int32 kind, uint32 number,
+ PgDatum *result, PgPgError *error)
+{
+ doltgres_entries_testing_identity_versions_frame_0_t frame = {.caps = caps, .kind = kind, .number = number,
+ .result = result, .error = error};
+
+ std_landing(&frame, doltgres_entries_testing_identity_versions_body_0,
+ doltgres_entries_testing_identity_versions_pad_0);
+ doltgres_entries_testing_identity_versions_pad_0(&frame);
+ return frame.ok;
+}
+
+/*
+ * doltgres_entries_testing_doltgres_test_save runs item 6's save outside a
+ * checkpoint for the backend's database's repository, as a states' pool's
+ * make does, and returns whether it took an identity version out and
+ * whether it turned a state it gathered clean, as two numbers.
+ */
+bool
+doltgres_entries_testing_doltgres_test_save(const PgCaps *caps, PgDatum *result, PgPgError *error)
+{
+ const PgMayRaise *raise = pg_Caps_may_raise(caps);
+ HostIoIo io = pghost_io(raise);
+ StdArcInner *repo;
+ uint32 own;
+ bool took;
+ bool cleaned;
+ HostError failed;
+ uint64 numbers[2];
+
+ if (!doltgres_entries_repository(&io, &repo, error))
+ return false;
+ own = repo_Repo_own(STD_ARC_VALUE(repo, _Alignof(RepoRepo)));
+ std_arc_drop(repo, sizeof(RepoRepo), _Alignof(RepoRepo), doltgres_entries_testing_Repo_drop);
+ if (!txn_runs_save(&io, pg_calls_my_database_id(), own, &took, &cleaned, &failed))
+ {
+ *error = pghost_raised(&failed);
+ return false;
+ }
+ numbers[0] = took;
+ numbers[1] = cleaned;
+ return doltgres_entries_testing_numbers_datum(raise, numbers, lengthof(numbers), "two numbers hold no NUL",
+ __LINE__, 58, result, error);
+}
+
/* state.rs' held at Option, which entries.c defines. */
#define TXN_STATE_TS OptionBase
#define TXN_STATE_RETURNS bool
@@ -7812,6 +7982,71 @@ doltgres_entries_testing_doltgres_test_base_sizes(const PgCatalogCaps *caps, con
return doltgres_entries_testing_held_run(&frame, doltgres_entries_testing_base_sizes_body_0);
}
+static void doltgres_entries_testing_hex(StdBytes bytes, StdString *result);
+
+/* doltgres_entries_testing_base_points_body_0 reads the newest base's root, fold point, and read point as their text. */
+static void
+doltgres_entries_testing_base_points_body_0(void *frame_)
+{
+ doltgres_entries_testing_held_frame_0_t *frame = frame_;
+ const PgMayRaise *raise = pg_CatalogCaps_may_raise(frame->caps);
+ HostError failed;
+ bool some;
+ StdString shown;
+
+ if (!doltgres_entries_testing_opened(frame))
+ return;
+ if (!txn_state_held_OptionBase(&frame->io, doltgres_entries_testing_held_repo(frame), &frame->reading,
+ frame->found, doltgres_entries_testing_doltgres_test_base_sizes_closure_0, NULL, NULL, &some,
+ &frame->base, &failed))
+ {
+ *frame->error = pghost_raised(&failed);
+ return;
+ }
+ frame->base_live = some;
+ if (some)
+ {
+ StdString root;
+ StdFmtPiece pieces[] = {STD_FMT_ARG(0), STD_FMT_LIT(" "), STD_FMT_ARG(1), STD_FMT_LIT(" "), STD_FMT_ARG(2)};
+ StdFmtArg args[] = {{&root, std_String_Display_fmt}, {&frame->base.folded, std_u64_Display_fmt},
+ {&frame->base.read, std_u64_Display_fmt}};
+ StdFmtArguments arguments = {pieces, lengthof(pieces), args};
+
+ if (frame->base.has_root)
+ {
+ StdBytes address = {frame->base.root.address.f0, sizeof(frame->base.root.address.f0)};
+
+ doltgres_entries_testing_hex(address, &root);
+ }
+ else
+ std_vec_new(&root, 1, 1);
+ std_fmt_format_Checked(&shown, &arguments);
+ std_String_drop(&root);
+ frame->base_live = false;
+ txn_base_Base_drop(&frame->base);
+ }
+ else
+ std_string_from_Checked(&shown, "none", 4);
+ frame->ok = doltgres_entries_testing_text_datum(raise, &shown, "a base's points' text holds no NUL", 2899, 47,
+ frame->datum, frame->error);
+}
+
+/*
+ * doltgres_entries_testing_doltgres_test_base_points returns a table's
+ * storage's newest base's root as its address in hex, with its fold point and
+ * its read point, or "none" for a storage with no base, so a standby's base
+ * is compared with its primary's.
+ */
+bool
+doltgres_entries_testing_doltgres_test_base_points(const PgCatalogCaps *caps, const PgRel *target, PgDatum *result,
+ PgPgError *error)
+{
+ doltgres_entries_testing_held_frame_0_t frame = {.caps = caps, .target = target, .datum = result,
+ .error = error};
+
+ return doltgres_entries_testing_held_run(&frame, doltgres_entries_testing_base_points_body_0);
+}
+
/* doltgres_entries_testing_doltgres_test_held_closure_0 is doltgres_test_held's lent call: whether a version holds the tuple id. */
static bool
doltgres_entries_testing_doltgres_test_held_closure_0(void *captures, HostIoIo *io, const RepoRepo *repo, const HostSharedHeld *held,
@@ -7959,6 +8194,33 @@ doltgres_entries_testing_doltgres_test_totals(const PgCatalogCaps *caps, const P
return doltgres_entries_testing_held_run(&frame, doltgres_entries_testing_totals_body_0);
}
+/*
+ * doltgres_entries_testing_doltgres_test_scan_shape returns a fork table's
+ * shape as its scans' costs read it: its tree's leaves and levels above
+ * them, then its tuple id tree's.
+ */
+bool
+doltgres_entries_testing_doltgres_test_scan_shape(const PgCatalogCaps *caps, const PgRel *target, PgDatum *result,
+ PgPgError *error)
+{
+ DoltgresEntriesScanned shape;
+ uint64 heights[2];
+ StdString shown;
+ StdFmtPiece pieces[] = {STD_FMT_ARG(0), STD_FMT_LIT(" "), STD_FMT_ARG(1), STD_FMT_LIT(" "), STD_FMT_ARG(2),
+ STD_FMT_LIT(" "), STD_FMT_ARG(3)};
+ StdFmtArg args[] = {{&shape.rows.leaves, std_u64_Display_fmt}, {&heights[0], std_u64_Display_fmt},
+ {&shape.tuple_ids.leaves, std_u64_Display_fmt}, {&heights[1], std_u64_Display_fmt}};
+ StdFmtArguments arguments = {pieces, lengthof(pieces), args};
+
+ if (!doltgres_entries_scanned(caps, 0, pg_calls_rel_id(target), &shape, error))
+ return false;
+ heights[0] = shape.rows.height;
+ heights[1] = shape.tuple_ids.height;
+ std_fmt_format_Checked(&shown, &arguments);
+ return doltgres_entries_testing_text_datum(pg_CatalogCaps_may_raise(caps), &shown,
+ "a scan shape's text holds no NUL", __LINE__, 0, result, error);
+}
+
/* doltgres_entries_testing_op_name returns the name doltgres_test_changes gives a version's op. */
static StdStr
doltgres_entries_testing_op_name(TxnMemtableVersionOpRefTag op)
diff --git a/src/doltgres/doltgres/c/entries_index.c b/src/doltgres/doltgres/c/entries_index.c
index 9fc91ec7d1e17..ab36ce93d165e 100644
--- a/src/doltgres/doltgres/c/entries_index.c
+++ b/src/doltgres/doltgres/c/entries_index.c
@@ -1421,21 +1421,28 @@ typedef struct DoltgresEntriesOthers
StdBytes keyed;
} DoltgresEntriesOthers;
-/* doltgres_entries_others_closure_1 is others' read of the index's base the snapshot sees. */
-static bool
-doltgres_entries_others_closure_1(void *captures, HostIoIo *io, const RepoRepo *repo, const HostSharedHeld *held,
- bool *some, TxnBaseBase *result, HostError *error)
-{
- return txn_state_readable(io, held, captures, some, result, error);
-}
-
-/* doltgres_entries_others_captures_t is others' next's captures: the snapshot and the reader. */
+/*
+ * doltgres_entries_others_captures_t is others' lent calls' captures: the
+ * snapshot, the base generation its base was read at, and the reader.
+ */
typedef struct doltgres_entries_others_captures_t
{
const HostSnapshot *seen;
+ uint64 generation;
HostOwnedHandle reader;
} doltgres_entries_others_captures_t;
+/* doltgres_entries_others_closure_1 is others' read of the index's base the snapshot sees, with the generation before it. */
+static bool
+doltgres_entries_others_closure_1(void *captures, HostIoIo *io, const RepoRepo *repo, const HostSharedHeld *held,
+ bool *some, TxnBaseBase *result, HostError *error)
+{
+ doltgres_entries_others_captures_t *c = captures;
+
+ c->generation = txn_state_generation(host_shared_Held_record(held));
+ return txn_state_readable(io, held, c->seen, some, result, error);
+}
+
/* doltgres_entries_others_closure_2 is others' next: the reader's next entry under the snapshot. */
static bool
doltgres_entries_others_closure_2(void *captures, HostIoIo *io, const RepoRepo *repo, const HostSharedHeld *held,
@@ -1531,7 +1538,7 @@ doltgres_entries_others_entries(doltgres_entries_others_frame_t *frame)
if (!doltgres_entries_index_making(&frame->io, which->index, &found, frame->error))
return false;
if (!txn_state_held_OptionBase(&frame->io, which->repo, which->reading, found, doltgres_entries_others_closure_1,
- (void *) frame->seen, NULL, &some, &frame->base, &failed))
+ &c, NULL, &some, &frame->base, &failed))
{
*frame->error = pghost_raised(&failed);
return false;
@@ -1543,7 +1550,8 @@ doltgres_entries_others_entries(doltgres_entries_others_frame_t *frame)
std_slice_to_vec_Checked(&keyed, which->keyed.ptr, which->keyed.len, 1, 1);
frame->order_live = false;
if (!doltgres_scan_scan_begin(&frame->io, TREES_STORE_READER_NODE_STORE(&store), 0, some ? &frame->base : NULL,
- &keyed, pg_calls_index_table(which->index), &frame->order, &c.reader, &failed) ||
+ c.generation, &keyed, pg_calls_index_table(which->index), &frame->order, &c.reader,
+ &failed) ||
!doltgres_scan_seek(&frame->io, &stores, c.reader, which->prefix.ptr, which->prefix.len,
DOLTGRES_SCAN_DIRECTION_FORWARD, &failed))
{
@@ -1964,11 +1972,10 @@ doltgres_entries_doltgres_index_insert_killed_op(void *value, void *captures)
}
}
-/* doltgres_entries_doltgres_index_insert_captures_t is index_insert's key read's captures: the reader, the table's base, and the row's tuple id. */
+/* doltgres_entries_doltgres_index_insert_captures_t is index_insert's key read's captures: the reader and the row's tuple id. */
typedef struct doltgres_entries_doltgres_index_insert_captures_t
{
const HostSharedReader *reading;
- const TxnBaseBase *base;
uint64 tuple;
} doltgres_entries_doltgres_index_insert_captures_t;
@@ -1981,9 +1988,8 @@ doltgres_entries_doltgres_index_insert_closure_1(void *captures, HostIoIo *io, c
const doltgres_entries_doltgres_index_insert_captures_t *c = captures;
TreesStoreReader store = repo_Repo_reader(repo, HOST_CACHE_PLACEMENT_SERVER, c->reading);
- return doltgres_scan_keyed(io, TREES_STORE_READER_NODE_STORE(&store), host_shared_Held_record(held),
- c->base->has_tuple_ids, c->base->tuple_ids, c->tuple, some, result_0,
- result_1, error);
+ return doltgres_scan_keyed(io, TREES_STORE_READER_NODE_STORE(&store), held, c->tuple, some, result_0, result_1,
+ error);
}
/* doltgres_entries_Described_drop_glue is an Arc's value's drop glue. */
@@ -2101,8 +2107,7 @@ doltgres_entries_doltgres_index_insert_body(void *frame_)
uint64 tuple = pg_calls_tid_value(frame->heap_tid);
doltgres_entries_doltgres_index_insert_killed_captures_t killed = {.relation = pg_calls_rel_id(frame->heap),
.tuple = tuple};
- doltgres_entries_doltgres_index_insert_captures_t c = {.reading = &frame->opened.reading, .base = &frame->base,
- .tuple = tuple};
+ doltgres_entries_doltgres_index_insert_captures_t c = {.reading = &frame->opened.reading, .tuple = tuple};
const RepoRepo *repo;
const DoltgresIndexDescribed *described;
TxnMemtableVersionWriter writer;
diff --git a/src/doltgres/doltgres/c/entries_tail.c b/src/doltgres/doltgres/c/entries_tail.c
index 022250ae32998..c05f6dec8bb7c 100644
--- a/src/doltgres/doltgres/c/entries_tail.c
+++ b/src/doltgres/doltgres/c/entries_tail.c
@@ -25,6 +25,7 @@
#include "access/nbtree.h"
#include "access/tableam.h"
+#include "access/visibilitymapdefs.h"
#include "access/xact.h"
#include "catalog/pg_class_d.h"
#include "commands/progress.h"
@@ -338,8 +339,6 @@ doltgres_entries_chained_closure_0_body(void *frame_)
TreesStoreReader store = repo_Repo_reader(frame->repo, HOST_CACHE_PLACEMENT_SERVER, c->reading);
TxnBaseBase newest;
bool some;
- bool has_tuple_ids = false;
- TreesStoreTree tuple_ids = {0};
HostLsn folded = 0;
bool has_key;
bool keyless;
@@ -350,13 +349,11 @@ doltgres_entries_chained_closure_0_body(void *frame_)
return;
if (some)
{
- has_tuple_ids = newest.has_tuple_ids;
- tuple_ids = newest.tuple_ids;
- folded = newest.folded;
+ folded = newest.read;
txn_base_Base_drop(&newest);
}
- if (!doltgres_scan_keyed(frame->io, TREES_STORE_READER_NODE_STORE(&store), host_shared_Held_record(frame->held),
- has_tuple_ids, tuple_ids, c->tuple, &has_key, &frame->key, &keyless, frame->error))
+ if (!doltgres_scan_keyed(frame->io, TREES_STORE_READER_NODE_STORE(&store), frame->held, c->tuple, &has_key,
+ &frame->key, &keyless, frame->error))
return;
*frame->some = false;
if (!has_key)
@@ -1214,7 +1211,7 @@ doltgres_entries_doltgres_relation_nontransactional_truncate(const PgCatalogCaps
static void
doltgres_entries_Carry_drop(void *carry)
{
- txn_record_RowChange_drop(&((DoltgresRewriteCarry *) carry)->f0);
+ txn_record_RowChange_drop(&((TxnRewriteCarry *) carry)->f0);
}
/* doltgres_entries_Copied_drop is Copied's drop glue, an element of copied's answer. */
@@ -1380,7 +1377,7 @@ doltgres_entries_doltgres_relation_copy_for_cluster_closure_5_body(void *frame_)
{
doltgres_entries_doltgres_relation_copy_for_cluster_closure_5_frame_t *frame = frame_;
doltgres_entries_doltgres_relation_copy_for_cluster_captures_t *c = frame->c;
- const DoltgresRewriteCarry *carried = c->carried->ptr;
+ const TxnRewriteCarry *carried = c->carried->ptr;
TxnInsertPage page = doltgres_entries_page();
bool some;
bool first = true;
@@ -1433,10 +1430,9 @@ doltgres_entries_doltgres_relation_copy_for_cluster_closure_5_body(void *frame_)
return;
}
store = repo_Repo_reader(frame->repo, HOST_CACHE_PLACEMENT_SERVER, c->reading);
- for (size_t i = 0; i < c->carried->len; i++)
- if (!txn_rewrite_carried(frame->io, TREES_STORE_READER_NODE_STORE(&store), frame->held, &carried[i].f0,
- carried[i].f1, carried[i].f2, c->logged, &page, frame->error))
- return;
+ if (!txn_rewrite_carried(frame->io, TREES_STORE_READER_NODE_STORE(&store), frame->held, NULL, 0, carried,
+ c->carried->len, c->logged, &page, pg_calls_max_heap_tuples_per_page(), frame->error))
+ return;
*frame->result = true;
frame->ok = true;
}
@@ -1524,7 +1520,7 @@ doltgres_entries_doltgres_relation_copy_for_cluster_pad(void *frame_)
std_vec_drop_Checked(&frame->copied, sizeof(TxnRewriteCopied), _Alignof(TxnRewriteCopied),
doltgres_entries_Copied_drop);
if (frame->carried_live)
- std_vec_drop_Checked(&frame->carried, sizeof(DoltgresRewriteCarry), _Alignof(DoltgresRewriteCarry),
+ std_vec_drop_Checked(&frame->carried, sizeof(TxnRewriteCarry), _Alignof(TxnRewriteCarry),
doltgres_entries_Carry_drop);
if (frame->toast_live)
host_shared_Held_drop(&frame->values);
@@ -1693,7 +1689,7 @@ doltgres_entries_doltgres_relation_copy_for_cluster_body(void *frame_)
return;
for (size_t i = 0; i < frame->carried.len; i++)
{
- const TxnMemtableVersionWriter *writer = &STD_VEC_AT(&frame->carried, DoltgresRewriteCarry, i).f0.writer;
+ const TxnMemtableVersionWriter *writer = &STD_VEC_AT(&frame->carried, TxnRewriteCarry, i).f0.writer;
uint64 xid = writer->has_top ? writer->top : writer->xid;
if (xid < TXN_STATUS_FIRST_NORMAL || (has_oldest && xid >= oldest))
@@ -1830,7 +1826,7 @@ typedef struct doltgres_entries_moved_into_captures_t
bool logged;
const TxnRecordMapCarried *spaces;
size_t spaces_len;
- const DoltgresRewriteCarry *carried;
+ const TxnRewriteCarry *carried;
size_t carried_len;
} doltgres_entries_moved_into_captures_t;
@@ -1869,14 +1865,38 @@ doltgres_entries_moved_into_closure_0_body(void *frame_)
doltgres_entries_moved_into_closure_0_frame_t *frame = frame_;
doltgres_entries_moved_into_captures_t *c = frame->c;
HostLsn point = c->newest->folded;
+ HostLsn read;
bool copied;
TreesStoreReader store;
+ StdVec writers;
+ bool ok;
std_vec_new(&frame->copies.copies, sizeof(txn_TupleLocatorLocator_t), _Alignof(txn_TupleLocatorLocator_t));
frame->copies_live = true;
*c->newest_live = false;
- if (!txn_fold_copied(frame->io, frame->repo, c->reading, frame->held, c->newest, point, c->has_nodes, c->nodes,
- &frame->copies, &copied, frame->error))
+ std_vec_new(&writers, sizeof(uint64), _Alignof(uint64));
+ for (size_t i = 0; i < c->spaces_len; i++)
+ for (size_t v = 0; v < c->spaces[i].versions.len; v++)
+ {
+ const TxnMemtableVersionWriter *writer = &STD_VEC_AT(&c->spaces[i].versions, TxnRecordCarriedTake,
+ v).writer;
+ uint64 top = writer->has_top ? writer->top : writer->xid;
+
+ std_vec_push_Checked(&writers, &top, sizeof(top), _Alignof(uint64));
+ }
+ for (size_t i = 0; i < c->carried_len; i++)
+ {
+ const TxnMemtableVersionWriter *writer = &c->carried[i].f0.writer;
+ uint64 top = writer->has_top ? writer->top : writer->xid;
+
+ std_vec_push_Checked(&writers, &top, sizeof(top), _Alignof(uint64));
+ }
+ ok = txn_rewrite_moved_point(frame->io, c->newest->read, writers.ptr, writers.len, &read, frame->error);
+ std_vec_free_Checked(&writers, sizeof(uint64), _Alignof(uint64));
+ if (!ok)
+ return;
+ if (!txn_fold_copied(frame->io, frame->repo, c->reading, frame->held, c->newest, point, read, c->has_nodes,
+ c->nodes, &frame->copies, &copied, frame->error))
return;
if (!copied)
{
@@ -1884,15 +1904,11 @@ doltgres_entries_moved_into_closure_0_body(void *frame_)
frame->ok = true;
return;
}
- for (size_t i = 0; i < c->spaces_len; i++)
- if (!txn_rewrite_spaced(frame->io, frame->held, &c->spaces[i], c->logged, pg_calls_max_heap_tuples_per_page(),
- false, 0, frame->error))
- return;
store = repo_Repo_reader(frame->repo, HOST_CACHE_PLACEMENT_SERVER, c->reading);
- for (size_t i = 0; i < c->carried_len; i++)
- if (!txn_rewrite_carried(frame->io, TREES_STORE_READER_NODE_STORE(&store), frame->held, &c->carried[i].f0,
- c->carried[i].f1, c->carried[i].f2, c->logged, NULL, frame->error))
- return;
+ if (!txn_rewrite_carried(frame->io, TREES_STORE_READER_NODE_STORE(&store), frame->held, c->spaces, c->spaces_len,
+ c->carried, c->carried_len, c->logged, NULL, pg_calls_max_heap_tuples_per_page(),
+ frame->error))
+ return;
*frame->result = true;
frame->ok = true;
}
@@ -1997,7 +2013,7 @@ doltgres_entries_moved_into_body(void *frame_)
static bool
doltgres_entries_moved_into(const PgMayRaise *raise, HostIoIo *io, const RepoRepo *repo,
const HostSharedReader *reading, const PgRel *rel, HostStorage moved, TxnBaseBase *newest,
- const TxnRecordMapCarried *spaces, size_t spaces_len, const DoltgresRewriteCarry *carried,
+ const TxnRecordMapCarried *spaces, size_t spaces_len, const TxnRewriteCarry *carried,
size_t carried_len, PgPgError *error)
{
doltgres_entries_moved_into_frame_t frame = {.newest = *newest, .newest_live = true,
@@ -2066,7 +2082,7 @@ doltgres_entries_doltgres_relation_copy_data_closure_0_pad(void *frame_)
doltgres_entries_doltgres_relation_copy_data_closure_0_frame_t *frame = frame_;
if (frame->carried_live)
- std_vec_drop_Checked(frame->carried, sizeof(DoltgresRewriteCarry), _Alignof(DoltgresRewriteCarry),
+ std_vec_drop_Checked(frame->carried, sizeof(TxnRewriteCarry), _Alignof(TxnRewriteCarry),
doltgres_entries_Carry_drop);
if (frame->base_live)
txn_base_Base_drop(frame->base);
@@ -2102,7 +2118,7 @@ doltgres_entries_doltgres_relation_copy_data_closure_0_body(void *frame_)
if (!doltgres_entries_moved_base(frame->io, frame->held, frame->base, frame->error))
return;
frame->base_live = true;
- if (!doltgres_rewrite_moving(frame->io, frame->held, frame->base->folded, c->moved, c->table, false, 0,
+ if (!doltgres_rewrite_moving(frame->io, frame->held, frame->base->read, c->moved, c->table, false, 0,
frame->carried, frame->error))
return;
frame->carried_live = true;
@@ -2175,7 +2191,7 @@ doltgres_entries_doltgres_relation_copy_data_pad(void *frame_)
std_vec_drop_Checked(&frame->spaces, sizeof(TxnRecordMapCarried), _Alignof(TxnRecordMapCarried),
doltgres_entries_MapCarried_drop);
if (frame->carried_live)
- std_vec_drop_Checked(&frame->carried, sizeof(DoltgresRewriteCarry), _Alignof(DoltgresRewriteCarry),
+ std_vec_drop_Checked(&frame->carried, sizeof(TxnRewriteCarry), _Alignof(TxnRewriteCarry),
doltgres_entries_Carry_drop);
if (frame->base_live)
txn_base_Base_drop(&frame->base);
@@ -2208,7 +2224,7 @@ doltgres_entries_doltgres_relation_copy_data_named(doltgres_entries_doltgres_rel
&frame->opened.reading);
for (size_t i = 0; i < frame->carried.len; i++)
{
- TxnRecordRowChange *change = &STD_VEC_AT(&frame->carried, DoltgresRewriteCarry, i).f0;
+ TxnRecordRowChange *change = &STD_VEC_AT(&frame->carried, TxnRewriteCarry, i).f0;
const TxnMemtablePut *put;
const uint32 *ids = NULL;
size_t ids_len = 0;
@@ -2326,7 +2342,7 @@ doltgres_entries_doltgres_relation_copy_data_body(void *frame_)
{
const TxnMemtablePut *put;
- valued = txn_record_RowChange_put(&STD_VEC_AT(&frame->carried, DoltgresRewriteCarry, i).f0, &put) &&
+ valued = txn_record_RowChange_put(&STD_VEC_AT(&frame->carried, TxnRewriteCarry, i).f0, &put) &&
put->values.len > 0;
}
if (valued && !doltgres_entries_doltgres_relation_copy_data_named(frame))
@@ -2429,7 +2445,7 @@ doltgres_entries_doltgres_index_copy_data_closure_0_body(void *frame_)
if (!doltgres_entries_moved_base(frame->io, frame->held, frame->base, frame->error))
return;
frame->base_live = true;
- if (!doltgres_rewrite_moving(frame->io, frame->held, frame->base->folded, c->moved, c->table, true, c->index,
+ if (!doltgres_rewrite_moving(frame->io, frame->held, frame->base->read, c->moved, c->table, true, c->index,
frame->carried, frame->error))
return;
frame->base_live = false;
@@ -2480,7 +2496,7 @@ doltgres_entries_doltgres_index_copy_data_pad(void *frame_)
doltgres_entries_doltgres_index_copy_data_frame_t *frame = frame_;
if (frame->carried_live)
- std_vec_drop_Checked(&frame->carried, sizeof(DoltgresRewriteCarry), _Alignof(DoltgresRewriteCarry),
+ std_vec_drop_Checked(&frame->carried, sizeof(TxnRewriteCarry), _Alignof(TxnRewriteCarry),
doltgres_entries_Carry_drop);
frame->carried_live = false;
doltgres_entries_opened_drop(&frame->opened);
@@ -2888,7 +2904,8 @@ doltgres_entries_doltgres_relation_size_body(void *frame_)
/*
* doltgres_entries_doltgres_relation_size is the fork's relation_size: the
* main fork's block count times the block size, from the storage's tree's
- * totals and its tuple id counter (PLAN item 5's sizes for costs), and 0 for
+ * totals and its tuple id counter (PLAN item 5's sizes for costs), the free
+ * space map fork's own pages, which Postgres keeps (bugs row 87), and 0 for
* every other fork, which the fork's relations don't have.
*/
bool
@@ -2898,6 +2915,15 @@ doltgres_entries_doltgres_relation_size(const PgCatalogCaps *caps, const PgRel *
doltgres_entries_doltgres_relation_size_frame_t frame = {.caps = caps, .rel = rel, .result = result,
.error = error};
+ if (forkNumber == FSM_FORKNUM)
+ {
+ uint32 blocks;
+
+ if (!pg_calls_fork_blocks(pg_CatalogCaps_may_raise(caps), rel, forkNumber, &blocks, error))
+ return false;
+ *result = (uint64) blocks * BLCKSZ;
+ return true;
+ }
if (forkNumber != MAIN_FORKNUM)
{
*result = 0;
@@ -3038,7 +3064,8 @@ doltgres_entries_size_added(int64 size, uint64 added)
* nothing, in pg_relation_size for the fork it names and in the sums over a
* table's relations for every fork: a table's block count and an index's,
* times BLCKSZ, and nothing for a relation the fork doesn't store or another
- * fork, which has no file.
+ * fork, which has no file but the free space map's, whose pages Postgres
+ * summed.
*/
bool
doltgres_entries_doltgres_relation_size_added(const PgCatalogCaps *caps, const PgRel *rel, const PgText *forkName,
@@ -3065,13 +3092,393 @@ doltgres_entries_doltgres_relation_size_added(const PgCatalogCaps *caps, const P
if (!doltgres_relation_stored(caps, rel, &stored, error))
return false;
- if (stored && !doltgres_entries_doltgres_relation_size(caps, rel, fork_number, &added, error))
+ if (stored && fork_number == MAIN_FORKNUM &&
+ !doltgres_entries_doltgres_relation_size(caps, rel, fork_number, &added, error))
return false;
}
*result = doltgres_entries_size_added(size, added);
return true;
}
+/*
+ * doltgres_entries_doltgres_visibility_counted counts every block of a table
+ * the fork stores all-visible and all-frozen, in visibilitymap_count, since
+ * the fork keeps no visibility map and every block is both (design items 5
+ * and 16): its block count, as RelationGetNumberOfBlocks gives it, the
+ * all-frozen count only where its caller asks for one; it answers nothing
+ * for a relation the fork doesn't store.
+ */
+bool
+doltgres_entries_doltgres_visibility_counted(const PgCatalogCaps *caps, const PgRel *rel, uint32 *all_visible,
+ uint32 *all_frozen, bool *result, PgPgError *error)
+{
+ uint64 bytes;
+
+ if (!doltgres_relation_stored(caps, rel, result, error))
+ return false;
+ if (!*result)
+ return true;
+ if (!doltgres_entries_doltgres_relation_size(caps, rel, MAIN_FORKNUM, &bytes, error))
+ return false;
+ *all_visible = (uint32) ((bytes + (BLCKSZ - 1)) / BLCKSZ);
+ if (all_frozen != NULL)
+ *all_frozen = *all_visible;
+ return true;
+}
+
+/*
+ * doltgres_entries_doltgres_heap_only_refused answers whether the fork
+ * stores a relation, a table by access's rule and an index by whether its
+ * entries are the fork's, for the heap-only contrib modules' checks (PLAN
+ * item 5), which refuse it as they refuse a relation of another access
+ * method.
+ */
+bool
+doltgres_entries_doltgres_heap_only_refused(const PgCatalogCaps *caps, const PgRel *rel, bool *result,
+ PgPgError *error)
+{
+ if (pg_calls_rel_kind(rel) == RELKIND_INDEX)
+ return doltgres_index_ported(pg_CatalogCaps_may_raise(caps), pg_CatalogCaps_catalog(caps), rel, result,
+ error);
+ return doltgres_relation_stored(caps, rel, result, error);
+}
+
+/*
+ * doltgres_entries_doltgres_visibility_kept answers whether the fork stores
+ * a relation, for pg_visibility, which then reads none of its pages and
+ * answers each of its blocks all-visible and all-frozen, since the fork keeps
+ * no visibility map (design item 5): none of the fork's reads consults a
+ * block's bits, and none of its versions ever needs a freeze. Its
+ * pg_check_visible and pg_check_frozen find nothing because it has no map
+ * fork to name a block to check.
+ */
+bool
+doltgres_entries_doltgres_visibility_kept(const PgCatalogCaps *caps, const PgRel *rel, bool *result, PgPgError *error)
+{
+ return doltgres_relation_stored(caps, rel, result, error);
+}
+
+/*
+ * doltgres_entries_doltgres_visibility_bits gives pg_visibility a block's
+ * visibility map bits: for a table the fork stores, all-visible and
+ * all-frozen for a block within its block count, as RelationGetNumberOfBlocks
+ * gives it, and neither past it; for any other relation, the map's own bits.
+ */
+bool
+doltgres_entries_doltgres_visibility_bits(const PgCatalogCaps *caps, const PgRel *rel, uint32 blkno, int32 mapbits,
+ int32 *result, PgPgError *error)
+{
+ bool stored;
+ uint64 bytes;
+
+ if (!doltgres_relation_stored(caps, rel, &stored, error))
+ return false;
+ if (!stored)
+ {
+ *result = mapbits;
+ return true;
+ }
+ if (!doltgres_entries_doltgres_relation_size(caps, rel, MAIN_FORKNUM, &bytes, error))
+ return false;
+ *result = blkno < (bytes + (BLCKSZ - 1)) / BLCKSZ ? VISIBILITYMAP_VALID_BITS : 0;
+ return true;
+}
+
+/*
+ * doltgres_entries_scan_frame_t is a scan cost's state across its pad: what
+ * it opened, the index and its table, and the table's recorded base, each
+ * while live, and the table's shape once read.
+ */
+typedef struct doltgres_entries_scan_frame_t
+{
+ doltgres_entries_opened_t opened;
+ PgRefRel index;
+ bool index_live;
+ PgRefRel table;
+ bool table_live;
+ TxnBaseBase recorded;
+ bool recorded_live;
+ const PgCatalogCaps *caps;
+ HostIoIo io;
+
+ /*
+ * indexoid is the index a scan reads, or 0 for a bitmap scan's table
+ * alone.
+ */
+ uint32 indexoid;
+ uint32 tableoid;
+ /* stored is whether the fork stores the table, and so answers its costs. */
+ bool stored;
+
+ /*
+ * keyed is whether the index is the table's primary key's, whose leaves
+ * are the table's rows.
+ */
+ bool keyed;
+
+ /*
+ * rows and tuple_ids are the blocks of the table's tree and of its tuple
+ * id tree.
+ */
+ TxnBaseBlockCounts rows;
+ TxnBaseBlockCounts tuple_ids;
+ PgPgError *error;
+ bool ok;
+} doltgres_entries_scan_frame_t;
+
+/* doltgres_entries_scan_pad frees what a scan cost holds. */
+static void
+doltgres_entries_scan_pad(void *frame_)
+{
+ doltgres_entries_scan_frame_t *frame = frame_;
+
+ if (frame->recorded_live)
+ txn_base_Base_drop(&frame->recorded);
+ if (frame->table_live)
+ pg_RefRel_drop(&frame->table);
+ if (frame->index_live)
+ pg_RefRel_drop(&frame->index);
+ frame->recorded_live = frame->table_live = frame->index_live = false;
+ doltgres_entries_opened_drop(&frame->opened);
+}
+
+/* doltgres_entries_scan_opened opens a relation the planner holds a lock on, as a fork error when it is gone. */
+static bool
+doltgres_entries_scan_opened(doltgres_entries_scan_frame_t *frame, uint32 relid, PgRefRel *result, bool *live)
+{
+ bool has;
+
+ if (!pg_RefRel_opened(pg_CatalogCaps_may_raise(frame->caps), pg_CatalogCaps_catalog(frame->caps), relid, &has,
+ result, frame->error))
+ return false;
+ if (!has)
+ {
+ *frame->error = doltgres_entries_pg_fork("XX000", "a relation the planner costs is gone");
+ return false;
+ }
+ *live = true;
+ return true;
+}
+
+/*
+ * doltgres_entries_scan_bytes returns entries times a size each plus more
+ * bytes, saturating: a tuple id tree's bytes, whose entries are a tuple id's
+ * six bytes keying the row's key and its four bytes of heap length.
+ */
+static uint64
+doltgres_entries_scan_bytes(uint64 entries, uint64 each, uint64 more)
+{
+ uint64 product;
+ uint64 sum;
+
+ if (pg_mul_u64_overflow(entries, each, &product) || pg_add_u64_overflow(product, more, &sum))
+ return PG_UINT64_MAX;
+ return sum;
+}
+
+/*
+ * doltgres_entries_scan_body reads a scanned table's shape: whether the fork
+ * stores it, the blocks of its tree and of its tuple id tree from its
+ * storage's totals, and whether the index is its primary key's.
+ */
+static void
+doltgres_entries_scan_body(void *frame_)
+{
+ doltgres_entries_scan_frame_t *frame = frame_;
+ const PgMayRaise *raise = pg_CatalogCaps_may_raise(frame->caps);
+ const PgRel *table;
+ const RepoRepo *repo;
+ TxnStateFound found;
+ TxnBaseSizes totals;
+ TxnBaseSizes tuple_ids = {0};
+ HostError failed;
+
+ if (frame->indexoid != 0)
+ {
+ if (!doltgres_entries_scan_opened(frame, frame->indexoid, &frame->index, &frame->index_live))
+ return;
+ frame->tableoid = pg_calls_index_table(pg_RefRel_rel(&frame->index));
+ }
+ if (!doltgres_entries_scan_opened(frame, frame->tableoid, &frame->table, &frame->table_live))
+ return;
+ table = pg_RefRel_rel(&frame->table);
+ if (!doltgres_relation_stored(frame->caps, table, &frame->stored, frame->error))
+ return;
+ if (!frame->stored)
+ {
+ frame->ok = true;
+ return;
+ }
+ frame->io = pghost_io_catalog(frame->caps);
+ if (!doltgres_entries_open(&frame->io, &frame->opened, frame->error))
+ return;
+ repo = doltgres_entries_opened_repo(&frame->opened);
+ if (!doltgres_entries_counted(frame->caps, &frame->io, repo, &frame->opened.reading, table, table, false,
+ frame->error) ||
+ !doltgres_entries_making(frame->caps, &frame->io, table, &found, frame->error))
+ return;
+ if (!txn_state_held_Sizes(&frame->io, repo, &frame->opened.reading, found,
+ doltgres_entries_doltgres_index_blocks_closure_1, NULL, NULL, &totals, &failed))
+ {
+ *frame->error = pghost_raised(&failed);
+ return;
+ }
+ frame->rows = txn_base_Sizes_counts(&totals, TREES_CHUNKER_DEFAULT_TARGET);
+ tuple_ids.entries = totals.entries;
+ tuple_ids.key_bytes = doltgres_entries_scan_bytes(totals.entries, 6, 0);
+ tuple_ids.leaf_bytes = doltgres_entries_scan_bytes(totals.entries, 10, totals.key_bytes);
+ frame->tuple_ids = txn_base_Sizes_counts(&tuple_ids, TREES_CHUNKER_DEFAULT_TARGET);
+ if (frame->indexoid != 0)
+ {
+ if (!doltgres_entries_recorded(frame->caps, &frame->io, repo, &frame->opened.reading, table,
+ &frame->recorded, frame->error))
+ return;
+ frame->recorded_live = true;
+ if (!doltgres_index_keyed(raise, pg_CatalogCaps_catalog(frame->caps), pg_RefRel_rel(&frame->index),
+ &frame->recorded.key, &frame->keyed, frame->error))
+ return;
+ }
+ frame->ok = true;
+}
+
+/*
+ * doltgres_entries_scanned reads a scanned table's shape for its scans'
+ * costs, by the index it is scanned through, or by the table itself when the
+ * index is 0.
+ */
+bool
+doltgres_entries_scanned(const PgCatalogCaps *caps, uint32 indexoid, uint32 tableoid, DoltgresEntriesScanned *result,
+ PgPgError *error)
+{
+ doltgres_entries_scan_frame_t frame = {.caps = caps, .indexoid = indexoid, .tableoid = tableoid, .error = error};
+
+ std_landing(&frame, doltgres_entries_scan_body, doltgres_entries_scan_pad);
+ doltgres_entries_scan_pad(&frame);
+ if (!frame.ok)
+ return false;
+ result->stored = frame.stored;
+ result->keyed = frame.keyed;
+ result->rows = frame.rows;
+ result->tuple_ids = frame.tuple_ids;
+ return true;
+}
+
+/* doltgres_entries_cached_leaves returns the node cache's size in leaves of the default block size. */
+static float8
+doltgres_entries_cached_leaves(void)
+{
+ return (float8) pg_calls_settings_node_cache_size() * 1024.0 / BLCKSZ;
+}
+
+/*
+ * doltgres_entries_leaves_read is Mackert and Lohman's count of a tree's
+ * distinct leaves that fetches of its rows read past the node cache, as
+ * index_pages_fetched counts heap pages with the node cache in
+ * effective_cache_size's place, and none when the tree's leaves fit the
+ * cache, which then holds them.
+ */
+static float8
+doltgres_entries_leaves_read(float8 fetches, uint64 tree_leaves, float8 cached)
+{
+ float8 leaves = Max((float8) tree_leaves, 1.0);
+ float8 limit;
+
+ if (leaves <= cached)
+ return 0;
+ limit = 2.0 * leaves * cached / (2.0 * leaves - cached);
+ if (fetches <= limit)
+ return ceil(2.0 * leaves * fetches / (2.0 * leaves + fetches));
+ return ceil(cached + (fetches - limit) * (leaves - cached) / leaves);
+}
+
+/*
+ * doltgres_entries_doltgres_index_scan_io is a fork table's part of an index
+ * scan's costs (PLAN item 16): an index-only scan reads no row of the table;
+ * the primary key's own index reads its leaves, the table's rows, in key
+ * order, the first at random and the rest in sequence, when they don't fit
+ * the node cache; and a secondary index's entry leads to a descent of the
+ * table's tree by its row key, a keyless table's by the row's hash, its
+ * levels above the leaves held by the node cache at a comparison each and
+ * its leaf read at random when the leaves don't fit the cache. It leaves
+ * heap's costs for a table the fork doesn't store.
+ */
+bool
+doltgres_entries_doltgres_index_scan_io(const PgCatalogCaps *caps, uint32 indexoid, bool indexonly,
+ float8 tuples_fetched, float8 selectivity, float8 loop_count,
+ float8 random_page_cost, float8 seq_page_cost, float8 cpu_operator_cost,
+ float8 *max_io, float8 *min_io, float8 *rand_heap_pages, PgPgError *error)
+{
+ DoltgresEntriesScanned frame;
+ float8 cached = doltgres_entries_cached_leaves();
+ float8 read;
+ float8 io;
+
+ if (!doltgres_entries_scanned(caps, indexoid, 0, &frame, error))
+ return false;
+ if (!frame.stored)
+ return true;
+ if (indexonly)
+ read = io = 0;
+ else if (frame.keyed)
+ {
+ read = ceil(selectivity * Max((float8) frame.rows.leaves, 1.0));
+ if ((float8) frame.rows.leaves <= cached)
+ read = io = 0;
+ else if (loop_count > 1)
+ {
+ read = doltgres_entries_leaves_read(read * loop_count, frame.rows.leaves, cached);
+ io = read * random_page_cost / loop_count;
+ }
+ else
+ io = read > 0 ? random_page_cost + (read - 1) * seq_page_cost : 0;
+ }
+ else
+ {
+ read = doltgres_entries_leaves_read(tuples_fetched * loop_count, frame.rows.leaves, cached);
+ io = tuples_fetched * frame.rows.height * cpu_operator_cost + read * random_page_cost / loop_count;
+ }
+ *max_io = *min_io = io;
+ *rand_heap_pages = read;
+ return true;
+}
+
+/*
+ * doltgres_entries_doltgres_bitmap_page_cost is a fork table's cost per page
+ * fetched of a bitmap heap scan (PLAN item 16): each row costs its tuple id's
+ * resolution, a descent of the tuple id tree, and a descent of the table's
+ * tree by its row key, each level above the leaves a comparison and each
+ * leaf past the node cache read at random, and each lossy block, split from
+ * the pages it fetches by the bitmap's entries as compute_bitmap_pages splits
+ * them, costs a range scan of the tuple id tree: a descent and its leaf. It
+ * leaves heap's cost for a table the fork doesn't store.
+ */
+bool
+doltgres_entries_doltgres_bitmap_page_cost(const PgCatalogCaps *caps, uint32 relid, float8 tuples_fetched,
+ float8 pages_fetched, uint32 pages, float8 maxentries,
+ float8 random_page_cost, float8 cpu_operator_cost, float8 *cost_per_page,
+ PgPgError *error)
+{
+ DoltgresEntriesScanned frame;
+ float8 cached = doltgres_entries_cached_leaves();
+ float8 heap_pages = Min(pages_fetched, (float8) pages);
+ float8 lossy = 0;
+ float8 io;
+
+ if (!doltgres_entries_scanned(caps, 0, relid, &frame, error))
+ return false;
+ if (!frame.stored)
+ return true;
+ if (maxentries < heap_pages)
+ lossy = Max(0, heap_pages - maxentries / 2);
+ io = tuples_fetched * (frame.tuple_ids.height + frame.rows.height) * cpu_operator_cost +
+ (doltgres_entries_leaves_read(tuples_fetched, frame.tuple_ids.leaves, cached) +
+ doltgres_entries_leaves_read(tuples_fetched, frame.rows.leaves, cached)) * random_page_cost +
+ lossy * (frame.tuple_ids.height * cpu_operator_cost +
+ ((float8) frame.tuple_ids.leaves > cached ? random_page_cost : 0));
+ *cost_per_page = pages_fetched > 0 ? io / pages_fetched : 0;
+ return true;
+}
+
/*
* doltgres_entries_doltgres_database_size_frame_t is database_size's state
* across its pad: a directory and the tablespaces, each while live.
@@ -3179,6 +3586,9 @@ typedef struct doltgres_entries_doltgres_tablespace_size_frame_t
bool ok;
} doltgres_entries_doltgres_tablespace_size_frame_t;
+/* DOLTGRES_ENTRIES_EMPTIED_NAP is the milliseconds DROP TABLESPACE sleeps between its asks to empty a tablespace. */
+#define DOLTGRES_ENTRIES_EMPTIED_NAP 1000
+
/* doltgres_entries_doltgres_tablespace_size_pad frees what tablespace_size holds. */
static void
doltgres_entries_doltgres_tablespace_size_pad(void *frame_)
@@ -3227,6 +3637,249 @@ doltgres_entries_doltgres_tablespace_size(const PgCaps *caps, uint32 tblspcOid,
return frame.ok;
}
+/*
+ * doltgres_entries_doltgres_tablespace_emptied_frame_t is tablespace_emptied's
+ * state across its pad: the tablespace's directory of repositories, a path,
+ * and the names listed in each, while live.
+ */
+typedef struct doltgres_entries_doltgres_tablespace_emptied_frame_t
+{
+ StdPathBuf versioned;
+ bool versioned_live;
+ StdPathBuf path;
+ bool path_live;
+ StdVec names;
+ bool names_live;
+ StdVec inner;
+ bool inner_live;
+ const PgCaps *caps;
+ HostIoIo io;
+ uint32 tablespace;
+ bool redo;
+ PgPgError *error;
+ bool ok;
+} doltgres_entries_doltgres_tablespace_emptied_frame_t;
+
+/* doltgres_entries_doltgres_tablespace_emptied_pad frees what tablespace_emptied holds. */
+static void
+doltgres_entries_doltgres_tablespace_emptied_pad(void *frame_)
+{
+ doltgres_entries_doltgres_tablespace_emptied_frame_t *frame = frame_;
+
+ if (frame->inner_live)
+ std_vec_drop_Checked(&frame->inner, sizeof(StdString), _Alignof(StdString), std_String_drop);
+ if (frame->names_live)
+ std_vec_drop_Checked(&frame->names, sizeof(StdString), _Alignof(StdString), std_String_drop);
+ if (frame->path_live)
+ std_vec_free_Checked(&frame->path.bytes, 1, 1);
+ if (frame->versioned_live)
+ std_vec_free_Checked(&frame->versioned.bytes, 1, 1);
+ frame->inner_live = frame->names_live = frame->path_live = frame->versioned_live = false;
+}
+
+/*
+ * doltgres_entries_tablespace_held reports whether a directory of the
+ * tablespace's version directory other than the fork's holds a file, as
+ * Postgres' removal of its directories finds a relation's file there.
+ */
+static bool
+doltgres_entries_tablespace_held(doltgres_entries_doltgres_tablespace_emptied_frame_t *frame, bool *result,
+ HostError *failed)
+{
+ StdStr version = host_directory_of((const char *) frame->versioned.bytes.ptr, frame->versioned.bytes.len);
+ bool has;
+
+ *result = false;
+ if (!host_list_dir_if_there(HOST_IO_IO_READ_DOORS(&frame->io), version.ptr, version.len, &has, &frame->names,
+ failed))
+ return false;
+ if (!has)
+ return true;
+ frame->names_live = true;
+ for (size_t k = 0, k_len = frame->names.len; k < k_len && !*result; k++)
+ {
+ const StdString *name = &STD_VEC_AT(&frame->names, StdString, k);
+ bool listed;
+
+ if (name->len == strlen(HOST_VERSIONED) && memcmp(name->ptr, HOST_VERSIONED, name->len) == 0)
+ continue;
+ std_path_join(version.ptr, version.len, (const char *) name->ptr, name->len, &frame->path);
+ frame->path_live = true;
+ if (!host_list_dir_if_there(HOST_IO_IO_READ_DOORS(&frame->io), (const char *) frame->path.bytes.ptr,
+ frame->path.bytes.len, &listed, &frame->inner, failed))
+ return false;
+ if (listed)
+ {
+ frame->inner_live = true;
+ *result = frame->inner.len != 0;
+ frame->inner_live = false;
+ std_vec_drop_Checked(&frame->inner, sizeof(StdString), _Alignof(StdString), std_String_drop);
+ }
+ frame->path_live = false;
+ std_vec_free_Checked(&frame->path.bytes, 1, 1);
+ }
+ frame->names_live = false;
+ std_vec_drop_Checked(&frame->names, sizeof(StdString), _Alignof(StdString), std_String_drop);
+ return true;
+}
+
+/*
+ * doltgres_entries_tablespace_asked answers whether a repository's directory
+ * is in the tablespace's directory of repositories, asking the collector
+ * worker first to empty the tablespace out of each such repository when
+ * asking, and removes the directory of repositories once none is.
+ */
+static bool
+doltgres_entries_tablespace_asked(doltgres_entries_doltgres_tablespace_emptied_frame_t *frame, bool asking,
+ bool *result, HostError *failed)
+{
+ bool has;
+ bool removed;
+
+ *result = false;
+ if (!host_list_dir_if_there(HOST_IO_IO_READ_DOORS(&frame->io), (const char *) frame->versioned.bytes.ptr,
+ frame->versioned.bytes.len, &has, &frame->names, failed))
+ return false;
+ if (!has)
+ return true;
+ frame->names_live = true;
+ for (size_t k = 0, k_len = frame->names.len; k < k_len; k++)
+ {
+ const StdString *name = &STD_VEC_AT(&frame->names, StdString, k);
+ uint32 repository;
+ uint64 counts[3];
+ StdParseIntError parsed;
+
+ if (!std_str_parse_u32(name->ptr, name->len, &repository, &parsed) || repository == 0)
+ continue;
+ *result = true;
+ if (!asking)
+ continue;
+ if (!host_collection_ask(&frame->io, repository, frame->tablespace, counts, failed))
+ return false;
+ if (counts[0] == PG_UINT64_MAX)
+ {
+ StdFmtPiece pieces[] = {STD_FMT_LIT("the collector's collection of database "), STD_FMT_ARG(0),
+ STD_FMT_LIT(" failed")};
+ StdFmtArg args[] = {{&repository, std_u32_Display_fmt}};
+ StdFmtArguments arguments = {pieces, lengthof(pieces), args};
+ StdString message;
+
+ std_fmt_format_Checked(&message, &arguments);
+ *failed = host_Error_fork((const uint8 *) "XX000", &message);
+ return false;
+ }
+ }
+ frame->names_live = false;
+ std_vec_drop_Checked(&frame->names, sizeof(StdString), _Alignof(StdString), std_String_drop);
+ if (*result)
+ return true;
+ return host_remove_tree(HOST_IO_IO_IO_DOORS(&frame->io), (const char *) frame->versioned.bytes.ptr,
+ frame->versioned.bytes.len, &removed, failed);
+}
+
+/*
+ * doltgres_entries_tablespace_marked leaves the dropped mark, durably, in the
+ * tablespace's directory of repositories when redo's attempt left it there.
+ */
+static bool
+doltgres_entries_tablespace_marked(doltgres_entries_doltgres_tablespace_emptied_frame_t *frame, HostError *failed)
+{
+ bool there;
+ bool made;
+ HostFile mark;
+
+ if (!host_list_dir_if_there(HOST_IO_IO_READ_DOORS(&frame->io), (const char *) frame->versioned.bytes.ptr,
+ frame->versioned.bytes.len, &there, &frame->names, failed))
+ return false;
+ if (!there)
+ return true;
+ std_vec_drop_Checked(&frame->names, sizeof(StdString), _Alignof(StdString), std_String_drop);
+ std_path_join((const char *) frame->versioned.bytes.ptr, frame->versioned.bytes.len, DOLTGRES_DROPPED_MARK,
+ strlen(DOLTGRES_DROPPED_MARK), &frame->path);
+ frame->path_live = true;
+ if (!host_create_if_absent(HOST_IO_IO_IO_DOORS(&frame->io), (const char *) frame->path.bytes.ptr,
+ frame->path.bytes.len, &made, &mark, failed))
+ return false;
+ if (made)
+ host_File_drop(&mark);
+ return host_sync_path(HOST_IO_IO_IO_DOORS(&frame->io), (const char *) frame->versioned.bytes.ptr,
+ frame->versioned.bytes.len, true, failed);
+}
+
+/*
+ * doltgres_entries_doltgres_tablespace_emptied_body empties the fork's packs
+ * out of the tablespace, round by round until its directory of repositories
+ * is gone or a relation's file appears in Postgres' directories there, for
+ * Postgres to refuse the DROP, or once in redo, which then leaves the dropped
+ * mark on what is left for the collector's sweep.
+ */
+static void
+doltgres_entries_doltgres_tablespace_emptied_body(void *frame_)
+{
+ doltgres_entries_doltgres_tablespace_emptied_frame_t *frame = frame_;
+ const PgMayRaise *raise = pg_Caps_may_raise(frame->caps);
+ HostError failed;
+ bool held;
+ bool left;
+
+ frame->versioned = host_versioned_dir(frame->tablespace);
+ frame->versioned_live = true;
+ if (!doltgres_entries_tablespace_held(frame, &held, &failed))
+ goto raised;
+ if (held && !frame->redo)
+ {
+ if (!pg_calls_checkpoint_now(raise, frame->error))
+ return;
+ if (!doltgres_entries_tablespace_held(frame, &held, &failed))
+ goto raised;
+ }
+ for (bool asking = !held && (!frame->redo || host_collector_running()); asking;)
+ {
+ if (!doltgres_entries_tablespace_asked(frame, true, &left, &failed) ||
+ (left && !doltgres_entries_tablespace_asked(frame, false, &left, &failed)))
+ goto raised;
+ if (!left || frame->redo)
+ break;
+ if (!pg_calls_latch_sleep(raise, DOLTGRES_ENTRIES_EMPTIED_NAP, frame->error))
+ return;
+ if (!doltgres_entries_tablespace_held(frame, &held, &failed))
+ goto raised;
+ asking = !held;
+ }
+ if (frame->redo && !doltgres_entries_tablespace_marked(frame, &failed))
+ goto raised;
+ frame->ok = true;
+ return;
+raised:
+ *frame->error = pghost_raised(&failed);
+}
+
+/*
+ * doltgres_entries_doltgres_tablespace_emptied empties the fork's packs out of
+ * a tablespace and removes its directory of them before Postgres removes the
+ * tablespace's directories (design item 5, bugs row 99): when no directory of
+ * Postgres' there holds a file, after the checkpoint that removes the files
+ * of dropped relations, the collector worker copies every live chunk of
+ * each repository's packs there into the repository's own directory and
+ * retires them, the files go once their readers end, and a base backup
+ * keeps the collection from running, so DROP TABLESPACE waits for each,
+ * answering cancel. Redo makes one attempt, and none when no collector
+ * worker runs to answer, leaving the dropped mark on what is left, which
+ * the worker's sweep removes.
+ */
+bool
+doltgres_entries_doltgres_tablespace_emptied(const PgCaps *caps, uint32 tablespaceoid, bool redo, PgPgError *error)
+{
+ doltgres_entries_doltgres_tablespace_emptied_frame_t frame = {.caps = caps, .io = pghost_io(pg_Caps_may_raise(caps)),
+ .tablespace = tablespaceoid, .redo = redo, .error = error};
+
+ std_landing(&frame, doltgres_entries_doltgres_tablespace_emptied_body,
+ doltgres_entries_doltgres_tablespace_emptied_pad);
+ doltgres_entries_doltgres_tablespace_emptied_pad(&frame);
+ return frame.ok;
+}
+
/*
* doltgres_entries_doltgres_relation_estimate_size is the fork's
* relation_estimate_size: Postgres' block-based estimate over the fork's
@@ -4133,7 +4786,7 @@ doltgres_entries_doltgres_parallel_based_body(void *frame_)
frame->base_live = true;
base = &frame->base;
parts.fresh = base->fresh_end;
- parts.folded = base->folded;
+ parts.folded = base->read;
}
doltgres_scan_Epochs_based(base, &epoch, &has_chain, &chain);
store = repo_Repo_reader(repo, HOST_CACHE_PLACEMENT_SERVER, &frame->opened.reading);
@@ -5093,17 +5746,20 @@ doltgres_entries_entry_pushed(HostIoIo *io, const RepoRepo *repo, const HostShar
/*
* doltgres_entries_index_read_captures_t is an index read's lent calls'
- * captures: the Dirty snapshot it reads under and the scan reader it steps.
+ * captures: the Dirty snapshot it reads under, the base generation its base
+ * was read at, and the scan reader it steps.
*/
typedef struct doltgres_entries_index_read_captures_t
{
const HostSnapshot *read;
+ uint64 generation;
HostOwnedHandle reader;
} doltgres_entries_index_read_captures_t;
/*
* doltgres_entries_index_read_readable is an index read's first lent call's
- * body: the base the Dirty snapshot may read.
+ * body: the base the Dirty snapshot may read, with the generation read
+ * before it.
*/
static bool
doltgres_entries_index_read_readable(void *captures, HostIoIo *io, const RepoRepo *repo, const HostSharedHeld *held,
@@ -5111,6 +5767,7 @@ doltgres_entries_index_read_readable(void *captures, HostIoIo *io, const RepoRep
{
doltgres_entries_index_read_captures_t *c = captures;
+ c->generation = txn_state_generation(host_shared_Held_record(held));
return txn_state_readable(io, held, c->read, some, result, error);
}
@@ -5224,8 +5881,8 @@ doltgres_entries_index_read_begin(doltgres_entries_index_read_frame_t *frame, co
std_slice_to_vec_Checked(&keyed, frame->keyed, frame->keyed_len, 1, 1);
frame->order_live = false;
if (!doltgres_scan_scan_begin(frame->io, TREES_STORE_READER_NODE_STORE(&store), 0, some ? &frame->base : NULL,
- &keyed, pg_calls_index_table(frame->index), &frame->order, &frame->captures.reader,
- &failed) ||
+ frame->captures.generation, &keyed, pg_calls_index_table(frame->index),
+ &frame->order, &frame->captures.reader, &failed) ||
!doltgres_scan_seek(frame->io, &stores, frame->captures.reader, from, from_len,
DOLTGRES_SCAN_DIRECTION_FORWARD, &failed))
{
@@ -6435,10 +7092,6 @@ doltgres_entries_pushed_closure_0(void *captures, HostIoIo *io, const RepoRepo *
doltgres_entries_pushed_captures_t *c = captures;
const doltgres_entries_Ends *ends = c->ends;
HostIoWaits waits = pghost_waits();
- bool has_newest;
- TxnBaseBase newest;
- bool has_tuple_ids = false;
- TreesStoreTree tuple_ids = {0};
bool has_key;
bool keyless;
TxnChangeEnding row;
@@ -6446,16 +7099,8 @@ doltgres_entries_pushed_closure_0(void *captures, HostIoIo *io, const RepoRepo *
c->store = repo_Repo_reader(repo, HOST_CACHE_PLACEMENT_SERVER, c->reading);
c->held = held;
- if (!txn_state_newest(io, held, &has_newest, &newest, error))
- return false;
- if (has_newest)
- {
- has_tuple_ids = newest.has_tuple_ids;
- tuple_ids = newest.tuple_ids;
- txn_base_Base_drop(&newest);
- }
- if (!doltgres_scan_keyed(io, TREES_STORE_READER_NODE_STORE(&c->store), host_shared_Held_record(held),
- has_tuple_ids, tuple_ids, c->tuple, &has_key, &row.key, &keyless, error))
+ if (!doltgres_scan_keyed(io, TREES_STORE_READER_NODE_STORE(&c->store), held, c->tuple, &has_key, &row.key,
+ &keyless, error))
return false;
*some = has_key;
if (!has_key)
@@ -7273,10 +7918,6 @@ doltgres_entries_doltgres_tuple_update_closure_1_body(void *frame_)
doltgres_entries_doltgres_tuple_update_captures_t *c = frame->c;
const doltgres_entries_Written *w = c->written;
HostStorage storage = doltgres_entries_storage(c->rel);
- bool has_newest;
- TxnBaseBase newest;
- bool has_tuple_ids = false;
- TreesStoreTree tuple_ids = {0};
bool has_key;
bool keyless;
TxnInsertKeyed keyed = {.stripe = w->stripe};
@@ -7284,16 +7925,7 @@ doltgres_entries_doltgres_tuple_update_closure_1_body(void *frame_)
c->store = repo_Repo_reader(frame->repo, HOST_CACHE_PLACEMENT_SERVER, c->reading);
c->held = frame->held;
- if (!txn_state_newest(frame->io, frame->held, &has_newest, &newest, frame->error))
- return;
- if (has_newest)
- {
- has_tuple_ids = newest.has_tuple_ids;
- tuple_ids = newest.tuple_ids;
- txn_base_Base_drop(&newest);
- }
- if (!doltgres_scan_keyed(frame->io, TREES_STORE_READER_NODE_STORE(&c->store),
- host_shared_Held_record(frame->held), has_tuple_ids, tuple_ids, c->tuple, &has_key,
+ if (!doltgres_scan_keyed(frame->io, TREES_STORE_READER_NODE_STORE(&c->store), frame->held, c->tuple, &has_key,
&frame->replaces.key, &keyless, frame->error))
return;
*frame->some = has_key;
@@ -8113,9 +8745,21 @@ doltgres_entries_build_scanned(const PgCatalogCaps *caps, HostIoIo *io, const Re
#define TXN_STATE_DECLARE
#include "doltgres/txn/state_template.h"
+/*
+ * doltgres_entries_doltgres_index_build_range_scan_captures_t is the build
+ * scan's lent call's captures: the table, and the base generation its base
+ * was read at.
+ */
+typedef struct doltgres_entries_doltgres_index_build_range_scan_captures_t
+{
+ const PgRel *table;
+ uint64 generation;
+} doltgres_entries_doltgres_index_build_range_scan_captures_t;
+
/*
* doltgres_entries_doltgres_index_build_range_scan_closure_0 is the build
- * scan's lent call: the table's blocks and newest base.
+ * scan's lent call: the table's blocks and newest base, with the generation
+ * read before it.
*/
static bool
doltgres_entries_doltgres_index_build_range_scan_closure_0(void *captures, HostIoIo *io, const RepoRepo *repo,
@@ -8123,7 +8767,10 @@ doltgres_entries_doltgres_index_build_range_scan_closure_0(void *captures, HostI
bool *has_result_1, TxnBaseBase *result_1,
HostError *error)
{
- *result_0 = doltgres_entries_blocks(captures, held);
+ doltgres_entries_doltgres_index_build_range_scan_captures_t *c = captures;
+
+ *result_0 = doltgres_entries_blocks(c->table, held);
+ c->generation = txn_state_generation(host_shared_Held_record(held));
return txn_state_newest(io, held, has_result_1, result_1, error);
}
@@ -8209,6 +8856,7 @@ doltgres_entries_doltgres_index_build_range_scan_own(doltgres_entries_doltgres_i
TreesStoreReader store;
uint64 last;
HostError failed;
+ doltgres_entries_doltgres_index_build_range_scan_captures_t c = {frame->table};
if (!doltgres_entries_recorded(frame->caps, &frame->io, repo, &frame->opened.reading, frame->table,
&frame->recorded, frame->error))
@@ -8227,9 +8875,8 @@ doltgres_entries_doltgres_index_build_range_scan_own(doltgres_entries_doltgres_i
if (!doltgres_entries_making(frame->caps, &frame->io, frame->table, &found, frame->error))
return false;
if (!txn_state_held_TupleU64OptionBase(&frame->io, repo, &frame->opened.reading, found,
- doltgres_entries_doltgres_index_build_range_scan_closure_0,
- (void *) frame->table, NULL, blocks, &frame->base_live, &frame->base,
- &failed))
+ doltgres_entries_doltgres_index_build_range_scan_closure_0, &c, NULL,
+ blocks, &frame->base_live, &frame->base, &failed))
{
frame->base_live = false;
*frame->error = pghost_raised(&failed);
@@ -8238,7 +8885,7 @@ doltgres_entries_doltgres_index_build_range_scan_own(doltgres_entries_doltgres_i
store = repo_Repo_reader(repo, HOST_CACHE_PLACEMENT_SERVER, &frame->opened.reading);
frame->keyed_live = frame->order_live = false;
if (!doltgres_scan_scan_begin(&frame->io, TREES_STORE_READER_NODE_STORE(&store), *blocks,
- frame->base_live ? &frame->base : NULL, &frame->keyed,
+ frame->base_live ? &frame->base : NULL, c.generation, &frame->keyed,
pg_calls_rel_id(frame->table), &frame->order, handle, &failed))
{
*frame->error = pghost_raised(&failed);
@@ -8359,6 +9006,169 @@ doltgres_entries_doltgres_index_build_range_scan(const PgCatalogCaps *caps, cons
return frame.ok;
}
+/*
+ * doltgres_entries_BuildSnapshot is a concurrent build's snapshot, which its
+ * validation reads in the same backend: the index built, and the snapshot's
+ * bounds, whether its subtransactions overflowed, and its running xids, each
+ * a Vec.
+ */
+typedef struct doltgres_entries_BuildSnapshot
+{
+ uint32 index;
+ uint64 xmin;
+ uint64 xmax;
+ bool overflowed;
+ StdVec xip;
+ StdVec subxip;
+} doltgres_entries_BuildSnapshot;
+
+/* doltgres_entries_BuildSnapshot_drop is BuildSnapshot's drop: its running xids. */
+static void
+doltgres_entries_BuildSnapshot_drop(void *snapshot)
+{
+ doltgres_entries_BuildSnapshot *self = snapshot;
+
+ std_vec_free_Checked(&self->xip, sizeof(uint32), _Alignof(uint32));
+ std_vec_free_Checked(&self->subxip, sizeof(uint32), _Alignof(uint32));
+}
+
+/* doltgres_entries_VecBuildSnapshot_drop is Vec's drop. */
+static void
+doltgres_entries_VecBuildSnapshot_drop(void *snapshots)
+{
+ std_vec_drop_Checked(snapshots, sizeof(doltgres_entries_BuildSnapshot), _Alignof(doltgres_entries_BuildSnapshot),
+ doltgres_entries_BuildSnapshot_drop);
+}
+
+/* BUILD_SNAPSHOTS' IoMutex, host's lock at the backend's concurrent builds' snapshots. */
+#define HOST_LOCK_SCOPE static
+#define HOST_LOCK_DECLARE
+#define HOST_LOCK_DEFINE
+#define HOST_LOCK_TAKEN_TS VecBuildSnapshot
+#define HOST_LOCK_TAKEN_T StdVec
+#define HOST_LOCK_TAKEN_DEFAULT(place) \
+ std_vec_new((place), sizeof(doltgres_entries_BuildSnapshot), _Alignof(doltgres_entries_BuildSnapshot))
+#define HOST_LOCK_TAKEN_DROP(place) doltgres_entries_VecBuildSnapshot_drop(place)
+#define HOST_LOCK_TAKEN_TYPE DOLTGRES_ENTRIES_BUILD_SNAPSHOTS_LOCK_TYPE
+#define HOST_LOCK_IO_MUTEX
+#define HOST_LOCK_TS VecBuildSnapshot
+#define HOST_LOCK_T StdVec
+#define HOST_LOCK_DEFAULT(place) \
+ std_vec_new((place), sizeof(doltgres_entries_BuildSnapshot), _Alignof(doltgres_entries_BuildSnapshot))
+#include "doltgres/host/lock_template.h"
+
+/* doltgres_entries_IoMutexVecBuildSnapshot_drop is IoMutex>' drop glue: its value's. */
+static void
+doltgres_entries_IoMutexVecBuildSnapshot_drop(void *value)
+{
+ doltgres_entries_VecBuildSnapshot_drop(&((HostLockIoMutexVecBuildSnapshot *) value)->value);
+}
+
+/* DOLTGRES_ENTRIES_BUILD_SNAPSHOTS_TYPE is IoMutex>' size, Default, and drop. */
+static const StdDefaultType DOLTGRES_ENTRIES_BUILD_SNAPSHOTS_TYPE = {sizeof(HostLockIoMutexVecBuildSnapshot),
+ _Alignof(HostLockIoMutexVecBuildSnapshot), host_lock_IoMutexVecBuildSnapshot_Default_default,
+doltgres_entries_IoMutexVecBuildSnapshot_drop};
+
+/*
+ * DOLTGRES_ENTRIES_BUILD_SNAPSHOTS is BUILD_SNAPSHOTS: each concurrent build's
+ * snapshot from its build's end until its validation takes it, in backend
+ * memory alone (bugs row 313); its BackendLocal header and the process's
+ * value.
+ */
+static struct
+{
+ HostLocalBackendLocal local;
+ HostLockIoMutexVecBuildSnapshot process;
+} DOLTGRES_ENTRIES_BUILD_SNAPSHOTS = {HOST_LOCAL_BACKEND_LOCAL_NEW};
+
+/* doltgres_entries_build_snapshot_kept_op moves a build's snapshot into BUILD_SNAPSHOTS, in place of its index's older one. */
+static void
+doltgres_entries_build_snapshot_kept_op(void *value, void *captures)
+{
+ StdVec *self = value;
+ doltgres_entries_BuildSnapshot *kept = captures;
+
+ for (size_t i = 0; i < self->len; i++)
+ {
+ doltgres_entries_BuildSnapshot *at = &STD_VEC_AT(self, doltgres_entries_BuildSnapshot, i);
+
+ if (at->index == kept->index)
+ {
+ doltgres_entries_BuildSnapshot_drop(at);
+ *at = *kept;
+ return;
+ }
+ }
+ std_vec_push_Checked(self, kept, sizeof(*kept), _Alignof(doltgres_entries_BuildSnapshot));
+}
+
+/* doltgres_entries_build_snapshot_taken_t is a take of BUILD_SNAPSHOTS' captures: the index, and its snapshot when it had one. */
+typedef struct doltgres_entries_build_snapshot_taken_t
+{
+ uint32 index;
+ bool has;
+ doltgres_entries_BuildSnapshot snapshot;
+} doltgres_entries_build_snapshot_taken_t;
+
+/* doltgres_entries_build_snapshot_taken_op takes an index's build snapshot out of BUILD_SNAPSHOTS into its captures. */
+static void
+doltgres_entries_build_snapshot_taken_op(void *value, void *captures)
+{
+ StdVec *self = value;
+ doltgres_entries_build_snapshot_taken_t *taken = captures;
+
+ for (size_t i = 0; i < self->len; i++)
+ if (STD_VEC_AT(self, doltgres_entries_BuildSnapshot, i).index == taken->index)
+ {
+ std_vec_swap_remove(self, i, &taken->snapshot, sizeof(doltgres_entries_BuildSnapshot),
+ _Alignof(doltgres_entries_BuildSnapshot));
+ taken->has = true;
+ return;
+ }
+}
+
+/*
+ * doltgres_entries_build_snapshot_copied copies the transaction's snapshot
+ * for a concurrent build of an index, taken before the build reads a row, as
+ * heap's concurrent build scan takes its own.
+ */
+static bool
+doltgres_entries_build_snapshot_copied(const PgMayRaise *raise, uint32 index, doltgres_entries_BuildSnapshot *result,
+ PgPgError *error)
+{
+ PgSnapshotView view;
+
+ if (!pg_transaction_snapshot_view(raise, &view, error))
+ return false;
+ result->index = index;
+ result->xmin = view.xmin;
+ result->xmax = view.xmax;
+ result->overflowed = view.overflowed;
+ std_slice_to_vec_Checked(&result->xip, view.xip, view.xip_len, sizeof(uint32), _Alignof(uint32));
+ std_slice_to_vec_Checked(&result->subxip, view.subxip, view.subxip_len, sizeof(uint32), _Alignof(uint32));
+ return true;
+}
+
+/*
+ * doltgres_entries_build_snapshot_saw reports whether a concurrent build's
+ * snapshot saw a row's writer ended, so that the build indexed the row: an
+ * xid the snapshot counts as no longer running, which an overflowed snapshot
+ * answers only below its xmin.
+ */
+static bool
+doltgres_entries_build_snapshot_saw(const doltgres_entries_BuildSnapshot *built, uint64 writer)
+{
+ HostSnapshot snapshot = {.kind = HOST_SNAPSHOT_KIND_MVCC, .xmin = {built->xmin}, .xmax = {built->xmax},
+ .overflowed = built->overflowed, .xip = built->xip.ptr, .xip_len = built->xip.len,
+ .subxip = built->subxip.ptr, .subxip_len = built->subxip.len};
+
+ if (writer == InvalidTransactionId)
+ return false;
+ if (built->overflowed)
+ return writer < built->xmin;
+ return !txn_visible_in_snapshot(&snapshot, writer, writer);
+}
+
/* doltgres_entries_doltgres_index_validate_scan_closure_0 is the validate scan's lent call: the table's block count. */
static bool
doltgres_entries_doltgres_index_validate_scan_closure_0(void *captures, HostIoIo *io, const RepoRepo *repo,
@@ -8387,6 +9197,7 @@ typedef struct doltgres_entries_doltgres_index_validate_scan_frame_t
PgIndexInfo *info;
const PgSnapshot *snapshot;
PgValidateState *state;
+ doltgres_entries_build_snapshot_taken_t built;
PgPgError *error;
bool ok;
} doltgres_entries_doltgres_index_validate_scan_frame_t;
@@ -8401,7 +9212,9 @@ doltgres_entries_doltgres_index_validate_scan_pad(void *frame_)
pg_OwnedBuild_drop(&frame->build);
if (frame->slot_live)
pg_OwnedSlot_drop(&frame->slot);
- frame->build_live = frame->slot_live = false;
+ if (frame->built.has)
+ doltgres_entries_BuildSnapshot_drop(&frame->built.snapshot);
+ frame->build_live = frame->slot_live = frame->built.has = false;
doltgres_entries_opened_drop(&frame->opened);
}
@@ -8430,6 +9243,10 @@ doltgres_entries_doltgres_index_validate_scan_body(void *frame_)
HostError failed;
frame->io = pghost_io_catalog(frame->caps);
+ frame->built.index = pg_calls_rel_id(frame->index);
+ doltgres_entries_locked(&frame->io, &DOLTGRES_ENTRIES_BUILD_SNAPSHOTS.local, &DOLTGRES_ENTRIES_BUILD_SNAPSHOTS_TYPE,
+ &DOLTGRES_ENTRIES_BUILD_SNAPSHOTS_LOCK_TYPE, doltgres_entries_build_snapshot_taken_op,
+ &frame->built);
if (!doltgres_entries_open(&frame->io, &frame->opened, frame->error) ||
!doltgres_entries_making(frame->caps, &frame->io, frame->table, &found, frame->error))
return;
@@ -8487,6 +9304,7 @@ doltgres_entries_doltgres_index_validate_scan_body(void *frame_)
ended = !has_sorted;
}
if ((!has_sorted || sorted != tuple) &&
+ !(frame->built.has && doltgres_entries_build_snapshot_saw(&frame->built.snapshot, pg_calls_slot_writer(slot))) &&
!pg_calls_validate_insert(raise, catalog, pg_OwnedBuild_build(&frame->build), frame->index, frame->table,
frame->info, slot, &added, frame->error))
return;
@@ -8513,11 +9331,13 @@ doltgres_entries_doltgres_index_validate_scan_body(void *frame_)
* row whose tid the index lacks and the index's predicate admits is inserted
* through the index's own aminsert, a unique index checking the row. An
* index the fork stores hands the merge no tid, since its ambulkdelete
- * reports none, so each row is checked against the index's own entries
- * instead (index_holds, bugs row 168): VACUUM's callback, which would hand
- * the merge those tids, would make every VACUUM walk the whole index for
- * nothing, since no entry the fork stores names a dead row, and a keyed
- * table's entries hold no tids. It reports the scan's blocks to the build's
+ * reports none, so each row but one the build indexed is checked against the
+ * index's own entries instead (index_holds, bugs row 168): VACUUM's
+ * callback, which would hand the merge those tids, would make every VACUUM
+ * walk the whole index for nothing, since no entry the fork stores names a
+ * dead row, and a keyed table's entries hold no tids. A row the build indexed is one whose writer
+ * the build's snapshot saw ended (BUILD_SNAPSHOTS), and its key is not formed
+ * again (bugs row 313). It reports the scan's blocks to the build's
* progress, counts the rows it read and inserted in the validation's state,
* and clears the index's expression state, which lived in the scan's
* executor state.
@@ -8765,7 +9585,7 @@ doltgres_entries_rekeyed_closure_4(void *captures, HostIoIo *io, const RepoRepo
TxnBaseBase installing;
TreesStoreReader store;
const TxnRecordMapCarried *spaces = c->spaces->ptr;
- const DoltgresRewriteCarry *carried = c->carried->ptr;
+ const TxnRewriteCarry *carried = c->carried->ptr;
if (!txn_state_base_record(io, held, &some, &installing, error))
return false;
@@ -8786,16 +9606,9 @@ doltgres_entries_rekeyed_closure_4(void *captures, HostIoIo *io, const RepoRepo
return false;
if (!*result)
return true;
- for (size_t i = 0; i < c->spaces->len; i++)
- if (!txn_rewrite_spaced(io, held, &spaces[i], c->logged, pg_calls_max_heap_tuples_per_page(), false, 0,
- error))
- return false;
store = repo_Repo_reader(repo, HOST_CACHE_PLACEMENT_SERVER, c->reading);
- for (size_t i = 0; i < c->carried->len; i++)
- if (!txn_rewrite_carried(io, TREES_STORE_READER_NODE_STORE(&store), held, &carried[i].f0, carried[i].f1,
- carried[i].f2, c->logged, NULL, error))
- return false;
- return true;
+ return txn_rewrite_carried(io, TREES_STORE_READER_NODE_STORE(&store), held, spaces, c->spaces->len, carried,
+ c->carried->len, c->logged, NULL, pg_calls_max_heap_tuples_per_page(), error);
}
/* doltgres_entries_Rekeyed_carried_drop is a rekeyed version's drop glue: its key's and its op's. */
@@ -8888,7 +9701,7 @@ doltgres_entries_rekeyed_pad(void *frame_)
if (frame->blobs_live)
std_vec_free_Checked(&frame->blobs, sizeof(chunks_TupleAddressU64_t), _Alignof(chunks_TupleAddressU64_t));
if (frame->carried_live)
- std_vec_drop_Checked(&frame->carried, sizeof(DoltgresRewriteCarry), _Alignof(DoltgresRewriteCarry),
+ std_vec_drop_Checked(&frame->carried, sizeof(TxnRewriteCarry), _Alignof(TxnRewriteCarry),
doltgres_entries_Carry_drop);
if (frame->toast_live)
host_shared_Held_drop(&frame->values);
@@ -8970,12 +9783,12 @@ doltgres_entries_rekeyed_carried(doltgres_entries_rekeyed_frame_t *frame, HostSt
_Alignof(doltgres_TupleVecU8OpWriterShownLockMode_t));
frame->versions_live = true;
std_vec_with_capacity_Checked(&frame->carried, std_VecIntoIter_len(&frame->versions),
- sizeof(DoltgresRewriteCarry), _Alignof(DoltgresRewriteCarry));
+ sizeof(TxnRewriteCarry), _Alignof(TxnRewriteCarry));
frame->carried_live = true;
while (std_VecIntoIter_Iterator_next(&frame->versions, &version))
{
const TxnMemtablePut *put = NULL;
- DoltgresRewriteCarry carry = {0};
+ TxnRewriteCarry carry = {0};
TxnRecordRowChange *change = &carry.f0;
if (version.f1.tag == TXN_MEMTABLE_OP_PUT)
@@ -9030,7 +9843,7 @@ doltgres_entries_rekeyed_carried(doltgres_entries_rekeyed_frame_t *frame, HostSt
std_vec_new(&change->kept, sizeof(size_t), _Alignof(size_t));
carry.f1 = version.f3;
carry.f2 = version.f4;
- std_vec_push_Checked(&frame->carried, &carry, sizeof(DoltgresRewriteCarry), _Alignof(DoltgresRewriteCarry));
+ std_vec_push_Checked(&frame->carried, &carry, sizeof(TxnRewriteCarry), _Alignof(TxnRewriteCarry));
}
frame->versions_live = false;
std_VecIntoIter_drop(&frame->versions);
@@ -10416,9 +11229,38 @@ doltgres_entries_loaded_records(doltgres_entries_loaded_frame_t *frame, bool key
}
}
+/* doltgres_entries_loaded_logging_t is an index build's install's logging's captures: the build and its storage. */
+typedef struct doltgres_entries_loaded_logging_t
+{
+ const doltgres_entries_loaded_frame_t *frame;
+ HostStorage storage;
+} doltgres_entries_loaded_logging_t;
+
+/*
+ * doltgres_entries_loaded_logged is an index build's install's logging: it
+ * inserts the installed base's record and gives its end.
+ */
+static bool
+doltgres_entries_loaded_logged(void *captures, HostIoIoDoors io, HostLsn reached, HostLsn *end, HostError *error)
+{
+ const doltgres_entries_loaded_logging_t *logging = captures;
+ const doltgres_entries_loaded_frame_t *frame = logging->frame;
+ TxnRecordBaseAdvanced advanced = {.storage = logging->storage, .folded = frame->point,
+ .max_folded = frame->max_folded, .base = frame->installed, .packs = frame->packs};
+ StdVec body;
+ bool ok;
+
+ (void) reached;
+ txn_record_BaseAdvanced_encode(&advanced, &body);
+ ok = host_wal_insert(io, HOST_WAL_KIND_BASE_ADVANCED, body.ptr, body.len, end, error);
+ std_vec_free_Checked(&body, 1, 1);
+ return ok;
+}
+
/*
* doltgres_entries_loaded_closure_7 is loaded's register run: the tree's root
- * held at a slot and published, and the base installed.
+ * held at a slot and published, and the base installed, a permanent
+ * storage's packs listed first and its record logged in the install's step.
*/
static bool
doltgres_entries_loaded_closure_7(void *captures, HostIoRegistering *registering, ChunksPacksRootsRootSlots *slots,
@@ -10429,6 +11271,11 @@ doltgres_entries_loaded_closure_7(void *captures, HostIoRegistering *registering
ChunksPacksRootsRootSlot *slot;
bool installed;
HostError failed;
+ TxnStatePersistence persistence;
+ doltgres_entries_loaded_logging_t logging = {.frame = frame};
+ bool permanent = txn_state_described(host_shared_Held_record(&frame->held), &logging.storage,
+ &persistence) &&
+ persistence == TXN_STATE_PERSISTENCE_PERMANENT;
chunks_packs_roots_RootSlots_one(slots, &slot);
if (!trees_store_BrandedWriter_hold(&frame->writer, io, slot, frame->root, error))
@@ -10444,9 +11291,13 @@ doltgres_entries_loaded_closure_7(void *captures, HostIoRegistering *registering
frame->installed.root =
trees_published_Published_base(&STD_VEC_AT(&frame->published, TreesPublishedPublished, 0));
frame->installed.folded = frame->point;
+ frame->installed.read = frame->point;
frame->installed.max_folded = frame->max_folded;
frame->installed.sizes = frame->sizes;
- if (!txn_state_installed(io, &frame->held, frame->generation, &frame->installed, &installed, &failed))
+ if (permanent && !chunks_packs_backend_Packs_flush(&frame->repo->packs, io, frame->reading, error))
+ return false;
+ if (!txn_state_installed(io, &frame->held, frame->generation, &frame->installed,
+ permanent ? doltgres_entries_loaded_logged : NULL, &logging, &installed, &failed))
{
*error = chunks_error_Error_FromError_from(&failed);
return false;
@@ -10466,55 +11317,22 @@ doltgres_entries_loaded_closure_7(void *captures, HostIoRegistering *registering
return true;
}
-/* doltgres_entries_loaded_advanced logs the installed base as advanced, for a permanent index storage. */
+/* doltgres_entries_loaded_flushed flushes the WAL through the point, for a permanent index storage. */
static bool
-doltgres_entries_loaded_advanced(doltgres_entries_loaded_frame_t *frame)
+doltgres_entries_loaded_flushed(doltgres_entries_loaded_frame_t *frame)
{
HostStorage storage;
TxnStatePersistence persistence;
- TxnRecordBaseAdvanced advanced = {0};
- StdVec body;
- HostLsn lsn;
- ChunksErrorError failed;
- bool ok;
+ HostError host;
if (!txn_state_described(host_shared_Held_record(&frame->held), &storage, &persistence) ||
persistence != TXN_STATE_PERSISTENCE_PERMANENT)
return true;
- if (!chunks_packs_backend_Packs_flush(&frame->repo->packs, frame->io, frame->reading, &failed))
+ if (!host_wal_flush(HOST_IO_IO_IO_DOORS(frame->io), frame->point, &host))
{
- doltgres_entries_loaded_failed(frame, &failed);
+ *frame->error = pghost_raised(&host);
return false;
}
- {
- HostError host;
-
- if (!host_wal_flush(HOST_IO_IO_IO_DOORS(frame->io), frame->point, &host))
- {
- *frame->error = pghost_raised(&host);
- return false;
- }
- }
- advanced.storage = storage;
- advanced.folded = frame->point;
- advanced.max_folded = frame->max_folded;
- advanced.base = frame->installed;
- advanced.packs = frame->packs;
- frame->installed_live = frame->packs_live = false;
- txn_record_BaseAdvanced_encode(&advanced, &body);
- {
- HostError host;
-
- ok = host_wal_insert(HOST_IO_IO_IO_DOORS(frame->io), HOST_WAL_KIND_BASE_ADVANCED, body.ptr, body.len, &lsn,
- &host);
- std_vec_free_Checked(&body, 1, 1);
- txn_record_BaseAdvanced_drop(&advanced);
- if (!ok)
- {
- *frame->error = pghost_raised(&host);
- return false;
- }
- }
return true;
}
@@ -10655,17 +11473,31 @@ doltgres_entries_loaded_body(void *frame_)
*frame->error = doltgres_entries_internal(message, sizeof(message) - 1);
return;
}
- if (!doltgres_entries_loaded_advanced(frame))
+ if (!doltgres_entries_loaded_flushed(frame))
return;
store = repo_Repo_reader(frame->repo, HOST_CACHE_PLACEMENT_SERVER, frame->reading);
- for (size_t i = 0; i < frame->late.len; i++)
- if (!txn_rewrite_carried(frame->io, TREES_STORE_READER_NODE_STORE(&store), &frame->held,
- &STD_VEC_AT(&frame->late, TxnRecordRowChange, i), shown, TXN_MEMTABLE_LOCK_MODE_UPDATE,
- pg_calls_rel_permanent(frame->index), NULL, &host))
+ {
+ StdVec late;
+ bool ok;
+
+ std_vec_with_capacity_Checked(&late, frame->late.len, sizeof(TxnRewriteCarry), _Alignof(TxnRewriteCarry));
+ for (size_t i = 0; i < frame->late.len; i++)
+ {
+ TxnRewriteCarry carry = {STD_VEC_AT(&frame->late, TxnRecordRowChange, i), shown,
+ TXN_MEMTABLE_LOCK_MODE_UPDATE};
+
+ std_vec_push_Checked(&late, &carry, sizeof(carry), _Alignof(TxnRewriteCarry));
+ }
+ ok = txn_rewrite_carried(frame->io, TREES_STORE_READER_NODE_STORE(&store), &frame->held, NULL, 0, late.ptr,
+ late.len, pg_calls_rel_permanent(frame->index), NULL,
+ pg_calls_max_heap_tuples_per_page(), &host);
+ std_vec_free_Checked(&late, sizeof(TxnRewriteCarry), _Alignof(TxnRewriteCarry));
+ if (!ok)
{
*frame->error = pghost_raised(&host);
return;
}
+ }
*frame->result = frame->sizes.rows + late_rows;
frame->ok = true;
}
@@ -10742,6 +11574,8 @@ typedef struct doltgres_entries_doltgres_index_build_frame_t
bool described_live;
PgOwnedSort sort;
bool sort_live;
+ doltgres_entries_BuildSnapshot built;
+ bool built_live;
const PgCatalogCaps *caps;
HostIoIo io;
const PgRel *heap;
@@ -10767,10 +11601,24 @@ doltgres_entries_doltgres_index_build_pad(void *frame_)
doltgres_layout_VecKeyed_drop(&frame->table_key);
if (frame->key_live)
txn_base_Key_drop(&frame->key);
- frame->sort_live = frame->described_live = frame->table_key_live = frame->key_live = false;
+ if (frame->built_live)
+ doltgres_entries_BuildSnapshot_drop(&frame->built);
+ frame->sort_live = frame->described_live = frame->table_key_live = frame->key_live = frame->built_live = false;
doltgres_entries_opened_drop(&frame->opened);
}
+/* doltgres_entries_doltgres_index_build_kept moves a concurrent build's snapshot into BUILD_SNAPSHOTS once the build succeeds. */
+static void
+doltgres_entries_doltgres_index_build_kept(doltgres_entries_doltgres_index_build_frame_t *frame)
+{
+ if (!frame->ok || !frame->built_live)
+ return;
+ doltgres_entries_locked(&frame->io, &DOLTGRES_ENTRIES_BUILD_SNAPSHOTS.local, &DOLTGRES_ENTRIES_BUILD_SNAPSHOTS_TYPE,
+ &DOLTGRES_ENTRIES_BUILD_SNAPSHOTS_LOCK_TYPE, doltgres_entries_build_snapshot_kept_op,
+ &frame->built);
+ frame->built_live = false;
+}
+
/* doltgres_entries_doltgres_index_build_body scans the table into the build's sort and loads the sorted entries. */
static void
doltgres_entries_doltgres_index_build_body(void *frame_)
@@ -10824,6 +11672,12 @@ doltgres_entries_doltgres_index_build_body(void *frame_)
!pg_calls_sort_begin(raise, &frame->sort, frame->error))
return;
frame->sort_live = true;
+ if (pg_calls_index_info_concurrent(frame->info))
+ {
+ if (!doltgres_entries_build_snapshot_copied(raise, pg_calls_rel_id(frame->index), &frame->built, frame->error))
+ return;
+ frame->built_live = true;
+ }
pg_calls_progress_update(PROGRESS_CREATEIDX_SUBPHASE, PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN);
doltgres_entries_locked(&frame->io, &DOLTGRES_ENTRIES_BUILD_DESCRIBED.local, &DOLTGRES_ENTRIES_BUILD_DESCRIBED_TYPE,
&DOLTGRES_ENTRIES_BUILD_DESCRIBED_LOCK_TYPE, doltgres_entries_describing_replaced_op,
@@ -10839,6 +11693,7 @@ doltgres_entries_doltgres_index_build_body(void *frame_)
if (doltgres_index_Described_keyed(described))
{
frame->ok = pg_calls_build_result(raise, heap_tuples, (float8) put, frame->result, frame->error);
+ doltgres_entries_doltgres_index_build_kept(frame);
return;
}
pg_calls_progress_update_three(PROGRESS_CREATEIDX_TUPLES_TOTAL, put > PG_INT64_MAX ? PG_INT64_MAX : (int64) put,
@@ -10866,6 +11721,7 @@ doltgres_entries_doltgres_index_build_body(void *frame_)
frame->info, &frame->sort, described, horizon, based, &entries, frame->error))
return;
frame->ok = pg_calls_build_result(raise, heap_tuples, (float8) entries, frame->result, frame->error);
+ doltgres_entries_doltgres_index_build_kept(frame);
}
/*
@@ -10879,7 +11735,8 @@ doltgres_entries_doltgres_index_build_body(void *frame_)
* the sort as the load's total, with the scan's block counts reset in the
* same update. It answers the table's live rows and the rows its entries
* hold. A new index records its identity pairs first, in its creating
- * transaction (design item 10).
+ * transaction (design item 10). A concurrent build keeps the snapshot it took
+ * before its scan for its validation (BUILD_SNAPSHOTS).
*/
bool
doltgres_entries_doltgres_index_build(const PgCatalogCaps *caps, const PgRel *heap, const PgRel *index,
@@ -11267,13 +12124,11 @@ doltgres_entries_doltgres_index_delete_tuples(const PgCatalogCaps *caps, const P
/*
* doltgres_entries_doltgres_index_holds_captures_t is index_holds' lent call's
- * captures: the reader, the base's tuple ids, and the row's tuple id.
+ * captures: the reader and the row's tuple id.
*/
typedef struct doltgres_entries_doltgres_index_holds_captures_t
{
const HostSharedReader *reading;
- bool has_tuple_ids;
- TreesStoreTree tuple_ids;
uint64 tuple;
} doltgres_entries_doltgres_index_holds_captures_t;
@@ -11286,8 +12141,8 @@ doltgres_entries_doltgres_index_holds_closure_0(void *captures, HostIoIo *io, co
doltgres_entries_doltgres_index_holds_captures_t *c = captures;
TreesStoreReader store = repo_Repo_reader(repo, HOST_CACHE_PLACEMENT_SERVER, c->reading);
- return doltgres_scan_keyed(io, TREES_STORE_READER_NODE_STORE(&store), host_shared_Held_record(held),
- c->has_tuple_ids, c->tuple_ids, c->tuple, some, result_0, result_1, error);
+ return doltgres_scan_keyed(io, TREES_STORE_READER_NODE_STORE(&store), held, c->tuple, some, result_0, result_1,
+ error);
}
/*
@@ -11388,8 +12243,6 @@ doltgres_entries_doltgres_index_holds_body(void *frame_)
return;
}
captures.tuple = pg_calls_tid_value(frame->heap_tid);
- captures.has_tuple_ids = frame->base.has_tuple_ids;
- captures.tuple_ids = frame->base.tuple_ids;
if (!doltgres_entries_making(frame->caps, &frame->io, frame->heap, &made, frame->error))
return;
if (!txn_state_held_OptionTupleVecU8Bool(&frame->io, repo, &frame->opened.reading, made,
diff --git a/src/doltgres/doltgres/c/rewrite.c b/src/doltgres/doltgres/c/rewrite.c
index 30a3588495980..f753f1264c61c 100644
--- a/src/doltgres/doltgres/c/rewrite.c
+++ b/src/doltgres/doltgres/c/rewrite.c
@@ -354,7 +354,7 @@ doltgres_rewrite_read_body(void *frame_)
std_vec_new(&read->olds, DOLTGRES_REWRITE_OLD);
frame->rest_live = true;
read->left_out = read->base.max_ended >= frame->horizon ? read->base.max_ended : 0;
- if (!txn_rewrite_keys(frame->io, frame->held, read->based.ptr, read->based.len, read->base.folded,
+ if (!txn_rewrite_keys(frame->io, frame->held, read->based.ptr, read->based.len, read->base.read,
doltgres_rewrite_read_closure_0, &captures, NULL, frame->error))
return;
*frame->result = *read;
@@ -1646,7 +1646,7 @@ doltgres_rewrite_Reformer_reformed(DoltgresRewriteReformer *self, const PgCatalo
/* More element layouts of the rewrite's vectors. */
#define DOLTGRES_REWRITE_COPIED sizeof(TxnRewriteCopied), _Alignof(TxnRewriteCopied)
#define DOLTGRES_REWRITE_COPY sizeof(txn_TupleU64U64U32_t), _Alignof(txn_TupleU64U64U32_t)
-#define DOLTGRES_REWRITE_CARRY sizeof(DoltgresRewriteCarry), _Alignof(DoltgresRewriteCarry)
+#define DOLTGRES_REWRITE_CARRY sizeof(TxnRewriteCarry), _Alignof(TxnRewriteCarry)
#define DOLTGRES_REWRITE_KEPT_ROW sizeof(doltgres_rewrite_kept_t), _Alignof(doltgres_rewrite_kept_t)
#define DOLTGRES_REWRITE_ROW_ENDS sizeof(doltgres_rewrite_row_ends_t), _Alignof(doltgres_rewrite_row_ends_t)
#define DOLTGRES_REWRITE_ROWS sizeof(StdVec), _Alignof(StdVec)
@@ -1756,7 +1756,7 @@ doltgres_rewrite_copied(const DoltgresRewriteRead *read, DoltgresRewriteRewritte
static void
doltgres_rewrite_Carry_drop(void *carry)
{
- txn_record_RowChange_drop(&((DoltgresRewriteCarry *) carry)->f0);
+ txn_record_RowChange_drop(&((TxnRewriteCarry *) carry)->f0);
}
/*
@@ -2134,7 +2134,7 @@ doltgres_rewrite_carrying_body(void *frame_)
TxnMemtableVersionOpRef ref;
TxnMemtableLockMode lock;
const StdVec *values = NULL;
- DoltgresRewriteCarry carry = {0};
+ TxnRewriteCarry carry = {0};
HostRow row = {0};
StdBytes rekeyed;
bool has_row = true;
@@ -2331,7 +2331,7 @@ doltgres_rewrite_moving_closure_0(void *captures, HostIoIo *io, const uint8 *key
TxnMemtableVersionVersion *version = &STD_VEC_AT(versions, TxnMemtableVersionVersion, i);
TxnMemtableVersionRecord record;
TxnMemtableVersionOpRef ref;
- DoltgresRewriteCarry carry = {0};
+ TxnRewriteCarry carry = {0};
HostStatus outcome;
if (!txn_outcome_outcome(io, version, &outcome, error))
diff --git a/src/doltgres/doltgres/c/scan.c b/src/doltgres/doltgres/c/scan.c
index 836204d817e13..7804063bc2972 100644
--- a/src/doltgres/doltgres/c/scan.c
+++ b/src/doltgres/doltgres/c/scan.c
@@ -51,6 +51,7 @@
#include "doltgres/txn/change.h"
#include "doltgres/txn/counted.h"
#include "doltgres/txn/insert.h"
+#include "doltgres/txn/spill.h"
#include "doltgres/txn/state.h"
#include "doltgres/txn/written.h"
#endif
@@ -162,12 +163,16 @@ typedef enum DoltgresScanTurnTag
DOLTGRES_SCAN_TURN_END,
DOLTGRES_SCAN_TURN_RUN,
DOLTGRES_SCAN_TURN_COMPARE,
+ DOLTGRES_SCAN_TURN_MOVED,
} DoltgresScanTurnTag;
/*
* DoltgresScanTurn is Turn: what a turn of a read in the scan's record
* leaves it, a row, another turn, the end, a compared table's run to make
- * by its order, or two candidates to compare by it.
+ * by its order, two candidates to compare by it, or an install's move of the
+ * storage's base during the turn's read of the delta, which changed nothing
+ * in the record: the scan moves to the newest base (reseated) and takes the
+ * turn again.
*/
typedef struct DoltgresScanTurn
{
@@ -271,8 +276,12 @@ typedef struct DoltgresScanUnit
/*
* DoltgresScanParallel is Parallel: a parallel scan's part of its record,
* the base's count of entries, the count of units, the unit being read, a
- * tid range unit's first and last tuple ids, and the delta's split keys (a
- * Vec>).
+ * tid range unit's first and last tuple ids, the delta's split keys (a
+ * Vec>), the table tree of the base the leader shared, whose
+ * entries' ordinals every worker's units split whatever newer base a worker
+ * moved to (rebased), and the unit's low and high keys (bounds), which a
+ * worker on a newer base than the shared one finds its unit's place and stop
+ * in.
*/
typedef struct DoltgresScanParallel
{
@@ -284,6 +293,19 @@ typedef struct DoltgresScanParallel
uint64 ranged_first;
uint64 ranged_last;
StdVec splits;
+ bool has_shared;
+ TreesStoreTree shared;
+ doltgres_OptionVecU8_t low;
+ doltgres_OptionVecU8_t high;
+
+ /*
+ * kept is the delta's run in the table's order (a Vec>), read at
+ * the scan's generation kept_at, which each unit takes its keys from
+ * (bugs row 309).
+ */
+ bool has_kept;
+ StdVec kept;
+ uint64 kept_at;
} DoltgresScanParallel;
/*
@@ -301,6 +323,12 @@ typedef struct DoltgresScanPaired
uint32 table;
TreesKeyOrder order;
StdVec transform;
+
+ /*
+ * resolved is the memo of the fetch's scan, or its call, which the pair
+ * doesn't own.
+ */
+ DoltgresScanResolved *resolved;
} DoltgresScanPaired;
/* DoltgresScanScan is Scan: a table scan's record, its fields as the Rust's, in its order. */
@@ -321,6 +349,14 @@ struct DoltgresScanScan
TreesStoreTree tree;
uint64 folded;
DoltgresScanEpochs epochs;
+
+ /*
+ * resolved is the memo of the base leaf the scan's fetches last resolved
+ * a row in.
+ */
+ DoltgresScanResolved resolved;
+ /* generation is the base generation the scan's base was read at. */
+ uint64 generation;
uint64 blocks;
StdVec keyed;
uint32 table;
@@ -419,6 +455,47 @@ doltgres_scan_Epochs_of(HostIoIo *io, TreesStoreNodeStore store, uint32 epoch, b
return true;
}
+/* doltgres_scan_internal returns an internal error of the fork's with a literal message. */
+static HostError
+doltgres_scan_internal(const char *message)
+{
+ StdString text;
+
+ std_string_from_Checked(&text, message, strlen(message));
+ return host_Error_fork((const uint8 *) "XX000", &text);
+}
+
+/*
+ * doltgres_scan_Epochs_moved takes the epochs of a newer base a read moved to
+ * (rebased, Trees_newer), which it takes, keeping the reader's epoch and its
+ * layout's check; a newer base whose epoch or chain doesn't reach the
+ * reader's epoch is an internal error, as a row past it is.
+ */
+static bool
+doltgres_scan_Epochs_moved(DoltgresScanEpochs *self, DoltgresScanEpochs *newer, HostError *error)
+{
+ size_t end = 0;
+
+ if (self->has_end)
+ {
+ size_t reached = (size_t) self->base + self->end;
+
+ if (reached < newer->base || reached - newer->base > newer->chain.len)
+ {
+ doltgres_scan_Epochs_drop(newer);
+ *error = doltgres_scan_internal("a newer base is past its reader's epoch");
+ return false;
+ }
+ end = reached - newer->base;
+ }
+ newer->has_end = self->has_end;
+ newer->end = end;
+ newer->checked = self->checked;
+ doltgres_scan_Epochs_drop(self);
+ *self = *newer;
+ return true;
+}
+
/* doltgres_scan_Epochs_based answers a base's epoch and chain tree, which of reads, epoch 0 and no chain for none. */
void
doltgres_scan_Epochs_based(const TxnBaseBase *base, uint32 *result_0, bool *has_result_1, TreesStoreTree *result_1)
@@ -711,6 +788,7 @@ doltgres_scan_Turn_drop(DoltgresScanTurn *self)
break;
case DOLTGRES_SCAN_TURN_AGAIN:
case DOLTGRES_SCAN_TURN_END:
+ case DOLTGRES_SCAN_TURN_MOVED:
break;
}
}
@@ -788,6 +866,16 @@ doltgres_scan_Paired_drop(DoltgresScanPaired *self)
std_vec_free_Checked(&self->transform, DOLTGRES_SCAN_BYTES);
}
+/* doltgres_scan_Resolved_drop drops a memo of a base leaf: its stamps and its kept cursor, when it holds them. */
+void
+doltgres_scan_Resolved_drop(DoltgresScanResolved *self)
+{
+ trees_cursor_LeafStamps_drop(&self->stamps);
+ if (self->has_kept)
+ trees_cursor_KeptCursor_drop(&self->kept);
+ self->has_kept = false;
+}
+
/* doltgres_scan_Scan_drop drops a scan's record, its fields in their order, its reader first. */
static void
doltgres_scan_Scan_drop(void *record)
@@ -803,12 +891,19 @@ doltgres_scan_Scan_drop(void *record)
if (scan->has_written)
std_vec_free_Checked(&scan->written, DOLTGRES_SCAN_U64);
doltgres_scan_Epochs_drop(&scan->epochs);
+ doltgres_scan_Resolved_drop(&scan->resolved);
std_vec_free_Checked(&scan->keyed, DOLTGRES_SCAN_BYTES);
trees_KeyOrder_drop(&scan->order);
doltgres_scan_option_drop(&scan->bound);
doltgres_scan_option_drop(&scan->seek);
if (scan->has_parallel)
+ {
doltgres_scan_vecs_drop(&scan->parallel.splits);
+ doltgres_scan_option_drop(&scan->parallel.low);
+ doltgres_scan_option_drop(&scan->parallel.high);
+ if (scan->parallel.has_kept)
+ doltgres_scan_vecs_drop(&scan->parallel.kept);
+ }
doltgres_scan_Page_drop(&scan->page);
if (scan->has_sample)
doltgres_scan_rows_drop(&scan->sample.rows);
@@ -1331,6 +1426,8 @@ static bool doltgres_scan_split(HostIoIo *io, const StdVec *keys, const uint8 *f
static bool doltgres_scan_pick(HostIoIo *io, StdVec *base_key, DoltgresScanCopies *base_copies, StdVec *delta_key,
size_t delta_at, const TreesKeyOrder *order, DoltgresScanDirection way,
DoltgresScanPicked *result, HostError *error);
+static bool doltgres_scan_reseated(HostIoIo *io, const DoltgresScanStores *stores, const DoltgresScanUnder *under,
+ HostOwnedHandle handle, DoltgresScanDirection way, HostError *error);
/* doltgres_scan_Repo_Stores_with is the repository's Stores::with: the call lent the repository's reader of nodes. */
static void
@@ -1426,6 +1523,7 @@ typedef struct doltgres_scan_begin_frame_t
TreesStoreNodeStore store;
uint64 blocks;
const TxnBaseBase *base;
+ uint64 generation;
StdVec keyed;
bool keyed_live;
uint32 table;
@@ -1483,8 +1581,9 @@ doltgres_scan_begin_body(void *frame_)
scan.has_tree = base != NULL && base->has_root;
if (scan.has_tree)
scan.tree = base->root;
- scan.folded = base == NULL ? 0 : base->folded;
+ scan.folded = base == NULL ? 0 : base->read;
scan.epochs = frame->epochs;
+ scan.generation = frame->generation;
scan.blocks = frame->blocks;
scan.keyed = frame->keyed;
scan.table = frame->table;
@@ -1501,16 +1600,18 @@ doltgres_scan_begin_body(void *frame_)
/*
* doltgres_scan_scan_begin makes a scan of a storage of a count of blocks,
- * whose base, or no base (NULL), is in a table of a keyed layout, an object
- * id, and a tree's order, which it takes, and answers its handle; its first
- * read gives the storage's first row its snapshot sees.
+ * whose base, or no base (NULL), read at a base generation or after it, is in
+ * a table of a keyed layout, an object id, and a tree's order, which it
+ * takes, and answers its handle; its first read gives the storage's first
+ * row its snapshot sees.
*/
bool
doltgres_scan_scan_begin(HostIoIo *io, TreesStoreNodeStore store, uint64 blocks, const TxnBaseBase *base,
- StdVec *keyed, uint32 table, TreesKeyOrder *order, HostOwnedHandle *result,
- HostError *error)
+ uint64 generation, StdVec *keyed, uint32 table, TreesKeyOrder *order,
+ HostOwnedHandle *result, HostError *error)
{
- doltgres_scan_begin_frame_t frame = {.io = io, .store = store, .blocks = blocks, .base = base, .keyed = *keyed,
+ doltgres_scan_begin_frame_t frame = {.io = io, .store = store, .blocks = blocks, .base = base,
+ .generation = generation, .keyed = *keyed,
.keyed_live = true, .table = table, .order = *order, .order_live = true, .result = result, .error = error};
std_landing(&frame, doltgres_scan_begin_body, doltgres_scan_begin_pad);
@@ -1629,8 +1730,8 @@ doltgres_scan_gather_closure_2(void *captures, HostIoIo *io, TreesStoreNodeStore
/*
* doltgres_scan_advance_frame_t is advance's state across its pad: the
- * group's key it moves from, the base's entry, the delta's hold, the key a
- * seek left, and the delta's key and versions, each while live.
+ * base's entry, the delta's hold, and the delta's key and versions, each
+ * while live.
*/
typedef struct doltgres_scan_advance_frame_t
{
@@ -1639,11 +1740,9 @@ typedef struct doltgres_scan_advance_frame_t
const DoltgresScanUnder *under;
DoltgresScanScan *scan;
DoltgresScanDirection way;
- doltgres_OptionVecU8_t from;
doltgres_scan_base_read_t entry;
HostDeltaHold hold;
bool hold_live;
- doltgres_OptionVecU8_t at;
bool has_delta;
txn_TupleVecU8VecVersion_t delta;
DoltgresScanTurn *result;
@@ -1682,12 +1781,10 @@ doltgres_scan_advance_pad(void *frame_)
doltgres_scan_advance_frame_t *frame = frame_;
doltgres_scan_delta_drop(&frame->has_delta, &frame->delta);
- doltgres_scan_option_drop(&frame->at);
if (frame->hold_live)
host_delta_Hold_drop(&frame->hold);
frame->hold_live = false;
doltgres_scan_entry_drop(&frame->entry);
- doltgres_scan_option_drop(&frame->from);
}
/* doltgres_scan_advance_body finds the nearer of the base's next entry and the delta's next key and makes its group. */
@@ -1698,6 +1795,9 @@ doltgres_scan_advance_body(void *frame_)
DoltgresScanScan *scan = frame->scan;
DoltgresScanDirection way = frame->way;
bool entries = scan->index && !scan->records;
+ uint8 record = TXN_ENTRY_RECORD;
+ const uint8 *from = NULL;
+ size_t from_len = 0;
StdVec key;
bool has_copies = false;
DoltgresScanCopies copies;
@@ -1705,64 +1805,63 @@ doltgres_scan_advance_body(void *frame_)
if (scan->has_group)
{
- frame->from.has = true;
- frame->from.v = scan->group.key;
- std_vec_free_Checked(&scan->group.base, DOLTGRES_SCAN_BYTES);
- std_ptr_drop_in_place_slice(scan->group.rows.ptr, scan->group.rows.len, sizeof(DoltgresScanFound),
- doltgres_scan_Found_drop);
- std_vec_free_Checked(&scan->group.rows, DOLTGRES_SCAN_FOUND);
- scan->has_group = false;
+ from = scan->group.key.ptr;
+ from_len = scan->group.key.len;
}
- frame->entry.scan = scan;
- frame->entry.keyless = scan->keyed.len == 0;
- frame->entry.entries = entries ? &scan->order : NULL;
- frame->entry.way = way;
- frame->entry.error = frame->error;
- frame->stores->ops->with(frame->stores->self, frame->io, &scan->reader, doltgres_scan_advance_closure_1,
- &frame->entry);
- if (!frame->entry.ok)
- return;
- if (!frame->from.has && entries && way == DOLTGRES_SCAN_DIRECTION_BACKWARD)
+ else if (entries && way == DOLTGRES_SCAN_DIRECTION_BACKWARD)
{
- uint8 record = TXN_ENTRY_RECORD;
-
- std_slice_to_vec_Checked(&frame->from.v, &record, 1, DOLTGRES_SCAN_BYTES);
- frame->from.has = true;
+ from = &record;
+ from_len = 1;
}
if (!host_delta_hold(frame->io, &frame->hold, frame->error))
return;
frame->hold_live = true;
if (scan->seek.has)
{
- frame->at = scan->seek;
- scan->seek.has = false;
- if (!txn_visible_versions(frame->io, &frame->hold, frame->under->state, frame->at.v.ptr, frame->at.v.len,
+ if (!txn_visible_versions(frame->io, &frame->hold, frame->under->state, scan->seek.v.ptr, scan->seek.v.len,
scan->folded, &versions, frame->error))
return;
if (versions.len == 0)
{
txn_memtable_version_versions_drop(&versions, 0);
- if (!txn_visible_next(frame->io, &frame->hold, frame->under->state, true, frame->at.v.ptr,
- frame->at.v.len, doltgres_scan_toward(way), scan->folded, &frame->has_delta,
+ if (!txn_visible_next(frame->io, &frame->hold, frame->under->state, true, scan->seek.v.ptr,
+ scan->seek.v.len, doltgres_scan_toward(way), scan->folded, &frame->has_delta,
&frame->delta, frame->error))
return;
- doltgres_scan_option_drop(&frame->at);
}
else
{
- frame->delta.f0 = frame->at.v;
+ std_vec_clone(&frame->delta.f0, &scan->seek.v, DOLTGRES_SCAN_BYTES);
frame->delta.f1 = versions;
- frame->at.has = false;
frame->has_delta = true;
}
}
- else if (!txn_visible_next(frame->io, &frame->hold, frame->under->state, frame->from.has, frame->from.v.ptr,
- frame->from.has ? frame->from.v.len : 0, doltgres_scan_toward(way), scan->folded,
- &frame->has_delta, &frame->delta, frame->error))
+ else if (!txn_visible_next(frame->io, &frame->hold, frame->under->state, from != NULL, from, from_len,
+ doltgres_scan_toward(way), scan->folded, &frame->has_delta, &frame->delta,
+ frame->error))
return;
if (frame->has_delta && entries && frame->delta.f0.len > 0 &&
((const uint8 *) frame->delta.f0.ptr)[0] == TXN_ENTRY_RECORD)
doltgres_scan_delta_drop(&frame->has_delta, &frame->delta);
+ if (txn_state_moved_past(frame->under->state, scan->generation))
+ {
+ frame->result->tag = DOLTGRES_SCAN_TURN_MOVED;
+ frame->ok = true;
+ return;
+ }
+ if (scan->has_group)
+ doltgres_scan_Group_drop(&scan->group);
+ scan->has_group = false;
+ doltgres_scan_option_drop(&scan->seek);
+ frame->entry.scan = scan;
+ frame->entry.keyless = scan->keyed.len == 0;
+ frame->entry.entries = entries ? &scan->order : NULL;
+ frame->entry.way = way;
+ frame->entry.error = frame->error;
+ frame->stores->ops->with(frame->stores->self, frame->io, &scan->reader, doltgres_scan_advance_closure_1,
+ &frame->entry);
+ if (!frame->entry.ok)
+ return;
if (!frame->entry.has_entry && !frame->has_delta)
{
DoltgresScanPlace start = {.tag = DOLTGRES_SCAN_PLACE_START};
@@ -1817,7 +1916,10 @@ doltgres_scan_advance_body(void *frame_)
* the way a read goes and makes that key's group: the nearer of the base's
* next entry and the delta's next key with a version past the group's key,
* by their bytes, one group when they are the same key. With neither, the
- * scan starts over.
+ * scan starts over. It reads the delta first and, when an install moved the
+ * storage's base since the scan took its own (txn_state_moved_past),
+ * changes nothing and answers Moved; so the delta it pairs with its base is
+ * the delta before any newer base's drop.
*/
static bool
doltgres_scan_advance(HostIoIo *io, const DoltgresScanStores *stores, const DoltgresScanUnder *under,
@@ -1832,8 +1934,9 @@ doltgres_scan_advance(HostIoIo *io, const DoltgresScanStores *stores, const Dolt
}
/*
- * doltgres_scan_chosen_frame_t is chosen's state across its pad: the choice,
- * the delta's hold, and the key's versions, while live.
+ * doltgres_scan_chosen_frame_t is chosen's state across its pad: the
+ * choice, with where the run's split goes, the delta's hold, and the key's
+ * versions, while live, and whether an install moved the base.
*/
typedef struct doltgres_scan_chosen_frame_t
{
@@ -1841,6 +1944,8 @@ typedef struct doltgres_scan_chosen_frame_t
const DoltgresScanUnder *under;
DoltgresScanScan *scan;
DoltgresScanDirection way;
+ bool has_at;
+ size_t at;
bool delta_key;
StdVec key;
bool key_live;
@@ -1848,6 +1953,7 @@ typedef struct doltgres_scan_chosen_frame_t
DoltgresScanCopies copies;
HostDeltaHold hold;
bool hold_live;
+ bool *moved;
HostError *error;
bool ok;
} doltgres_scan_chosen_frame_t;
@@ -1871,13 +1977,15 @@ doltgres_scan_chosen_pad(void *frame_)
/*
* doltgres_scan_chosen_body reads the delta's versions of the key, when the
- * group takes the delta's key, and makes the group.
+ * group takes the delta's key, and, unless an install moved the base, moves
+ * the run's split and makes the group.
*/
static void
doltgres_scan_chosen_body(void *frame_)
{
doltgres_scan_chosen_frame_t *frame = frame_;
DoltgresScanScan *scan = frame->scan;
+ DoltgresScanDirection way = frame->way;
StdVec versions;
if (!host_delta_hold(frame->io, &frame->hold, frame->error))
@@ -1891,6 +1999,26 @@ doltgres_scan_chosen_body(void *frame_)
}
else
std_vec_new(&versions, sizeof(TxnMemtableVersionVersion), _Alignof(TxnMemtableVersionVersion));
+ *frame->moved = txn_state_moved_past(frame->under->state, scan->generation);
+ if (*frame->moved)
+ {
+ txn_memtable_version_versions_drop(&versions, 0);
+ frame->ok = true;
+ return;
+ }
+ doltgres_scan_option_drop(&scan->seek);
+ if (scan->has_run)
+ {
+ if (frame->has_at && (frame->delta_key || way == DOLTGRES_SCAN_DIRECTION_BACKWARD))
+ scan->run.split = frame->at + 1;
+ else if (frame->has_at)
+ scan->run.split = frame->at;
+ else if (way == DOLTGRES_SCAN_DIRECTION_FORWARD)
+ scan->run.split = scan->run.keys.len;
+ else
+ scan->run.split = 0;
+ }
+ doltgres_scan_took(&scan->place, frame->has_copies);
frame->key_live = false;
if (!doltgres_scan_grouped(frame->io, frame->under, &scan->epochs, &frame->key, frame->has_copies,
&frame->copies, &versions, &scan->group, frame->error))
@@ -1907,17 +2035,17 @@ doltgres_scan_chosen_body(void *frame_)
* doltgres_scan_chosen makes a compared table's next group from its
* candidates as chosen, which it takes, the delta's key's versions read
* again by the key, and moves the run's split past the keys at or before the
- * group's.
+ * group's. It reads the delta key's versions first and, when an install
+ * moved the storage's base since the scan took its own
+ * (txn_state_moved_past), changes nothing and answers moved, as advance
+ * answers Moved.
*/
static bool
doltgres_scan_chosen(HostIoIo *io, const DoltgresScanUnder *under, DoltgresScanScan *scan,
- DoltgresScanPicked *picked, DoltgresScanDirection way, HostError *error)
+ DoltgresScanPicked *picked, DoltgresScanDirection way, bool *moved, HostError *error)
{
doltgres_scan_chosen_frame_t frame = {.io = io, .under = under, .scan = scan, .way = way, .key_live = true,
- .error = error};
- bool has_at;
- size_t at = 0;
- bool delta_key;
+ .moved = moved, .error = error};
switch (picked->tag)
{
@@ -1925,15 +2053,14 @@ doltgres_scan_chosen(HostIoIo *io, const DoltgresScanUnder *under, DoltgresScanS
frame.key = picked->base_key;
frame.copies = picked->base_copies;
frame.has_copies = true;
- has_at = picked->has_before;
- at = picked->before;
- delta_key = false;
+ frame.has_at = picked->has_before;
+ frame.at = picked->before;
break;
case DOLTGRES_SCAN_PICKED_DELTA:
frame.key = picked->delta_key;
- has_at = true;
- at = picked->delta_at;
- delta_key = true;
+ frame.has_at = true;
+ frame.at = picked->delta_at;
+ frame.delta_key = true;
break;
case DOLTGRES_SCAN_PICKED_BOTH:
default:
@@ -1941,24 +2068,11 @@ doltgres_scan_chosen(HostIoIo *io, const DoltgresScanUnder *under, DoltgresScanS
frame.key = picked->delta_key;
frame.copies = picked->base_copies;
frame.has_copies = true;
- has_at = true;
- at = picked->delta_at;
- delta_key = true;
+ frame.has_at = true;
+ frame.at = picked->delta_at;
+ frame.delta_key = true;
break;
}
- if (scan->has_run)
- {
- if (has_at && (delta_key || way == DOLTGRES_SCAN_DIRECTION_BACKWARD))
- scan->run.split = at + 1;
- else if (has_at)
- scan->run.split = at;
- else if (way == DOLTGRES_SCAN_DIRECTION_FORWARD)
- scan->run.split = scan->run.keys.len;
- else
- scan->run.split = 0;
- }
- doltgres_scan_took(&scan->place, frame.has_copies);
- frame.delta_key = delta_key;
std_landing(&frame, doltgres_scan_chosen_body, doltgres_scan_chosen_pad);
doltgres_scan_chosen_pad(&frame);
return frame.ok;
@@ -2011,9 +2125,8 @@ doltgres_scan_gather_body(void *frame_)
bool has_at;
size_t at = 0;
DoltgresScanPicked picked = {0};
+ bool moved;
- if (seeked)
- doltgres_scan_option_drop(&scan->seek);
if (!scan->has_group && seeked && way == DOLTGRES_SCAN_DIRECTION_FORWARD)
{
has_at = run->split < run->keys.len;
@@ -2075,6 +2188,7 @@ doltgres_scan_gather_body(void *frame_)
doltgres_scan_Group_drop(&scan->group);
scan->has_group = false;
doltgres_scan_Place_set(&scan->place, start);
+ doltgres_scan_option_drop(&scan->seek);
frame->result->tag = DOLTGRES_SCAN_TURN_END;
frame->ok = true;
return;
@@ -2104,9 +2218,9 @@ doltgres_scan_gather_body(void *frame_)
picked.delta_at = frame->delta_at;
}
frame->entry.has_entry = frame->has_delta = false;
- if (!doltgres_scan_chosen(frame->io, frame->under, scan, &picked, way, frame->error))
+ if (!doltgres_scan_chosen(frame->io, frame->under, scan, &picked, way, &moved, frame->error))
return;
- frame->result->tag = DOLTGRES_SCAN_TURN_AGAIN;
+ frame->result->tag = moved ? DOLTGRES_SCAN_TURN_MOVED : DOLTGRES_SCAN_TURN_AGAIN;
frame->ok = true;
}
@@ -2182,6 +2296,15 @@ typedef struct doltgres_scan_TupleOptionVecU8VecVecU8_t
#define HOST_OWNED_WITH_T_DROP(place) DOLTGRES_SCAN_SEEK_KEYS_DROP(place)
#include "doltgres/host/owned_template.h"
+/* doltgres_scan_generation_closure_0 is a read's look in the record: the base generation the scan took its base at. */
+static bool
+doltgres_scan_generation_closure_0(void *captures, HostIoIo *io, DoltgresScanScan *scan, uint64 *result,
+ HostError *error)
+{
+ *result = scan->generation;
+ return true;
+}
+
/* doltgres_scan_turn_t is a read's turn's captures: the store's source, what the read is under, and its way. */
typedef struct doltgres_scan_turn_t
{
@@ -2259,13 +2382,15 @@ doltgres_scan_next_closure_2(void *captures, HostIoIo *io, DoltgresScanScan *sca
/*
* doltgres_scan_chose_t is the captures of a call that makes the chosen group
- * in the record: the choice, the read's state, and its way.
+ * in the record: the choice, the read's state, its way, and whether an
+ * install moved the base.
*/
typedef struct doltgres_scan_chose_t
{
DoltgresScanPicked picked;
const DoltgresScanUnder *under;
DoltgresScanDirection way;
+ bool moved;
} doltgres_scan_chose_t;
/* doltgres_scan_chose_drop frees the choice of a call that never ran. */
@@ -2281,7 +2406,7 @@ doltgres_scan_next_closure_3(void *captures, HostIoIo *io, DoltgresScanScan *sca
{
doltgres_scan_chose_t *chose = captures;
- return doltgres_scan_chosen(io, chose->under, scan, &chose->picked, chose->way, error);
+ return doltgres_scan_chosen(io, chose->under, scan, &chose->picked, chose->way, &chose->moved, error);
}
/*
@@ -2347,15 +2472,35 @@ doltgres_scan_next_body(void *frame_)
frame->ok = true;
return;
}
- if (step.tag == DOLTGRES_SCAN_TURN_RUN)
+ if (step.tag == DOLTGRES_SCAN_TURN_MOVED)
+ {
+ if (!doltgres_scan_reseated(frame->io, frame->stores, frame->under, frame->handle, frame->way,
+ frame->error))
+ return;
+ }
+ else if (step.tag == DOLTGRES_SCAN_TURN_RUN)
{
doltgres_scan_TupleOptionVecU8VecVecU8_t looked;
doltgres_scan_run_set_t set = {0};
+ uint64 generation;
frame->order = step.u.run;
frame->order_live = true;
if (!doltgres_scan_run(frame->io, frame->under, &frame->order, &frame->keys, frame->error))
return;
+ frame->keys_live = true;
+ if (!host_owned_TableDoltgresScanScan_withU64(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
+ doltgres_scan_generation_closure_0, NULL, NULL, &generation,
+ frame->error))
+ return;
+ if (txn_state_moved_past(frame->under->state, generation))
+ {
+ doltgres_scan_next_pad(frame);
+ if (!doltgres_scan_reseated(frame->io, frame->stores, frame->under, frame->handle, frame->way,
+ frame->error))
+ return;
+ continue;
+ }
frame->keys_live = false;
if (!host_owned_TableDoltgresScanScan_withTupleOptionVecU8VecVecU8(&DOLTGRES_SCAN_SCANS, frame->io,
frame->handle,
@@ -2393,6 +2538,10 @@ doltgres_scan_next_body(void *frame_)
doltgres_scan_chose_drop, frame->error))
return;
doltgres_scan_next_pad(frame);
+ if (chose.moved &&
+ !doltgres_scan_reseated(frame->io, frame->stores, frame->under, frame->handle, frame->way,
+ frame->error))
+ return;
}
}
}
@@ -2544,7 +2693,7 @@ doltgres_scan_run_body(void *frame_)
* version, in byte order, and sorts them by the table's order with ties
* broken by their bytes, so that keys its comparator finds equal make
* adjacent groups; it runs with no record lent, since the comparator is user
- * code.
+ * code. It pauses at doltgres-scan-run first, so a case counts its reads.
*/
static bool
doltgres_scan_run(HostIoIo *io, const DoltgresScanUnder *under, const TreesKeyOrder *order, StdVec *result,
@@ -2552,6 +2701,8 @@ doltgres_scan_run(HostIoIo *io, const DoltgresScanUnder *under, const TreesKeyOr
{
doltgres_scan_run_frame_t frame = {.io = io, .under = under, .order = order, .result = result, .error = error};
+ if (!host_pause(HOST_SHARED_IO_PAUSES(io), "doltgres-scan-run", error))
+ return false;
std_landing(&frame, doltgres_scan_run_body, doltgres_scan_run_pad);
doltgres_scan_run_pad(&frame);
return frame.ok;
@@ -3146,7 +3297,7 @@ doltgres_scan_grouped(HostIoIo *io, const DoltgresScanUnder *under, DoltgresScan
/*
* doltgres_scan_rescan_closure_0 is rescan's call: the scan's place, group,
- * run, seek, page, sample, and unit cleared.
+ * run, seek, page, sample, unit, and kept run cleared.
*/
static bool
doltgres_scan_rescan_closure_0(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
@@ -3172,7 +3323,12 @@ doltgres_scan_rescan_closure_0(void *captures, HostIoIo *io, DoltgresScanScan *s
scan->has_written = false;
scan->records = false;
if (scan->has_parallel)
+ {
scan->parallel.has_unit = scan->parallel.has_ranged = false;
+ if (scan->parallel.has_kept)
+ doltgres_scan_vecs_drop(&scan->parallel.kept);
+ scan->parallel.has_kept = false;
+ }
return true;
}
@@ -3188,7 +3344,11 @@ doltgres_scan_scan_rescan(HostIoIo *io, HostOwnedHandle handle, HostError *error
NULL, NULL, error);
}
-/* doltgres_scan_placed_captures_t is placed's arguments in a pin scope: the tree, the key, the way, and the order. */
+/*
+ * doltgres_scan_placed_captures_t is placed's arguments in a pin scope: the
+ * tree, the key, the way, the order, and whether the key is a group's, which
+ * a newer base's place takes when the tree holds it (reseated).
+ */
typedef struct doltgres_scan_placed_captures_t
{
TreesStoreTree tree;
@@ -3196,6 +3356,7 @@ typedef struct doltgres_scan_placed_captures_t
size_t from_len;
DoltgresScanDirection way;
const TreesKeyOrder *order;
+ bool grouped;
} doltgres_scan_placed_captures_t;
/* doltgres_scan_placed_frame_t is placed's state across its pad: its cursor, while live. */
@@ -3273,11 +3434,15 @@ doltgres_scan_placed_body(void *frame_)
}
if (trees_cursor_Cursor_is_valid(&frame->cursor))
{
+ StdBytes key = trees_cursor_Cursor_key(&frame->cursor);
+ bool taken = captures->grouped &&
+ std_bytes_Ord_cmp(key.ptr, key.len, captures->from, captures->from_len) == STD_ORDERING_EQUAL;
+
frame->result->tag = DOLTGRES_SCAN_PLACE_AT;
frame->cursor_live = false;
trees_cursor_Cursor_keep(&frame->cursor, &frame->result->u.at.cursor);
frame->result->u.at.way = captures->way;
- frame->result->u.at.taken = false;
+ frame->result->u.at.taken = taken;
}
else
*frame->result = doltgres_scan_off(captures->way);
@@ -3287,7 +3452,9 @@ doltgres_scan_placed_body(void *frame_)
/*
* doltgres_scan_placed answers a base's place at a key in its tree's order:
* its first entry not below the key going forward, or its last not above it
- * going backward, or off its end that way.
+ * going backward, or off its end that way; at a group's key the tree holds,
+ * the place is taken, so the next advance steps past it whichever way it
+ * goes, as an advance leaves a nearer entry its group didn't take untaken.
*/
static bool
doltgres_scan_placed(void *captures, HostIoIo *io, TreesPinPin *pin, DoltgresScanPlace *result, HostError *error)
@@ -3325,116 +3492,511 @@ doltgres_scan_place_read(void *captures, HostIoIo *io, TreesStoreNodeStore store
read->error);
}
-/* doltgres_scan_seek_closure_1_0 is seek's store call, the base's place at the key found under the scan's reader. */
-static void
-doltgres_scan_seek_closure_1_0(void *captures, HostIoIo *io, TreesStoreNodeStore store)
+/* doltgres_scan_Trees_of returns the trees of a base, or of none (NULL), read at a base generation or after it. */
+DoltgresScanTrees
+doltgres_scan_Trees_of(const TxnBaseBase *base, uint64 generation)
{
- doltgres_scan_place_read(captures, io, store);
+ DoltgresScanTrees trees = {.generation = generation};
+
+ if (base == NULL)
+ return trees;
+ trees.has_root = base->has_root;
+ if (trees.has_root)
+ trees.root = base->root;
+ trees.has_tuple_ids = base->has_tuple_ids;
+ if (trees.has_tuple_ids)
+ trees.tuple_ids = base->tuple_ids;
+ trees.folded = base->read;
+ return trees;
}
/*
- * doltgres_scan_seek_compared_closure_1 is seek_compared's store call, the
- * base's place at the key found under its own reader.
+ * DoltgresScanNewest is what a read whose base an install moved reads of its
+ * storage's newest base: whether the generation moved, the base, none for a
+ * storage with no base, the generation it read it at, and its epochs, each
+ * while live.
*/
+typedef struct DoltgresScanNewest
+{
+ bool moved;
+ bool some;
+ TxnBaseBase base;
+ uint64 now;
+ DoltgresScanEpochs epochs;
+ bool epochs_live;
+} DoltgresScanNewest;
+
+/* doltgres_scan_Newest_drop frees a newest base's read, its epochs and its base. */
static void
-doltgres_scan_seek_compared_closure_1(void *captures, HostIoIo *io, TreesStoreNodeStore store)
+doltgres_scan_Newest_drop(DoltgresScanNewest *self)
{
- doltgres_scan_place_read(captures, io, store);
+ if (self->epochs_live)
+ doltgres_scan_Epochs_drop(&self->epochs);
+ self->epochs_live = false;
+ if (self->some)
+ txn_base_Base_drop(&self->base);
+ self->some = false;
}
-/* doltgres_scan_OptionKeyOrder_t is an Option. */
-typedef struct doltgres_scan_OptionKeyOrder_t
+/*
+ * doltgres_scan_newest reads a storage's newest base for a snapshot to read,
+ * as readable gives it, with the generation it read it at and its epochs,
+ * its chain read through a store, once an install moved the generation past
+ * one a read took its base at, and nothing while it hasn't, which costs one
+ * load of the generation.
+ */
+static bool
+doltgres_scan_newest(HostIoIo *io, TreesStoreNodeStore store, const DoltgresScanUnder *under, uint64 generation,
+ DoltgresScanNewest *result, HostError *error)
{
- bool has;
- TreesKeyOrder v;
-} doltgres_scan_OptionKeyOrder_t;
+ uint32 epoch;
+ bool has_chain;
+ TreesStoreTree chain;
-/* DOLTGRES_SCAN_OPTION_KEY_ORDER_DROP drops an Option a call left. */
-#define DOLTGRES_SCAN_OPTION_KEY_ORDER_DROP(place) \
- do { \
- if ((place)->has) \
- trees_KeyOrder_drop(&(place)->v); \
- } while (0)
+ if (!txn_state_readable_since(io, under->state, under->snapshot, generation, &result->moved, &result->some,
+ &result->base, &result->now, error))
+ return false;
+ if (!result->moved)
+ return true;
+ doltgres_scan_Epochs_based(result->some ? &result->base : NULL, &epoch, &has_chain, &chain);
+ if (!doltgres_scan_Epochs_of(io, store, epoch, has_chain, chain, &result->epochs, error))
+ return false;
+ result->epochs_live = true;
+ return true;
+}
-/* Scan's Table's with at Option, for seek. */
-#define HOST_OWNED_SCOPE static
-#define HOST_OWNED_DECLARE
-#define HOST_OWNED_DEFINE
-#define HOST_OWNED_PREFIX doltgres_scan_Scan
-#define HOST_OWNED_RS DoltgresScanScan
-#define HOST_OWNED_R DoltgresScanScan
-#define HOST_OWNED_R_DROP doltgres_scan_Scan_drop
-#define HOST_OWNED_KIND 0
-#define HOST_OWNED_TABLE_FN doltgres_scan_Scan_Owned_table
-#define HOST_OWNED_WITH_TS OptionKeyOrder
-#define HOST_OWNED_WITH_T doltgres_scan_OptionKeyOrder_t
-#define HOST_OWNED_WITH_T_DROP(place) DOLTGRES_SCAN_OPTION_KEY_ORDER_DROP(place)
-#include "doltgres/host/owned_template.h"
+/* doltgres_scan_Trees_newer_frame_t is Trees_newer's state across its pad: the newest base's read. */
+typedef struct doltgres_scan_Trees_newer_frame_t
+{
+ DoltgresScanTrees *self;
+ HostIoIo *io;
+ TreesStoreNodeStore store;
+ const DoltgresScanUnder *under;
+ DoltgresScanEpochs *epochs;
+ DoltgresScanNewest newest;
+ HostError *error;
+ bool ok;
+} doltgres_scan_Trees_newer_frame_t;
-/* doltgres_scan_TupleOptionTreeOptionRun_t is an (Option, Option): a scan's tree and its run, taken. */
-typedef struct doltgres_scan_TupleOptionTreeOptionRun_t
+/* doltgres_scan_Trees_newer_pad frees the newest base's read. */
+static void
+doltgres_scan_Trees_newer_pad(void *frame_)
{
- bool has_tree;
- TreesStoreTree tree;
- bool has_run;
- DoltgresScanRun run;
-} doltgres_scan_TupleOptionTreeOptionRun_t;
+ doltgres_scan_Newest_drop(&((doltgres_scan_Trees_newer_frame_t *) frame_)->newest);
+}
-/* DOLTGRES_SCAN_TREE_RUN_DROP drops the run a call left. */
-#define DOLTGRES_SCAN_TREE_RUN_DROP(place) \
- do { \
- if ((place)->has_run) \
- doltgres_scan_vecs_drop(&(place)->run.keys); \
- } while (0)
+/* doltgres_scan_Trees_newer_body reads the newest base and moves the read's trees and epochs to it. */
+static void
+doltgres_scan_Trees_newer_body(void *frame_)
+{
+ doltgres_scan_Trees_newer_frame_t *frame = frame_;
+ DoltgresScanNewest *newest = &frame->newest;
-/* Scan's Table's with at (Option, Option), for seek_compared. */
-#define HOST_OWNED_SCOPE static
-#define HOST_OWNED_DECLARE
-#define HOST_OWNED_DEFINE
-#define HOST_OWNED_PREFIX doltgres_scan_Scan
-#define HOST_OWNED_RS DoltgresScanScan
-#define HOST_OWNED_R DoltgresScanScan
-#define HOST_OWNED_R_DROP doltgres_scan_Scan_drop
-#define HOST_OWNED_KIND 0
-#define HOST_OWNED_TABLE_FN doltgres_scan_Scan_Owned_table
-#define HOST_OWNED_WITH_TS TupleOptionTreeOptionRun
-#define HOST_OWNED_WITH_T doltgres_scan_TupleOptionTreeOptionRun_t
-#define HOST_OWNED_WITH_T_DROP(place) DOLTGRES_SCAN_TREE_RUN_DROP(place)
-#include "doltgres/host/owned_template.h"
+ if (!doltgres_scan_newest(frame->io, frame->store, frame->under, frame->self->generation, newest, frame->error))
+ return;
+ if (newest->moved)
+ {
+ newest->epochs_live = false;
+ if (!doltgres_scan_Epochs_moved(frame->epochs, &newest->epochs, frame->error))
+ return;
+ *frame->self = doltgres_scan_Trees_of(newest->some ? &newest->base : NULL, newest->now);
+ }
+ frame->ok = true;
+}
-/* doltgres_scan_seek_closure_0 is seek's look in the record: the order cloned out of it when a comparator orders it. */
+/*
+ * doltgres_scan_Trees_newer moves the trees of a read whose base an install
+ * moved to its storage's newest base, as readable gives it for the read's
+ * snapshot, with the generation it read it at, and the read's epochs to
+ * that base's (Epochs_moved), leaving them as they are while the base
+ * hasn't moved.
+ */
static bool
-doltgres_scan_seek_closure_0(void *captures, HostIoIo *io, DoltgresScanScan *scan,
- doltgres_scan_OptionKeyOrder_t *result, HostError *error)
+doltgres_scan_Trees_newer(DoltgresScanTrees *self, HostIoIo *io, TreesStoreNodeStore store,
+ const DoltgresScanUnder *under, DoltgresScanEpochs *epochs, HostError *error)
{
- result->has = !trees_KeyOrder_is_bytes(&scan->order);
- if (result->has)
- trees_KeyOrder_Clone_clone(&scan->order, &result->v);
- return true;
+ doltgres_scan_Trees_newer_frame_t frame = {.self = self, .io = io, .store = store, .under = under,
+ .epochs = epochs, .error = error};
+
+ std_landing(&frame, doltgres_scan_Trees_newer_body, doltgres_scan_Trees_newer_pad);
+ doltgres_scan_Trees_newer_pad(&frame);
+ return frame.ok;
}
-/* doltgres_scan_seek_t is a seek's captures: the store's source, the key, and the way. */
-typedef struct doltgres_scan_seek_t
+/*
+ * DoltgresScanRebased is Rebased: what a move of a scan to its storage's
+ * newest base leaves its place to go on from: whether it moved, the new
+ * base's tree, the key a read goes on from, with whether it is its group's
+ * key, gone past, or a seek's, gone to, none for a scan not begun, and the
+ * table's order, cloned for the comparisons outside the record, while live.
+ */
+typedef struct DoltgresScanRebased
{
- const DoltgresScanStores *stores;
- const uint8 *from;
- size_t from_len;
- DoltgresScanDirection way;
-} doltgres_scan_seek_t;
+ bool moved;
+ bool has_tree;
+ TreesStoreTree tree;
+ doltgres_OptionVecU8_t from;
+ bool grouped;
+ TreesKeyOrder order;
+ bool order_live;
+} DoltgresScanRebased;
-/* doltgres_scan_seek_records returns whether a seek's key is an index's record. */
-static bool
-doltgres_scan_seek_records(const uint8 *from, size_t from_len)
+/* doltgres_scan_Rebased_drop frees a move's key and order, each while live. */
+static void
+doltgres_scan_Rebased_drop(DoltgresScanRebased *self)
{
- return from_len > 0 && from[0] == TXN_ENTRY_RECORD;
+ doltgres_scan_option_drop(&self->from);
+ if (self->order_live)
+ trees_KeyOrder_drop(&self->order);
+ self->order_live = self->moved = false;
}
-/* doltgres_scan_seek_closure_1 is seek's placing in the record of a byte-ordered tree. */
-static bool
-doltgres_scan_seek_closure_1(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
+/* doltgres_scan_rebased_put_t is the captures of rebased's put in the record: the newest base's read and the move. */
+typedef struct doltgres_scan_rebased_put_t
{
- doltgres_scan_seek_t *seek = captures;
- DoltgresScanPlace place = doltgres_scan_off(seek->way);
+ DoltgresScanNewest *newest;
+ DoltgresScanRebased *result;
+} doltgres_scan_rebased_put_t;
+
+/*
+ * doltgres_scan_rebased_closure_0 is rebased's put in the record: the newest
+ * base's trees, read point, epochs, and generation, the tid range read's
+ * delta tuple ids cleared, and the key the scan goes on from.
+ */
+static bool
+doltgres_scan_rebased_closure_0(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
+{
+ doltgres_scan_rebased_put_t *put = captures;
+ DoltgresScanNewest *newest = put->newest;
+ DoltgresScanRebased *result = put->result;
+ DoltgresScanTrees trees = doltgres_scan_Trees_of(newest->some ? &newest->base : NULL, newest->now);
+
+ newest->epochs_live = false;
+ if (!doltgres_scan_Epochs_moved(&scan->epochs, &newest->epochs, error))
+ return false;
+ scan->has_tree = trees.has_root;
+ scan->tree = trees.root;
+ scan->has_tuple_ids = trees.has_tuple_ids;
+ scan->tuple_ids = trees.tuple_ids;
+ scan->folded = trees.folded;
+ scan->generation = trees.generation;
+ if (scan->has_written)
+ std_vec_free_Checked(&scan->written, DOLTGRES_SCAN_U64);
+ scan->has_written = false;
+ result->moved = true;
+ result->has_tree = scan->has_tree;
+ result->tree = scan->tree;
+ result->grouped = scan->has_group;
+ if (scan->has_group)
+ std_vec_clone(&result->from.v, &scan->group.key, DOLTGRES_SCAN_BYTES);
+ else if (scan->seek.has)
+ std_vec_clone(&result->from.v, &scan->seek.v, DOLTGRES_SCAN_BYTES);
+ result->from.has = scan->has_group || scan->seek.has;
+ trees_KeyOrder_Clone_clone(&scan->order, &result->order);
+ result->order_live = true;
+ return true;
+}
+
+/* doltgres_scan_rebased_frame_t is rebased's state across its pad: the newest base's read. */
+typedef struct doltgres_scan_rebased_frame_t
+{
+ HostIoIo *io;
+ TreesStoreNodeStore store;
+ const DoltgresScanUnder *under;
+ HostOwnedHandle handle;
+ DoltgresScanNewest newest;
+ DoltgresScanRebased *result;
+ HostError *error;
+ bool ok;
+} doltgres_scan_rebased_frame_t;
+
+/* doltgres_scan_rebased_pad frees the newest base's read. */
+static void
+doltgres_scan_rebased_pad(void *frame_)
+{
+ doltgres_scan_Newest_drop(&((doltgres_scan_rebased_frame_t *) frame_)->newest);
+}
+
+/* doltgres_scan_rebased_body reads the newest base once the generation moved and puts it in the record. */
+static void
+doltgres_scan_rebased_body(void *frame_)
+{
+ doltgres_scan_rebased_frame_t *frame = frame_;
+ doltgres_scan_rebased_put_t put = {&frame->newest, frame->result};
+ uint64 generation;
+
+ *frame->result = (DoltgresScanRebased)
+ {
+ .moved = false
+ };
+ if (!host_owned_TableDoltgresScanScan_withU64(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
+ doltgres_scan_generation_closure_0, NULL, NULL, &generation,
+ frame->error) ||
+ !doltgres_scan_newest(frame->io, frame->store, frame->under, generation, &frame->newest, frame->error))
+ return;
+ frame->ok = !frame->newest.moved ||
+ host_owned_TableDoltgresScanScan_withUnit(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
+ doltgres_scan_rebased_closure_0, &put, NULL, frame->error);
+}
+
+/*
+ * doltgres_scan_rebased moves a scan to its storage's newest base when an
+ * install moved the base since the scan took its own (bugs rows 240 and
+ * 241), at a read's start and never inside one: the record takes the new
+ * base's trees, read point, epochs (Epochs_moved), and generation, the
+ * epochs' chain read through a store, and a tid range read's delta tuple ids
+ * are collected again at that point by its next read. Every live snapshot
+ * may read the newest base (txn_state_readable), and its read point passes
+ * over the delta's versions it holds, so a read going on from its group's
+ * key reads nothing twice and misses nothing. It answers what the caller's
+ * place goes on from, or that the base hasn't moved, which costs one load of
+ * the generation.
+ */
+static bool
+doltgres_scan_rebased(HostIoIo *io, TreesStoreNodeStore store, const DoltgresScanUnder *under,
+ HostOwnedHandle handle, DoltgresScanRebased *result, HostError *error)
+{
+ doltgres_scan_rebased_frame_t frame = {.io = io, .store = store, .under = under, .handle = handle,
+ .result = result, .error = error};
+
+ std_landing(&frame, doltgres_scan_rebased_body, doltgres_scan_rebased_pad);
+ doltgres_scan_rebased_pad(&frame);
+ return frame.ok;
+}
+
+/*
+ * doltgres_scan_rebase_read_t is a move to the newest base under a reader, a
+ * Stores::with call's captures: rebased's arguments and its answer.
+ */
+typedef struct doltgres_scan_rebase_read_t
+{
+ const DoltgresScanUnder *under;
+ HostOwnedHandle handle;
+ DoltgresScanRebased *result;
+ HostError *error;
+ bool ok;
+} doltgres_scan_rebase_read_t;
+
+/* doltgres_scan_rebase_read moves the scan to the newest base, reseated's and next_parallel's store calls' body. */
+static void
+doltgres_scan_rebase_read(void *captures, HostIoIo *io, TreesStoreNodeStore store)
+{
+ doltgres_scan_rebase_read_t *read = captures;
+
+ read->ok = doltgres_scan_rebased(io, store, read->under, read->handle, read->result, read->error);
+}
+
+/* doltgres_scan_reseated_closure_0 is reseated's put of the place in the record. */
+static bool
+doltgres_scan_reseated_closure_0(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
+{
+ doltgres_scan_Place_set(&scan->place, *(DoltgresScanPlace *) captures);
+ return true;
+}
+
+/* doltgres_scan_place_put_drop drops the place of a put that never ran. */
+static void
+doltgres_scan_place_put_drop(void *captures)
+{
+ doltgres_scan_Place_drop(captures);
+}
+
+/* doltgres_scan_reseated_frame_t is reseated's state across its pad: its reader and the move, while live. */
+typedef struct doltgres_scan_reseated_frame_t
+{
+ HostIoIo *io;
+ const DoltgresScanStores *stores;
+ const DoltgresScanUnder *under;
+ HostOwnedHandle handle;
+ DoltgresScanDirection way;
+ HostSharedReader reading;
+ bool reading_live;
+ DoltgresScanRebased rebasing;
+ DoltgresScanPlace place;
+ HostError *error;
+ bool ok;
+} doltgres_scan_reseated_frame_t;
+
+/* doltgres_scan_reseated_pad drops what reseated holds. */
+static void
+doltgres_scan_reseated_pad(void *frame_)
+{
+ doltgres_scan_reseated_frame_t *frame = frame_;
+
+ doltgres_scan_Rebased_drop(&frame->rebasing);
+ if (frame->reading_live)
+ host_shared_Reader_drop(&frame->reading);
+ frame->reading_live = false;
+}
+
+/* doltgres_scan_reseated_body moves the scan to the newest base and puts its place in the new base's tree. */
+static void
+doltgres_scan_reseated_body(void *frame_)
+{
+ doltgres_scan_reseated_frame_t *frame = frame_;
+ DoltgresScanRebased *rebasing = &frame->rebasing;
+ doltgres_scan_rebase_read_t rebase = {frame->under, frame->handle, rebasing, frame->error};
+
+ if (!host_shared_read(frame->io, &frame->reading, frame->error))
+ return;
+ frame->reading_live = true;
+ frame->stores->ops->with(frame->stores->self, frame->io, &frame->reading, doltgres_scan_rebase_read, &rebase);
+ if (!rebase.ok)
+ return;
+ if (!rebasing->moved)
+ {
+ frame->ok = true;
+ return;
+ }
+ if (!rebasing->from.has)
+ frame->place.tag = DOLTGRES_SCAN_PLACE_START;
+ else if (rebasing->has_tree)
+ {
+ doltgres_scan_place_read_t read = {{rebasing->tree, rebasing->from.v.ptr, rebasing->from.v.len, frame->way,
+ &rebasing->order, rebasing->grouped}, .error = frame->error};
+
+ frame->stores->ops->with(frame->stores->self, frame->io, &frame->reading, doltgres_scan_place_read, &read);
+ if (!read.ok)
+ return;
+ frame->place = read.place;
+ }
+ else
+ frame->place = doltgres_scan_off(frame->way);
+ frame->ok = host_owned_TableDoltgresScanScan_withUnit(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
+ doltgres_scan_reseated_closure_0, &frame->place,
+ doltgres_scan_place_put_drop, frame->error);
+}
+
+/*
+ * doltgres_scan_reseated moves a scan whose read found an install moved its
+ * storage's base (txn_state_moved_past) to the newest base (rebased) and
+ * puts its place in the new base's tree, outside the record, since a
+ * compared tree's order runs user code: past its group's key (placed's
+ * grouped place), at a seek's key, or, for a scan not begun, whose read may
+ * have stepped its place in the old tree, at the new tree's start.
+ */
+static bool
+doltgres_scan_reseated(HostIoIo *io, const DoltgresScanStores *stores, const DoltgresScanUnder *under,
+ HostOwnedHandle handle, DoltgresScanDirection way, HostError *error)
+{
+ doltgres_scan_reseated_frame_t frame = {.io = io, .stores = stores, .under = under, .handle = handle,
+ .way = way, .error = error};
+
+ std_landing(&frame, doltgres_scan_reseated_body, doltgres_scan_reseated_pad);
+ doltgres_scan_reseated_pad(&frame);
+ return frame.ok;
+}
+
+/* doltgres_scan_seek_closure_1_0 is seek's store call, the base's place at the key found under the scan's reader. */
+static void
+doltgres_scan_seek_closure_1_0(void *captures, HostIoIo *io, TreesStoreNodeStore store)
+{
+ doltgres_scan_place_read(captures, io, store);
+}
+
+/*
+ * doltgres_scan_seek_compared_closure_1 is seek_compared's store call, the
+ * base's place at the key found under its own reader.
+ */
+static void
+doltgres_scan_seek_compared_closure_1(void *captures, HostIoIo *io, TreesStoreNodeStore store)
+{
+ doltgres_scan_place_read(captures, io, store);
+}
+
+/* doltgres_scan_OptionKeyOrder_t is an Option. */
+typedef struct doltgres_scan_OptionKeyOrder_t
+{
+ bool has;
+ TreesKeyOrder v;
+} doltgres_scan_OptionKeyOrder_t;
+
+/* DOLTGRES_SCAN_OPTION_KEY_ORDER_DROP drops an Option a call left. */
+#define DOLTGRES_SCAN_OPTION_KEY_ORDER_DROP(place) \
+ do { \
+ if ((place)->has) \
+ trees_KeyOrder_drop(&(place)->v); \
+ } while (0)
+
+/* Scan's Table's with at Option, for seek. */
+#define HOST_OWNED_SCOPE static
+#define HOST_OWNED_DECLARE
+#define HOST_OWNED_DEFINE
+#define HOST_OWNED_PREFIX doltgres_scan_Scan
+#define HOST_OWNED_RS DoltgresScanScan
+#define HOST_OWNED_R DoltgresScanScan
+#define HOST_OWNED_R_DROP doltgres_scan_Scan_drop
+#define HOST_OWNED_KIND 0
+#define HOST_OWNED_TABLE_FN doltgres_scan_Scan_Owned_table
+#define HOST_OWNED_WITH_TS OptionKeyOrder
+#define HOST_OWNED_WITH_T doltgres_scan_OptionKeyOrder_t
+#define HOST_OWNED_WITH_T_DROP(place) DOLTGRES_SCAN_OPTION_KEY_ORDER_DROP(place)
+#include "doltgres/host/owned_template.h"
+
+/* doltgres_scan_TupleOptionTreeOptionRun_t is an (Option, Option): a scan's tree and its run, taken. */
+typedef struct doltgres_scan_TupleOptionTreeOptionRun_t
+{
+ bool has_tree;
+ TreesStoreTree tree;
+ bool has_run;
+ DoltgresScanRun run;
+} doltgres_scan_TupleOptionTreeOptionRun_t;
+
+/* DOLTGRES_SCAN_TREE_RUN_DROP drops the run a call left. */
+#define DOLTGRES_SCAN_TREE_RUN_DROP(place) \
+ do { \
+ if ((place)->has_run) \
+ doltgres_scan_vecs_drop(&(place)->run.keys); \
+ } while (0)
+
+/* Scan's Table's with at (Option, Option), for seek_compared. */
+#define HOST_OWNED_SCOPE static
+#define HOST_OWNED_DECLARE
+#define HOST_OWNED_DEFINE
+#define HOST_OWNED_PREFIX doltgres_scan_Scan
+#define HOST_OWNED_RS DoltgresScanScan
+#define HOST_OWNED_R DoltgresScanScan
+#define HOST_OWNED_R_DROP doltgres_scan_Scan_drop
+#define HOST_OWNED_KIND 0
+#define HOST_OWNED_TABLE_FN doltgres_scan_Scan_Owned_table
+#define HOST_OWNED_WITH_TS TupleOptionTreeOptionRun
+#define HOST_OWNED_WITH_T doltgres_scan_TupleOptionTreeOptionRun_t
+#define HOST_OWNED_WITH_T_DROP(place) DOLTGRES_SCAN_TREE_RUN_DROP(place)
+#include "doltgres/host/owned_template.h"
+
+/* doltgres_scan_seek_closure_0 is seek's look in the record: the order cloned out of it when a comparator orders it. */
+static bool
+doltgres_scan_seek_closure_0(void *captures, HostIoIo *io, DoltgresScanScan *scan,
+ doltgres_scan_OptionKeyOrder_t *result, HostError *error)
+{
+ result->has = !trees_KeyOrder_is_bytes(&scan->order);
+ if (result->has)
+ trees_KeyOrder_Clone_clone(&scan->order, &result->v);
+ return true;
+}
+
+/* doltgres_scan_seek_t is a seek's captures: the store's source, the key, and the way. */
+typedef struct doltgres_scan_seek_t
+{
+ const DoltgresScanStores *stores;
+ const uint8 *from;
+ size_t from_len;
+ DoltgresScanDirection way;
+} doltgres_scan_seek_t;
+
+/* doltgres_scan_seek_records returns whether a seek's key is an index's record. */
+static bool
+doltgres_scan_seek_records(const uint8 *from, size_t from_len)
+{
+ return from_len > 0 && from[0] == TXN_ENTRY_RECORD;
+}
+
+/* doltgres_scan_seek_closure_1 is seek's placing in the record of a byte-ordered tree. */
+static bool
+doltgres_scan_seek_closure_1(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
+{
+ doltgres_scan_seek_t *seek = captures;
+ DoltgresScanPlace place = doltgres_scan_off(seek->way);
if (scan->has_group)
doltgres_scan_Group_drop(&scan->group);
@@ -4082,6 +4644,44 @@ doltgres_scan_resolve_closure_0(uint64 tuple_id)
return host_Error_fork((const uint8 *) "XX001", &text);
}
+/*
+ * doltgres_scan_resolved_seek puts the frame's cursor at the pair's key in
+ * its tree: in the memo's kept leaf when the memo kept a cursor sought from
+ * the same root and that leaf holds the key, else by a seek from the root,
+ * answering whether the tree holds the key.
+ */
+static bool
+doltgres_scan_resolved_seek(doltgres_scan_cursor_frame_t *frame, DoltgresScanPaired *paired, bool *at)
+{
+ DoltgresScanResolved *resolved = paired->resolved;
+ ChunksErrorError failed;
+
+ *at = false;
+ if (resolved->has_kept)
+ {
+ bool same = resolved->kept.path.len > 0 &&
+ trees_store_Tree_PartialEq_eq(&STD_VEC_AT(&resolved->kept.path, TreesCursorFrame, 0).tree, &paired->root);
+
+ resolved->has_kept = false;
+ if (!same)
+ trees_cursor_KeptCursor_drop(&resolved->kept);
+ else
+ {
+ if (!trees_cursor_KeptCursor_resume(&resolved->kept, frame->io, frame->pin, &frame->cursor, &failed))
+ return doltgres_scan_storage_failed(&failed, frame->error);
+ frame->cursor_live = true;
+ if (!trees_cursor_Cursor_find_in_node(&frame->cursor, frame->io, paired->key.ptr, paired->key.len,
+ &paired->order, at, &failed))
+ return doltgres_scan_storage_failed(&failed, frame->error);
+ if (*at)
+ return true;
+ frame->cursor_live = false;
+ trees_cursor_Cursor_drop(&frame->cursor);
+ }
+ }
+ return doltgres_scan_seek_exact(frame, &paired->root, paired->key.ptr, paired->key.len, &paired->order, at);
+}
+
/* doltgres_scan_resolve_closure_1_body seeks the pair's key and makes the row of its copy that holds the tuple id. */
static void
doltgres_scan_resolve_closure_1_body(void *frame_)
@@ -4093,49 +4693,43 @@ doltgres_scan_resolve_closure_1_body(void *frame_)
bool at;
uint64 count;
StdBytes tuple;
- const ChunksTrailerStamp *stamp = NULL;
+ uint64 copies;
+ bool some;
+ ChunksTrailerStamp stamp;
- if (!doltgres_scan_seek_exact(frame, &paired->root, paired->key.ptr, paired->key.len, &paired->order, &at))
+ if (!doltgres_scan_resolved_seek(frame, paired, &at))
return;
if (!at)
{
*frame->error = doltgres_scan_resolve_closure_0(paired->tuple_id);
return;
}
- if (!trees_cursor_Cursor_stamps(&frame->cursor, frame->io, frame->pin, &frame->stamps, &failed))
+ if (!trees_cursor_LeafStamps_stamp_of(&paired->resolved->stamps, frame->io, frame->pin, &frame->cursor, paired->tuple_id,
+ &copies, &some, &stamp, &frame->values, &failed))
{
doltgres_scan_storage_failed(&failed, frame->error);
return;
}
- frame->stamps_live = true;
+ frame->values_live = true;
if (!doltgres_scan_copies(&frame->cursor, paired->keyed.len == 0, &count, &tuple, frame->error))
return;
- if (frame->stamps.len != count)
+ if (copies != count)
{
*frame->error = doltgres_scan_uneven();
return;
}
- for (size_t i = 0; i < frame->stamps.len && stamp == NULL; i++)
- if (STD_VEC_AT(&frame->stamps, ChunksTrailerStamp, i).tuple == paired->tuple_id)
- stamp = &STD_VEC_AT(&frame->stamps, ChunksTrailerStamp, i);
- if (stamp == NULL)
+ if (!some)
{
*frame->error = doltgres_scan_resolve_closure_0(paired->tuple_id);
return;
}
- if (!trees_cursor_Cursor_values(&frame->cursor, frame->io, frame->pin, &frame->values, &failed))
- {
- doltgres_scan_storage_failed(&failed, frame->error);
- return;
- }
- frame->values_live = true;
std_slice_to_vec_Checked(&frame->tuple, tuple.ptr, tuple.len, DOLTGRES_SCAN_BYTES);
frame->tuple_live = true;
frame->bytes_live = true;
doltgres_scan_value_bytes(frame->values.ptr, frame->values.len, &frame->bytes);
*row = (DoltgresScanRow)
{
- .table = paired->table, .stamp = *stamp, .tuple = frame->tuple, .values = frame->bytes
+ .table = paired->table, .stamp = stamp, .tuple = frame->tuple, .values = frame->bytes
};
frame->tuple_live = frame->bytes_live = false;
row->transform = paired->transform;
@@ -4144,6 +4738,9 @@ doltgres_scan_resolve_closure_1_body(void *frame_)
std_vec_new(&paired->transform, DOLTGRES_SCAN_BYTES);
std_vec_new(&paired->keyed, DOLTGRES_SCAN_BYTES);
std_vec_new(&paired->key, DOLTGRES_SCAN_BYTES);
+ frame->cursor_live = false;
+ trees_cursor_Cursor_keep(&frame->cursor, &paired->resolved->kept);
+ paired->resolved->has_kept = true;
frame->ok = true;
}
@@ -4196,6 +4793,7 @@ typedef struct doltgres_scan_fetch_frame_t
TreesStoreNodeStore store;
const DoltgresScanUnder *under;
DoltgresScanEpochs *epochs;
+ DoltgresScanResolved *resolved;
DoltgresScanTrees trees;
DoltgresScanLayout layout;
bool layout_live;
@@ -4279,29 +4877,46 @@ doltgres_scan_fetch_body(void *frame_)
DoltgresScanPaired paired;
*frame->has_result = false;
- if (!txn_memtable_tuples_get(frame->io, under->state, tuple, &frame->written.has, &frame->written.v,
- frame->error))
- return;
- if (!frame->written.has && frame->trees.has_tuple_ids &&
- !doltgres_scan_pair(frame->io, frame->store, frame->trees.tuple_ids, frame->tuple_id, &frame->paired,
- frame->error))
+ if (!host_delta_hold(frame->io, &frame->hold, frame->error))
return;
- key = frame->written.has ? &frame->written.v : frame->paired.has ? &frame->paired.v : NULL;
+ frame->hold_live = true;
+ for (;;)
+ {
+ if (!txn_memtable_tuples_get(frame->io, under->state, tuple, &frame->written.has, &frame->written.v,
+ frame->error))
+ return;
+ if (!frame->written.has && frame->trees.has_tuple_ids &&
+ !doltgres_scan_pair(frame->io, frame->store, frame->trees.tuple_ids, frame->tuple_id, &frame->paired,
+ frame->error))
+ return;
+ key = frame->written.has ? &frame->written.v : frame->paired.has ? &frame->paired.v : NULL;
+ if (!host_pause(HOST_SHARED_IO_PAUSES(frame->io), "doltgres-fetch-keyed", frame->error))
+ return;
+ if (key != NULL)
+ {
+ if (!txn_memtable_slice_row_versions(frame->io, &frame->hold, under->state, key->ptr, key->len, tuple,
+ frame->trees.folded, &slice, frame->error))
+ return;
+ frame->versions = slice.versions;
+ frame->versions_live = true;
+ std_vec_new(&slice.versions, sizeof(TxnMemtableVersionVersion), _Alignof(TxnMemtableVersionVersion));
+ txn_memtable_slice_Slice_drop(&slice);
+ }
+ if (!txn_state_moved_past(under->state, frame->trees.generation))
+ break;
+ if (frame->versions_live)
+ txn_memtable_version_versions_drop(&frame->versions, 0);
+ frame->versions_live = false;
+ doltgres_scan_option_drop(&frame->paired);
+ doltgres_scan_option_drop(&frame->written);
+ if (!doltgres_scan_Trees_newer(&frame->trees, frame->io, frame->store, under, frame->epochs, frame->error))
+ return;
+ }
if (key == NULL)
{
frame->ok = true;
return;
}
- if (!host_delta_hold(frame->io, &frame->hold, frame->error))
- return;
- frame->hold_live = true;
- if (!txn_memtable_slice_row_versions(frame->io, &frame->hold, under->state, key->ptr, key->len, tuple,
- frame->trees.folded, &slice, frame->error))
- return;
- frame->versions = slice.versions;
- frame->versions_live = true;
- std_vec_new(&slice.versions, sizeof(TxnMemtableVersionVersion), _Alignof(TxnMemtableVersionVersion));
- txn_memtable_slice_Slice_drop(&slice);
if (!txn_visible_seen(frame->io, rule, frame->versions.ptr, frame->versions.len,
under->has_serial ? &under->serial.read : NULL, &frame->seen, frame->error))
return;
@@ -4353,7 +4968,7 @@ doltgres_scan_fetch_body(void *frame_)
{
.tuple_id = frame->tuple_id, .key = frame->paired.v, .has_root = frame->trees.has_root,
.root = frame->trees.root, .keyed = frame->layout.keyed, .table = frame->layout.table,
- .order = frame->layout.order, .transform = frame->transform
+ .order = frame->layout.order, .transform = frame->transform, .resolved = frame->resolved
};
frame->paired.has = frame->layout_live = frame->transform_live = false;
if (!doltgres_scan_resolve(frame->io, frame->store, &paired, &frame->row, frame->error))
@@ -4388,14 +5003,20 @@ doltgres_scan_fetch_body(void *frame_)
* or the base's tuple id tree pairs with the id, then the key's row version
* with the id among those the snapshot sees in the delta, or else the base's
* copy with the id, unless its ender in the delta ended it for the snapshot.
- * It takes the layout.
+ * Once it read the delta, a read whose base an install moved
+ * (txn_state_moved_past) reads again from the newest base, so it never pairs
+ * a base with the delta a newer base's drop changed; a case holds it inside
+ * its delta read, between its tuple id's lookup and its versions' read
+ * (doltgres-fetch-keyed). It takes the layout.
*/
bool
doltgres_scan_fetch(HostIoIo *io, TreesStoreNodeStore store, const DoltgresScanUnder *under,
- DoltgresScanEpochs *epochs, DoltgresScanTrees trees, DoltgresScanLayout *layout, uint64 tuple_id,
- bool *has_result, DoltgresScanRow *result, HostError *error)
+ DoltgresScanEpochs *epochs, DoltgresScanResolved *resolved, DoltgresScanTrees trees,
+ DoltgresScanLayout *layout, uint64 tuple_id, bool *has_result, DoltgresScanRow *result,
+ HostError *error)
{
- doltgres_scan_fetch_frame_t frame = {.io = io, .store = store, .under = under, .epochs = epochs, .trees = trees,
+ doltgres_scan_fetch_frame_t frame = {.io = io, .store = store, .under = under, .epochs = epochs,
+ .resolved = resolved, .trees = trees,
.layout = *layout, .layout_live = true, .tuple_id = tuple_id, .has_result = has_result, .result = result,
.error = error};
@@ -4532,21 +5153,33 @@ doltgres_scan_fetch_key_body(void *frame_)
StdVec key;
bool has_copies;
- if (frame->trees.has_root)
+ if (!host_delta_hold(frame->io, &frame->hold, frame->error))
+ return;
+ frame->hold_live = true;
+ for (;;)
{
- doltgres_scan_fetch_key_captures_t captures = {frame->trees.root, frame->key, frame->key_len,
- &frame->layout.order, frame->layout.keyed.len == 0};
+ if (frame->trees.has_root)
+ {
+ doltgres_scan_fetch_key_captures_t captures = {frame->trees.root, frame->key, frame->key_len,
+ &frame->layout.order, frame->layout.keyed.len == 0};
- if (!trees_pin_pin_ResultOptionCopies(frame->io, frame->store, doltgres_scan_fetch_key_closure_0, &captures,
- NULL, &frame->copied, frame->error))
+ if (!trees_pin_pin_ResultOptionCopies(frame->io, frame->store, doltgres_scan_fetch_key_closure_0,
+ &captures, NULL, &frame->copied, frame->error))
+ return;
+ }
+ if (!txn_visible_versions(frame->io, &frame->hold, frame->under->state, frame->key, frame->key_len,
+ frame->trees.folded, &versions, frame->error))
+ return;
+ if (!txn_state_moved_past(frame->under->state, frame->trees.generation))
+ break;
+ txn_memtable_version_versions_drop(&versions, 0);
+ if (frame->copied.has)
+ doltgres_scan_Copies_drop(&frame->copied.v);
+ frame->copied.has = false;
+ if (!doltgres_scan_Trees_newer(&frame->trees, frame->io, frame->store, frame->under, frame->epochs,
+ frame->error))
return;
}
- if (!host_delta_hold(frame->io, &frame->hold, frame->error))
- return;
- frame->hold_live = true;
- if (!txn_visible_versions(frame->io, &frame->hold, frame->under->state, frame->key, frame->key_len,
- frame->trees.folded, &versions, frame->error))
- return;
std_slice_to_vec_Checked(&key, frame->key, frame->key_len, DOLTGRES_SCAN_BYTES);
has_copies = frame->copied.has;
frame->copied.has = false;
@@ -4572,8 +5205,9 @@ doltgres_scan_fetch_key_body(void *frame_)
* doltgres_scan_fetch_key answers the rows a snapshot sees of a row key in a
* storage, read through a store, in the order a scan's group gives them: the
* copies of the base's entry of the key that the key's versions in the delta
- * leave seen, then the delta's rows it sees, oldest first. It takes the
- * layout.
+ * leave seen, then the delta's rows it sees, oldest first. Once it read the
+ * delta, a read whose base an install moved reads again from the newest
+ * base, as fetch does. It takes the layout.
*/
bool
doltgres_scan_fetch_key(HostIoIo *io, TreesStoreNodeStore store, const DoltgresScanUnder *under,
@@ -4594,9 +5228,7 @@ typedef struct doltgres_scan_keyed_frame_t
{
HostIoIo *io;
TreesStoreNodeStore store;
- HostSharedRecord state;
- bool has_tuple_ids;
- TreesStoreTree tuple_ids;
+ const HostSharedHeld *held;
uint64 tuple_id;
doltgres_OptionVecU8_t written;
bool *has_result;
@@ -4613,19 +5245,30 @@ doltgres_scan_keyed_pad(void *frame_)
doltgres_scan_option_drop(&((doltgres_scan_keyed_frame_t *) frame_)->written);
}
-/* doltgres_scan_keyed_body reads the delta's and the base's keys of the tuple id. */
+/* doltgres_scan_keyed_body reads the delta's key of the tuple id, then the newest base's. */
static void
doltgres_scan_keyed_body(void *frame_)
{
doltgres_scan_keyed_frame_t *frame = frame_;
HostRow tuple = {frame->tuple_id};
doltgres_OptionVecU8_t paired = {0};
+ bool some;
+ TxnBaseBase base;
+ bool has_tuple_ids;
+ TreesStoreTree tuple_ids;
- if (!txn_memtable_tuples_get(frame->io, frame->state, tuple, &frame->written.has, &frame->written.v,
- frame->error))
+ if (!host_pause(HOST_SHARED_IO_PAUSES(frame->io), "doltgres-tuple-keyed", frame->error) ||
+ !txn_memtable_tuples_get(frame->io, host_shared_Held_record(frame->held), tuple, &frame->written.has,
+ &frame->written.v, frame->error) ||
+ !txn_state_newest(frame->io, frame->held, &some, &base, frame->error))
return;
- if (frame->has_tuple_ids &&
- !doltgres_scan_pair(frame->io, frame->store, frame->tuple_ids, frame->tuple_id, &paired, frame->error))
+ has_tuple_ids = some && base.has_tuple_ids;
+ if (has_tuple_ids)
+ tuple_ids = base.tuple_ids;
+ if (some)
+ txn_base_Base_drop(&base);
+ if (has_tuple_ids &&
+ !doltgres_scan_pair(frame->io, frame->store, tuple_ids, frame->tuple_id, &paired, frame->error))
return;
*frame->result_1 = paired.has;
*frame->has_result = frame->written.has || paired.has;
@@ -4641,18 +5284,20 @@ doltgres_scan_keyed_body(void *frame_)
}
/*
- * doltgres_scan_keyed answers the row key a tuple id names in a storage,
- * the delta's tuple id table's, else the base's tuple id tree's, read
- * through a store, with whether the base holds the row, or none for a tuple
- * id no layer holds.
+ * doltgres_scan_keyed answers the row key a tuple id names in a held
+ * storage, the delta's tuple id table's, else the newest base's tuple id
+ * tree's, read through a store, with whether that base holds the row, or
+ * none for a tuple id no layer holds. It reads the base after the delta, so
+ * a row a fold's drop takes out of the delta after the delta's read is in
+ * the base it reads (bugs rows 240 and 315); it first pauses at
+ * doltgres-tuple-keyed.
*/
bool
-doltgres_scan_keyed(HostIoIo *io, TreesStoreNodeStore store, HostSharedRecord state, bool has_tuple_ids,
- TreesStoreTree tuple_ids, uint64 tuple_id, bool *has_result, StdVec *result_0, bool *result_1,
- HostError *error)
+doltgres_scan_keyed(HostIoIo *io, TreesStoreNodeStore store, const HostSharedHeld *held, uint64 tuple_id,
+ bool *has_result, StdVec *result_0, bool *result_1, HostError *error)
{
- doltgres_scan_keyed_frame_t frame = {io, store, state, has_tuple_ids, tuple_ids, tuple_id, {0}, has_result,
- result_0, result_1, error, false};
+ doltgres_scan_keyed_frame_t frame = {io, store, held, tuple_id, {0}, has_result, result_0, result_1, error,
+ false};
std_landing(&frame, doltgres_scan_keyed_body, doltgres_scan_keyed_pad);
doltgres_scan_keyed_pad(&frame);
@@ -5140,15 +5785,22 @@ doltgres_scan_delta_tid(const uint64 *written, size_t written_len, bool has_give
/*
* doltgres_scan_OptionChosen_t is next_in_range's choice in the record: a
* tuple id, the trees and layout its fetch reads, and the epochs moved out
- * of the record, or none.
+ * of the record, or none, with whether an install moved the base while it
+ * collected the delta's tuple ids, which the read moves to (rebased) before
+ * it chooses again. The tuple ids it chooses from are the base's and the
+ * delta's from before any newer base's drop, so none is missed; each fetch
+ * reads its row from the newest base (fetch), and a read whose fetch found a
+ * newer base moves to it once, so its later fetches start there.
*/
typedef struct doltgres_scan_OptionChosen_t
{
bool has;
+ bool moved;
uint64 tuple_id;
DoltgresScanTrees trees;
DoltgresScanLayout layout;
DoltgresScanEpochs epochs;
+ DoltgresScanResolved resolved;
} doltgres_scan_OptionChosen_t;
/* DOLTGRES_SCAN_CHOSEN_DROP drops a choice a call left. */
@@ -5158,6 +5810,7 @@ typedef struct doltgres_scan_OptionChosen_t
{ \
doltgres_scan_Layout_drop(&(place)->layout); \
doltgres_scan_Epochs_drop(&(place)->epochs); \
+ doltgres_scan_Resolved_drop(&(place)->resolved); \
} \
} while (0)
@@ -5200,13 +5853,19 @@ doltgres_scan_next_in_range_closure_0(void *captures, HostIoIo *io, DoltgresScan
bool has_delta;
uint64 delta = 0;
- result->has = false;
+ result->has = result->moved = false;
if (!has_range)
return true;
if (!scan->has_written)
{
if (!doltgres_scan_written(io, read->under, scan->folded, &scan->written, error))
return false;
+ if (txn_state_moved_past(read->under->state, scan->generation))
+ {
+ std_vec_free_Checked(&scan->written, DOLTGRES_SCAN_U64);
+ result->moved = true;
+ return true;
+ }
scan->has_written = true;
}
if (scan->has_tuple_ids &&
@@ -5227,34 +5886,24 @@ doltgres_scan_next_in_range_closure_0(void *captures, HostIoIo *io, DoltgresScan
trees_KeyOrder_Clone_clone(&scan->order, &result->layout.order);
result->trees = (DoltgresScanTrees)
{
- scan->has_tree, scan->tree, scan->has_tuple_ids, scan->tuple_ids, scan->folded
+ scan->has_tree, scan->tree, scan->has_tuple_ids, scan->tuple_ids, scan->folded, scan->generation
};
result->tuple_id = scan->given;
result->epochs = scan->epochs;
doltgres_scan_Epochs_Default_default(&scan->epochs);
+ result->resolved = scan->resolved;
+ scan->resolved = (DoltgresScanResolved)
+ {
+ 0
+ };
result->has = true;
return true;
}
-/* doltgres_scan_epochs_put_drop drops the epochs of a put that never ran. */
-static void
-doltgres_scan_epochs_put_drop(void *epochs)
-{
- doltgres_scan_Epochs_drop(epochs);
-}
-
-/* doltgres_scan_next_in_range_closure_1 is next_in_range's put of the epochs back in the record. */
-static bool
-doltgres_scan_next_in_range_closure_1(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
-{
- doltgres_scan_Epochs_drop(&scan->epochs);
- scan->epochs = *(DoltgresScanEpochs *) captures;
- return true;
-}
-
/*
* doltgres_scan_next_in_range_frame_t is next_in_range's state across its pad:
- * the choice's epochs and the fetched row, while live.
+ * the choice's epochs and memo of a base leaf's stamps, and the fetched row,
+ * while live.
*/
typedef struct doltgres_scan_next_in_range_frame_t
{
@@ -5264,6 +5913,7 @@ typedef struct doltgres_scan_next_in_range_frame_t
HostOwnedHandle handle;
DoltgresScanDirection way;
DoltgresScanEpochs epochs;
+ DoltgresScanResolved resolved;
bool epochs_live;
bool has_row;
DoltgresScanRow row;
@@ -5273,6 +5923,29 @@ typedef struct doltgres_scan_next_in_range_frame_t
bool ok;
} doltgres_scan_next_in_range_frame_t;
+/* doltgres_scan_epochs_put_drop drops the epochs and memo of a put that never ran. */
+static void
+doltgres_scan_epochs_put_drop(void *frame_)
+{
+ doltgres_scan_next_in_range_frame_t *frame = frame_;
+
+ doltgres_scan_Epochs_drop(&frame->epochs);
+ doltgres_scan_Resolved_drop(&frame->resolved);
+}
+
+/* doltgres_scan_next_in_range_closure_1 is next_in_range's put of the epochs and memo back in the record. */
+static bool
+doltgres_scan_next_in_range_closure_1(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
+{
+ doltgres_scan_next_in_range_frame_t *frame = captures;
+
+ doltgres_scan_Epochs_drop(&scan->epochs);
+ scan->epochs = frame->epochs;
+ doltgres_scan_Resolved_drop(&scan->resolved);
+ scan->resolved = frame->resolved;
+ return true;
+}
+
/* doltgres_scan_next_in_range_pad drops what next_in_range holds. */
static void
doltgres_scan_next_in_range_pad(void *frame_)
@@ -5283,7 +5956,7 @@ doltgres_scan_next_in_range_pad(void *frame_)
doltgres_scan_Row_drop(&frame->row);
frame->has_row = false;
if (frame->epochs_live)
- doltgres_scan_Epochs_drop(&frame->epochs);
+ doltgres_scan_epochs_put_drop(frame);
frame->epochs_live = false;
}
@@ -5297,11 +5970,19 @@ doltgres_scan_next_in_range_body(void *frame_)
for (;;)
{
doltgres_scan_OptionChosen_t chosen;
+ DoltgresScanRebased rebasing;
if (!host_owned_TableDoltgresScanScan_withOptionChosen(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
doltgres_scan_next_in_range_closure_0, &read, NULL,
&chosen, frame->error))
return;
+ if (chosen.moved)
+ {
+ if (!doltgres_scan_rebased(frame->io, frame->store, frame->under, frame->handle, &rebasing, frame->error))
+ return;
+ doltgres_scan_Rebased_drop(&rebasing);
+ continue;
+ }
if (!chosen.has)
{
*frame->has_result = false;
@@ -5309,15 +5990,22 @@ doltgres_scan_next_in_range_body(void *frame_)
return;
}
frame->epochs = chosen.epochs;
+ frame->resolved = chosen.resolved;
frame->epochs_live = true;
- if (!doltgres_scan_fetch(frame->io, frame->store, frame->under, &frame->epochs, chosen.trees, &chosen.layout,
- chosen.tuple_id, &frame->has_row, &frame->row, frame->error))
+ if (!doltgres_scan_fetch(frame->io, frame->store, frame->under, &frame->epochs, &frame->resolved, chosen.trees,
+ &chosen.layout, chosen.tuple_id, &frame->has_row, &frame->row, frame->error))
return;
frame->epochs_live = false;
if (!host_owned_TableDoltgresScanScan_withUnit(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
- doltgres_scan_next_in_range_closure_1, &frame->epochs,
+ doltgres_scan_next_in_range_closure_1, frame,
doltgres_scan_epochs_put_drop, frame->error))
return;
+ if (txn_state_moved_past(frame->under->state, chosen.trees.generation))
+ {
+ if (!doltgres_scan_rebased(frame->io, frame->store, frame->under, frame->handle, &rebasing, frame->error))
+ return;
+ doltgres_scan_Rebased_drop(&rebasing);
+ }
if (frame->has_row)
{
*frame->has_result = true;
@@ -5415,16 +6103,6 @@ doltgres_scan_splits(const StdVec *starts, size_t starts_len, StdVec *result)
std_landing(&frame, doltgres_scan_splits_body, doltgres_scan_splits_pad);
}
-/* doltgres_scan_internal returns an internal error of the fork's with a literal message. */
-static HostError
-doltgres_scan_internal(const char *message)
-{
- StdString text;
-
- std_string_from_Checked(&text, message, strlen(message));
- return host_Error_fork((const uint8 *) "XX000", &text);
-}
-
/* doltgres_scan_split_keys_frame_t is split_keys' state across its pad: the keys read so far, and the bytes left. */
typedef struct doltgres_scan_split_keys_frame_t
{
@@ -5496,6 +6174,7 @@ typedef struct doltgres_scan_begin_parallel_frame_t
HostIoIo *io;
DoltgresScanShared shared;
bool shared_live;
+ uint64 generation;
StdVec keyed;
bool keyed_live;
uint32 table;
@@ -5545,6 +6224,7 @@ doltgres_scan_begin_parallel_body(void *frame_)
scan.tree = shared->tree;
scan.folded = shared->folded;
scan.epochs = shared->epochs;
+ scan.generation = frame->generation;
scan.blocks = shared->has_tree ? (shared->fresh_end >> 16) + 1 : 0;
scan.keyed = frame->keyed;
scan.table = frame->table;
@@ -5555,6 +6235,8 @@ doltgres_scan_begin_parallel_body(void *frame_)
scan.parallel.entries = shared->entries;
scan.parallel.units = shared->units;
scan.parallel.splits = shared->splits;
+ scan.parallel.has_shared = shared->has_tree;
+ scan.parallel.shared = shared->tree;
scan.page = doltgres_scan_ended();
frame->reader_live = frame->shared_live = frame->keyed_live = frame->order_live = false;
frame->ok = host_owned_TableDoltgresScanScan_make(&DOLTGRES_SCAN_SCANS, frame->io, scan, frame->result,
@@ -5563,15 +6245,16 @@ doltgres_scan_begin_parallel_body(void *frame_)
/*
* doltgres_scan_begin_parallel makes a parallel scan of the base the leader
- * shared, in a table of a keyed layout, an object id, and a tree's order,
- * each of which it takes, and answers its handle; its first read asks for
- * its first unit.
+ * shared, the storage's newest at a base generation or none newer, in a
+ * table of a keyed layout, an object id, and a tree's order, each of which
+ * it takes, and answers its handle; its first read asks for its first unit.
*/
bool
-doltgres_scan_begin_parallel(HostIoIo *io, DoltgresScanShared *shared, StdVec *keyed, uint32 table,
- TreesKeyOrder *order, HostOwnedHandle *result, HostError *error)
+doltgres_scan_begin_parallel(HostIoIo *io, DoltgresScanShared *shared, uint64 generation, StdVec *keyed,
+ uint32 table, TreesKeyOrder *order, HostOwnedHandle *result, HostError *error)
{
- doltgres_scan_begin_parallel_frame_t frame = {.io = io, .shared = *shared, .shared_live = true, .keyed = *keyed,
+ doltgres_scan_begin_parallel_frame_t frame = {.io = io, .shared = *shared, .shared_live = true,
+ .generation = generation, .keyed = *keyed,
.keyed_live = true, .table = table, .order = *order, .order_live = true, .result = result,
.error = error};
@@ -5784,6 +6467,20 @@ doltgres_scan_bounds_closure_1(const StdVec *splits, uint64 segment, uint64 segm
return key;
}
+/* doltgres_scan_edges keeps a unit's bounds as its edges in the scan's parallel part, cloned. */
+static void
+doltgres_scan_edges(DoltgresScanParallel *parallel, const DoltgresScanBounds *bounds)
+{
+ doltgres_scan_option_drop(¶llel->low);
+ doltgres_scan_option_drop(¶llel->high);
+ parallel->low.has = bounds->low.has;
+ if (bounds->low.has)
+ std_vec_clone(¶llel->low.v, &bounds->low.v, DOLTGRES_SCAN_BYTES);
+ parallel->high.has = bounds->high.has;
+ if (bounds->high.has)
+ std_vec_clone(¶llel->high.v, &bounds->high.v, DOLTGRES_SCAN_BYTES);
+}
+
/*
* doltgres_scan_bounds_frame_t is bounds' state across its pad: the scan, the
* unit, the store's source, and the bounds being made.
@@ -5821,7 +6518,7 @@ doltgres_scan_bounds_body(void *frame_)
doltgres_scan_bounds_frame_t *frame = frame_;
DoltgresScanScan *scan = frame->scan;
DoltgresScanBounds *result = frame->result;
- doltgres_scan_bounds_captures_t captures = {.scan = scan, .tree = scan->tree, .unit = frame->unit,
+ doltgres_scan_bounds_captures_t captures = {.scan = scan, .tree = scan->parallel.shared, .unit = frame->unit,
.error = frame->error};
*result = (DoltgresScanBounds)
@@ -5836,7 +6533,7 @@ doltgres_scan_bounds_body(void *frame_)
return;
}
captures.entries = scan->parallel.entries;
- if (!scan->has_tree || captures.entries == 0)
+ if (!scan->parallel.has_shared || captures.entries == 0)
{
const StdVec *splits = &scan->parallel.splits;
uint64 segments = (uint64) splits->len + 1;
@@ -5856,6 +6553,7 @@ doltgres_scan_bounds_body(void *frame_)
std_vec_new(&result->high.v, DOLTGRES_SCAN_BYTES);
}
result->alone = !result->low.has && !result->high.has;
+ doltgres_scan_edges(&scan->parallel, result);
frame->live = false;
frame->ok = true;
return;
@@ -5870,6 +6568,7 @@ doltgres_scan_bounds_body(void *frame_)
if (result->high.has)
std_vec_clone(&scan->bound.v, &result->high.v, DOLTGRES_SCAN_BYTES);
scan->bound.has = result->high.has;
+ doltgres_scan_edges(&scan->parallel, result);
frame->live = false;
frame->ok = true;
}
@@ -5893,25 +6592,77 @@ doltgres_scan_bounds(HostIoIo *io, const DoltgresScanStores *stores, DoltgresSca
return frame.ok;
}
+/*
+ * doltgres_scan_Kept_t is a parallel worker's kept run outside its record:
+ * whether it has one, its keys (a Vec>), and the scan's generation
+ * it was read at, which a take sets to the record's generation when it finds
+ * none to give.
+ */
+typedef struct doltgres_scan_Kept_t
+{
+ bool has;
+ StdVec keys;
+ uint64 at;
+} doltgres_scan_Kept_t;
+
+/*
+ * doltgres_scan_kept_take_closure_0 is kept_take's call: the record's kept run
+ * moved out when it was read at the scan's generation, and dropped when it
+ * wasn't, since a rebase or a rescan left it behind.
+ */
+static bool
+doltgres_scan_kept_take_closure_0(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
+{
+ doltgres_scan_Kept_t *kept = captures;
+
+ kept->has = false;
+ kept->at = scan->generation;
+ if (!scan->has_parallel || !scan->parallel.has_kept)
+ return true;
+ scan->parallel.has_kept = false;
+ if (scan->parallel.kept_at != scan->generation)
+ {
+ doltgres_scan_vecs_drop(&scan->parallel.kept);
+ return true;
+ }
+ kept->has = true;
+ kept->keys = scan->parallel.kept;
+ return true;
+}
+
+/* doltgres_scan_kept_put_closure_0 is kept_put's call: the kept run moved back into the record. */
+static bool
+doltgres_scan_kept_put_closure_0(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
+{
+ doltgres_scan_Kept_t *kept = captures;
+
+ if (!scan->has_parallel)
+ return true;
+ scan->parallel.kept = kept->keys;
+ scan->parallel.kept_at = kept->at;
+ scan->parallel.has_kept = true;
+ kept->has = false;
+ return true;
+}
+
/*
* doltgres_scan_unit_keys_frame_t is unit_keys' state across its pad: the
- * run's keys not yet looked at and the ones kept, while live.
+ * kept run while it is out of the record, and the keys kept, while live.
*/
typedef struct doltgres_scan_unit_keys_frame_t
{
HostIoIo *io;
const DoltgresScanUnder *under;
+ HostOwnedHandle handle;
const DoltgresScanBounds *bounds;
- StdVec keys;
- size_t next;
- bool keys_live;
+ doltgres_scan_Kept_t kept;
StdVec *result;
bool result_live;
HostError *error;
bool ok;
} doltgres_scan_unit_keys_frame_t;
-/* doltgres_scan_unit_keys_pad drops the keys not yet looked at and the ones kept. */
+/* doltgres_scan_unit_keys_pad drops the kept run while it is out of the record, and the keys kept. */
static void
doltgres_scan_unit_keys_pad(void *frame_)
{
@@ -5920,21 +6671,51 @@ doltgres_scan_unit_keys_pad(void *frame_)
if (frame->result_live)
doltgres_scan_vecs_drop(frame->result);
frame->result_live = false;
- if (frame->keys_live)
+ if (frame->kept.has)
+ doltgres_scan_vecs_drop(&frame->kept.keys);
+ frame->kept.has = false;
+}
+
+/*
+ * doltgres_scan_unit_keys_at returns the place in the kept run of its first
+ * key at or past a bound in the table's order, by a binary search, since the
+ * run is in that order.
+ */
+static bool
+doltgres_scan_unit_keys_at(doltgres_scan_unit_keys_frame_t *frame, const StdVec *bound, size_t *result)
+{
+ size_t low = 0;
+ size_t high = frame->kept.keys.len;
+
+ while (low < high)
{
- for (size_t i = frame->next; i < frame->keys.len; i++)
- std_vec_free_Checked(&STD_VEC_AT(&frame->keys, StdVec, i), DOLTGRES_SCAN_BYTES);
- std_vec_free_Checked(&frame->keys, DOLTGRES_SCAN_VEC);
+ size_t middle = low + (high - low) / 2;
+ StdOrdering ordering;
+
+ if (!doltgres_scan_total(frame->io, &frame->bounds->order, &STD_VEC_AT(&frame->kept.keys, StdVec, middle),
+ bound, &ordering, frame->error))
+ return false;
+ if (ordering == STD_ORDERING_LESS)
+ low = middle + 1;
+ else
+ high = middle;
}
- frame->keys_live = false;
+ *result = low;
+ return true;
}
-/* doltgres_scan_unit_keys_body reads the run and keeps its keys within the unit's bounds. */
+/*
+ * doltgres_scan_unit_keys_body takes the kept run out of the record, reading
+ * the run when the record keeps none at the scan's generation, copies out its
+ * keys within the unit's bounds, and puts the run back.
+ */
static void
doltgres_scan_unit_keys_body(void *frame_)
{
doltgres_scan_unit_keys_frame_t *frame = frame_;
const DoltgresScanBounds *bounds = frame->bounds;
+ size_t first = 0;
+ size_t end;
std_vec_new(frame->result, DOLTGRES_SCAN_VEC);
if (!bounds->low.has && !bounds->high.has && !bounds->alone)
@@ -5943,39 +6724,30 @@ doltgres_scan_unit_keys_body(void *frame_)
return;
}
frame->result_live = true;
- if (!doltgres_scan_run(frame->io, frame->under, &bounds->order, &frame->keys, frame->error))
+ if (!host_owned_TableDoltgresScanScan_withUnit(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
+ doltgres_scan_kept_take_closure_0, &frame->kept, NULL, frame->error))
return;
- frame->keys_live = true;
- while (frame->next < frame->keys.len)
+ if (!frame->kept.has)
{
- StdVec key = STD_VEC_AT(&frame->keys, StdVec, frame->next);
- StdOrdering ordering;
- bool at_or_past = true;
- bool before = true;
+ if (!doltgres_scan_run(frame->io, frame->under, &bounds->order, &frame->kept.keys, frame->error))
+ return;
+ frame->kept.has = true;
+ }
+ end = frame->kept.keys.len;
+ if ((bounds->low.has && !doltgres_scan_unit_keys_at(frame, &bounds->low.v, &first)) ||
+ (bounds->high.has && !doltgres_scan_unit_keys_at(frame, &bounds->high.v, &end)))
+ return;
+ for (size_t i = first; i < end; i++)
+ {
+ const StdVec *key = &STD_VEC_AT(&frame->kept.keys, StdVec, i);
+ StdVec copy;
- if (bounds->low.has)
- {
- if (!doltgres_scan_total(frame->io, &bounds->order, &key, &bounds->low.v, &ordering, frame->error))
- return;
- at_or_past = ordering != STD_ORDERING_LESS;
- }
- if (bounds->high.has)
- {
- if (!doltgres_scan_total(frame->io, &bounds->order, &key, &bounds->high.v, &ordering, frame->error))
- return;
- before = ordering == STD_ORDERING_LESS;
- }
- if (at_or_past && before)
- {
- std_vec_push_Checked(frame->result, &key, DOLTGRES_SCAN_VEC);
- frame->next++;
- }
- else
- {
- frame->next++;
- std_vec_free_Checked(&key, DOLTGRES_SCAN_BYTES);
- }
+ std_slice_to_vec_Checked(©, key->ptr, key->len, DOLTGRES_SCAN_BYTES);
+ std_vec_push_Checked(frame->result, ©, DOLTGRES_SCAN_VEC);
}
+ if (!host_owned_TableDoltgresScanScan_withUnit(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
+ doltgres_scan_kept_put_closure_0, &frame->kept, NULL, frame->error))
+ return;
frame->result_live = false;
frame->ok = true;
}
@@ -5984,14 +6756,16 @@ doltgres_scan_unit_keys_body(void *frame_)
* doltgres_scan_unit_keys answers a parallel unit's delta keys in the
* table's order: every key at or past its low bound and before its high
* one, with ties broken by the keys' bytes, or every key when its bounds say
- * the unit reads them all alone.
+ * the unit reads them all alone. It takes them from the run the worker keeps,
+ * read once for each generation the scan reads at, so a scan's units read the
+ * delta's run once and not once each (bugs row 309).
*/
static bool
-doltgres_scan_unit_keys(HostIoIo *io, const DoltgresScanUnder *under, const DoltgresScanBounds *bounds,
- StdVec *result, HostError *error)
+doltgres_scan_unit_keys(HostIoIo *io, const DoltgresScanUnder *under, HostOwnedHandle handle,
+ const DoltgresScanBounds *bounds, StdVec *result, HostError *error)
{
- doltgres_scan_unit_keys_frame_t frame = {.io = io, .under = under, .bounds = bounds, .result = result,
- .error = error};
+ doltgres_scan_unit_keys_frame_t frame = {.io = io, .under = under, .handle = handle, .bounds = bounds,
+ .result = result, .error = error};
std_landing(&frame, doltgres_scan_unit_keys_body, doltgres_scan_unit_keys_pad);
doltgres_scan_unit_keys_pad(&frame);
@@ -6089,28 +6863,319 @@ doltgres_scan_next_parallel_closure_2(void *captures, HostIoIo *io, DoltgresScan
static bool
doltgres_scan_next_parallel_closure_3(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
{
- if (scan->has_parallel)
- scan->parallel.has_unit = false;
- return true;
+ if (scan->has_parallel)
+ scan->parallel.has_unit = false;
+ return true;
+}
+
+/* doltgres_scan_next_parallel_closure_4 is next_parallel's put of the delta's run in the record. */
+static bool
+doltgres_scan_next_parallel_closure_4(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
+{
+ doltgres_scan_run_put(scan, captures);
+ return true;
+}
+
+/* doltgres_scan_next_parallel_closure_5 is next_parallel's making of the chosen group in the record, forward. */
+static bool
+doltgres_scan_next_parallel_closure_5(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
+{
+ doltgres_scan_chose_t *chose = captures;
+
+ return doltgres_scan_chosen(io, chose->under, scan, &chose->picked, DOLTGRES_SCAN_DIRECTION_FORWARD,
+ &chose->moved, error);
+}
+
+/* doltgres_scan_sought_captures_t is sought's pin scope's captures: the tree, the key, and its order. */
+typedef struct doltgres_scan_sought_captures_t
+{
+ TreesStoreTree tree;
+ const uint8 *key;
+ size_t key_len;
+ const TreesKeyOrder *order;
+} doltgres_scan_sought_captures_t;
+
+/* doltgres_scan_sought_closure_0_body seeks the key and copies out the key of the first entry at or past it. */
+static void
+doltgres_scan_sought_closure_0_body(void *frame_)
+{
+ doltgres_scan_cursor_frame_t *frame = frame_;
+ doltgres_scan_sought_captures_t *captures = frame->captures;
+ doltgres_OptionVecU8_t *result = frame->result;
+ ChunksErrorError failed;
+ StdBytes key;
+
+ if (!trees_cursor_Cursor_seek(frame->io, frame->pin, TREES_STORE_TREE_NAMED(&captures->tree), captures->key,
+ captures->key_len, captures->order, &frame->cursor, &failed))
+ {
+ doltgres_scan_storage_failed(&failed, frame->error);
+ return;
+ }
+ frame->cursor_live = true;
+ result->has = trees_cursor_Cursor_is_valid(&frame->cursor);
+ if (result->has)
+ {
+ key = trees_cursor_Cursor_key(&frame->cursor);
+ std_slice_to_vec_Checked(&result->v, key.ptr, key.len, DOLTGRES_SCAN_BYTES);
+ }
+ frame->ok = true;
+}
+
+/* doltgres_scan_sought_closure_0 is sought's pin scope. */
+static bool
+doltgres_scan_sought_closure_0(void *captures, HostIoIo *io, TreesPinPin *pin, doltgres_OptionVecU8_t *result,
+ HostError *error)
+{
+ return doltgres_scan_cursor_scope(captures, io, pin, result, error, doltgres_scan_sought_closure_0_body);
+}
+
+/* doltgres_scan_sought_read_t is a stop's read under a reader, a Stores::with call's captures. */
+typedef struct doltgres_scan_sought_read_t
+{
+ doltgres_scan_sought_captures_t sought;
+ doltgres_OptionVecU8_t *result;
+ HostError *error;
+ bool ok;
+} doltgres_scan_sought_read_t;
+
+/* doltgres_scan_sought_read reads the key of a tree's first entry at or past a key going forward, in a pin scope. */
+static void
+doltgres_scan_sought_read(void *captures, HostIoIo *io, TreesStoreNodeStore store)
+{
+ doltgres_scan_sought_read_t *read = captures;
+
+ read->ok = trees_pin_pin_ResultOptionVecU8(io, store, doltgres_scan_sought_closure_0, &read->sought, NULL,
+ read->result, read->error);
+}
+
+/*
+ * DoltgresScanUnitPlacing is what unit_placed takes out of a parallel scan's
+ * record: whether the worker's tree is a newer base's than the shared one,
+ * that tree, its group's key, its unit's edges, and the table's order, each
+ * cloned and each while live, and the place and stop it puts back.
+ */
+typedef struct DoltgresScanUnitPlacing
+{
+ bool moved;
+ bool has_tree;
+ TreesStoreTree tree;
+ doltgres_OptionVecU8_t group;
+ doltgres_OptionVecU8_t low;
+ doltgres_OptionVecU8_t high;
+ TreesKeyOrder order;
+ bool order_live;
+ DoltgresScanPlace place;
+ doltgres_OptionVecU8_t bound;
+} DoltgresScanUnitPlacing;
+
+/* doltgres_scan_unit_placed_closure_0 is unit_placed's look in the record: a moved worker's tree, group, and edges. */
+static bool
+doltgres_scan_unit_placed_closure_0(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
+{
+ DoltgresScanUnitPlacing *placing = captures;
+ const DoltgresScanParallel *parallel = &scan->parallel;
+
+ if (!scan->has_parallel || !parallel->has_unit ||
+ (scan->has_tree == parallel->has_shared &&
+ (!scan->has_tree || trees_store_Tree_PartialEq_eq(&scan->tree, ¶llel->shared))))
+ return true;
+ placing->moved = true;
+ placing->has_tree = scan->has_tree;
+ placing->tree = scan->tree;
+ if (scan->has_group)
+ std_vec_clone(&placing->group.v, &scan->group.key, DOLTGRES_SCAN_BYTES);
+ placing->group.has = scan->has_group;
+ if (parallel->low.has)
+ std_vec_clone(&placing->low.v, ¶llel->low.v, DOLTGRES_SCAN_BYTES);
+ placing->low.has = parallel->low.has;
+ if (parallel->high.has)
+ std_vec_clone(&placing->high.v, ¶llel->high.v, DOLTGRES_SCAN_BYTES);
+ placing->high.has = parallel->high.has;
+ trees_KeyOrder_Clone_clone(&scan->order, &placing->order);
+ placing->order_live = true;
+ return true;
+}
+
+/* doltgres_scan_unit_placed_closure_1 is unit_placed's put of the place and the stop in the record. */
+static bool
+doltgres_scan_unit_placed_closure_1(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
+{
+ DoltgresScanUnitPlacing *placing = captures;
+
+ doltgres_scan_Place_set(&scan->place, placing->place);
+ doltgres_scan_option_drop(&scan->bound);
+ scan->bound = placing->bound;
+ placing->place.tag = DOLTGRES_SCAN_PLACE_START;
+ placing->bound.has = false;
+ return true;
+}
+
+/* doltgres_scan_unit_placed_frame_t is unit_placed's state across its pad: its reader and what it took, while live. */
+typedef struct doltgres_scan_unit_placed_frame_t
+{
+ HostIoIo *io;
+ const DoltgresScanStores *stores;
+ HostOwnedHandle handle;
+ HostSharedReader reading;
+ bool reading_live;
+ DoltgresScanUnitPlacing placing;
+ HostError *error;
+ bool ok;
+} doltgres_scan_unit_placed_frame_t;
+
+/* doltgres_scan_unit_placed_pad drops what unit_placed holds. */
+static void
+doltgres_scan_unit_placed_pad(void *frame_)
+{
+ doltgres_scan_unit_placed_frame_t *frame = frame_;
+ DoltgresScanUnitPlacing *placing = &frame->placing;
+
+ doltgres_scan_option_drop(&placing->bound);
+ doltgres_scan_Place_drop(&placing->place);
+ placing->place.tag = DOLTGRES_SCAN_PLACE_START;
+ if (placing->order_live)
+ trees_KeyOrder_drop(&placing->order);
+ doltgres_scan_option_drop(&placing->high);
+ doltgres_scan_option_drop(&placing->low);
+ doltgres_scan_option_drop(&placing->group);
+ placing->order_live = placing->moved = false;
+ if (frame->reading_live)
+ host_shared_Reader_drop(&frame->reading);
+ frame->reading_live = false;
+}
+
+/* doltgres_scan_unit_placed_body finds a moved worker's place and stop in its unit in the newer base's tree. */
+static void
+doltgres_scan_unit_placed_body(void *frame_)
+{
+ doltgres_scan_unit_placed_frame_t *frame = frame_;
+ DoltgresScanUnitPlacing *placing = &frame->placing;
+ const doltgres_OptionVecU8_t *from;
+
+ placing->place.tag = DOLTGRES_SCAN_PLACE_START;
+ if (!host_owned_TableDoltgresScanScan_withUnit(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
+ doltgres_scan_unit_placed_closure_0, placing, NULL, frame->error))
+ return;
+ if (!placing->moved)
+ {
+ frame->ok = true;
+ return;
+ }
+ from = placing->group.has ? &placing->group : &placing->low;
+ if (!placing->has_tree)
+ placing->place = doltgres_scan_off(DOLTGRES_SCAN_DIRECTION_FORWARD);
+ else
+ {
+ if (!host_shared_read(frame->io, &frame->reading, frame->error))
+ return;
+ frame->reading_live = true;
+ if (from->has)
+ {
+ doltgres_scan_place_read_t read = {{placing->tree, from->v.ptr, from->v.len,
+ DOLTGRES_SCAN_DIRECTION_FORWARD, &placing->order, placing->group.has}, .error = frame->error};
+
+ frame->stores->ops->with(frame->stores->self, frame->io, &frame->reading, doltgres_scan_place_read,
+ &read);
+ if (!read.ok)
+ return;
+ placing->place = read.place;
+ }
+ if (placing->high.has)
+ {
+ doltgres_scan_sought_read_t read = {{placing->tree, placing->high.v.ptr, placing->high.v.len,
+ &placing->order}, &placing->bound, frame->error};
+
+ frame->stores->ops->with(frame->stores->self, frame->io, &frame->reading, doltgres_scan_sought_read,
+ &read);
+ if (!read.ok)
+ return;
+ }
+ }
+ frame->ok = host_owned_TableDoltgresScanScan_withUnit(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
+ doltgres_scan_unit_placed_closure_1, placing, NULL,
+ frame->error);
+}
+
+/*
+ * doltgres_scan_unit_placed puts a parallel scan's worker that moved to a
+ * newer base than the shared one (rebased) at its place in its unit in the
+ * newer base's tree, outside the record, since a compared tree's order runs
+ * user code: past its group's key once it has one, as a serial read goes on,
+ * or else at the unit's low key, or the tree's start for unit 0; and stops it
+ * at the first entry at or past the unit's high key, which the newer tree
+ * need not hold, so each key is read by the unit whose edges hold it. A
+ * worker on the shared base, or with no unit, is left as it is.
+ */
+static bool
+doltgres_scan_unit_placed(HostIoIo *io, const DoltgresScanStores *stores, HostOwnedHandle handle, HostError *error)
+{
+ doltgres_scan_unit_placed_frame_t frame = {.io = io, .stores = stores, .handle = handle, .error = error};
+
+ std_landing(&frame, doltgres_scan_unit_placed_body, doltgres_scan_unit_placed_pad);
+ doltgres_scan_unit_placed_pad(&frame);
+ return frame.ok;
+}
+
+/* doltgres_scan_rebasing_frame_t is rebasing's state across its pad: its reader and the move, while live. */
+typedef struct doltgres_scan_rebasing_frame_t
+{
+ HostIoIo *io;
+ const DoltgresScanStores *stores;
+ const DoltgresScanUnder *under;
+ HostOwnedHandle handle;
+ HostSharedReader reading;
+ bool reading_live;
+ DoltgresScanRebased rebasing;
+ bool *moved;
+ HostError *error;
+ bool ok;
+} doltgres_scan_rebasing_frame_t;
+
+/* doltgres_scan_rebasing_pad drops what rebasing holds. */
+static void
+doltgres_scan_rebasing_pad(void *frame_)
+{
+ doltgres_scan_rebasing_frame_t *frame = frame_;
+
+ doltgres_scan_Rebased_drop(&frame->rebasing);
+ if (frame->reading_live)
+ host_shared_Reader_drop(&frame->reading);
+ frame->reading_live = false;
}
-/* doltgres_scan_next_parallel_closure_4 is next_parallel's put of the delta's run in the record. */
-static bool
-doltgres_scan_next_parallel_closure_4(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
+/* doltgres_scan_rebasing_body moves the worker to the newest base under its reader. */
+static void
+doltgres_scan_rebasing_body(void *frame_)
{
- doltgres_scan_run_put(scan, captures);
- return true;
+ doltgres_scan_rebasing_frame_t *frame = frame_;
+ doltgres_scan_rebase_read_t rebase = {frame->under, frame->handle, &frame->rebasing, frame->error};
+
+ if (!host_shared_read(frame->io, &frame->reading, frame->error))
+ return;
+ frame->reading_live = true;
+ frame->stores->ops->with(frame->stores->self, frame->io, &frame->reading, doltgres_scan_rebase_read, &rebase);
+ *frame->moved = rebase.ok && frame->rebasing.moved;
+ frame->ok = rebase.ok;
}
-/* doltgres_scan_next_parallel_closure_5 is next_parallel's making of the chosen group in the record, forward. */
+/*
+ * doltgres_scan_rebasing moves a parallel scan's worker to its storage's
+ * newest base under a reader of its own (rebased) and answers whether it
+ * moved.
+ */
static bool
-doltgres_scan_next_parallel_closure_5(void *captures, HostIoIo *io, DoltgresScanScan *scan, HostError *error)
+doltgres_scan_rebasing(HostIoIo *io, const DoltgresScanStores *stores, const DoltgresScanUnder *under,
+ HostOwnedHandle handle, bool *moved, HostError *error)
{
- doltgres_scan_chose_t *chose = captures;
+ doltgres_scan_rebasing_frame_t frame = {.io = io, .stores = stores, .under = under, .handle = handle,
+ .moved = moved, .error = error};
- return doltgres_scan_chosen(io, chose->under, scan, &chose->picked, DOLTGRES_SCAN_DIRECTION_FORWARD, error);
+ std_landing(&frame, doltgres_scan_rebasing_body, doltgres_scan_rebasing_pad);
+ doltgres_scan_rebasing_pad(&frame);
+ return frame.ok;
}
+
/*
* doltgres_scan_next_parallel_frame_t is next_parallel's state across its pad:
* a unit's bounds, a turn's order, and a run's keys, while live.
@@ -6125,6 +7190,7 @@ typedef struct doltgres_scan_next_parallel_frame_t
TreesKeyOrder order;
bool order_live;
StdVec keys;
+ bool keys_live;
DoltgresScanStep *result;
HostError *error;
bool ok;
@@ -6136,6 +7202,9 @@ doltgres_scan_next_parallel_pad(void *frame_)
{
doltgres_scan_next_parallel_frame_t *frame = frame_;
+ if (frame->keys_live)
+ doltgres_scan_vecs_drop(&frame->keys);
+ frame->keys_live = false;
if (frame->order_live)
trees_KeyOrder_drop(&frame->order);
frame->order_live = false;
@@ -6149,20 +7218,43 @@ doltgres_scan_next_parallel_body(void *frame_)
{
doltgres_scan_next_parallel_frame_t *frame = frame_;
doltgres_scan_turn_t turn = {frame->stores, frame->under, DOLTGRES_SCAN_DIRECTION_FORWARD};
+ bool moved = false;
for (;;)
{
DoltgresScanTurn step;
+ uint64 generation;
if (!host_owned_TableDoltgresScanScan_withOptionBounds(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
doltgres_scan_next_parallel_closure_0,
(void *) &frame->stores, NULL, &frame->begun,
frame->error))
return;
+ if (moved || frame->begun.has)
+ {
+ if (!doltgres_scan_unit_placed(frame->io, frame->stores, frame->handle, frame->error))
+ return;
+ moved = false;
+ }
if (frame->begun.has)
{
- if (!doltgres_scan_unit_keys(frame->io, frame->under, &frame->begun.v, &frame->keys, frame->error))
+ if (!doltgres_scan_unit_keys(frame->io, frame->under, frame->handle, &frame->begun.v, &frame->keys,
+ frame->error))
+ return;
+ frame->keys_live = true;
+ if (!host_owned_TableDoltgresScanScan_withU64(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
+ doltgres_scan_generation_closure_0, NULL, NULL, &generation,
+ frame->error))
return;
+ if (txn_state_moved_past(frame->under->state, generation))
+ {
+ doltgres_scan_next_parallel_pad(frame);
+ if (!doltgres_scan_rebasing(frame->io, frame->stores, frame->under, frame->handle, &moved,
+ frame->error))
+ return;
+ continue;
+ }
+ frame->keys_live = false;
if (!host_owned_TableDoltgresScanScan_withUnit(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
doltgres_scan_next_parallel_closure_1, &frame->keys,
doltgres_scan_keys_drop, frame->error))
@@ -6190,12 +7282,31 @@ doltgres_scan_next_parallel_body(void *frame_)
frame->ok = true;
return;
}
- if (step.tag == DOLTGRES_SCAN_TURN_RUN)
+ if (step.tag == DOLTGRES_SCAN_TURN_MOVED)
+ {
+ if (!doltgres_scan_rebasing(frame->io, frame->stores, frame->under, frame->handle, &moved, frame->error))
+ return;
+ }
+ else if (step.tag == DOLTGRES_SCAN_TURN_RUN)
{
frame->order = step.u.run;
frame->order_live = true;
if (!doltgres_scan_run(frame->io, frame->under, &frame->order, &frame->keys, frame->error))
return;
+ frame->keys_live = true;
+ if (!host_owned_TableDoltgresScanScan_withU64(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
+ doltgres_scan_generation_closure_0, NULL, NULL, &generation,
+ frame->error))
+ return;
+ if (txn_state_moved_past(frame->under->state, generation))
+ {
+ doltgres_scan_next_parallel_pad(frame);
+ if (!doltgres_scan_rebasing(frame->io, frame->stores, frame->under, frame->handle, &moved,
+ frame->error))
+ return;
+ continue;
+ }
+ frame->keys_live = false;
if (!host_owned_TableDoltgresScanScan_withUnit(&DOLTGRES_SCAN_SCANS, frame->io, frame->handle,
doltgres_scan_next_parallel_closure_4, &frame->keys,
doltgres_scan_keys_drop, frame->error))
@@ -6217,6 +7328,9 @@ doltgres_scan_next_parallel_body(void *frame_)
doltgres_scan_chose_drop, frame->error))
return;
doltgres_scan_next_parallel_pad(frame);
+ if (chose.moved &&
+ !doltgres_scan_rebasing(frame->io, frame->stores, frame->under, frame->handle, &moved, frame->error))
+ return;
}
}
}
@@ -6382,9 +7496,7 @@ doltgres_scan_tests_storage(void)
static DoltgresScanTrees
doltgres_scan_tests_trees(const TxnBaseBase *base)
{
- DoltgresScanTrees trees = {base->has_root, base->root, base->has_tuple_ids, base->tuple_ids, base->folded};
-
- return trees;
+ return doltgres_scan_Trees_of(base, 0);
}
/*
@@ -6514,18 +7626,20 @@ doltgres_scan_tests_le4(uint32 n)
}
/*
- * doltgres_scan_tests_base builds a tree of rows rows in a store, each keyed
- * by its number big-endian with its number's square as its value and
- * stamped by its number plus one as its tuple id, and returns its root's name
- * as a base's record would decode it.
+ * doltgres_scan_tests_built builds a tree of the rows numbered from one
+ * number up to another in a store, each keyed by its number big-endian with
+ * its number's square as its value and stamped by its number plus one as
+ * its tuple id, and returns its root's name as a base's record would decode
+ * it; its adds and its finish unwrap at a line and column each.
*/
static TreesStoreTree
-doltgres_scan_tests_base(HostIoIo *io, TreesStoreMemoryStore *store, uint32 rows)
+doltgres_scan_tests_built(HostIoIo *io, TreesStoreMemoryStore *store, uint32 from, uint32 to, uint32 add_line,
+ uint32 add_col, uint32 finished_line, uint32 finished_col)
{
TreesBuildBuilder builder;
trees_build_Builder_new(TREES_STORE_MEMORY_STORE_NODE_STORE(store), 256, &builder);
- for (uint32 n = 0; n < rows; n++)
+ for (uint32 n = from; n < to; n++)
{
StdVec key = doltgres_scan_tests_be4(n);
StdVec value = doltgres_scan_tests_le4(n * n);
@@ -6534,9 +7648,19 @@ doltgres_scan_tests_base(HostIoIo *io, TreesStoreMemoryStore *store, uint32 rows
trees_build_Entry_new_VecU8VecU8(&key, &value, &entry);
std_vec_push_Checked(&entry.stamps, &stamp, sizeof(ChunksTrailerStamp), _Alignof(ChunksTrailerStamp));
- doltgres_scan_tests_add(&builder, io, &entry, 1837, 105);
+ doltgres_scan_tests_add(&builder, io, &entry, add_line, add_col);
}
- return doltgres_scan_tests_finished(&builder, io, 1839, 53);
+ return doltgres_scan_tests_finished(&builder, io, finished_line, finished_col);
+}
+
+/*
+ * doltgres_scan_tests_base builds a tree of rows rows in a store, as built
+ * builds the rows from 0, and returns its root's name.
+ */
+static TreesStoreTree
+doltgres_scan_tests_base(HostIoIo *io, TreesStoreMemoryStore *store, uint32 rows)
+{
+ return doltgres_scan_tests_built(io, store, 0, rows, 1837, 105, 1839, 53);
}
/* doltgres_scan_tests_next reads a scan's next row, which unwraps at a line and column. */
@@ -6638,8 +7762,8 @@ doltgres_scan_tests_begin(HostIoIo *io, TreesStoreMemoryStore *store, uint64 blo
HostError failed;
std_slice_to_vec_Checked(&layout, keyed, keyed_len, DOLTGRES_SCAN_BYTES);
- if (!doltgres_scan_scan_begin(io, TREES_STORE_MEMORY_STORE_NODE_STORE(store), blocks, base, &layout, table, &order,
- &handle, &failed))
+ if (!doltgres_scan_scan_begin(io, TREES_STORE_MEMORY_STORE_NODE_STORE(store), blocks, base, 0, &layout, table,
+ &order, &handle, &failed))
STD_RESULT_EXPECT_FAILED(line, col, DOLTGRES_SCAN_TESTS_UNWRAP, &failed, host_Error_Debug_fmt);
return handle;
}
@@ -7084,11 +8208,13 @@ doltgres_scan_tests_fetch(HostIoIo *io, TreesStoreMemoryStore *store, const Dolt
DoltgresScanRow *row, HostError *failed)
{
DoltgresScanEpochs epochs;
+ DoltgresScanResolved resolved = {0};
bool ok;
doltgres_scan_Epochs_Default_default(&epochs);
- ok = doltgres_scan_fetch(io, TREES_STORE_MEMORY_STORE_NODE_STORE(store), under, &epochs,
+ ok = doltgres_scan_fetch(io, TREES_STORE_MEMORY_STORE_NODE_STORE(store), under, &epochs, &resolved,
doltgres_scan_tests_trees(base), &layout, tuple_id, has, row, failed);
+ doltgres_scan_Resolved_drop(&resolved);
doltgres_scan_Epochs_drop(&epochs);
return ok;
}
@@ -7652,7 +8778,7 @@ doltgres_scan_tests_begin_parallel(HostIoIo *io, DoltgresScanShared *shared, con
HostError failed;
std_slice_to_vec_Checked(&layout, keyed, keyed_len, DOLTGRES_SCAN_BYTES);
- if (!doltgres_scan_begin_parallel(io, shared, &layout, 0, &order, &handle, &failed))
+ if (!doltgres_scan_begin_parallel(io, shared, 0, &layout, 0, &order, &handle, &failed))
STD_RESULT_EXPECT_FAILED(line, col, DOLTGRES_SCAN_TESTS_UNWRAP, &failed, host_Error_Debug_fmt);
return handle;
}
@@ -8808,7 +9934,7 @@ test_doltgres_scan_tests_a_compared_scan_merges_the_deltas_keys_in_the_tables_or
under.has_serial = false;
based = doltgres_scan_tests_based(tree, NULL, 1);
std_slice_to_vec_Checked(&layout, keyed, 1, DOLTGRES_SCAN_BYTES);
- if (!doltgres_scan_scan_begin(io, TREES_STORE_MEMORY_STORE_NODE_STORE(&store), 1, &based, &layout, 0, &order,
+ if (!doltgres_scan_scan_begin(io, TREES_STORE_MEMORY_STORE_NODE_STORE(&store), 1, &based, 0, &layout, 0, &order,
&handle, &failed))
STD_RESULT_EXPECT_FAILED(2697, 95, DOLTGRES_SCAN_TESTS_UNWRAP, &failed, host_Error_Debug_fmt);
w = writer.f0;
@@ -9424,6 +10550,449 @@ test_doltgres_scan_tests_a_scan_with_no_sample_or_range_leaves_its_answers_as_th
doltgres_scan_tests_run(test_doltgres_scan_tests_a_scan_with_no_sample_or_range_leaves_its_answers_as_they_were_closure_0);
}
+/*
+ * test_doltgres_scan_tests_a_scan_moves_to_a_newer_base_at_its_next_read_and_gives_every_row_once_closure_0
+ * is the case's actor.
+ */
+static void
+test_doltgres_scan_tests_a_scan_moves_to_a_newer_base_at_its_next_read_and_gives_every_row_once_closure_0(void *captures)
+{
+ HostIoIo *io = ((doltgres_scan_tests_case_t *) captures)->io;
+ static const uint8 keyed[] = {1};
+ TreesStoreMemoryStore store = trees_store_MemoryStore_new();
+ HostSharedHeld held = doltgres_scan_tests_made(io);
+ HostSharedRecord state = host_shared_Held_record(&held);
+ TreesStoreTree older = doltgres_scan_tests_base(io, &store, 4);
+ TreesStoreTree newer = doltgres_scan_tests_base(io, &store, 8);
+ HostXid writer = simc_begin();
+ TxnStatusOutcome committed = {.tag = TXN_STATUS_OUTCOME_COMMITTED, .u.committed = 100};
+ TxnStatusRoomed roomed;
+ bool took;
+ SimcTaken after_taken;
+ HostSnapshot after;
+ TxnBaseBase based;
+ TxnBaseBase installed;
+ uint64 generation;
+ HostOwnedHandle handle;
+ DoltgresScanUnder under;
+ StdVec rows;
+ StdVec rest;
+ HostError error;
+
+ for (uint32 n = 4; n < 8; n++)
+ {
+ StdVec key = doltgres_scan_tests_be4(n);
+ StdVec tuple = doltgres_scan_tests_le4(n * n);
+ TxnMemtableOp op = {.tag = TXN_MEMTABLE_OP_PUT};
+
+ op.u.put = doltgres_scan_tests_put(tuple.ptr, tuple.len, (uint64) n + 1);
+ doltgres_scan_tests_pushed(io, &held, key.ptr, key.len, writer, 0, &op);
+ std_vec_free_Checked(&tuple, DOLTGRES_SCAN_BYTES);
+ std_vec_free_Checked(&key, DOLTGRES_SCAN_BYTES);
+ }
+ if (!txn_status_take(io, writer, committed, &roomed, &took, &error))
+ STD_RESULT_EXPECT_FAILED(__LINE__, 0, DOLTGRES_SCAN_TESTS_UNWRAP, &error, host_Error_Debug_fmt);
+ simc_commit(writer);
+ after_taken = simc_snapshot(HOST_SNAPSHOT_KIND_MVCC, false);
+ after = simc_Taken_lent(&after_taken);
+ generation = txn_state_generation(state);
+ based = doltgres_scan_tests_based(older, NULL, 1);
+ handle = doltgres_scan_tests_begin(io, &store, 1, &based, keyed, 1, 0, __LINE__, 0);
+ under.state = state;
+ under.snapshot = &after;
+ under.has_serial = false;
+ std_vec_new(&rows, sizeof(DoltgresScanRow), _Alignof(DoltgresScanRow));
+ for (int i = 0; i < 6; i++)
+ {
+ DoltgresScanRow row;
+
+ STD_ASSERT(__LINE__, 0,
+ doltgres_scan_tests_next(io, &store, &under, handle, DOLTGRES_SCAN_DIRECTION_FORWARD, &row,
+ __LINE__, 0),
+ "next(io, &store, &under, handle, Direction::Forward).unwrap().is_some()");
+ std_vec_push_Checked(&rows, &row, sizeof(DoltgresScanRow), _Alignof(DoltgresScanRow));
+ }
+ installed = doltgres_scan_tests_based(newer, NULL, 1);
+ installed.folded = 200;
+ if (!txn_state_installed(HOST_IO_IO_TAKES_SHORT(io), &held, generation, &installed, NULL, NULL, &took, &error))
+ STD_RESULT_EXPECT_FAILED(__LINE__, 0, DOLTGRES_SCAN_TESTS_UNWRAP, &error, host_Error_Debug_fmt);
+ STD_ASSERT(__LINE__, 0, took, "state::installed(io, &held, generation, &installed).unwrap()");
+ for (uint32 n = 4; n < 8; n++)
+ {
+ StdVec key = doltgres_scan_tests_be4(n);
+ StdVec versions;
+ StdVec handles;
+ StdVec removed;
+ HostDeltaHold hold;
+
+ if (!host_delta_hold(io, &hold, &error) ||
+ !txn_memtable_table_versions(io, &hold, state, key.ptr, key.len, &versions, &error))
+ STD_RESULT_EXPECT_FAILED(__LINE__, 0, DOLTGRES_SCAN_TESTS_UNWRAP, &error, host_Error_Debug_fmt);
+ std_vec_new(&handles, sizeof(TxnMemtableVersionHandle), _Alignof(TxnMemtableVersionHandle));
+ for (size_t v = 0; v < versions.len; v++)
+ std_vec_push_Checked(&handles, &STD_VEC_AT(&versions, TxnMemtableVersionVersion, v).handle,
+ sizeof(TxnMemtableVersionHandle), _Alignof(TxnMemtableVersionHandle));
+ txn_memtable_version_versions_drop(&versions, 0);
+ host_delta_Hold_drop(&hold);
+ if (!txn_memtable_table_removed(&TXN_MEMTABLE_TABLE_ROWS_TREE_OPS, io, state, key.ptr, key.len, handles.ptr,
+ handles.len, &removed, &error))
+ STD_RESULT_EXPECT_FAILED(__LINE__, 0, DOLTGRES_SCAN_TESTS_UNWRAP, &error, host_Error_Debug_fmt);
+ std_vec_free_Checked(&removed, sizeof(bool), _Alignof(bool));
+ std_vec_free_Checked(&handles, sizeof(TxnMemtableVersionHandle), _Alignof(TxnMemtableVersionHandle));
+ std_vec_free_Checked(&key, DOLTGRES_SCAN_BYTES);
+ }
+ rest = doltgres_scan_tests_read(io, &store, &under, handle, DOLTGRES_SCAN_DIRECTION_FORWARD);
+ for (size_t i = 0; i < rest.len; i++)
+ std_vec_push_Checked(&rows, &STD_VEC_AT(&rest, DoltgresScanRow, i), sizeof(DoltgresScanRow),
+ _Alignof(DoltgresScanRow));
+ std_vec_free_Checked(&rest, sizeof(DoltgresScanRow), _Alignof(DoltgresScanRow));
+ STD_ASSERT(__LINE__, 0, doltgres_scan_tests_expected(&rows, 0, 8, false), "every row once across the install");
+ doltgres_scan_tests_rows_drop(&rows);
+ rows = doltgres_scan_tests_read(io, &store, &under, handle, DOLTGRES_SCAN_DIRECTION_BACKWARD);
+ STD_ASSERT(__LINE__, 0, doltgres_scan_tests_expected(&rows, 0, 8, true),
+ "read(io, &store, &under, handle, Direction::Backward) == reversed(&expected(8))");
+ doltgres_scan_tests_rows_drop(&rows);
+ doltgres_scan_tests_end(io, handle, __LINE__, 0);
+ simc_Taken_drop(&after_taken);
+ host_shared_Held_drop(&held);
+ trees_store_MemoryStore_drop(&store);
+}
+
+/*
+ * Bugs rows 240 and 241: a scan that has read part of its base and of the
+ * delta when an install gives the storage a newer base holding the delta's
+ * committed rows, and a drop takes them out of the delta, moves to the newer
+ * base at its next read and goes on past its group's key, so it gives every
+ * row once, and its read back after gives them all once the other way. A
+ * C-only case.
+ */
+void
+test_doltgres_scan_tests_a_scan_moves_to_a_newer_base_at_its_next_read_and_gives_every_row_once(void)
+{
+ doltgres_scan_tests_run(test_doltgres_scan_tests_a_scan_moves_to_a_newer_base_at_its_next_read_and_gives_every_row_once_closure_0);
+}
+
+/* DOLTGRES_SCAN_TESTS_HELD_DATABASE is the held reader case's database. */
+#define DOLTGRES_SCAN_TESTS_HELD_DATABASE 9631
+
+/*
+ * doltgres_scan_tests_held_t is the held reader case's actors' captures: the
+ * store, the older and newer bases' trees, the base generation the reader
+ * takes its base at, and the rows the reader read.
+ */
+typedef struct doltgres_scan_tests_held_t
+{
+ TreesStoreMemoryStore *store;
+ TreesStoreTree older;
+ TreesStoreTree newer;
+ uint64 generation;
+ StdVec *rows;
+} doltgres_scan_tests_held_t;
+
+/* doltgres_scan_tests_held_storage returns the held reader case's storage. */
+static HostStorage
+doltgres_scan_tests_held_storage(void)
+{
+ HostStorage storage = {HOST_PG_DEFAULT, DOLTGRES_SCAN_TESTS_HELD_DATABASE, 16384};
+
+ return storage;
+}
+
+/* doltgres_scan_tests_held_state opens the held reader case's repository and its storage's state. */
+static bool
+doltgres_scan_tests_held_state(HostIoIo *io, HostSharedHeld *result, HostError *error)
+{
+ HostStorage storage = doltgres_scan_tests_held_storage();
+ RepoRepo repo;
+ HostSharedReader reading;
+ ChunksErrorError failed;
+ bool ok;
+
+ if (!repo_Repo_open(io, DOLTGRES_SCAN_TESTS_HELD_DATABASE, HOST_PG_DEFAULT, &repo, &failed))
+ {
+ *error = txn_base_storage(&failed);
+ return false;
+ }
+ ok = host_shared_read(io, &reading, error);
+ if (ok)
+ {
+ ok = txn_state_state(io, &repo, &reading, &storage, TXN_STATE_PERSISTENCE_PERMANENT, false, NULL, result,
+ error);
+ host_shared_Reader_drop(&reading);
+ }
+ repo_Repo_drop(&repo);
+ return ok;
+}
+
+/* doltgres_scan_tests_held_reader is the held reader case's reader: it reads the scan of the older base to its end. */
+static bool
+doltgres_scan_tests_held_reader(void *captures, HostIoIo *io, HostError *error)
+{
+ const doltgres_scan_tests_held_t *c = captures;
+ static const uint8 keyed[] = {1};
+ TxnBaseBase based = doltgres_scan_tests_based(c->older, NULL, 1);
+ DoltgresScanStores stores = doltgres_scan_tests_stores(c->store);
+ DoltgresScanLayout layout = doltgres_scan_tests_layout(keyed, 1, 0);
+ HostSharedHeld held;
+ SimcTaken taken;
+ HostSnapshot snapshot;
+ DoltgresScanUnder under = {.has_serial = false};
+ HostOwnedHandle handle;
+ bool ok;
+
+ if (!doltgres_scan_tests_held_state(io, &held, error))
+ {
+ doltgres_scan_Layout_drop(&layout);
+ return false;
+ }
+ taken = simc_snapshot(HOST_SNAPSHOT_KIND_MVCC, false);
+ snapshot = simc_Taken_lent(&taken);
+ under.state = host_shared_Held_record(&held);
+ under.snapshot = &snapshot;
+ ok = doltgres_scan_scan_begin(io, TREES_STORE_MEMORY_STORE_NODE_STORE(c->store), 1, &based, c->generation,
+ &layout.keyed, 0, &layout.order, &handle, error);
+ for (;;)
+ {
+ DoltgresScanRow row;
+ bool has;
+
+ if (!ok)
+ break;
+ ok = doltgres_scan_next(io, &stores, &under, handle, DOLTGRES_SCAN_DIRECTION_FORWARD, &has, &row, error);
+ if (!ok || !has)
+ break;
+ std_vec_push_Checked(c->rows, &row, sizeof(DoltgresScanRow), _Alignof(DoltgresScanRow));
+ }
+ ok = ok && doltgres_scan_scan_end(io, handle, error);
+ simc_Taken_drop(&taken);
+ host_shared_Held_drop(&held);
+ return ok;
+}
+
+/* doltgres_scan_tests_held_folder is the held reader case's folder: it installs the newer base and drops the spills. */
+static bool
+doltgres_scan_tests_held_folder(void *captures, HostIoIo *io, HostError *error)
+{
+ const doltgres_scan_tests_held_t *c = captures;
+ TxnBaseBase installed = doltgres_scan_tests_based(c->newer, NULL, 1);
+ HostSharedHeld held;
+ bool took = false;
+ bool ok;
+
+ installed.folded = 200;
+ if (!doltgres_scan_tests_held_state(io, &held, error))
+ return false;
+ ok = txn_state_installed(HOST_IO_IO_TAKES_SHORT(io), &held, c->generation, &installed, NULL, NULL, &took, error);
+ STD_ASSERT(__LINE__, 0, !ok || took, "state::installed(io, &held, generation, &installed)?");
+ ok = ok && txn_spill_folded(io, host_shared_Held_record(&held), 200, error);
+ host_shared_Held_drop(&held);
+ return ok;
+}
+
+/*
+ * Bugs rows 240 and 241's check after the delta read: a scan's first read,
+ * held inside its read of the delta before it reads the spill files that
+ * hold the delta's committed rows, all below its base's first entry, while
+ * an install gives the storage a newer base holding them and a drop takes
+ * their files out, finds once it read the delta that the base moved, moves
+ * to the newer base, and gives every row once; with the check before the
+ * delta read it pairs its base with the dropped delta and misses them. A
+ * C-only case.
+ */
+void
+test_doltgres_scan_tests_a_scan_held_inside_its_delta_read_across_an_install_and_drop_gives_every_row_once(void)
+{
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ HostStorage storage = doltgres_scan_tests_held_storage();
+ TreesStoreMemoryStore store = trees_store_MemoryStore_new();
+ StdVec rows;
+ doltgres_scan_tests_held_t captures = {.store = &store, .rows = &rows};
+ SimcActor reader = {doltgres_scan_tests_held_reader, &captures};
+ SimcActor folder = {doltgres_scan_tests_held_folder, &captures};
+ SimcStep steps[2] = {{"folder", 6, PG_UINT64_MAX}, {"reader", 6, PG_UINT64_MAX}};
+ SimcEnding returned = {.tag = SIMC_ENDING_RETURNED};
+ TxnStatusOutcome committed = {.tag = TXN_STATUS_OUTCOME_COMMITTED, .u.committed = 100};
+ TxnStatusRoomed roomed;
+ RepoRepo repo;
+ HostSharedReader reading;
+ HostSharedHeld held;
+ HostXid writer;
+ ChunksErrorError created;
+ HostError error;
+ SimcRun run;
+ SimcReport report;
+ bool spilled;
+ bool took;
+ bool has_count;
+ uint64 count;
+
+ captures.older = doltgres_scan_tests_built(io, &store, 8, 12, __LINE__, 0, __LINE__, 0);
+ captures.newer = doltgres_scan_tests_built(io, &store, 0, 12, __LINE__, 0, __LINE__, 0);
+ if (!repo_Repo_create(io, DOLTGRES_SCAN_TESTS_HELD_DATABASE, HOST_PG_DEFAULT, &repo, &created))
+ STD_RESULT_EXPECT_FAILED(__LINE__, 0, DOLTGRES_SCAN_TESTS_UNWRAP, &created, chunks_error_Error_Debug_fmt);
+ if (!host_shared_read(io, &reading, &error) ||
+ !txn_state_state(io, &repo, &reading, &storage, TXN_STATE_PERSISTENCE_PERMANENT, false, NULL, &held, &error))
+ STD_RESULT_EXPECT_FAILED(__LINE__, 0, DOLTGRES_SCAN_TESTS_UNWRAP, &error, host_Error_Debug_fmt);
+ writer = simc_begin();
+ for (uint32 n = 0; n < 8; n++)
+ {
+ StdVec key = doltgres_scan_tests_be4(n);
+ StdVec tuple = doltgres_scan_tests_le4(n * n);
+ TxnMemtableOp op = {.tag = TXN_MEMTABLE_OP_PUT};
+
+ op.u.put = doltgres_scan_tests_put(tuple.ptr, tuple.len, (uint64) n + 1);
+ doltgres_scan_tests_pushed(io, &held, key.ptr, key.len, writer, 0, &op);
+ std_vec_free_Checked(&tuple, DOLTGRES_SCAN_BYTES);
+ std_vec_free_Checked(&key, DOLTGRES_SCAN_BYTES);
+ }
+ if (!txn_spill_spilled(io, &storage, TXN_WRITTEN_OF_ROWS, writer.f0, &spilled, &error) ||
+ !txn_status_take(io, writer, committed, &roomed, &took, &error) ||
+ !txn_status_add(io, writer, 8, &has_count, &count, &error))
+ STD_RESULT_EXPECT_FAILED(__LINE__, 0, DOLTGRES_SCAN_TESTS_UNWRAP, &error, host_Error_Debug_fmt);
+ STD_ASSERT(__LINE__, 0, spilled, "spill::spilled(io, &storage, Of::Rows, writer.0).unwrap()");
+ simc_commit(writer);
+ captures.generation = txn_state_generation(host_shared_Held_record(&held));
+ host_shared_Held_drop(&held);
+ host_shared_Reader_drop(&reading);
+ repo_Repo_drop(&repo);
+ std_vec_new(&rows, sizeof(DoltgresScanRow), _Alignof(DoltgresScanRow));
+ run = simc_Run_new(5);
+ simc_Run_actor(&run, "reader", 6, reader);
+ simc_Run_actor(&run, "folder", 6, folder);
+ simc_Run_until(&run, "reader", 6, "doltgres-spill-slots-read");
+ simc_Run_script(&run, steps, lengthof(steps));
+ report = simc_Run_finish(run);
+ STD_ASSERT(__LINE__, 0, report.outcome.tag == SIMC_OUTCOME_ENDED, "the run ended");
+ for (size_t k = 0; k < report.outcome.u.ended.len; k++)
+ STD_ASSERT(__LINE__, 0,
+ simc_Ending_PartialEq_eq(&STD_VEC_AT(&report.outcome.u.ended, simc_TupleStringEnding_t, k).f1,
+ &returned),
+ "every actor returned Ok(())");
+ STD_ASSERT(__LINE__, 0, doltgres_scan_tests_expected(&rows, 0, 12, false),
+ "every row once across the install and drop");
+ doltgres_scan_tests_rows_drop(&rows);
+ simc_Report_drop(&report);
+ trees_store_MemoryStore_drop(&store);
+ simc_TestIo_drop(&test_io);
+}
+
+/* DOLTGRES_SCAN_TESTS_SPREAD is how many rows the memo case spreads over its four keyless entries. */
+#define DOLTGRES_SCAN_TESTS_SPREAD 4000
+
+/*
+ * doltgres_scan_tests_spread builds a keyless table's base of four entries
+ * whose copies take tuple ids in turn, as a statement inserting four values
+ * in turn writes them, so that each copy is a run of its own, the copy of
+ * tuple id t of transaction txn plus t / 1000, each entry's row the key's
+ * byte plus a shift, with its tuple id tree.
+ */
+static TxnBaseBase
+doltgres_scan_tests_spread(HostIoIo *io, TreesStoreMemoryStore *store, uint64 txn, uint8 shift)
+{
+ static uint64 tuple_ids[DOLTGRES_SCAN_TESTS_SPREAD];
+ static StdVec keys[DOLTGRES_SCAN_TESTS_SPREAD];
+ TreesBuildBuilder builder;
+ TreesStoreTree tree;
+ TreesStoreTree ids;
+
+ trees_build_Builder_new(TREES_STORE_MEMORY_STORE_NODE_STORE(store), 256, &builder);
+ for (uint8 byte = 1; byte <= 4; byte++)
+ {
+ StdVec key;
+ StdVec value;
+ TreesBuildEntry entry;
+ uint8 row = byte + shift;
+
+ std_vec_u8_from_elem(&key, byte, 16);
+ txn_counted_keyless_value(DOLTGRES_SCAN_TESTS_SPREAD / 4, &row, 1, &value);
+ trees_build_Entry_new_VecU8VecU8(&key, &value, &entry);
+ for (uint64 tuple = byte; tuple <= DOLTGRES_SCAN_TESTS_SPREAD; tuple += 4)
+ {
+ ChunksTrailerStamp stamp = {txn + tuple / 1000, tuple};
+
+ std_vec_push_Checked(&entry.stamps, &stamp, sizeof(ChunksTrailerStamp), _Alignof(ChunksTrailerStamp));
+ }
+ doltgres_scan_tests_add(&builder, io, &entry, __LINE__, 9);
+ }
+ tree = doltgres_scan_tests_finished(&builder, io, __LINE__, 9);
+ for (size_t i = 0; i < DOLTGRES_SCAN_TESTS_SPREAD; i++)
+ {
+ tuple_ids[i] = i + 1;
+ std_vec_u8_from_elem(&keys[i], (uint8) (i % 4 + 1), 16);
+ }
+ ids = doltgres_scan_tests_pairs(io, store, tuple_ids, keys, DOLTGRES_SCAN_TESTS_SPREAD);
+ return doltgres_scan_tests_based(tree, &ids, 1);
+}
+
+/* doltgres_scan_tests_memo_fetch fetches a tuple id of a spread base through a memo, asserting its row's key, row, and stamp at a line. */
+static void
+doltgres_scan_tests_memo_fetch(HostIoIo *io, doltgres_scan_tests_under_t *u, const TxnBaseBase *base,
+ DoltgresScanEpochs *epochs, DoltgresScanResolved *resolved, uint64 tuple, uint64 txn,
+ uint8 shift, uint32 line)
+{
+ DoltgresScanLayout layout = doltgres_scan_tests_layout(NULL, 0, 0);
+ uint8 key[16];
+ uint8 byte = (uint8) ((tuple - 1) % 4 + 1);
+ uint8 expected = byte + shift;
+ DoltgresScanRow row;
+ HostError failed;
+ bool has;
+
+ memset(key, byte, sizeof(key));
+ if (!doltgres_scan_fetch(io, TREES_STORE_MEMORY_STORE_NODE_STORE(&u->store), &u->under, epochs, resolved,
+ doltgres_scan_tests_trees(base), &layout, tuple, &has, &row, &failed))
+ STD_RESULT_EXPECT_FAILED(line, 13, DOLTGRES_SCAN_TESTS_UNWRAP, &failed, host_Error_Debug_fmt);
+ if (!has)
+ STD_OPTION_UNWRAP_FAILED(line, 13);
+ STD_ASSERT(line, 9,
+ doltgres_scan_tests_bytes_is(&row.key, key, sizeof(key)) &&
+ doltgres_scan_tests_bytes_is(&row.tuple, &expected, 1) && row.stamp.tuple == tuple &&
+ row.stamp.txn == txn + tuple / 1000,
+ "(row.key, row.tuple, row.stamp) == (vec![byte; 16], vec![byte + shift], (txn + tuple / 1000, tuple))");
+ doltgres_scan_Row_drop(&row);
+}
+
+/* test_doltgres_scan_tests_fetches_through_one_memo_give_each_keyless_copys_own_stamp_closure_0 is the case's actor. */
+static void
+test_doltgres_scan_tests_fetches_through_one_memo_give_each_keyless_copys_own_stamp_closure_0(void *captures)
+{
+ HostIoIo *io = ((doltgres_scan_tests_case_t *) captures)->io;
+ doltgres_scan_tests_under_t u;
+ DoltgresScanEpochs epochs;
+ DoltgresScanResolved resolved = {0};
+ TxnBaseBase first;
+ TxnBaseBase second;
+
+ doltgres_scan_tests_under(io, &u);
+ doltgres_scan_Epochs_Default_default(&epochs);
+ first = doltgres_scan_tests_spread(io, &u.store, 100, 0);
+ for (uint64 tuple = 1; tuple <= DOLTGRES_SCAN_TESTS_SPREAD; tuple++)
+ doltgres_scan_tests_memo_fetch(io, &u, &first, &epochs, &resolved, tuple, 100, 0, __LINE__);
+ second = doltgres_scan_tests_spread(io, &u.store, 900, 10);
+ for (uint64 tuple = 1; tuple <= DOLTGRES_SCAN_TESTS_SPREAD; tuple += 333)
+ doltgres_scan_tests_memo_fetch(io, &u, &second, &epochs, &resolved, tuple, 900, 10, __LINE__);
+ doltgres_scan_tests_memo_fetch(io, &u, &first, &epochs, &resolved, 7, 100, 0, __LINE__);
+ doltgres_scan_Resolved_drop(&resolved);
+ doltgres_scan_Epochs_drop(&epochs);
+ doltgres_scan_tests_under_drop(&u);
+}
+
+/*
+ * test_doltgres_scan_tests_fetches_through_one_memo_give_each_keyless_copys_own_stamp:
+ * bugs row 346: fetches of every tuple id of a keyless table whose copies
+ * take tuple ids in turn, through one memo of the leaf they last resolved a
+ * row in, each give the copy's own key and stamp; a second base of the same
+ * keys, other rows and other transactions' copies, fetched through the same
+ * memo, gives its own rows and stamps, never the first base's kept leaf's,
+ * and the first again its own.
+ */
+void
+test_doltgres_scan_tests_fetches_through_one_memo_give_each_keyless_copys_own_stamp(void)
+{
+ doltgres_scan_tests_run(test_doltgres_scan_tests_fetches_through_one_memo_give_each_keyless_copys_own_stamp_closure_0);
+}
+
#endif /* DOLTGRES_TEST_DOLTGRES */
#endif /* FRONTEND */
diff --git a/src/doltgres/doltgres/in_progress.c b/src/doltgres/doltgres/in_progress.c
index 3052110682115..60493fcb9b73c 100644
--- a/src/doltgres/doltgres/in_progress.c
+++ b/src/doltgres/doltgres/in_progress.c
@@ -25,6 +25,9 @@
#include "postgres.h"
#include "access/subtrans.h"
+#include "access/transam.h"
+#include "access/xlog.h"
+#include "access/xlogutils.h"
#include "doltgres/in_progress.h"
#include "storage/proc.h"
#include "storage/procarray.h"
@@ -33,7 +36,10 @@
* doltgres_xid_in_progress says whether a transaction or a subtransaction is
* in progress, as TransactionIdIsInProgress does, also for a caller outside the
* proc array: the transaction in the slot at the caller's offset, its cached
- * subtransactions, and, past its cache, the ones pg_subtrans records.
+ * subtransactions, and, past its cache, the ones pg_subtrans records. In
+ * recovery with no standby's bookkeeping, where redo marks pg_xact but never
+ * advances latestCompletedXid, it answers from pg_xact alone, an xid below
+ * the oldest one pg_xact keeps being one that ended (bugs row 340).
*/
bool
doltgres_xid_in_progress(TransactionId xid)
@@ -43,6 +49,16 @@ doltgres_xid_in_progress(TransactionId xid)
bool found = false;
bool overflowed = false;
+ if (standbyState == STANDBY_DISABLED && RecoveryInProgress())
+ {
+ if (!TransactionIdIsNormal(xid))
+ return false;
+ LWLockAcquire(XactTruncationLock, LW_SHARED);
+ found = !TransactionIdPrecedes(xid, TransamVariables->oldestClogXid) && !TransactionIdDidCommit(xid) &&
+ !TransactionIdDidAbort(xid);
+ LWLockRelease(XactTruncationLock);
+ return found;
+ }
if (TransactionIdIsInProgress(xid))
return true;
if (MyProc == NULL || !TransactionIdIsNormal(xid))
diff --git a/src/doltgres/host/c/delta/directory.c b/src/doltgres/host/c/delta/directory.c
index 29cfa5260f3dc..3108772fc5336 100644
--- a/src/doltgres/host/c/delta/directory.c
+++ b/src/doltgres/host/c/delta/directory.c
@@ -56,6 +56,8 @@ static void host_delta_directory_held(HostDeltaDirectoryDirectory directory, uin
static bool host_delta_directory_raise(HostDeltaDirectoryDirectory directory, uint64 stamp);
static bool host_delta_directory_owns(HostDeltaDirectoryDirectory directory, uint32 slot,
HostDeltaDirectoryOwner owner, HostError *error);
+static void host_delta_directory_freed_slot(HostDeltaDirectoryDirectory directory, uint32 slot, uint64 loaded,
+ uint64 oldest, bool *has_kept, uint64 *kept, bool *keep);
/* host_delta_directory_internal returns the fork's internal error with a message. */
static HostError
@@ -994,11 +996,47 @@ host_delta_directory_seal(HostDeltaDirectoryDirectory directory,
return true;
}
+/*
+ * host_delta_directory_listed_record pushes an aged record its retirement
+ * just retired onto its kind's retired list, by a compare-and-swap of the
+ * list's head (bugs row 309).
+ */
+static void
+host_delta_directory_listed_record(HostDeltaDirectoryDirectory directory, uint32 kind, uint32 index)
+{
+ for (;;)
+ {
+ uint64 head = directory.ops->record_retired_head(directory.self, kind);
+
+ directory.ops->set_record_link(directory.self, kind, index, head);
+ if (directory.ops->record_retired_swap(directory.self, kind, head, (uint64) index + 1))
+ return;
+ }
+}
+
+/*
+ * host_delta_directory_listed pushes a slot its retirement just retired onto
+ * the retired list, by a compare-and-swap of the list's head (bugs row 309).
+ */
+static void
+host_delta_directory_listed(HostDeltaDirectoryDirectory directory, uint32 slot)
+{
+ for (;;)
+ {
+ uint64 head = directory.ops->retired_head(directory.self);
+
+ directory.ops->set_link(directory.self, slot, (uint32) head);
+ if (directory.ops->retired_swap(directory.self, head, (uint64) slot + 1))
+ return;
+ }
+}
+
/*
* host_delta_directory_retire retires one batch of records and names under
* one take of the epoch lock: it stores the batch's one stamp and then the
* retired state in each record that is still published at its location and
- * each page that is still live at its name, and then advances the epoch.
+ * each page that is still live at its name, pushing each onto its retired
+ * list, and then advances the epoch.
*/
void
host_delta_directory_retire(HostDeltaDirectoryDirectory directory,
@@ -1032,7 +1070,8 @@ host_delta_directory_retire(HostDeltaDirectoryDirectory directory,
{
directory.ops->set_stamp(directory.self, kind, index, stamp);
directory.ops->stop(directory.self, HOST_DELTA_DIRECTORY_POINT_STAMPING);
- directory.ops->record_swap(directory.self, kind, index, loaded, retired);
+ if (directory.ops->record_swap(directory.self, kind, index, loaded, retired))
+ host_delta_directory_listed_record(directory, kind, index);
}
}
for (size_t i = 0; i < names_len; i++)
@@ -1055,7 +1094,8 @@ host_delta_directory_retire(HostDeltaDirectoryDirectory directory,
{
directory.ops->store(directory.self, slot, HOST_DELTA_DIRECTORY_FIELD_STAMP, stamp);
directory.ops->stop(directory.self, HOST_DELTA_DIRECTORY_POINT_STAMPING);
- directory.ops->swap(directory.self, slot, loaded, retired);
+ if (directory.ops->swap(directory.self, slot, loaded, retired))
+ host_delta_directory_listed(directory, slot);
}
}
directory.ops->stop(directory.self, HOST_DELTA_DIRECTORY_POINT_STAMPED);
@@ -1065,12 +1105,55 @@ host_delta_directory_retire(HostDeltaDirectoryDirectory directory,
}
/*
- * host_delta_directory_pass is the free pass: with the oldest published epoch
- * loaded once, it walks every aged kind's records in the table's order and
- * then the page directory, and claims each retired record or slot whose stamp
- * that epoch has passed and frees it at its next generation, a page's body
- * given back to its block first, each of which counts as a free. A pass that
- * loses a claim does nothing with that record or slot. A pass that left no
+ * host_delta_directory_freed_slot frees a retired slot whose stamp the oldest
+ * epoch has passed, a page's body given back to its block first, counting the
+ * free, or else sets keep and lowers the lowest stamp kept; a slot not
+ * retired is neither. Its claim can't lose, since only a pass's claim takes a
+ * word out of retired and passes run one at a time.
+ */
+static void
+host_delta_directory_freed_slot(HostDeltaDirectoryDirectory directory, uint32 slot, uint64 loaded, uint64 oldest,
+ bool *has_kept, uint64 *kept, bool *keep)
+{
+ uint64 freeing;
+ uint64 stamp;
+ uint64 released;
+ bool claimed PG_USED_FOR_ASSERTS_ONLY;
+
+ *keep = false;
+ if (!host_delta_page_reap(loaded, &freeing))
+ return;
+ stamp = directory.ops->load(directory.self, slot, HOST_DELTA_DIRECTORY_FIELD_STAMP);
+ if (!host_delta_page_passed(stamp, oldest))
+ {
+ *kept = *has_kept ? std_min_u64(*kept, stamp) : stamp;
+ *has_kept = true;
+ *keep = true;
+ return;
+ }
+ claimed = directory.ops->swap(directory.self, slot, loaded, freeing);
+ Assert(claimed);
+ directory.ops->stop(directory.self, HOST_DELTA_DIRECTORY_POINT_CLAIMED);
+ host_delta_blocks_give(directory, directory.ops->load(directory.self, slot, HOST_DELTA_DIRECTORY_FIELD_BODY));
+ directory.ops->store(directory.self, slot, HOST_DELTA_DIRECTORY_FIELD_BODY, 0);
+ host_delta_directory_freed(directory, slot, false);
+ released = host_delta_directory_emptied(directory, slot, freeing);
+ directory.ops->stop(directory.self, HOST_DELTA_DIRECTORY_POINT_HINTING);
+ host_delta_directory_hinted(directory, slot, released);
+ directory.ops->freed(directory.self);
+}
+
+/*
+ * host_delta_directory_pass is the free pass, one at a time under the pass's
+ * lock: with the oldest published epoch loaded once, it takes each aged
+ * kind's retired list whole, in the table's order, and then the slots', and
+ * claims each retired record or slot whose stamp that epoch has passed and
+ * frees it at its next generation, a page's body given back to its block
+ * first, each of which counts as a free, pushing the ones it keeps back onto
+ * their list as one chain, so its work is the retirements it finds and never
+ * the directory's or a pool's size (bugs row 309). Its claims can't lose,
+ * since only a pass's claim takes a word out of retired, the pool's doors
+ * refusing an aged kind, and passes run one at a time. A pass that left no
* retired record or slot unfreed clears the count of retirements it loaded,
* unless a retirement since changed it. It loads the current epoch before the
* oldest, and records the lowest stamp it kept (held).
@@ -1084,6 +1167,7 @@ host_delta_directory_pass(HostDeltaDirectoryDirectory directory)
uint64 current;
uint64 oldest;
+ directory.ops->lock_pass(directory.self);
directory.ops->stop(directory.self, HOST_DELTA_DIRECTORY_POINT_PASSING);
left = directory.ops->left(directory.self);
current = directory.ops->epoch(directory.self);
@@ -1091,12 +1175,21 @@ host_delta_directory_pass(HostDeltaDirectoryDirectory directory)
directory.ops->stop(directory.self, HOST_DELTA_DIRECTORY_POINT_OLDEST);
for (uint32 kind = 0, kind_len = directory.ops->aged(directory.self); kind < kind_len; kind++)
{
- for (uint32 index = 0, index_len = directory.ops->records(directory.self, kind); index < index_len; index++)
+ uint64 at = directory.ops->record_retired_head(directory.self, kind);
+ uint64 first = 0;
+ uint32 last = 0;
+
+ while (!directory.ops->record_retired_swap(directory.self, kind, at, 0))
+ at = directory.ops->record_retired_head(directory.self, kind);
+ while (at != 0)
{
+ uint32 index = (uint32) (at - 1);
uint64 loaded = directory.ops->record_word(directory.self, kind, index);
uint64 freeing;
uint64 stamp;
+ bool claimed PG_USED_FOR_ASSERTS_ONLY;
+ at = directory.ops->record_link(directory.self, kind, index);
if (!host_delta_page_reap(loaded, &freeing))
continue;
stamp = directory.ops->stamp(directory.self, kind, index);
@@ -1104,45 +1197,62 @@ host_delta_directory_pass(HostDeltaDirectoryDirectory directory)
{
kept = has_kept ? std_min_u64(kept, stamp) : stamp;
has_kept = true;
+ if (first == 0)
+ last = index;
+ directory.ops->set_record_link(directory.self, kind, index, first);
+ first = (uint64) index + 1;
continue;
}
- if (!directory.ops->record_swap(directory.self, kind, index, loaded, freeing))
- continue;
+ claimed = directory.ops->record_swap(directory.self, kind, index, loaded, freeing);
+ Assert(claimed);
directory.ops->record_swap(directory.self, kind, index, freeing, host_delta_page_released(freeing));
directory.ops->freed(directory.self);
}
+ while (first != 0)
+ {
+ uint64 head = directory.ops->record_retired_head(directory.self, kind);
+
+ directory.ops->set_record_link(directory.self, kind, last, head);
+ if (directory.ops->record_retired_swap(directory.self, kind, head, first))
+ break;
+ }
}
directory.ops->stop(directory.self, HOST_DELTA_DIRECTORY_POINT_WALKED);
- for (uint32 slot = 0, slot_len = directory.ops->slots(directory.self); slot < slot_len; slot++)
{
- uint64 loaded = directory.ops->word(directory.self, slot);
- uint64 freeing;
- uint64 stamp;
- uint64 released;
+ uint64 at = directory.ops->retired_head(directory.self);
+ uint32 first = 0;
+ uint32 last = 0;
- if (!host_delta_page_reap(loaded, &freeing))
- continue;
- stamp = directory.ops->load(directory.self, slot, HOST_DELTA_DIRECTORY_FIELD_STAMP);
- if (!host_delta_page_passed(stamp, oldest))
+ while (!directory.ops->retired_swap(directory.self, at, 0))
+ at = directory.ops->retired_head(directory.self);
+ while (at != 0)
{
- kept = has_kept ? std_min_u64(kept, stamp) : stamp;
- has_kept = true;
- continue;
+ uint32 slot = (uint32) (at - 1);
+ bool keep;
+
+ at = directory.ops->link(directory.self, slot);
+ host_delta_directory_freed_slot(directory, slot, directory.ops->word(directory.self, slot), oldest,
+ &has_kept, &kept, &keep);
+ if (!keep)
+ continue;
+ if (first == 0)
+ last = slot;
+ directory.ops->set_link(directory.self, slot, first);
+ first = slot + 1;
+ }
+ while (first != 0)
+ {
+ uint64 head = directory.ops->retired_head(directory.self);
+
+ directory.ops->set_link(directory.self, last, (uint32) head);
+ if (directory.ops->retired_swap(directory.self, head, (uint64) first))
+ break;
}
- if (!directory.ops->swap(directory.self, slot, loaded, freeing))
- continue;
- directory.ops->stop(directory.self, HOST_DELTA_DIRECTORY_POINT_CLAIMED);
- host_delta_blocks_give(directory, directory.ops->load(directory.self, slot, HOST_DELTA_DIRECTORY_FIELD_BODY));
- directory.ops->store(directory.self, slot, HOST_DELTA_DIRECTORY_FIELD_BODY, 0);
- host_delta_directory_freed(directory, slot, false);
- released = host_delta_directory_emptied(directory, slot, freeing);
- directory.ops->stop(directory.self, HOST_DELTA_DIRECTORY_POINT_HINTING);
- host_delta_directory_hinted(directory, slot, released);
- directory.ops->freed(directory.self);
}
if (!has_kept)
directory.ops->settle(directory.self, left);
host_delta_directory_held(directory, current, oldest, has_kept ? kept : 0);
+ directory.ops->unlock_pass(directory.self);
}
/* host_delta_directory_rank orders kept stamps, zero, for none, after every stamp. */
@@ -1377,4 +1487,643 @@ test_host_delta_directory_tests_a_words_index_is_below_its_pages_count(void)
host_delta_directory_tests_ResultTupleArray5U8_Debug_fmt);
}
+/* HOST_DELTA_DIRECTORY_TESTS_MOST is the most slots, and aged records, a test's directory holds. */
+#define HOST_DELTA_DIRECTORY_TESTS_MOST 4096
+
+/*
+ * host_delta_directory_tests_Directory_t is a test's directory in process
+ * memory: its slots and their words and links, one aged kind's records,
+ * stamps, and links, the lists' heads, the header's counts, the epochs, and
+ * the count of the slot and record words loaded while a case counts them.
+ */
+typedef struct host_delta_directory_tests_Directory_t
+{
+ uint32 slots;
+ uint64 words[HOST_DELTA_DIRECTORY_TESTS_MOST];
+ uint64 fields[HOST_DELTA_DIRECTORY_TESTS_MOST][7];
+ uint32 links[HOST_DELTA_DIRECTORY_TESTS_MOST];
+ uint64 records[HOST_DELTA_DIRECTORY_TESTS_MOST];
+ uint64 stamps[HOST_DELTA_DIRECTORY_TESTS_MOST];
+ uint64 record_links[HOST_DELTA_DIRECTORY_TESTS_MOST];
+ uint64 head;
+ uint64 record_head;
+ uint64 frees;
+ uint64 left;
+ uint64 kept;
+ uint64 overshoot;
+ uint64 epoch;
+ uint64 oldest;
+ /* passing is whether visits counts the slot and record words loaded. */
+ bool passing;
+ uint64 visits;
+} host_delta_directory_tests_Directory_t;
+
+/* HOST_DELTA_DIRECTORY_TESTS_AGED is the id of a test directory's one aged kind. */
+#define HOST_DELTA_DIRECTORY_TESTS_AGED UINT64CONST(0x74657374)
+
+/* host_delta_directory_tests_directory is the directory a case builds, too large for a stack. */
+static host_delta_directory_tests_Directory_t host_delta_directory_tests_directory;
+
+/* HOST_DELTA_DIRECTORY_TESTS_SELF is a test directory's ops' self as the directory it is. */
+#define HOST_DELTA_DIRECTORY_TESTS_SELF(self) ((host_delta_directory_tests_Directory_t *) (self))
+
+/* host_delta_directory_tests_slots is a test directory's count of slots. */
+static uint32
+host_delta_directory_tests_slots(void *self)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->slots;
+}
+
+/* host_delta_directory_tests_budget is a test directory's budget, one 64 KiB block for each slot. */
+static uint64
+host_delta_directory_tests_budget(void *self)
+{
+ return (uint64) HOST_DELTA_DIRECTORY_TESTS_SELF(self)->slots * HOST_DELTA_PAGE_BLOCK;
+}
+
+/* host_delta_directory_tests_word loads a slot's word, counting it while a case counts. */
+static uint64
+host_delta_directory_tests_word(void *self, uint32 slot)
+{
+ host_delta_directory_tests_Directory_t *d = HOST_DELTA_DIRECTORY_TESTS_SELF(self);
+
+ if (d->passing)
+ d->visits++;
+ return d->words[slot];
+}
+
+/* host_delta_directory_tests_swap stores a slot's word over the one loaded. */
+static bool
+host_delta_directory_tests_swap(void *self, uint32 slot, uint64 loaded, uint64 word)
+{
+ host_delta_directory_tests_Directory_t *d = HOST_DELTA_DIRECTORY_TESTS_SELF(self);
+
+ if (d->words[slot] != loaded)
+ return false;
+ d->words[slot] = word;
+ return true;
+}
+
+/* host_delta_directory_tests_load loads one of a slot's other words. */
+static uint64
+host_delta_directory_tests_load(void *self, uint32 slot, HostDeltaDirectoryField field)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->fields[slot][field];
+}
+
+/* host_delta_directory_tests_store stores one of a slot's other words. */
+static void
+host_delta_directory_tests_store(void *self, uint32 slot, HostDeltaDirectoryField field, uint64 value)
+{
+ HOST_DELTA_DIRECTORY_TESTS_SELF(self)->fields[slot][field] = value;
+}
+
+/* host_delta_directory_tests_blocks is a test directory's count of blocks, which no case takes. */
+static uint32
+host_delta_directory_tests_blocks(void *self)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->slots;
+}
+
+/* host_delta_directory_tests_region loads a region word, none of which a case sets. */
+static uint64
+host_delta_directory_tests_region(void *self, HostDeltaBlocksRegion word)
+{
+ return 0;
+}
+
+/* host_delta_directory_tests_set_region stores a region word, which no case reads. */
+static void
+host_delta_directory_tests_set_region(void *self, HostDeltaBlocksRegion word, uint64 value)
+{
+}
+
+/* host_delta_directory_tests_block loads a block's word, none of which a case sets. */
+static uint64
+host_delta_directory_tests_block(void *self, uint32 block, HostDeltaBlocksBlock word)
+{
+ return 0;
+}
+
+/* host_delta_directory_tests_set_block stores a block's word, which no case reads. */
+static void
+host_delta_directory_tests_set_block(void *self, uint32 block, HostDeltaBlocksBlock word, uint64 value)
+{
+}
+
+/* host_delta_directory_tests_nothing does nothing, as a test directory's locks and listings do. */
+static void
+host_delta_directory_tests_nothing(void *self)
+{
+}
+
+/* host_delta_directory_tests_frees is a test directory's count of frees. */
+static uint64
+host_delta_directory_tests_frees(void *self)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->frees;
+}
+
+/* host_delta_directory_tests_freed counts a free. */
+static void
+host_delta_directory_tests_freed(void *self)
+{
+ HOST_DELTA_DIRECTORY_TESTS_SELF(self)->frees++;
+}
+
+/* host_delta_directory_tests_left is a test directory's count of retirements. */
+static uint64
+host_delta_directory_tests_left(void *self)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->left;
+}
+
+/* host_delta_directory_tests_leave counts a retirement. */
+static void
+host_delta_directory_tests_leave(void *self)
+{
+ HOST_DELTA_DIRECTORY_TESTS_SELF(self)->left++;
+}
+
+/* host_delta_directory_tests_settle clears the count of retirements while it holds the one loaded. */
+static void
+host_delta_directory_tests_settle(void *self, uint64 loaded)
+{
+ host_delta_directory_tests_Directory_t *d = HOST_DELTA_DIRECTORY_TESTS_SELF(self);
+
+ if (d->left == loaded)
+ d->left = 0;
+}
+
+/* host_delta_directory_tests_kept is a test directory's kept stamp. */
+static uint64
+host_delta_directory_tests_kept(void *self)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->kept;
+}
+
+/* host_delta_directory_tests_kept_swap stores the kept stamp over the one loaded. */
+static bool
+host_delta_directory_tests_kept_swap(void *self, uint64 loaded, uint64 word)
+{
+ host_delta_directory_tests_Directory_t *d = HOST_DELTA_DIRECTORY_TESTS_SELF(self);
+
+ if (d->kept != loaded)
+ return false;
+ d->kept = word;
+ return true;
+}
+
+/* host_delta_directory_tests_zero answers 0 for a count no case moves. */
+static uint64
+host_delta_directory_tests_zero_count(void *self)
+{
+ return 0;
+}
+
+/* host_delta_directory_tests_count_waiter counts nothing, since no case waits. */
+static void
+host_delta_directory_tests_count_waiter(void *self, bool add)
+{
+}
+
+/* host_delta_directory_tests_overshoot is a test directory's overshoot word. */
+static uint64
+host_delta_directory_tests_overshoot(void *self)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->overshoot;
+}
+
+/* host_delta_directory_tests_overshoot_swap stores the overshoot word over the one loaded. */
+static bool
+host_delta_directory_tests_overshoot_swap(void *self, uint64 loaded, uint64 word)
+{
+ host_delta_directory_tests_Directory_t *d = HOST_DELTA_DIRECTORY_TESTS_SELF(self);
+
+ if (d->overshoot != loaded)
+ return false;
+ d->overshoot = word;
+ return true;
+}
+
+/* host_delta_directory_tests_hand is a test directory's hand, its first slot. */
+static uint32
+host_delta_directory_tests_hand(void *self)
+{
+ return 0;
+}
+
+/* host_delta_directory_tests_hint is a word of a test directory's free hint, which holds no free slot. */
+static uint64
+host_delta_directory_tests_hint(void *self, uint32 word)
+{
+ return 0;
+}
+
+/* host_delta_directory_tests_hint_slot sets or clears no bit, since no case frees a slot. */
+static void
+host_delta_directory_tests_hint_slot(void *self, uint32 slot)
+{
+}
+
+/* host_delta_directory_tests_spent is a test directory's count of spent slots, none. */
+static uint64
+host_delta_directory_tests_spent(void *self, bool over)
+{
+ return 0;
+}
+
+/* host_delta_directory_tests_spend counts no spent slot, since no case spends one. */
+static void
+host_delta_directory_tests_spend(void *self, bool over)
+{
+}
+
+/* host_delta_directory_tests_lock takes a test directory's allocation lock, which no other process takes. */
+static bool
+host_delta_directory_tests_lock(void *self, const void *raise, HostError *error)
+{
+ return true;
+}
+
+/* host_delta_directory_tests_zero_body zeroes no body, since no case takes one. */
+static void
+host_delta_directory_tests_zero_body(void *self, uint64 body, HostDeltaPageSize size)
+{
+}
+
+/* host_delta_directory_tests_write writes no body, since no case takes one. */
+static void
+host_delta_directory_tests_write(void *self, uint64 body, uint64 at, const uint8 *bytes, size_t bytes_len)
+{
+}
+
+/* host_delta_directory_tests_set stores no body word, since no case takes one. */
+static void
+host_delta_directory_tests_set(void *self, uint64 body, uint64 index, uint64 value)
+{
+}
+
+/* host_delta_directory_tests_own lists no page, since no case takes one. */
+static void
+host_delta_directory_tests_own(void *self, HostDeltaPagePage name)
+{
+}
+
+/* host_delta_directory_tests_retiring is the epoch a retirement stamps: the current one. */
+static uint64
+host_delta_directory_tests_retiring(void *self)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->epoch;
+}
+
+/* host_delta_directory_tests_retiring_now takes the epoch lock, which no other process holds. */
+static bool
+host_delta_directory_tests_retiring_now(void *self, uint64 *result)
+{
+ *result = HOST_DELTA_DIRECTORY_TESTS_SELF(self)->epoch;
+ return true;
+}
+
+/* host_delta_directory_tests_retired advances the epoch. */
+static void
+host_delta_directory_tests_retired(void *self)
+{
+ HOST_DELTA_DIRECTORY_TESTS_SELF(self)->epoch++;
+}
+
+/* host_delta_directory_tests_epoch is a test directory's current epoch. */
+static uint64
+host_delta_directory_tests_epoch(void *self)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->epoch;
+}
+
+/* host_delta_directory_tests_oldest is a test directory's oldest epoch, which a reader holds below every retirement. */
+static uint64
+host_delta_directory_tests_oldest(void *self)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->oldest;
+}
+
+/* host_delta_directory_tests_stop holds at no point. */
+static void
+host_delta_directory_tests_stop(void *self, HostDeltaDirectoryPoint point)
+{
+}
+
+/* host_delta_directory_tests_aged is a test directory's count of aged kinds: one. */
+static uint32
+host_delta_directory_tests_aged(void *self)
+{
+ return 1;
+}
+
+/* host_delta_directory_tests_aged_kind is the number of a test directory's one aged kind. */
+static bool
+host_delta_directory_tests_aged_kind(void *self, uint64 id, uint32 *result)
+{
+ *result = 0;
+ return id == HOST_DELTA_DIRECTORY_TESTS_AGED;
+}
+
+/* host_delta_directory_tests_records is the count of a test directory's aged records, one for each slot. */
+static uint32
+host_delta_directory_tests_records(void *self, uint32 kind)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->slots;
+}
+
+/* host_delta_directory_tests_record_word loads an aged record's word, counting it while a case counts. */
+static uint64
+host_delta_directory_tests_record_word(void *self, uint32 kind, uint32 index)
+{
+ host_delta_directory_tests_Directory_t *d = HOST_DELTA_DIRECTORY_TESTS_SELF(self);
+
+ if (d->passing)
+ d->visits++;
+ return d->records[index];
+}
+
+/* host_delta_directory_tests_record_swap stores an aged record's word over the one loaded. */
+static bool
+host_delta_directory_tests_record_swap(void *self, uint32 kind, uint32 index, uint64 loaded, uint64 word)
+{
+ host_delta_directory_tests_Directory_t *d = HOST_DELTA_DIRECTORY_TESTS_SELF(self);
+
+ if (d->records[index] != loaded)
+ return false;
+ d->records[index] = word;
+ return true;
+}
+
+/* host_delta_directory_tests_stamp loads an aged record's stamp. */
+static uint64
+host_delta_directory_tests_stamp(void *self, uint32 kind, uint32 index)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->stamps[index];
+}
+
+/* host_delta_directory_tests_set_stamp stores an aged record's stamp. */
+static void
+host_delta_directory_tests_set_stamp(void *self, uint32 kind, uint32 index, uint64 stamp)
+{
+ HOST_DELTA_DIRECTORY_TESTS_SELF(self)->stamps[index] = stamp;
+}
+
+/* host_delta_directory_tests_record_hand is an aged kind's hand, its first record. */
+static uint32
+host_delta_directory_tests_record_hand(void *self, uint32 kind)
+{
+ return 0;
+}
+
+/* host_delta_directory_tests_record_nothing does nothing with an aged record, as its clear and listings do here. */
+static void
+host_delta_directory_tests_record_nothing(void *self, uint32 kind, uint32 index)
+{
+}
+
+/* host_delta_directory_tests_retired_head loads the retired list's head. */
+static uint64
+host_delta_directory_tests_retired_head(void *self)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->head;
+}
+
+/* host_delta_directory_tests_retired_swap stores the retired list's head over the one loaded. */
+static bool
+host_delta_directory_tests_retired_swap(void *self, uint64 loaded, uint64 word)
+{
+ host_delta_directory_tests_Directory_t *d = HOST_DELTA_DIRECTORY_TESTS_SELF(self);
+
+ if (d->head != loaded)
+ return false;
+ d->head = word;
+ return true;
+}
+
+/* host_delta_directory_tests_link loads a slot's link. */
+static uint32
+host_delta_directory_tests_link(void *self, uint32 slot)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->links[slot];
+}
+
+/* host_delta_directory_tests_set_link stores a slot's link. */
+static void
+host_delta_directory_tests_set_link(void *self, uint32 slot, uint32 value)
+{
+ HOST_DELTA_DIRECTORY_TESTS_SELF(self)->links[slot] = value;
+}
+
+/* host_delta_directory_tests_record_retired_head loads the aged kind's retired list's head. */
+static uint64
+host_delta_directory_tests_record_retired_head(void *self, uint32 kind)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->record_head;
+}
+
+/* host_delta_directory_tests_record_retired_swap stores the aged kind's retired list's head over the one loaded. */
+static bool
+host_delta_directory_tests_record_retired_swap(void *self, uint32 kind, uint64 loaded, uint64 word)
+{
+ host_delta_directory_tests_Directory_t *d = HOST_DELTA_DIRECTORY_TESTS_SELF(self);
+
+ if (d->record_head != loaded)
+ return false;
+ d->record_head = word;
+ return true;
+}
+
+/* host_delta_directory_tests_record_link loads an aged record's link. */
+static uint64
+host_delta_directory_tests_record_link(void *self, uint32 kind, uint32 index)
+{
+ return HOST_DELTA_DIRECTORY_TESTS_SELF(self)->record_links[index];
+}
+
+/* host_delta_directory_tests_set_record_link stores an aged record's link. */
+static void
+host_delta_directory_tests_set_record_link(void *self, uint32 kind, uint32 index, uint64 value)
+{
+ HOST_DELTA_DIRECTORY_TESTS_SELF(self)->record_links[index] = value;
+}
+
+/* HOST_DELTA_DIRECTORY_TESTS_OPS is a test directory's Directory table. */
+static const HostDeltaDirectoryDirectoryOps HOST_DELTA_DIRECTORY_TESTS_OPS = {
+ .slots = host_delta_directory_tests_slots,
+ .budget = host_delta_directory_tests_budget,
+ .word = host_delta_directory_tests_word,
+ .swap = host_delta_directory_tests_swap,
+ .load = host_delta_directory_tests_load,
+ .store = host_delta_directory_tests_store,
+ .blocks = host_delta_directory_tests_blocks,
+ .region = host_delta_directory_tests_region,
+ .set_region = host_delta_directory_tests_set_region,
+ .block = host_delta_directory_tests_block,
+ .set_block = host_delta_directory_tests_set_block,
+ .lock_region = host_delta_directory_tests_nothing,
+ .unlock_region = host_delta_directory_tests_nothing,
+ .frees = host_delta_directory_tests_frees,
+ .freed = host_delta_directory_tests_freed,
+ .left = host_delta_directory_tests_left,
+ .leave = host_delta_directory_tests_leave,
+ .settle = host_delta_directory_tests_settle,
+ .kept = host_delta_directory_tests_kept,
+ .kept_swap = host_delta_directory_tests_kept_swap,
+ .waiting = host_delta_directory_tests_zero_count,
+ .count_waiter = host_delta_directory_tests_count_waiter,
+ .overshoot = host_delta_directory_tests_overshoot,
+ .overshoot_swap = host_delta_directory_tests_overshoot_swap,
+ .hand = host_delta_directory_tests_hand,
+ .hint = host_delta_directory_tests_hint,
+ .hint_set = host_delta_directory_tests_hint_slot,
+ .hint_clear = host_delta_directory_tests_hint_slot,
+ .spent = host_delta_directory_tests_spent,
+ .spend = host_delta_directory_tests_spend,
+ .lock = host_delta_directory_tests_lock,
+ .unlock = host_delta_directory_tests_nothing,
+ .zero = host_delta_directory_tests_zero_body,
+ .write = host_delta_directory_tests_write,
+ .set = host_delta_directory_tests_set,
+ .own = host_delta_directory_tests_own,
+ .disown = host_delta_directory_tests_own,
+ .retiring = host_delta_directory_tests_retiring,
+ .retiring_now = host_delta_directory_tests_retiring_now,
+ .retired = host_delta_directory_tests_retired,
+ .unretired = host_delta_directory_tests_nothing,
+ .epoch = host_delta_directory_tests_epoch,
+ .oldest = host_delta_directory_tests_oldest,
+ .stop = host_delta_directory_tests_stop,
+ .aged = host_delta_directory_tests_aged,
+ .aged_kind = host_delta_directory_tests_aged_kind,
+ .records = host_delta_directory_tests_records,
+ .record_word = host_delta_directory_tests_record_word,
+ .record_swap = host_delta_directory_tests_record_swap,
+ .stamp = host_delta_directory_tests_stamp,
+ .set_stamp = host_delta_directory_tests_set_stamp,
+ .record_hand = host_delta_directory_tests_record_hand,
+ .clear = host_delta_directory_tests_record_nothing,
+ .made = host_delta_directory_tests_record_nothing,
+ .unmade = host_delta_directory_tests_record_nothing,
+ .retired_head = host_delta_directory_tests_retired_head,
+ .retired_swap = host_delta_directory_tests_retired_swap,
+ .link = host_delta_directory_tests_link,
+ .set_link = host_delta_directory_tests_set_link,
+ .record_retired_head = host_delta_directory_tests_record_retired_head,
+ .record_retired_swap = host_delta_directory_tests_record_retired_swap,
+ .record_link = host_delta_directory_tests_record_link,
+ .set_record_link = host_delta_directory_tests_set_record_link,
+ .lock_pass = host_delta_directory_tests_nothing,
+ .unlock_pass = host_delta_directory_tests_nothing,
+};
+
+/*
+ * host_delta_directory_tests_retirements makes a test directory of a count of
+ * slots, every slot and aged record live, a reader holding the oldest epoch
+ * below every retirement, and retires ten pages and ten aged records, the
+ * last slots' and records'.
+ */
+static HostDeltaDirectoryDirectory
+host_delta_directory_tests_retirements(uint32 slots)
+{
+ host_delta_directory_tests_Directory_t *d = &host_delta_directory_tests_directory;
+ HostDeltaDirectoryDirectory directory = {&HOST_DELTA_DIRECTORY_TESTS_OPS, d};
+ HostDeltaPagePage names[10];
+ HostDeltaLeaving leaving[10];
+ uint64 owned;
+ uint64 live;
+
+ memset(d, 0, sizeof(*d));
+ d->slots = slots;
+ d->epoch = 5;
+ d->oldest = 1;
+ if (!host_delta_page_claim(0, &owned))
+ std_panic(__FILE__, __LINE__, 0, "a free slot is claimed", sizeof("a free slot is claimed") - 1);
+ live = host_delta_page_link(owned);
+ for (uint32 at = 0; at < slots; at++)
+ {
+ d->words[at] = live;
+ d->records[at] = live;
+ }
+ for (uint32 i = 0; i < 10; i++)
+ {
+ uint32 at = slots - 1 - i;
+
+ names[i] = host_delta_page_Page_of(at, live);
+ leaving[i].id = HOST_DELTA_DIRECTORY_TESTS_AGED;
+ leaving[i].location = host_delta_page_Page_value(host_delta_page_Page_of(at, live));
+ }
+ host_delta_directory_retire(directory, leaving, lengthof(leaving), names, lengthof(names));
+ return directory;
+}
+
+/* host_delta_directory_tests_visited asserts that a case loaded want words, citing this file's line. */
+static void
+host_delta_directory_tests_visited(uint32 line, uint64 want)
+{
+ uint64 visits = host_delta_directory_tests_directory.visits;
+
+ if (visits != want)
+ std_assert_failed(__FILE__, line, 0, "==", &visits, std_u64_Debug_fmt, &want, std_u64_Debug_fmt, NULL);
+}
+
+/*
+ * Bugs row 309's free pass: a pass after ten pages and ten aged records
+ * retire loads the words of those twenty and of no other slot or record,
+ * at a directory of 128 slots and at one of 4,096, so its work is the
+ * retirements and not the directory's size.
+ */
+void
+test_host_delta_directory_tests_a_free_pass_loads_only_the_retirements(void)
+{
+ static const uint32 sizes[] = {128, HOST_DELTA_DIRECTORY_TESTS_MOST};
+
+ for (size_t i = 0; i < lengthof(sizes); i++)
+ {
+ HostDeltaDirectoryDirectory directory = host_delta_directory_tests_retirements(sizes[i]);
+
+ host_delta_directory_tests_directory.passing = true;
+ host_delta_directory_pass(directory);
+ host_delta_directory_tests_visited(__LINE__, 20);
+ host_delta_directory_tests_directory.visits = 0;
+ host_delta_directory_pass(directory);
+ host_delta_directory_tests_visited(__LINE__, 20);
+ host_delta_directory_tests_directory.passing = false;
+ }
+}
+
+/*
+ * Bugs row 309's refused allocation: an allocation the directory refuses,
+ * every slot live, runs a free pass that loads the words of the ten pages
+ * and ten aged records retired and of no other, at both sizes.
+ */
+void
+test_host_delta_directory_tests_a_refused_allocations_pass_loads_only_the_retirements(void)
+{
+ static const uint32 sizes[] = {128, HOST_DELTA_DIRECTORY_TESTS_MOST};
+
+ for (size_t i = 0; i < lengthof(sizes); i++)
+ {
+ HostDeltaDirectoryDirectory directory = host_delta_directory_tests_retirements(sizes[i]);
+ HostDeltaPageSize wanted[1] = {{4096, 1}};
+ StdVec names;
+ bool has_full;
+ uint64 full;
+ HostError error;
+
+ std_vec_with_capacity_Checked(&names, 1, sizeof(HostDeltaPagePage), _Alignof(HostDeltaPagePage));
+ host_delta_directory_tests_directory.passing = true;
+ if (!host_delta_directory_alloc(directory, NULL, wanted, lengthof(wanted), &names, &has_full, &full, &error))
+ std_panic(__FILE__, __LINE__, 0, "a refused allocation answers its budget's signal",
+ sizeof("a refused allocation answers its budget's signal") - 1);
+ host_delta_directory_tests_directory.passing = false;
+ if (!has_full || full != 0)
+ std_panic(__FILE__, __LINE__, 0, "the allocation is refused", sizeof("the allocation is refused") - 1);
+ host_delta_directory_tests_visited(__LINE__, 20);
+ std_vec_free_Checked(&names, sizeof(HostDeltaPagePage), _Alignof(HostDeltaPagePage));
+ }
+}
+
#endif /* DOLTGRES_TEST_HOST */
diff --git a/src/doltgres/host/c/lib.c b/src/doltgres/host/c/lib.c
index 1d8232410ae59..86c3d134b0541 100644
--- a/src/doltgres/host/c/lib.c
+++ b/src/doltgres/host/c/lib.c
@@ -382,6 +382,9 @@ host_WalKind_of(uint8 value, HostWalKind *result)
case 13:
*result = HOST_WAL_KIND_DATABASE_CHANGE;
return true;
+ case 14:
+ *result = HOST_WAL_KIND_CARRY_UNLISTED;
+ return true;
default:
return false;
}
@@ -423,6 +426,8 @@ host_WalKind_value(HostWalKind self)
return 12;
case HOST_WAL_KIND_DATABASE_CHANGE:
return 13;
+ case HOST_WAL_KIND_CARRY_UNLISTED:
+ return 14;
}
pg_unreachable();
}
@@ -459,6 +464,8 @@ host_WalKind_name(HostWalKind self)
return "MapCarried";
case HOST_WAL_KIND_DATABASE_CHANGE:
return "DatabaseChange";
+ case HOST_WAL_KIND_CARRY_UNLISTED:
+ return "CarryUnlisted";
#ifdef USE_INJECTION_POINTS
case HOST_WAL_KIND_TEST:
return "Test";
@@ -581,6 +588,17 @@ host_Xid_Debug_fmt(const void *value, StdFormatter *f)
return std_DebugTuple_finish(&t);
}
+/* host_Command_Debug_fmt is Command's derived Debug. */
+bool
+host_Command_Debug_fmt(const void *value, StdFormatter *f)
+{
+ const HostCommand *self = value;
+ StdDebugTuple t = std_fmt_debug_tuple(f, "Command", 7);
+
+ std_DebugTuple_field(&t, &self->f0, std_u32_Debug_fmt);
+ return std_DebugTuple_finish(&t);
+}
+
/* host_Row_PartialEq_eq reports whether two tuple ids are the same. */
bool
host_Row_PartialEq_eq(const HostRow *self, const HostRow *other)
@@ -1441,6 +1459,22 @@ host_wal_insert(HostIoIoDoors io, HostWalKind kind, const uint8 *body, size_t bo
return host.ops->wal_insert(host.self, &raise, kind, body, body_len, result, error);
}
+/* host_NewCid_Debug_fmt is NewCid's derived Debug. */
+bool
+host_NewCid_Debug_fmt(const void *value, StdFormatter *f)
+{
+ const HostNewCid *self = value;
+ StdOptionRef cmin = {self->has_cmin, {&self->cmin, host_Command_Debug_fmt}};
+ StdOptionRef cmax = {self->has_cmax, {&self->cmax, host_Command_Debug_fmt}};
+ StdOptionRef combo = {self->has_combo, {&self->combo, host_Command_Debug_fmt}};
+ StdDebugStruct s = std_fmt_debug_struct(f, "NewCid", 6);
+
+ std_DebugStruct_field(&s, "cmin", 4, &cmin, std_option_Debug_fmt);
+ std_DebugStruct_field(&s, "cmax", 4, &cmax, std_option_Debug_fmt);
+ std_DebugStruct_field(&s, "combo", 5, &combo, std_option_Debug_fmt);
+ return std_DebugStruct_finish(&s);
+}
+
/*
* host_wal_new_cid appends heap's own record of the command ids of a row a
* transaction wrote or changed in a relation logical decoding reads, so
@@ -2027,6 +2061,21 @@ host_refuse_recovery(HostIoIoDoors io, const char *detail, size_t detail_len)
host.ops->refuse_recovery(host.self, detail, detail_len);
}
+/*
+ * host_load_pauses loads pauses a later checkpoint window runs, so a failure
+ * to load one comes before the change the window finishes, as Postgres'
+ * INJECTION_POINT_LOAD comes before its START_CRIT_SECTION; with no pauses
+ * built in, it does nothing.
+ */
+bool
+host_load_pauses(HostIoIoDoors io, const char *const *pauses, size_t pauses_len, HostError *error)
+{
+ HostHost host = host_current();
+ HostMayRaise raise = host_doors_of_io_doors(io);
+
+ return host.ops->load_pauses(host.self, &raise, pauses, pauses_len, error);
+}
+
/*
* host_delay_checkpoint opens a window holding off the start of a checkpoint
* until it finishes, with the names of the pauses inside it, which its open
@@ -2189,30 +2238,31 @@ host_collection_end(void)
/*
* host_collection_ask asks the collector worker for a full collection of a
- * database's repository and waits for its answer, the packs it retired, those
+ * database's repository, emptying a tablespace unless that is
+ * CHUNKS_PACKS_DEFAULT_SPACE, and waits for its answer, the packs it retired, those
* it copied out, and its live bytes, each PG_UINT64_MAX when it failed (the
* collector text's §8.2); a cancel ends the wait with Postgres' error, the ask
* taken back, and the collection goes on.
*/
bool
-host_collection_ask(HostIoIo *io, uint32 database, uint64 *result, HostError *error)
+host_collection_ask(HostIoIo *io, uint32 database, uint32 emptied, uint64 *result, HostError *error)
{
HostHost host = host_current();
HostMayRaise raise = host_doors(HOST_IO_IO_READ_DOORS(io));
- return host.ops->collection_ask(host.self, &raise, database, result, error);
+ return host.ops->collection_ask(host.self, &raise, database, emptied, result, error);
}
/*
- * host_collection_asked returns the unanswered ask's word and the database it
- * asks for, which the collector worker reads.
+ * host_collection_asked returns the unanswered ask's word, the database it
+ * asks for, and the tablespace it empties, which the collector worker reads.
*/
bool
-host_collection_asked(uint64 *result_0, uint32 *result_1)
+host_collection_asked(uint64 *result_0, uint32 *result_1, uint32 *result_2)
{
HostHost host = host_current();
- return host.ops->collection_asked(host.self, result_0, result_1);
+ return host.ops->collection_asked(host.self, result_0, result_1, result_2);
}
/* host_collection_answer answers an ask, by its word, with the collection's counts, and wakes its asker. */
@@ -2233,6 +2283,15 @@ host_collector_started(void)
host.ops->collector_started(host.self);
}
+/* host_collector_running reports whether a collector worker has started, without which an ask would wait for good. */
+bool
+host_collector_running(void)
+{
+ HostHost host = host_current();
+
+ return host.ops->collector_running(host.self);
+}
+
/*
* host_database_locked takes a database's lock in session mode without
* waiting, answering whether it took it, so the collector worker opens the
@@ -2742,6 +2801,19 @@ host_tests_Test_Host_refuse_recovery(void *self, const char *detail, size_t deta
std_panic(RUST_FILE, 2157, 13, text, sizeof(text) - 1);
}
+/* host_tests_Test_Host_load_pauses loads nothing: the test host runs no pause. */
+static bool
+host_tests_Test_Host_load_pauses(void *self, const HostMayRaise *raise, const char *const *pauses, size_t pauses_len,
+ HostError *error)
+{
+ (void) self;
+ (void) raise;
+ (void) pauses;
+ (void) pauses_len;
+ (void) error;
+ return true;
+}
+
/* host_tests_Test_Host_delay_checkpoints is unreachable here: only `pghost` makes the capability a WAL seam takes. */
static bool
host_tests_Test_Host_delay_checkpoints(void *self, const HostMayRaise *raise, const char *const *pauses, size_t pauses_len, HostError *error)
@@ -3646,7 +3718,8 @@ host_tests_Test_Host_collection_end(void *self)
/* host_tests_Test_Host_collection_ask is unreachable here: no test of `host` collects. */
static bool
-host_tests_Test_Host_collection_ask(void *self, const HostMayRaise *raise, uint32 database, uint64 *result, HostError *error)
+host_tests_Test_Host_collection_ask(void *self, const HostMayRaise *raise, uint32 database, uint32 emptied,
+ uint64 *result, HostError *error)
{
static const char text[] = "internal error: entered unreachable code: no test of `host` collects";
@@ -3655,7 +3728,7 @@ host_tests_Test_Host_collection_ask(void *self, const HostMayRaise *raise, uint3
/* host_tests_Test_Host_collection_asked is unreachable here: no test of `host` collects. */
static bool
-host_tests_Test_Host_collection_asked(void *self, uint64 *result_0, uint32 *result_1)
+host_tests_Test_Host_collection_asked(void *self, uint64 *result_0, uint32 *result_1, uint32 *result_2)
{
static const char text[] = "internal error: entered unreachable code: no test of `host` collects";
@@ -3680,6 +3753,15 @@ host_tests_Test_Host_collector_started(void *self)
std_panic(RUST_FILE, 2590, 13, text, sizeof(text) - 1);
}
+/* host_tests_Test_Host_collector_running is unreachable here: no test of `host` collects. */
+static bool
+host_tests_Test_Host_collector_running(void *self)
+{
+ static const char text[] = "internal error: entered unreachable code: no test of `host` collects";
+
+ std_panic(__FILE__, __LINE__, 0, text, sizeof(text) - 1);
+}
+
/* host_tests_Test_Host_database_locked is unreachable here: no test of `host` collects. */
static bool
host_tests_Test_Host_database_locked(void *self, const HostMayRaise *raise, uint32 database, bool *result, HostError *error)
@@ -3889,6 +3971,7 @@ static const HostHostOps HOST_TESTS_TEST_HOST_OPS = {
.log = host_tests_Test_Host_log,
.refuse_recovery = host_tests_Test_Host_refuse_recovery,
.delay_checkpoints = host_tests_Test_Host_delay_checkpoints,
+ .load_pauses = host_tests_Test_Host_load_pauses,
.resume_checkpoints = host_tests_Test_Host_resume_checkpoints,
.xid_status = host_tests_Test_Host_xid_status,
.xid_in_progress = host_tests_Test_Host_xid_in_progress,
@@ -3995,6 +4078,7 @@ static const HostHostOps HOST_TESTS_TEST_HOST_OPS = {
.collection_asked = host_tests_Test_Host_collection_asked,
.collection_answer = host_tests_Test_Host_collection_answer,
.collector_started = host_tests_Test_Host_collector_started,
+ .collector_running = host_tests_Test_Host_collector_running,
.database_locked = host_tests_Test_Host_database_locked,
.database_unlocked = host_tests_Test_Host_database_unlocked,
.database_lock_waited = host_tests_Test_Host_database_lock_waited,
diff --git a/src/doltgres/host/c/shared.c b/src/doltgres/host/c/shared.c
index 3c376f66c5406..00c874857d0bd 100644
--- a/src/doltgres/host/c/shared.c
+++ b/src/doltgres/host/c/shared.c
@@ -109,8 +109,9 @@ host_shared_Kind_pool(const HostSharedPoolOps *s)
/*
* host_shared_Kind_aged returns the line of a pool the delta's epochs free,
- * whose records hold one atomic past their shape's, the host's, for the
- * epoch of their retirement.
+ * whose records hold two atomics past their shape's, the host's: the epoch of
+ * their retirement, and their link in their kind's retired list (bugs row
+ * 309).
*/
HostSharedKind
host_shared_Kind_aged(const HostSharedAgedOps *s)
@@ -122,7 +123,7 @@ host_shared_Kind_aged(const HostSharedAgedOps *s)
form.u.aged.names = s->names;
form.u.aged.names_len = s->names_len;
kind = host_shared_Kind_of(&s->shape, form);
- kind.atomics = STD_ADD(usize, kind.atomics, 1, 221, 9);
+ kind.atomics = STD_ADD(usize, kind.atomics, 2, 221, 9);
return kind;
}
diff --git a/src/doltgres/pg/c/lib.c b/src/doltgres/pg/c/lib.c
index e4daaf2ca0b23..e4ca967d517f0 100644
--- a/src/doltgres/pg/c/lib.c
+++ b/src/doltgres/pg/c/lib.c
@@ -212,16 +212,13 @@ pg_snapshot_view_closure_0(void *captures, const uint32 *xids, size_t count, con
}
}
-/* pg_snapshot_view reads a snapshot an entry got into its view, its arrays lent for as long as the snapshot is. */
-bool
-pg_snapshot_view(const PgMayRaise *raise, const PgSnapshot *snapshot, PgSnapshotView *result, PgPgError *error)
+/* pg_snapshot_view_of reads a snapshot's fields into its view, its arrays lent for as long as the snapshot is. */
+static void
+pg_snapshot_view_of(DgSnapshotView fields, PgSnapshotView *result)
{
- DgSnapshotView fields;
size_t subxcnt;
StdTryFromIntError tried;
- if (!pg_calls_snapshot_fields(raise, snapshot, &fields, error))
- return false;
result->kind = fields.kind;
result->xmin = fields.xmin;
result->xmax = fields.xmax;
@@ -232,6 +229,33 @@ pg_snapshot_view(const PgMayRaise *raise, const PgSnapshot *snapshot, PgSnapshot
if (!std_usize_try_from_i32(fields.subxcnt, &subxcnt, &tried))
subxcnt = 0;
pg_snapshot_view_closure_0(NULL, fields.subxip, subxcnt, &result->subxip, &result->subxip_len);
+}
+
+/*
+ * pg_transaction_snapshot_view reads the transaction's snapshot, as
+ * GetTransactionSnapshot takes it, into its view, its arrays lent until the
+ * backend's next snapshot.
+ */
+bool
+pg_transaction_snapshot_view(const PgMayRaise *raise, PgSnapshotView *result, PgPgError *error)
+{
+ DgSnapshotView fields;
+
+ if (!pg_calls_transaction_snapshot_fields(raise, &fields, error))
+ return false;
+ pg_snapshot_view_of(fields, result);
+ return true;
+}
+
+/* pg_snapshot_view reads a snapshot an entry got into its view, its arrays lent for as long as the snapshot is. */
+bool
+pg_snapshot_view(const PgMayRaise *raise, const PgSnapshot *snapshot, PgSnapshotView *result, PgPgError *error)
+{
+ DgSnapshotView fields;
+
+ if (!pg_calls_snapshot_fields(raise, snapshot, &fields, error))
+ return false;
+ pg_snapshot_view_of(fields, result);
return true;
}
diff --git a/src/doltgres/pg/calls.list b/src/doltgres/pg/calls.list
index fa61bc519d2d8..5c94baf7f80cd 100644
--- a/src/doltgres/pg/calls.list
+++ b/src/doltgres/pg/calls.list
@@ -254,6 +254,7 @@ log_new_cid uint64 DgStorage storage, uint64 tid, CommandId cmin, CommandId cmax
rel_att_type Oid Relation rel, int attnum noraise nocatalog it reads the relation's tuple descriptor, which its relcache entry holds TupleDescAttr(RelationGetDescr(rel), attnum - 1)->atttypid test/t/033_scan.pl: a scan of a table keyed by later columns gives its rows in key order
register_subxact_end void raises nocatalog RegisterSubXactCallback allocates its entry in TopMemoryContext, which raises only when memory runs out; it reads no catalog RegisterSubXactCallback(doltgres_subxact_ended, NULL) test/t/044_removal.pl: a rolled-back savepoint's rows are taken back and its parent's kept
snapshot_fields DgSnapshotView Snapshot snapshot raises nocatalog ReadNextFullTransactionId takes XidGenLock shared to widen an MVCC or historic snapshot's bounds against the next full xid, which every bound is within 2^31 of, and FullTransactionIdFromAllowableAt keeps a special xid as it is; the rest reads the snapshot's own fields, whose arrays live as long as the snapshot, and of any other kind only its type, which alone its rule reads, as heap's do, since a Dirty snapshot's bounds are unset until a read sets them; it reads no catalog { if (snapshot->snapshot_type != SNAPSHOT_MVCC && snapshot->snapshot_type != SNAPSHOT_HISTORIC_MVCC) *out = (DgSnapshotView) {(int) snapshot->snapshot_type}; else { FullTransactionId next = ReadNextFullTransactionId(); *out = (DgSnapshotView) {(int) snapshot->snapshot_type, U64FromFullTransactionId(FullTransactionIdFromAllowableAt(next, snapshot->xmin)), U64FromFullTransactionId(FullTransactionIdFromAllowableAt(next, snapshot->xmax)), snapshot->curcid, snapshot->takenDuringRecovery, snapshot->suboverflowed, snapshot->xip, snapshot->xcnt, snapshot->subxip, snapshot->subxcnt}; } } test/t/042_snapshot.pl: a snapshot taken while a transaction runs sees it in progress after it commits
+transaction_snapshot_fields DgSnapshotView raises nocatalog GetTransactionSnapshot reads the proc array, as heap's concurrent build scan takes its snapshot, and ReadNextFullTransactionId takes XidGenLock shared to widen the snapshot's bounds against the next full xid, as snapshot_fields does; its arrays live until the backend's next snapshot, and it reads no catalog { Snapshot snapshot = GetTransactionSnapshot(); FullTransactionId next = ReadNextFullTransactionId(); *out = (DgSnapshotView) {(int) snapshot->snapshot_type, U64FromFullTransactionId(FullTransactionIdFromAllowableAt(next, snapshot->xmin)), U64FromFullTransactionId(FullTransactionIdFromAllowableAt(next, snapshot->xmax)), snapshot->curcid, snapshot->takenDuringRecovery, snapshot->suboverflowed, snapshot->xip, snapshot->xcnt, snapshot->subxip, snapshot->subxcnt}; } test/t/045_btree.pl: a REINDEX CONCURRENTLY computes an indexed row's expression once, as heap's validation does
snapshot_removable bool Snapshot snapshot, uint64 xid raises nocatalog GlobalVisTestIsRemovableFullXid may take ProcArrayLock to move the NonVacuumable snapshot's own horizon, which raises only when the process holds too many locks; it reads no catalog *out = GlobalVisTestIsRemovableFullXid(snapshot->vistest, FullTransactionIdFromU64(xid), true) test/t/042_snapshot.pl: a NonVacuumable snapshot sees a deleted row until its deleter passes the horizon
scan_relation Relation const TableScanDesc scan noraise nocatalog it reads the scan's own field, the relation its begin was given scan->rs_rd test/t/042_snapshot.pl: a seq scan gives the rows its snapshot sees of the base and the delta
scan_snapshot Snapshot const TableScanDesc scan noraise nocatalog it reads the scan's own field, the snapshot its begin was given scan->rs_snapshot test/t/042_snapshot.pl: a seq scan gives the rows its snapshot sees of the base and the delta
@@ -292,7 +293,7 @@ lock_tuple bool Oid dbid, Oid relid, uint64 tid, int mode, bool wait raises noca
unlock_tuple void Oid dbid, Oid relid, uint64 tid, int mode escalates nocatalog LockRelease warns only for a lock this backend doesn't hold, which the tuple lock's guard, its only caller, rules out by construction { static const LOCKMODE modes[] = {AccessShareLock, RowShareLock, ExclusiveLock, AccessExclusiveLock}; LOCKTAG tag; SET_LOCKTAG_TUPLE(tag, dbid, relid, (BlockNumber) (tid >> 16), (OffsetNumber) (tid & 0xffff)); LockRelease(&tag, modes[mode], false); } test/t/046_update.pl: a second updater of a row waits for the first's transaction
rel_key_immediate bool Relation rel raises catalog it reads the relation's primary key index's pg_index row through one INDEXRELID lookup, refusing a missing row with relcache.c's words { Oid index = RelationGetPrimaryKeyIndex(rel, true); *out = true; if (OidIsValid(index)) { HeapTuple t = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index)); if (!HeapTupleIsValid(t)) elog(ERROR, "cache lookup failed for index %u", index); *out = ((Form_pg_index) GETSTRUCT(t))->indimmediate; ReleaseSysCache(t); } } test/t/046_update.pl: a deferrable key's rows go beside a live row
rel_key_equal_image bool Relation rel raises catalog _bt_allequalimage calls each key column's opclass's BTEQUALIMAGE_PROC, which may run user code, and opens the primary key index { Oid index = RelationGetPrimaryKeyIndex(rel, true); *out = true; if (OidIsValid(index)) { Relation irel = index_open(index, AccessShareLock); *out = _bt_allequalimage(irel, false); index_close(irel, AccessShareLock); } } test/t/046_update.pl: two writers of a text key under a nondeterministic collation, one waiting
-refuse_duplicate_key void Relation rel, TupleTableSlot *slot raises catalog it raises the btree's unique violation, 23505, with the primary key index's name and the key's values as BuildIndexValueDescription gives them, which run the columns' output functions { Oid index = RelationGetPrimaryKeyIndex(rel, true); Relation irel = index_open(index, AccessShareLock); IndexInfo *info = BuildIndexInfo(irel); Datum values[INDEX_MAX_KEYS]; bool isnull[INDEX_MAX_KEYS]; char *desc; FormIndexDatum(info, slot, NULL, values, isnull); desc = BuildIndexValueDescription(irel, values, isnull); ereport(ERROR, (errcode(ERRCODE_UNIQUE_VIOLATION), errmsg("duplicate key value violates unique constraint \"%s\"", RelationGetRelationName(irel)), desc ? errdetail("Key %s already exists.", desc) : 0, errtableconstraint(rel, RelationGetRelationName(irel)))); } test/t/046_update.pl: an insert of a live row's key is refused as heap's is
+refuse_duplicate_key void Relation rel, TupleTableSlot *slot raises catalog it raises the btree's unique violation, 23505, with the primary key index's name and the key's values as BuildIndexValueDescription gives them, which run the columns' output functions, or, for a primary key WITHOUT OVERLAPS, whose index is an exclusion constraint's, the exclusion violation, 23P01, worded as check_exclusion_or_unique_constraint words it, the existing key the same values { Oid index = RelationGetPrimaryKeyIndex(rel, true); Relation irel = index_open(index, AccessShareLock); IndexInfo *info = BuildIndexInfo(irel); Datum values[INDEX_MAX_KEYS]; bool isnull[INDEX_MAX_KEYS]; char *desc; FormIndexDatum(info, slot, NULL, values, isnull); desc = BuildIndexValueDescription(irel, values, isnull); if (irel->rd_index->indisexclusion) ereport(ERROR, (errcode(ERRCODE_EXCLUSION_VIOLATION), errmsg("conflicting key value violates exclusion constraint \"%s\"", RelationGetRelationName(irel)), desc ? errdetail("Key %s conflicts with existing key %s.", desc, desc) : errdetail("Key conflicts with existing key."), errtableconstraint(rel, RelationGetRelationName(irel)))); ereport(ERROR, (errcode(ERRCODE_UNIQUE_VIOLATION), errmsg("duplicate key value violates unique constraint \"%s\"", RelationGetRelationName(irel)), desc ? errdetail("Key %s already exists.", desc) : 0, errtableconstraint(rel, RelationGetRelationName(irel)))); } test/t/046_update.pl: an insert of a live row's key is refused as heap's is; test/t/046_update.pl: an insert or an update of a WITHOUT OVERLAPS primary key's existing key is refused as heap's exclusion check refuses it
rel_key_index Oid Relation rel raises catalog RelationGetPrimaryKeyIndex reads the relation cache's index list, which it may build from the catalog *out = RelationGetPrimaryKeyIndex(rel, true) test/t/046_update.pl: an insert of a live row's key is refused as heap's is
combo_command uint32 uint64 xid, uint32 cmin, uint32 cmax raises nocatalog HeapTupleHeaderAdjustCmax gives a change of a row its own transaction wrote a combo command id from the backend's combo table, which GetComboCommandId grows in TopTransactionContext and which can raise out of memory; the header it reads holds only the row's writer and command, so no catalog is read { HeapTupleHeaderData header = {0}; bool combo; HeapTupleHeaderSetXmin(&header, XidFromFullTransactionId(FullTransactionIdFromU64(xid))); HeapTupleHeaderSetCmin(&header, cmin); *out = cmax; HeapTupleHeaderAdjustCmax(&header, out, &combo); } test/t/046_update.pl: a row its own transaction inserted and then changed shows the combo command id as heap's does
set_failure void TM_FailureData *tmfd, uint64 ctid, uint32 xmax, uint32 cmax noraise nocatalog it writes the failure data's own fields, as heap_delete, heap_update, and heap_lock_tuple fill them ItemPointerSet(&tmfd->ctid, (BlockNumber) (ctid >> 16), (OffsetNumber) (ctid & 0xffff)); tmfd->xmax = xmax; tmfd->cmax = cmax test/t/046_update.pl: a second updater of a row waits for the first's transaction
@@ -398,7 +399,7 @@ start_fold_worker uint64 Oid database, bool asked raises nocatalog RegisterDynam
fold_worker_running bool uint64 handle raises nocatalog GetBackgroundWorkerPid reads the worker's slot under BackgroundWorkerLock, raising only when the process holds too many locks { pid_t pid; *out = GetBackgroundWorkerPid((BackgroundWorkerHandle *) (uintptr_t) handle, &pid) != BGWH_STOPPED; } test/t/062_fold_trigger.pl: a storage whose delta passes fold_threshold is folded with no call from a test
free_worker_handle void uint64 handle noraise nocatalog pfree frees the handle start_fold_worker allocated, which holds nothing else pfree((void *) (uintptr_t) handle) test/t/062_fold_trigger.pl: a storage whose delta passes fold_threshold is folded with no call from a test
worker_connect_database void Oid database raises catalog BackgroundWorkerInitializeConnectionByOid connects the worker to its database, reading pg_database, after the worker takes SIGHUP as a reload of its configuration { pqsignal(SIGHUP, SignalHandlerForConfigReload); BackgroundWorkerUnblockSignals(); BackgroundWorkerInitializeConnectionByOid(database, InvalidOid, 0); } test/t/062_fold_trigger.pl: a storage whose delta passes fold_threshold is folded with no call from a test
-fold_as_owner bool Oid relid, Oid relfilenode raises catalog in a transaction of its own it opens the relation, an index's table before it, as Postgres locks a table before its indexes, which reads its catalog, and passes over one gone or rewritten since, or one whose lock another transaction holds, as a transaction that made or truncated the table holds it while its writes wait at delta_memory for the folds of the others; it runs the fold as the relation's owner in a security-restricted operation with search_path restricted and every user-settable setting at its built-in default (PLAN item 6's User code in folds), lc_messages at C because its built-in empty value, the environment's locale, is one only the server's start may set, set at a GUC nest level the commit undoes, as VACUUM runs index functions { Relation rel = NULL; Relation heap = NULL; Oid heapid; Oid saved_user; int saved_context; StartTransactionCommand(); PushActiveSnapshot(GetTransactionSnapshot()); heapid = IndexGetRelation(relid, true); if (OidIsValid(heapid) && ConditionalLockRelationOid(heapid, AccessShareLock) && (heap = try_relation_open(heapid, NoLock)) == NULL) UnlockRelationOid(heapid, AccessShareLock); if ((!OidIsValid(heapid) || heap != NULL) && ConditionalLockRelationOid(relid, AccessShareLock) && (rel = try_relation_open(relid, NoLock)) == NULL) UnlockRelationOid(relid, AccessShareLock); *out = false; if (rel != NULL && rel->rd_locator.relNumber == relfilenode && (rel->rd_index == NULL || heap != NULL)) { int level; GetUserIdAndSecContext(&saved_user, &saved_context); SetUserIdAndSecContext(rel->rd_rel->relowner, saved_context | SECURITY_RESTRICTED_OPERATION); level = NewGUCNestLevel(); { int count; struct config_generic **vars = get_guc_variables(&count); for (int at = 0; at < count; at++) { struct config_generic *var = vars[at]; const char *value = NULL; char number[64]; if ((var->context != PGC_SUSET && var->context != PGC_USERSET) || (var->flags & GUC_NO_RESET_ALL)) continue; switch (var->vartype) { case PGC_BOOL: value = var->_bool.boot_val ? "on" : "off"; break; case PGC_INT: snprintf(number, sizeof(number), "%d", var->_int.boot_val); value = number; break; case PGC_REAL: snprintf(number, sizeof(number), "%.17g", var->_real.boot_val); value = number; break; case PGC_STRING: value = strcmp(var->name, "lc_messages") == 0 ? "C" : var->_string.boot_val; break; case PGC_ENUM: value = config_enum_lookup_by_value(var, var->_enum.boot_val); break; } if (value != NULL) (void) set_config_option(var->name, value, PGC_SUSET, PGC_S_SESSION, GUC_ACTION_SAVE, true, ERROR, false); } } RestrictSearchPath(); *out = doltgres_fold_relation(rel, heap != NULL ? heap : rel); AtEOXact_GUC(false, level); SetUserIdAndSecContext(saved_user, saved_context); } if (rel != NULL) relation_close(rel, AccessShareLock); if (heap != NULL) relation_close(heap, AccessShareLock); PopActiveSnapshot(); CommitTransactionCommand(); } test/t/062_fold_trigger.pl: a fold orders a user comparator's keys under the built-in settings, not the server's
+fold_as_owner bool Oid relid, Oid relfilenode raises catalog in a transaction of its own it opens the relation, an index's table before it, as Postgres locks a table before its indexes, which reads its catalog, and passes over one gone or rewritten since, or one whose lock another transaction holds, as a transaction that made or truncated the table holds it while its writes wait at delta_memory for the folds of the others; it runs the fold as the relation's owner in a security-restricted operation with search_path restricted and every user-settable setting at its built-in default (PLAN item 6's User code in folds), lc_messages at C because its built-in empty value, the environment's locale, is one only the server's start may set, set at a GUC nest level the commit undoes, as VACUUM runs index functions; before its snapshot it marks its transaction as a safe concurrent index build marks its own (PROC_IN_SAFE_IC, which only WaitForOlderSnapshots reads and its commit clears), so a concurrent index build doesn't wait for it: the fold reads its storage's base and delta only by the fold horizon, never by its snapshot or through an index, the horizon stays at or below every running snapshot's xmin, the build's own included, and the fold only applies committed versions to a new base, adding no row and no index entry, so neither a fold of the build's table nor one of the new index's own storage leaves out an entry the index must hold, and the only user code it runs is the key's support and collation functions, which a safe build also runs { Relation rel = NULL; Relation heap = NULL; Oid heapid; Oid saved_user; int saved_context; StartTransactionCommand(); LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); MyProc->statusFlags |= PROC_IN_SAFE_IC; ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); PushActiveSnapshot(GetTransactionSnapshot()); heapid = IndexGetRelation(relid, true); if (OidIsValid(heapid) && ConditionalLockRelationOid(heapid, AccessShareLock) && (heap = try_relation_open(heapid, NoLock)) == NULL) UnlockRelationOid(heapid, AccessShareLock); if ((!OidIsValid(heapid) || heap != NULL) && ConditionalLockRelationOid(relid, AccessShareLock) && (rel = try_relation_open(relid, NoLock)) == NULL) UnlockRelationOid(relid, AccessShareLock); *out = false; if (rel != NULL && rel->rd_locator.relNumber == relfilenode && (rel->rd_index == NULL || heap != NULL)) { int level; GetUserIdAndSecContext(&saved_user, &saved_context); SetUserIdAndSecContext(rel->rd_rel->relowner, saved_context | SECURITY_RESTRICTED_OPERATION); level = NewGUCNestLevel(); { int count; struct config_generic **vars = get_guc_variables(&count); for (int at = 0; at < count; at++) { struct config_generic *var = vars[at]; const char *value = NULL; char number[64]; if ((var->context != PGC_SUSET && var->context != PGC_USERSET) || (var->flags & GUC_NO_RESET_ALL)) continue; switch (var->vartype) { case PGC_BOOL: value = var->_bool.boot_val ? "on" : "off"; break; case PGC_INT: snprintf(number, sizeof(number), "%d", var->_int.boot_val); value = number; break; case PGC_REAL: snprintf(number, sizeof(number), "%.17g", var->_real.boot_val); value = number; break; case PGC_STRING: value = strcmp(var->name, "lc_messages") == 0 ? "C" : var->_string.boot_val; break; case PGC_ENUM: value = config_enum_lookup_by_value(var, var->_enum.boot_val); break; } if (value != NULL) (void) set_config_option(var->name, value, PGC_SUSET, PGC_S_SESSION, GUC_ACTION_SAVE, true, ERROR, false); } } RestrictSearchPath(); *out = doltgres_fold_relation(rel, heap != NULL ? heap : rel); AtEOXact_GUC(false, level); SetUserIdAndSecContext(saved_user, saved_context); } if (rel != NULL) relation_close(rel, AccessShareLock); if (heap != NULL) relation_close(heap, AccessShareLock); PopActiveSnapshot(); CommitTransactionCommand(); } test/t/062_fold_trigger.pl: a fold orders a user comparator's keys under the built-in settings, not the server's
worker_connect void raises catalog BackgroundWorkerInitializeConnection connects the worker to no database, for its reads of the shared catalogs, after the worker takes SIGHUP as a reload of its configuration { pqsignal(SIGHUP, SignalHandlerForConfigReload); BackgroundWorkerUnblockSignals(); BackgroundWorkerInitializeConnection(NULL, NULL, 0); } test/t/056_collect.pl: the collector worker collects every database's repository on its own
latch_sleep void int64 ms raises nocatalog WaitLatch waits on the process's latch, exiting with the postmaster, and CHECK_FOR_INTERRUPTS raises a pending cancel or ends a worker at its SIGTERM; it reads no catalog { (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, ms, PG_WAIT_EXTENSION); ResetLatch(MyLatch); CHECK_FOR_INTERRUPTS(); } test/t/056_collect.pl: the collector worker collects every database's repository on its own
disk_space void const char *path, DgBuffer sizes raises nocatalog statvfs or GetDiskFreeSpaceExA reads the volume's sizes, raising when the system can't say; it reads no catalog { uint64 found[2]; doltgres_disk_space(path, &found[0], &found[1]); memcpy(sizes.data, found, Min(sizes.len, sizeof(found))); } test/t/056_collect.pl: the data directory's volume reports free bytes below its size
@@ -430,7 +431,7 @@ filelocator_storage DgStorage const RelFileLocator *locator noraise nocatalog it
drop_storage void Relation rel raises nocatalog RelationDropStorage schedules the relation's file's unlink at commit, raising only when memory runs out; it reads no catalog RelationDropStorage(rel) test/t/047_rewrite.pl: SET TABLESPACE keeps every row version with its ctid, xmin, xmax, cmin, and cmax, for an older snapshot too
xlog_is_needed bool noraise nocatalog XLogIsNeeded reads the wal_level setting XLogIsNeeded() test/t/047_rewrite.pl: SET TABLESPACE keeps every row version with its ctid, xmin, xmax, cmin, and cmax, for an older snapshot too
index_routine_forks bool Relation rel noraise nocatalog it compares the relcache entry's routine's aminsert with the fork's rel->rd_indam->aminsert == doltgres_index_insert test/t/047_rewrite.pl: ALTER INDEX SET TABLESPACE keeps a fork index's entries, for an index of a keyless table too
-fold_horizon uint64 Relation rel raises nocatalog GetOldestNonRemovableTransactionId takes ProcArrayLock shared and reads the caller's database's horizon, which the Assert holds to the relation's; LWLockAcquire then takes it shared again to lower that horizon to the xmin and xid of each process in VACUUM the horizon covers, which ComputeXidHorizons passes over though a fold takes no lock that conflicts with VACUUM's, read through the PGPROC mirror of their flags, which ProcArrayEndTransaction clears at a transaction's end and InitProcess at a slot's reuse; it raises only past the held locks' limit { TransactionId horizon = GetOldestNonRemovableTransactionId(rel); bool shared = rel->rd_rel->relisshared; Assert(shared || rel->rd_locator.dbOid == MyDatabaseId || !OidIsValid(MyDatabaseId) || RecoveryInProgress()); LWLockAcquire(ProcArrayLock, LW_SHARED); for (int i = 0; i < ProcGlobal->allProcCount; i++) { PGPROC *proc = &ProcGlobal->allProcs[i]; uint8 flags = *(volatile uint8 *) &proc->statusFlags; TransactionId xmin = *(volatile TransactionId *) &proc->xmin; TransactionId xid = *(volatile TransactionId *) &proc->xid; if (!(flags & PROC_IN_VACUUM) || (!shared && proc->databaseId != rel->rd_locator.dbOid)) continue; if (TransactionIdIsNormal(xmin) && TransactionIdPrecedes(xmin, horizon)) horizon = xmin; if (TransactionIdIsNormal(xid) && TransactionIdPrecedes(xid, horizon)) horizon = xid; } LWLockRelease(ProcArrayLock); *out = U64FromFullTransactionId(FullTransactionIdFromAllowableAt(ReadNextFullTransactionId(), horizon)); } test/t/050_fold.pl: a lazy VACUUM held paused holds back a fold of its database's table, and finishes after it
+fold_horizon uint64 Relation rel raises nocatalog GetOldestNonRemovableTransactionId takes ProcArrayLock shared and reads the caller's database's horizon, which the Assert holds to the relation's; LWLockAcquire then takes it shared again to lower that horizon to the xmin and xid of each process in VACUUM the horizon covers, which ComputeXidHorizons passes over though a fold takes no lock that conflicts with VACUUM's, read through the PGPROC mirror of their flags, which ProcArrayEndTransaction clears at a transaction's end and InitProcess at a slot's reuse; it raises only past the held locks' limit { TransactionId horizon = GetOldestNonRemovableTransactionId(rel); TransactionId own = MyProc->xmin; bool shared = rel->rd_rel->relisshared; Assert(shared || rel->rd_locator.dbOid == MyDatabaseId || !OidIsValid(MyDatabaseId) || RecoveryInProgress()); if (TransactionIdIsNormal(own) && TransactionIdPrecedes(own, horizon)) horizon = own; LWLockAcquire(ProcArrayLock, LW_SHARED); for (int i = 0; i < ProcGlobal->allProcCount; i++) { PGPROC *proc = &ProcGlobal->allProcs[i]; uint8 flags = *(volatile uint8 *) &proc->statusFlags; TransactionId xmin = *(volatile TransactionId *) &proc->xmin; TransactionId xid = *(volatile TransactionId *) &proc->xid; if (!(flags & PROC_IN_VACUUM) || (!shared && proc->databaseId != rel->rd_locator.dbOid)) continue; if (TransactionIdIsNormal(xmin) && TransactionIdPrecedes(xmin, horizon)) horizon = xmin; if (TransactionIdIsNormal(xid) && TransactionIdPrecedes(xid, horizon)) horizon = xid; } LWLockRelease(ProcArrayLock); *out = U64FromFullTransactionId(FullTransactionIdFromAllowableAt(ReadNextFullTransactionId(), horizon)); } test/t/050_fold.pl: a lazy VACUUM held paused holds back a fold of its database's table, and finishes after it
lwlock_try_sure bool LWLock *lock, LWLockMode mode escalates nocatalog LWLockConditionalAcquire raises only when too many locks are held, which its one caller rules out: a free pass takes the epoch lock behind an Io no guard has borrowed, so with no lock of the fork's held, and anything raised becomes a PANIC; it never waits and reads no catalog LWLockConditionalAcquire(lock, mode) test/t/017_pages.pl: a pass that loaded its oldest epoch before a record's retirement walks while it is held
atomic_or_u64 uint64 pg_atomic_uint64 *atomic, uint64 bits noraise nocatalog pg_atomic_fetch_or_u64 only sets bits pg_atomic_fetch_or_u64(atomic, bits) test/t/017_pages.pl: bytes appended by one backend are read by another
atomic_and_u64 uint64 pg_atomic_uint64 *atomic, uint64 bits noraise nocatalog pg_atomic_fetch_and_u64 only clears bits pg_atomic_fetch_and_u64(atomic, bits) test/t/017_pages.pl: bytes appended by one backend are read by another
@@ -502,7 +503,7 @@ rel_local bool Relation rel noraise nocatalog it reads the relcache entry's own
warn_parallel_temporary void Relation rel raises nocatalog ereport at WARNING raises only when memory for the report runs out, as heap's dead_items_alloc's report of the same words does; it reads no catalog ereport(WARNING, (errmsg("disabling parallel option of vacuum on \"%s\" --- cannot vacuum temporary tables in parallel", RelationGetRelationName(rel)))) test/t/062_size_sites.pl: VACUUM warns that a temporary fork table is never vacuumed in parallel
vacuum_relstats void Relation rel, BlockNumber pages, double tuples, bool hasindex raises catalog vac_update_relstats updates the relation's pg_class row in place, as heap_vacuum_rel's call does, with every block all-visible and all-frozen (Differences row 10) and no xid horizon of its own vac_update_relstats(rel, pages, tuples, pages, pages, hasindex, InvalidTransactionId, InvalidMultiXactId, NULL, NULL, false) test/t/062_size_sites.pl: VACUUM writes a fork table's pages and rows to pg_class
report_vacuum void Relation rel, double live, int64 started raises nocatalog pgstat_report_vacuum counts the vacuum in the relation's pending statistics entry, allocating it the first time, as heap_vacuum_rel's call does; it reads no catalog pgstat_report_vacuum(rel, (PgStat_Counter) live, 0, started) test/t/062_size_sites.pl: VACUUM counts a vacuum of a fork table in the cumulative statistics
-fork_named int text *name raises nocatalog forkname_to_number raises pg_relation_size's 22023 for a name no fork has, and text_to_cstring raises only when memory runs out; they read no catalog *out = forkname_to_number(text_to_cstring(name)) test/t/062_size_sites.pl: a fork table's other forks have no size
+fork_named int text *name raises nocatalog forkname_to_number raises pg_relation_size's 22023 for a name no fork has, and text_to_cstring raises only when memory runs out; they read no catalog *out = forkname_to_number(text_to_cstring(name)) test/t/062_size_sites.pl: a fork table's free space map fork is its real pages, and its other forks have no size
relation_opened void * Oid relid raises catalog RelationIdGetRelation reads the relation's relcache entry, building it from the catalog when it isn't there, and answers NULL for a relation that no longer exists, taking no lock, since the transaction holds one on every relation it wrote *out = RelationIdGetRelation(relid) test/t/062_size_sites.pl: an aborted insert and a rolled-back subtransaction leave a fork table's totals as they were
relation_closed void void *rel noraise nocatalog RelationClose only gives the relcache reference back RelationClose((Relation) rel) test/t/062_size_sites.pl: an aborted insert and a rolled-back subtransaction leave a fork table's totals as they were
slot_transform DgBytes const TupleTableSlot *slot noraise nocatalog it reads the slot's own buffer slot->tts_ops == &doltgres_slot_ops && ((const DoltgresSlot *) slot)->tuple_len != 0 ? (DgBytes) {((const DoltgresSlot *) slot)->buffer + ((const DoltgresSlot *) slot)->keyed_len + ((const DoltgresSlot *) slot)->key_len + ((const DoltgresSlot *) slot)->tuple_len + ((const DoltgresSlot *) slot)->values_len, ((const DoltgresSlot *) slot)->transform_len} : (DgBytes) {NULL, 0} test/t/067_layouts.pl: a keyed table's DROP COLUMN reads its rows as heap's
@@ -510,6 +511,7 @@ slot_missing void TupleTableSlot *slot noraise nocatalog slot_getmissingattrs on
rekeyed_rows void Relation rel raises catalog the table's update and its indexes' inserts run the fork's routines and the indexes' access methods, which read catalogs EState *estate = CreateExecutorState(); ResultRelInfo *info = makeNode(ResultRelInfo); Snapshot snapshot = RegisterSnapshot(GetLatestSnapshot()); TupleTableSlot *slot = table_slot_create(rel, NULL); TableScanDesc scan; InitResultRelInfo(info, rel, 1, NULL, 0); ExecOpenIndices(info, false); scan = table_beginscan(rel, snapshot, 0, NULL, SO_NONE); while (table_scan_getnextslot(scan, ForwardScanDirection, slot)) { ItemPointerData tid = slot->tts_tid; TU_UpdateIndexes indexes; MemoryContext caller = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); slot_getallattrs(slot); simple_table_tuple_update(rel, &tid, slot, snapshot, &indexes); if (info->ri_NumIndices > 0 && indexes != TU_None) ExecInsertIndexTuples(info, estate, indexes == TU_Summarizing ? EIIT_ONLY_SUMMARIZING : 0, slot, NIL, NULL); MemoryContextSwitchTo(caller); ResetPerTupleExprContext(estate); } table_endscan(scan); ExecDropSingleTupleTableSlot(slot); ExecCloseIndices(info); UnregisterSnapshot(snapshot); FreeExecutorState(estate) test/t/067_layouts.pl: a keyless table's index scan agrees with its rows after a DROP COLUMN
toast_index Oid Oid toast raises catalog toast_get_valid_index opens the TOAST relation and reads its index list from the catalog, under AccessShareLock, which the transaction that made it holds already *out = toast_get_valid_index(toast, AccessShareLock) test/t/057_identity.pl: a CREATE TABLE's TOAST pair reads in its transaction
index_info_unique bool struct IndexInfo *info noraise nocatalog it reads a field of the build's own IndexInfo info->ii_Unique test/t/045_btree.pl: a VACUUM FULL rebuilds a unique index a failed CIC left invalid without its unique check, and a DELETE after leaves the index that isn't ready alone, as heap's twin's
+index_info_concurrent bool struct IndexInfo *info noraise nocatalog it reads a field of the build's own IndexInfo info->ii_Concurrent test/t/045_btree.pl: a REINDEX CONCURRENTLY computes an indexed row's expression once, as heap's validation does
index_recorded bool Relation rel noraise nocatalog it reads the index's own pg_index row in its relcache entry !heap_attisnull(rel->rd_indextuple, Anum_pg_index_indexprs, NULL) || !heap_attisnull(rel->rd_indextuple, Anum_pg_index_indpred, NULL) test/t/045_btree.pl: a DELETE from a table whose expression index a failed CIC left invalid deletes the row and evaluates no expression, as heap's twin's
old_entries_unchanged bool int32 at, int32 index, TupleTableSlot *old_row, TupleTableSlot *new_row raises nocatalog pull_varattnos and bms_add_member allocate, which can raise out of memory, and slot_getallattrs deforms the rows; none reads the catalog { IndexInfo *info = doltgres_old_entries[at].infos[index]; TupleDesc desc = old_row->tts_tupleDescriptor; Bitmapset *attrs = NULL; int member = -1; for (int i = 0; i < info->ii_NumIndexAttrs; i++) if (info->ii_IndexAttrNumbers[i] != 0) attrs = bms_add_member(attrs, info->ii_IndexAttrNumbers[i] - FirstLowInvalidHeapAttributeNumber); pull_varattnos((Node *) info->ii_Expressions, 1, &attrs); pull_varattnos((Node *) info->ii_Predicate, 1, &attrs); slot_getallattrs(old_row); slot_getallattrs(new_row); *out = true; while ((member = bms_next_member(attrs, member)) >= 0) { int column = member + FirstLowInvalidHeapAttributeNumber - 1; Form_pg_attribute att; if (column < 0) { *out = false; break; } att = TupleDescAttr(desc, column); if (old_row->tts_isnull[column] != new_row->tts_isnull[column] || (!old_row->tts_isnull[column] && !datumIsEqual(old_row->tts_values[column], new_row->tts_values[column], att->attbyval, att->attlen))) { *out = false; break; } } bms_free(attrs); } test/t/045_btree.pl: an UPDATE of a column a volatile expression index doesn't read calls its function no time, and one of a column it reads calls it once, as heap's twin's
register_key_settle void raises nocatalog RegisterXactCallback allocates its entry in TopMemoryContext, which raises only when memory runs out; it reads no catalog RegisterXactCallback(doltgres_key_settle_commit, NULL) test/t/047_rewrite.pl: a drop through performDeletion from a test function has its table's key settled by commit
@@ -522,3 +524,7 @@ oldest_nonremovable uint32 Relation rel raises nocatalog GetOldestNonRemovableTr
rel_att_storage uint8 Relation rel, int attnum noraise nocatalog it reads the relation's tuple descriptor, which its relcache entry holds (uint8) TupleDescAttr(RelationGetDescr(rel), attnum - 1)->attstorage test/t/038_insert.pl: an INSERT ... SELECT of out-of-line values into a PLAIN column of a table with no TOAST relation stores them inline as heap's twin does
transaction_snapshot_push void raises nocatalog GetTransactionSnapshot reads the proc array and PushActiveSnapshot copies it into memory it allocates, neither reading a catalog PushActiveSnapshot(GetTransactionSnapshot()) test/t/062_fold_trigger.pl: while its launcher is held, a standby's slot keeps a comparator-keyed table's first reached point unfolded
snapshot_pop void noraise nocatalog PopActiveSnapshot only frees the top of the backend's snapshot stack PopActiveSnapshot() test/t/062_fold_trigger.pl: while its launcher is held, a standby's slot keeps a comparator-keyed table's first reached point unfolded
+read_barrier void noraise nocatalog pg_read_barrier only orders the loads around it pg_read_barrier() test/t/071_spill_reads.pl: a scan held across a fold that applied its spilled rows reads every row
+atomic_read_u32 uint32 pg_atomic_uint32 *atomic noraise nocatalog pg_atomic_read_u32 only reads pg_atomic_read_u32(atomic) test/t/017_pages.pl: a paused free pass goes on when woken
+fork_blocks uint32 Relation rel, int forknum raises nocatalog smgrexists and smgrnblocks open the relation's fork's file through md.c and raise only on an I/O error; they read no catalog *out = smgrexists(RelationGetSmgr(rel), forknum) ? smgrnblocks(RelationGetSmgr(rel), forknum) : 0 test/t/062_size_sites.pl: a fork table's free space map fork is its real pages, and its other forks have no size
+aborted_xid uint32 XLogReaderState *record noraise nocatalog ParseAbortRecord only reads the record's bytes { uint8 info = XLogRecGetInfo(record) & XLOG_XACT_OPMASK; result = InvalidTransactionId; if (info == XLOG_XACT_ABORT) result = XLogRecGetXid(record); else if (info == XLOG_XACT_ABORT_PREPARED) { xl_xact_parsed_abort parsed; ParseAbortRecord(XLogRecGetInfo(record), (xl_xact_abort *) XLogRecGetData(record), &parsed); result = parsed.twophase_xid; } } test/t/053_spill.pl: a standby that replayed an aborted transaction's spills reads only the committed rows
diff --git a/src/doltgres/pg/entries.list b/src/doltgres/pg/entries.list
index 3cf839a98ddd3..d53c106c729a4 100644
--- a/src/doltgres/pg/entries.list
+++ b/src/doltgres/pg/entries.list
@@ -154,6 +154,7 @@ doltgres_index_blocks bool Relation relation, ForkNumber forkNum, BlockNumber *b
doltgres_relation_size_added int64 Relation rel, text *forkName, int64 size error catalog it opens the relation's storage state through the backend's repository, and an index's answer reads its table's pg_class row test/t/062_size_sites.pl: a fork index is whole pages, its metapage and at least one leaf, and every size function sums its relations
doltgres_database_size int64 Oid dbOid, int64 size error nocatalog it reads the repository's directories through fd.c's directory functions and stat test/t/062_size_sites.pl: pg_database_size is the database directory's files and its repository's
doltgres_tablespace_size int64 Oid tblspcOid, int64 size error nocatalog it reads the tablespace's directory of repositories through fd.c's directory functions and stat test/t/062_size_sites.pl: pg_tablespace_size adds every repository's files to the default tablespace's
+doltgres_tablespace_emptied void Oid tablespaceoid, bool redo error nocatalog DropTableSpace calls it before it takes TablespaceCreateLock, so its wait answers cancel, and redo calls it before its first removal; it reads the tablespace's directories through fd.c and asks the collector worker, reading no catalog test/t/041_database.pl: DROP TABLESPACE of a location a fork table left empties the fork's packs out of it
doltgres_altering void Relation relation, AlterTableCmd *cmd, int rewrite error catalog ATExecCmd runs each subcommand inside the altering transaction, before the subcommand changes the catalog test/t/067_layouts.pl: a keyed table's DROP COLUMN reads its rows as heap's
doltgres_column_dropped void Oid relid, int attnum error catalog doDeletion runs inside the dropping transaction, after the column's catalog row is marked dropped and before the command counter makes that visible test/t/067_layouts.pl: a keyless table's columns DROP TYPE, DROP DOMAIN, and DROP EXTENSION cascade to read as heap's
doltgres_toast_created void Relation relation, Oid toast error catalog create_toast_table runs inside the transaction that makes the TOAST relation, after the table's pg_class row names it test/t/057_identity.pl: a CREATE TABLE's TOAST pair reads in its transaction
@@ -165,3 +166,9 @@ doltgres_key_settle_table void Relation rel error catalog it compares the table'
doltgres_key_settle_commit void XactEvent event, void *arg error catalog before a commit or a prepare it settles every table still named, which reads the catalog, and at any other event it does nothing test/t/047_rewrite.pl: a drop through performDeletion from a test function has its table's key settled by commit
doltgres_new_heap void Oid new_table, Relation old_table error catalog it reads the old table's primary key from the catalog test/t/047_rewrite.pl: CLUSTER gives a table's rows the tids heap's rewrite gives them in each of its orders
doltgres_backup_checkpointed void uint64 checkpoint error nocatalog do_pg_backup_start calls it after its checkpoint, in its error cleanup's block, where a raise runs do_pg_abort_backup test/t/056_collect.pl: a standby made from a base backup taken while a fold writes a pack starts and reads every row
+doltgres_visibility_counted bool Relation rel, BlockNumber *all_visible, BlockNumber *all_frozen error catalog it opens the table's storage state through the backend's repository, which reads the repository's newest manifest, and its rule reads the relation's relcache entry test/t/062_size_sites.pl: CREATE INDEX, REINDEX, and ANALYZE keep a fork table's every block all-visible and all-frozen
+doltgres_index_scan_io void Oid indexoid, bool indexonly, double tuples_fetched, double selectivity, double loop_count, double random_page_cost, double seq_page_cost, double cpu_operator_cost, double *max_io, double *min_io, double *rand_heap_pages error catalog the planner costs an index path inside a transaction, where the fork opens the index and its table, whose locks the planner holds, and their storage's state through the backend's repository, and reads the index's keys against the table's test/t/131_scan_costs.pl: an index scan of a fork table costs the fork's descents and leaves, an index-only scan none of the table
+doltgres_bitmap_page_cost void Oid relid, double tuples_fetched, double pages_fetched, BlockNumber pages, double maxentries, double random_page_cost, double cpu_operator_cost, double *cost_per_page error catalog the planner costs a bitmap path inside a transaction, where the fork opens the table, whose lock the planner holds, and its storage's state through the backend's repository test/t/131_scan_costs.pl: a bitmap heap scan of a fork table costs a tuple id resolution and a descent a row
+doltgres_heap_only_refused bool Relation rel error catalog the check reads the relation's relcache entry and, for an index, its table's and the index's method test/t/132_heap_only_contrib.pl: each heap-only contrib function refuses a fork relation with its own check's error
+doltgres_visibility_kept bool Relation rel error catalog the check reads the relation's relcache entry test/t/132_heap_only_contrib.pl: pg_visibility answers every block of a fork table all-visible and all-frozen
+doltgres_visibility_bits int Relation rel, BlockNumber blkno, int mapbits error catalog it opens the table's storage state through the backend's repository, which reads the repository's newest manifest, and its rule reads the relation's relcache entry test/t/132_heap_only_contrib.pl: pg_visibility answers every block of a fork table all-visible and all-frozen
diff --git a/src/doltgres/pg/test-entries.list b/src/doltgres/pg/test-entries.list
index dfed8724a0f51..2b64624167ef7 100644
--- a/src/doltgres/pg/test-entries.list
+++ b/src/doltgres/pg/test-entries.list
@@ -69,3 +69,7 @@ doltgres_test_epochs Datum error nocatalog it reads the epoch lock's words in s
doltgres_test_base_levels int Relation target error catalog it opens the relation's storage's state through the backend's repository and walks its base tree test/t/045_btree.pl: a fork btree index answers its base tree's levels above its leaves, a primary key's index its table's tree's, and 0 for an empty base
doltgres_test_totals Datum Relation target error catalog it opens the target's storage's state through the backend's repository, which reads the repository's newest manifest test/t/062_size_sites.pl: an aborted insert and a rolled-back subtransaction leave a fork table's totals as they were
doltgres_test_count Datum Relation target error catalog it opens the target's storage's state through the backend's repository and reads its table's key from the catalog for the count's fold test/t/062_size_sites.pl: an aborted insert and a rolled-back subtransaction leave a fork table's totals as they were
+doltgres_test_scan_shape Datum Relation target error catalog it opens the target's storage's state through the backend's repository, which reads the repository's newest manifest test/t/131_scan_costs.pl: an index scan of a fork table costs the fork's descents and leaves, an index-only scan none of the table
+doltgres_test_base_points Datum Relation target error catalog it opens the target's storage's state through the backend's repository, which reads the repository's newest manifest test/t/047_rewrite.pl: a standby's base of a moved table has its primary's root, fold point, and read point
+doltgres_test_identity_versions Datum int32 kind, Oid number error nocatalog it reads the working state's delta and its identity spills under a hold, reading no catalog test/t/133_identity_spill.pl: a restart after a checkpoint that listed a running transaction's identity spill holds each replayed key's version once
+doltgres_test_save Datum error nocatalog it saves the backend's database's repository outside a checkpoint, as a states' pool's make does, through its states and identity tree in shared memory, reading no catalog test/t/134_saves.pl: a save's manifest names a fold's base only once the base's record is flushed
diff --git a/src/doltgres/pghost/c/delta.c b/src/doltgres/pghost/c/delta.c
index d94109445e555..a0a3a45f3cea6 100644
--- a/src/doltgres/pghost/c/delta.c
+++ b/src/doltgres/pghost/c/delta.c
@@ -216,11 +216,18 @@ pghost_delta_slot_of(uint32 aged, uint32 index)
return STD_ADD(u32, at, index, 112, 5);
}
-/* pghost_delta_stamp_of returns the place among a record's atomics of the epoch of its retirement: its last. */
+/* pghost_delta_stamp_of returns the place among a record's atomics of the epoch of its retirement: the one before its last. */
static size_t
pghost_delta_stamp_of(size_t kind)
{
- return STD_SUB(usize, shmem_records_atomics(kind), 1, 117, 5);
+ return STD_SUB(usize, shmem_records_atomics(kind), 2, 117, 5);
+}
+
+/* pghost_delta_link_of returns the place among a record's atomics of its link in its kind's retired list: its last (bugs row 309). */
+static size_t
+pghost_delta_link_of(size_t kind)
+{
+ return shmem_records_atomics(kind) - 1;
}
/* pghost_delta_Pages_Directory_slots is the directory's count of slots. */
@@ -801,6 +808,101 @@ pghost_delta_Pages_Directory_unmade(void *self, uint32 kind, uint32 index)
std_RwLockWriteGuard_drop(&made);
}
+/* pghost_delta_Pages_Directory_retired_head loads the retired list's head. */
+static uint64
+pghost_delta_Pages_Directory_retired_head(void *self)
+{
+ (void) self;
+ return shmem_delta_retired();
+}
+
+/* pghost_delta_Pages_Directory_retired_swap stores the retired list's head over the one loaded. */
+static bool
+pghost_delta_Pages_Directory_retired_swap(void *self, uint64 loaded, uint64 word)
+{
+ (void) self;
+ return shmem_delta_retired_swap(loaded, word);
+}
+
+/* pghost_delta_Pages_Directory_link loads a slot's link in the retired list. */
+static uint32
+pghost_delta_Pages_Directory_link(void *self, uint32 slot)
+{
+ (void) self;
+ return shmem_delta_link(slot);
+}
+
+/* pghost_delta_Pages_Directory_set_link stores a slot's link in the retired list. */
+static void
+pghost_delta_Pages_Directory_set_link(void *self, uint32 slot, uint32 value)
+{
+ (void) self;
+ shmem_delta_set_link(slot, value);
+}
+
+/* pghost_delta_Pages_Directory_record_retired_head loads an aged kind's retired list's head. */
+static uint64
+pghost_delta_Pages_Directory_record_retired_head(void *self, uint32 kind)
+{
+ (void) self;
+ return shmem_records_retired(pghost_delta_kind_of(kind));
+}
+
+/* pghost_delta_Pages_Directory_record_retired_swap stores an aged kind's retired list's head over the one loaded. */
+static bool
+pghost_delta_Pages_Directory_record_retired_swap(void *self, uint32 kind, uint64 loaded, uint64 word)
+{
+ (void) self;
+ return shmem_records_retired_swap(pghost_delta_kind_of(kind), loaded, word);
+}
+
+/* pghost_delta_Pages_Directory_record_link loads an aged record's link in its kind's retired list. */
+static uint64
+pghost_delta_Pages_Directory_record_link(void *self, uint32 kind, uint32 index)
+{
+ size_t region_kind = pghost_delta_kind_of(kind);
+ ShmemRecordsOp load = {SHMEM_RECORDS_OP_LOAD};
+
+ (void) self;
+ return shmem_records_atomic(region_kind, index, pghost_delta_link_of(region_kind), load);
+}
+
+/* pghost_delta_Pages_Directory_set_record_link stores an aged record's link in its kind's retired list by exchanges. */
+static void
+pghost_delta_Pages_Directory_set_record_link(void *self, uint32 kind, uint32 index, uint64 value)
+{
+ size_t region_kind = pghost_delta_kind_of(kind);
+ ShmemRecordsOp load = {SHMEM_RECORDS_OP_LOAD};
+ uint64 held = shmem_records_atomic(region_kind, index, pghost_delta_link_of(region_kind), load);
+
+ (void) self;
+ for (;;)
+ {
+ ShmemRecordsOp exchange = {SHMEM_RECORDS_OP_EXCHANGE, {.exchange = {held, value}}};
+ uint64 before = shmem_records_atomic(region_kind, index, pghost_delta_link_of(region_kind), exchange);
+
+ if (before == held)
+ return;
+ held = before;
+ }
+}
+
+/* pghost_delta_Pages_Directory_lock_pass takes the free pass's lock. */
+static void
+pghost_delta_Pages_Directory_lock_pass(void *self)
+{
+ (void) self;
+ shmem_delta_lock_pass();
+}
+
+/* pghost_delta_Pages_Directory_unlock_pass releases the free pass's lock. */
+static void
+pghost_delta_Pages_Directory_unlock_pass(void *self)
+{
+ (void) self;
+ shmem_delta_unlock_pass();
+}
+
/* PGHOST_DELTA_PAGES_DIRECTORY_OPS is Pages' Directory table. */
static const HostDeltaDirectoryDirectoryOps PGHOST_DELTA_PAGES_DIRECTORY_OPS = {
.slots = pghost_delta_Pages_Directory_slots,
@@ -858,6 +960,16 @@ static const HostDeltaDirectoryDirectoryOps PGHOST_DELTA_PAGES_DIRECTORY_OPS = {
.clear = pghost_delta_Pages_Directory_clear,
.made = pghost_delta_Pages_Directory_made,
.unmade = pghost_delta_Pages_Directory_unmade,
+ .retired_head = pghost_delta_Pages_Directory_retired_head,
+ .retired_swap = pghost_delta_Pages_Directory_retired_swap,
+ .link = pghost_delta_Pages_Directory_link,
+ .set_link = pghost_delta_Pages_Directory_set_link,
+ .record_retired_head = pghost_delta_Pages_Directory_record_retired_head,
+ .record_retired_swap = pghost_delta_Pages_Directory_record_retired_swap,
+ .record_link = pghost_delta_Pages_Directory_record_link,
+ .set_record_link = pghost_delta_Pages_Directory_set_record_link,
+ .lock_pass = pghost_delta_Pages_Directory_lock_pass,
+ .unlock_pass = pghost_delta_Pages_Directory_unlock_pass,
};
/* PGHOST_DELTA_DIRECTORY is &Pages as host's sequences take it. */
@@ -1015,11 +1127,53 @@ pghost_delta_ended(uint64 epoch)
host_delta_directory_released(PGHOST_DELTA_DIRECTORY, epoch);
}
-/* pghost_delta_open opens a page under an epoch. */
+/* PGHOST_DELTA_OPENS is how many admitted opens a process keeps, a power of two a name's slot indexes. */
+#define PGHOST_DELTA_OPENS 1024
+StaticAssertDecl((PGHOST_DELTA_OPENS & (PGHOST_DELTA_OPENS - 1)) == 0,
+ "PGHOST_DELTA_OPENS is a power of two");
+
+/* PghostDeltaOpen is an open the directory admitted: the page's name, the hold's epoch, and what it reached. */
+typedef struct PghostDeltaOpen
+{
+ bool some;
+ HostDeltaPagePage name;
+ uint64 epoch;
+ HostDeltaDirectoryOpened opened;
+} PghostDeltaOpen;
+
+/*
+ * PGHOST_DELTA_OPENED is this process's last admitted open of each of
+ * PGHOST_DELTA_OPENS sets of names (bugs row 355). An entry answers only the
+ * same name, generation included, at the same epoch, and only an admission,
+ * never a refusal: for one name and epoch the directory's answer never turns
+ * from admitted to refused while a hold at that epoch can ask, since a
+ * retirement stamps an epoch at least that of every hold it can be seen
+ * under, and a slot is freed and given its next generation only once every
+ * hold at an epoch at or below that stamp has dropped, so no hold begun
+ * after it has the epoch. Its body and size are fixed while its generation
+ * holds the slot. The body is an offset into this process's mapping, which a
+ * process never outlives, so nothing clears the table.
+ */
+static PghostDeltaOpen PGHOST_DELTA_OPENED[PGHOST_DELTA_OPENS];
+
+/* pghost_delta_open opens a page under an epoch, answering a repeat of an admitted open from the process's table. */
bool
pghost_delta_open(HostDeltaPagePage name, uint64 epoch, HostDeltaDirectoryOpened *result, HostError *error)
{
- return host_delta_directory_open(PGHOST_DELTA_DIRECTORY, name, epoch, result, error);
+ PghostDeltaOpen *open = &PGHOST_DELTA_OPENED[host_delta_page_Page_number(name) & (PGHOST_DELTA_OPENS - 1)];
+
+ if (open->some && open->name.f0 == name.f0 && open->epoch == epoch)
+ {
+ *result = open->opened;
+ return true;
+ }
+ if (!host_delta_directory_open(PGHOST_DELTA_DIRECTORY, name, epoch, result, error))
+ return false;
+ *open = (PghostDeltaOpen)
+ {
+ true, name, epoch, *result
+ };
+ return true;
}
/* pghost_delta_bytes_of returns bytes of an opened page, mapped from the region. */
diff --git a/src/doltgres/pghost/c/lib.c b/src/doltgres/pghost/c/lib.c
index ef4c264bc9a74..5711cf272f50a 100644
--- a/src/doltgres/pghost/c/lib.c
+++ b/src/doltgres/pghost/c/lib.c
@@ -1919,17 +1919,17 @@ pghost_PgHost_Host_refuse_recovery(void *self, const char *detail, size_t detail
shmem_delta_refuse(detail, detail_len);
}
-/* pghost_PgHost_Host_delay_checkpoints holds checkpoints off, loading first the pauses its window runs inside its critical section. */
+/* pghost_PgHost_Host_load_pauses loads pauses' callbacks, as INJECTION_POINT_LOAD does, and does nothing in a build without injection points. */
static bool
-pghost_PgHost_Host_delay_checkpoints(void *self, const HostMayRaise *raise, const char *const *pauses,
- size_t pauses_len, HostError *error)
+pghost_PgHost_Host_load_pauses(void *self, const HostMayRaise *raise, const char *const *pauses, size_t pauses_len,
+ HostError *error)
{
- PgPgError error_;
-
(void) self;
#ifdef USE_INJECTION_POINTS
for (size_t i = 0, i_len = pauses_len; i < i_len; i++)
{
+ PgPgError error_;
+
if (!pg_calls_injection_point_load(pghost_pg_raise(raise), pauses[i], &error_))
{
*error = pghost_held(&error_);
@@ -1937,9 +1937,23 @@ pghost_PgHost_Host_delay_checkpoints(void *self, const HostMayRaise *raise, cons
}
}
#else
+ (void) raise;
(void) pauses;
(void) pauses_len;
+ (void) error;
#endif
+ return true;
+}
+
+/* pghost_PgHost_Host_delay_checkpoints holds checkpoints off, loading first the pauses its window runs inside its critical section. */
+static bool
+pghost_PgHost_Host_delay_checkpoints(void *self, const HostMayRaise *raise, const char *const *pauses,
+ size_t pauses_len, HostError *error)
+{
+ PgPgError error_;
+
+ if (!pghost_PgHost_Host_load_pauses(self, raise, pauses, pauses_len, error))
+ return false;
if (!pg_calls_delay_checkpoints(pghost_pg_raise(raise), &error_))
{
*error = pghost_held(&error_);
@@ -2519,6 +2533,9 @@ pghost_PgHost_Host_atomic(void *self, uint64 place, size_t atomic, HostSharedOp
records_op.u.exchange.f0 = op.u.exchange.f0;
records_op.u.exchange.f1 = op.u.exchange.f1;
break;
+ case HOST_SHARED_OP_LOAD_ACQUIRE:
+ records_op.tag = SHMEM_RECORDS_OP_LOAD_ACQUIRE;
+ break;
}
pghost_shared_placed(place, &kind, &index);
return shmem_records_atomic(kind, index, atomic, records_op);
@@ -3088,15 +3105,15 @@ pghost_PgHost_Host_collection_end(void *self)
shmem_collection_collection_end();
}
-/* pghost_PgHost_Host_collection_ask asks the collector to collect a database and waits for its counts. */
+/* pghost_PgHost_Host_collection_ask asks the collector to collect a database, emptying a tablespace, and waits for its counts. */
static bool
-pghost_PgHost_Host_collection_ask(void *self, const HostMayRaise *raise, uint32 database, uint64 *result,
- HostError *error)
+pghost_PgHost_Host_collection_ask(void *self, const HostMayRaise *raise, uint32 database, uint32 emptied,
+ uint64 *result, HostError *error)
{
PgPgError error_;
(void) self;
- if (!shmem_collection_ask(pghost_pg_raise(raise), database, result, &error_))
+ if (!shmem_collection_ask(pghost_pg_raise(raise), database, emptied, result, &error_))
{
*error = pghost_held(&error_);
return false;
@@ -3104,14 +3121,14 @@ pghost_PgHost_Host_collection_ask(void *self, const HostMayRaise *raise, uint32
return true;
}
-/* pghost_PgHost_Host_collection_asked returns the unanswered ask's word and the database it asks for. */
+/* pghost_PgHost_Host_collection_asked returns the unanswered ask's word, the database it asks for, and the tablespace it empties. */
static bool
-pghost_PgHost_Host_collection_asked(void *self, uint64 *result_0, uint32 *result_1)
+pghost_PgHost_Host_collection_asked(void *self, uint64 *result_0, uint32 *result_1, uint32 *result_2)
{
std_TupleU64U32_t asked;
(void) self;
- if (!shmem_collection_asked_for(&asked))
+ if (!shmem_collection_asked_for(&asked, result_2))
return false;
*result_0 = asked.f0;
*result_1 = asked.f1;
@@ -3134,6 +3151,14 @@ pghost_PgHost_Host_collector_started(void *self)
shmem_collection_worker_started();
}
+/* pghost_PgHost_Host_collector_running reports whether a collector worker has started. */
+static bool
+pghost_PgHost_Host_collector_running(void *self)
+{
+ (void) self;
+ return shmem_collection_worker_running();
+}
+
/* pghost_PgHost_Host_database_locked takes a database's lock without waiting, reporting whether it did. */
static bool
pghost_PgHost_Host_database_locked(void *self, const HostMayRaise *raise, uint32 database, bool *result,
@@ -4138,6 +4163,7 @@ static const HostHostOps PGHOST_PG_HOST_HOST_OPS = {
.log = pghost_PgHost_Host_log,
.refuse_recovery = pghost_PgHost_Host_refuse_recovery,
.delay_checkpoints = pghost_PgHost_Host_delay_checkpoints,
+ .load_pauses = pghost_PgHost_Host_load_pauses,
.resume_checkpoints = pghost_PgHost_Host_resume_checkpoints,
.xid_status = pghost_PgHost_Host_xid_status,
.xid_in_progress = pghost_PgHost_Host_xid_in_progress,
@@ -4244,6 +4270,7 @@ static const HostHostOps PGHOST_PG_HOST_HOST_OPS = {
.collection_asked = pghost_PgHost_Host_collection_asked,
.collection_answer = pghost_PgHost_Host_collection_answer,
.collector_started = pghost_PgHost_Host_collector_started,
+ .collector_running = pghost_PgHost_Host_collector_running,
.database_locked = pghost_PgHost_Host_database_locked,
.database_unlocked = pghost_PgHost_Host_database_unlocked,
.database_lock_waited = pghost_PgHost_Host_database_lock_waited,
diff --git a/src/doltgres/repo/c/lib.c b/src/doltgres/repo/c/lib.c
index 060d4647ad381..05c6d830e8b6c 100644
--- a/src/doltgres/repo/c/lib.c
+++ b/src/doltgres/repo/c/lib.c
@@ -150,14 +150,20 @@ repo_runs_pad_0(void *frame_)
}
}
-/* repo_runs_file reads one file of a manifest's runs from its item, refusing what runs refuses. */
+/*
+ * repo_runs_file reads one file of a manifest's runs from its item, refusing
+ * what runs refuses: a spill's xid without its number, its number without its
+ * xid, or its passing point or residual's point without either.
+ */
static bool
repo_runs_file(const ChunksRecordRecord *item, RepoRunFile *file, ChunksErrorError *error)
{
- static const uint32 numbers[] = {1, 2, 3, 4, 5, 6, 7};
+ static const uint32 numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
uint64 kind;
bool has_xid;
bool has_number;
+ bool has_passed;
+ bool has_from;
if (!repo_known(item, numbers, lengthof(numbers)))
return repo_corrupt(error);
@@ -168,12 +174,22 @@ repo_runs_file(const ChunksRecordRecord *item, RepoRunFile *file, ChunksErrorErr
file->kind = (uint8) kind;
has_xid = chunks_record_Record_field(item, 6) != NULL;
has_number = chunks_record_Record_field(item, 7) != NULL;
- if (has_xid != has_number)
+ has_passed = chunks_record_Record_field(item, 8) != NULL;
+ has_from = chunks_record_Record_field(item, 9) != NULL;
+ if (has_xid != has_number || (!has_xid && (has_passed || has_from)))
return repo_corrupt(error);
file->has_spilled = has_xid;
if (has_xid && (!chunks_record_Record_uint(item, 6, &file->spilled.xid, error) ||
!repo_narrow(item, 7, &file->spilled.number, error)))
return false;
+ if (has_xid)
+ {
+ file->spilled.has_passed = has_passed;
+ file->spilled.has_from = has_from;
+ if ((has_passed && !chunks_record_Record_uint(item, 8, &file->spilled.passed, error)) ||
+ (has_from && !chunks_record_Record_uint(item, 9, &file->spilled.from, error)))
+ return false;
+ }
return repo_narrow(item, 2, &file->tablespace, error) && repo_narrow(item, 3, &file->relation, error) &&
chunks_record_Record_uint(item, 4, &file->len, error) && chunks_record_Record_uint(item, 5, &file->hash, error);
}
@@ -346,6 +362,10 @@ repo_Manifest_encode_body_0(void *frame_)
{
chunks_record_RecordWriter_uint(&frame->item, 6, file->spilled.xid);
chunks_record_RecordWriter_uint(&frame->item, 7, file->spilled.number);
+ if (file->spilled.has_passed)
+ chunks_record_RecordWriter_uint(&frame->item, 8, file->spilled.passed);
+ if (file->spilled.has_from)
+ chunks_record_RecordWriter_uint(&frame->item, 9, file->spilled.from);
}
std_vec_push_Checked(&frame->files, &frame->item, sizeof(frame->item), _Alignof(ChunksRecordRecordWriter));
frame->item_live = false;
@@ -612,10 +632,14 @@ bool
repo_Spilled_Debug_fmt(const void *value, StdFormatter *f)
{
const RepoSpilled *self = value;
+ StdOptionRef passed = {self->has_passed, {&self->passed, std_u64_Debug_fmt}};
+ StdOptionRef from = {self->has_from, {&self->from, std_u64_Debug_fmt}};
StdDebugStruct s = std_fmt_debug_struct(f, "Spilled", strlen("Spilled"));
std_DebugStruct_field(&s, "xid", strlen("xid"), &self->xid, std_u64_Debug_fmt);
std_DebugStruct_field(&s, "number", strlen("number"), &self->number, std_u32_Debug_fmt);
+ std_DebugStruct_field(&s, "passed", strlen("passed"), &passed, std_option_Debug_fmt);
+ std_DebugStruct_field(&s, "from", strlen("from"), &from, std_option_Debug_fmt);
return std_DebugStruct_finish(&s);
}
@@ -623,7 +647,9 @@ repo_Spilled_Debug_fmt(const void *value, StdFormatter *f)
bool
repo_Spilled_PartialEq_eq(const RepoSpilled *self, const RepoSpilled *other)
{
- return self->xid == other->xid && self->number == other->number;
+ return self->xid == other->xid && self->number == other->number && self->has_passed == other->has_passed &&
+ (!self->has_passed || self->passed == other->passed) && self->has_from == other->has_from &&
+ (!self->has_from || self->from == other->from);
}
/* repo_RunFile_Debug_fmt is RunFile's derived Debug. */
@@ -1845,9 +1871,10 @@ repo_decoded(TreesPublishedRepoId repo, bool has, const chunks_TupleAddressLocat
/*
* repo_Repo_save_run_0 is save's run under the root slots: it holds and
- * publishes the roots the pass wrote, writes the manifest that names them
- * if no kept file of the repository was written since the pass's read, and
- * takes the runs back out of it.
+ * publishes the roots the pass wrote, lists every pack whose add the
+ * repository defers, writes the manifest that names them if no kept file of
+ * the repository was written since the pass's read, and takes the runs back
+ * out of it.
*/
static bool
repo_Repo_save_run_inner(repo_Repo_save_frame_0_t *frame, HostIoRegistering *registering,
@@ -1904,6 +1931,8 @@ repo_Repo_save_run_inner(repo_Repo_save_frame_0_t *frame, HostIoRegistering *reg
}
manifest->has_runs = false;
frame->written_manifest_live = true;
+ if (!chunks_packs_backend_Packs_flush(&frame->self->packs, io, frame->reading, error))
+ return false;
if (!host_last_redo(doors, &manifest->redo, &host_error))
{
*error = chunks_error_Error_FromError_from(&host_error);
@@ -2094,7 +2123,14 @@ repo_Repo_save_body_0(void *frame_)
}
if (frame->landed)
{
+ HostError host_error;
+
repo_Repo_save_pad_0(frame);
+ if (!host_pause(HOST_SHARED_IO_PAUSES(frame->io), "doltgres-repo-save-landed", &host_error))
+ {
+ *frame->error = chunks_error_Error_FromError_from(&host_error);
+ return;
+ }
frame->ok = frame->saved(frame->saved_captures, frame->io, frame->error);
return;
}
@@ -2966,7 +3002,7 @@ repo_tests_runs(HostLsn redo, RepoRuns *result)
{.kind = 0, .tablespace = HOST_PG_DEFAULT, .relation = 16384, .len = 4096, .hash = 0xfeed},
{.kind = 3, .tablespace = 0, .relation = 0, .len = 128, .hash = PG_UINT64_MAX},
{.kind = 5, .tablespace = HOST_PG_DEFAULT, .relation = 16384, .len = 512, .hash = 7, .has_spilled = true,
- .spilled = {((uint64) 1 << 40) | 742, 3}},
+ .spilled = {((uint64) 1 << 40) | 742, 3, true, ((uint64) 1 << 33) | 5, true, ((uint64) 1 << 33) | 9}},
};
result->redo = redo;
@@ -3045,7 +3081,7 @@ repo_tests_runs_body_0(void *frame_)
{
uint32 number;
uint64 value;
- } listings[] = {{1, 256}, {2, (uint64) 1 << 32}, {3, (uint64) 1 << 32}, {6, 1}, {7, 1}, {8, 1}};
+ } listings[] = {{1, 256}, {2, (uint64) 1 << 32}, {3, (uint64) 1 << 32}, {6, 1}, {7, 1}, {8, 1}, {9, 1}};
for (size_t k = 0, k_len = 3; k < k_len; k++)
{
diff --git a/src/doltgres/shmem/c/collection.c b/src/doltgres/shmem/c/collection.c
index 924ceff878020..c702864f0383b 100644
--- a/src/doltgres/shmem/c/collection.c
+++ b/src/doltgres/shmem/c/collection.c
@@ -49,6 +49,9 @@ static const char SHMEM_COLLECTION_ASKING[] = "versioned collection ask";
/* SHMEM_COLLECTION_DONE is the bit of the ask's word the worker sets once it has written its answer. */
#define SHMEM_COLLECTION_DONE ((uint64) 1 << 32)
+/* SHMEM_COLLECTION_HELD is the bit of the ask's word its asker holds while it writes the tablespace the ask empties. */
+#define SHMEM_COLLECTION_HELD ((uint64) 1 << 63)
+
/*
* SHMEM_COLLECTION_PG_WAIT_LWLOCK is Postgres' wait event class of a
* lightweight lock's tranche, which the backup's sleep names with the
@@ -61,8 +64,9 @@ static const char SHMEM_COLLECTION_ASKING[] = "versioned collection ask";
* backups, the collection's word (one while a collection runs), the condition
* variable a backup's start sleeps on, and the tranche its sleep names; then
* the ask's word (0, or the asker's process number plus one above DONE, DONE
- * once answered, and the database asked for below it), the collector
- * worker's process number plus one (0 before it starts), the answer's
+ * once answered, and the database asked for below it), the tablespace the
+ * ask empties, the collector worker's process number plus one (0 before it
+ * starts), the answer's
* counts, the condition variable askers sleep on, and the tranche their
* sleep names.
*/
@@ -73,6 +77,7 @@ typedef struct ShmemCollectionMeeting
ConditionVariable wake;
uint32 tranche;
pg_atomic_uint64 asked;
+ pg_atomic_uint64 emptied;
pg_atomic_uint64 worker;
pg_atomic_uint64 answer[3];
ConditionVariable answered;
@@ -117,6 +122,8 @@ shmem_collection_initialize(const PgMayRaise *raise, PgPgError *error)
region = shmem_collection_region();
pg_calls_atomic_init_u64(®ion->asked, 0);
region = shmem_collection_region();
+ pg_calls_atomic_init_u64(®ion->emptied, 0);
+ region = shmem_collection_region();
pg_calls_atomic_init_u64(®ion->worker, 0);
for (size_t at = 0, at_end = 3; at < at_end; at = std_forward_unchecked_usize_Checked(at, 1))
{
@@ -278,6 +285,13 @@ shmem_collection_worker_started(void)
pg_calls_atomic_write_u64(&shmem_collection_region()->worker, number);
}
+/* shmem_collection_worker_running reports whether a collector worker has started, which an ask needs to be answered. */
+bool
+shmem_collection_worker_running(void)
+{
+ return shmem_collection_read(&shmem_collection_region()->worker) != 0;
+}
+
/* shmem_collection_ask_closure_0 is map's closure: an answer's count at a place. */
static uint64
shmem_collection_ask_closure_0(void *captures, size_t at)
@@ -339,14 +353,15 @@ shmem_collection_ask_pad_0(void *frame_)
/*
* shmem_collection_ask asks the collector worker for a full collection of a
- * database's repository and sleeps until it answers with the packs it
+ * database's repository, emptying a tablespace unless that is
+ * CHUNKS_PACKS_DEFAULT_SPACE, and sleeps until it answers with the packs it
* retired, those it copied out, and its live bytes, each
* SHMEM_COLLECTION_FAILED when it failed; one ask is out at a time, and a
* later asker sleeps until it may write its own. A cancel ends the sleep,
* taking back an ask not yet answered, and the collection goes on.
*/
bool
-shmem_collection_ask(const PgMayRaise *raise, uint32 database, uint64 result[3], PgPgError *error)
+shmem_collection_ask(const PgMayRaise *raise, uint32 database, uint32 emptied, uint64 result[3], PgPgError *error)
{
uint64 tmp_1 = (uint64) (uint32) pg_calls_my_proc_number();
uint64 tmp_2 = STD_ADD(u64, tmp_1, 1, 218, 18);
@@ -358,11 +373,13 @@ shmem_collection_ask(const PgMayRaise *raise, uint32 database, uint64 result[3],
STD_CHECK_ALIGNED(region, _Alignof(ShmemCollectionMeeting), 222, 42);
STD_CHECK_NULL_DEREF(region, 222, 42);
wait = SHMEM_COLLECTION_PG_WAIT_LWLOCK | region->asking;
- while (!shmem_collection_swapped(shmem_collection_asked(), 0, mine))
+ while (!shmem_collection_swapped(shmem_collection_asked(), 0, mine | SHMEM_COLLECTION_HELD))
{
if (!shmem_collection_slept(raise, wait, false, 0, error))
return false;
}
+ pg_calls_atomic_write_u64(&shmem_collection_region()->emptied, emptied);
+ (void) shmem_collection_swapped(shmem_collection_asked(), mine | SHMEM_COLLECTION_HELD, mine);
worker = shmem_collection_read(&shmem_collection_region()->worker);
if (worker != 0)
{
@@ -409,9 +426,9 @@ shmem_collection_slept(const PgMayRaise *raise, uint32 wait, bool has_mine, uint
return false;
}
-/* shmem_collection_asked_for returns the unanswered ask's word and the database it asks for, the collector worker's read. */
+/* shmem_collection_asked_for returns the unanswered ask's word, the database it asks for, and the tablespace it empties, the collector worker's read. */
bool
-shmem_collection_asked_for(std_TupleU64U32_t *result)
+shmem_collection_asked_for(std_TupleU64U32_t *result, uint32 *emptied)
{
uint64 word = shmem_collection_read(shmem_collection_asked());
uint32 database;
@@ -421,10 +438,12 @@ shmem_collection_asked_for(std_TupleU64U32_t *result)
std_result_unwrap_failed("the word's low half fits a database id",
sizeof("the word's low half fits a database id") - 1, &tried,
std_TryFromIntError_Debug_fmt, RUST_FILE, 273, 62);
- if (word != 0 && (word & SHMEM_COLLECTION_DONE) == 0)
+ if (word != 0 && (word & (SHMEM_COLLECTION_DONE | SHMEM_COLLECTION_HELD)) == 0)
{
+ pg_calls_read_barrier();
result->f0 = word;
result->f1 = database;
+ *emptied = (uint32) shmem_collection_read(&shmem_collection_region()->emptied);
return true;
}
return false;
diff --git a/src/doltgres/shmem/c/delta.c b/src/doltgres/shmem/c/delta.c
index bb3bd09550210..af9c2b815b9cb 100644
--- a/src/doltgres/shmem/c/delta.c
+++ b/src/doltgres/shmem/c/delta.c
@@ -17,9 +17,10 @@
/*
* The delta's memory in the server (design item 14's S6): the page
* directory, a fixed region of a header, one slot of eight words for each 1
- * KiB of delta_memory, three words for each block of the region, and a word of
+ * KiB of delta_memory, three words for each block of the region, a word of
* the free hint for each 64 slots below the block past the budget's (bugs row
- * 150); and the region of blocks, a region of its own that holds the pages'
+ * 150), and a slot's link in the retired list for each slot (bugs row 309);
+ * and the region of blocks, a region of its own that holds the pages'
* bodies and nothing else (bugs row 116). The postmaster makes both. Postgres
* keeps the regions' addresses in pg's C globals, so this module keeps only
* numbers and translates them inside a call.
@@ -43,16 +44,16 @@ static const char SHMEM_DELTA_WAIT[] = "versioned delta budget";
/* SHMEM_DELTA_REFUSAL is the most bytes of a refusal's detail, its ending zero byte included. */
#define SHMEM_DELTA_REFUSAL ((size_t) 384)
-/* SHMEM_DELTA_SLOTS is the offset of the slots in the directory: after the header's ten cache lines. */
-#define SHMEM_DELTA_SLOTS ((size_t) 640)
+/* SHMEM_DELTA_SLOTS is the offset of the slots in the directory: after the header's eleven cache lines. */
+#define SHMEM_DELTA_SLOTS ((size_t) 704)
/*
* ShmemDeltaHeader is the directory's fixed part, on its first cache lines:
* the count of frees, the hand of an allocation's sweep, the allocation lock,
* the condition variable of the budget's wait with its wait event, the count
* of slots, the overshoot word, the region's words, lock, and count of
- * blocks, the fold launcher's asks and process, and a standby's root
- * mismatch.
+ * blocks, the fold launcher's asks and process, a standby's root mismatch,
+ * and the retired list's head and the free pass's lock.
*/
typedef struct ShmemDeltaHeader
{
@@ -111,6 +112,12 @@ typedef struct ShmemDeltaHeader
*/
pg_atomic_uint64 refused;
+ /*
+ * retired is the first slot plus one of the retired list, the slots the
+ * free pass has yet to free, 0 for none (bugs row 309).
+ */
+ pg_atomic_uint64 retired;
+
/*
* refusal is the detail of the startup process's FATAL, ended by a zero
* byte, once refused is 2.
@@ -119,6 +126,8 @@ typedef struct ShmemDeltaHeader
LWLock lock;
/* region_lock is the region's lock, held only for a block's take or give. */
LWLock region_lock;
+ /* pass_lock is the free pass's lock, which runs one pass at a time. */
+ LWLock pass_lock;
ConditionVariable wait;
uint32 wait_event;
uint32 slots;
@@ -214,6 +223,22 @@ shmem_delta_hint_word(uint32 word)
return (pg_atomic_uint64 *) STD_PTR_AT(pg_calls_delta_directory(), STD_ADD(usize, blocks, tmp_5, 134, 53), 1);
}
+/*
+ * shmem_delta_link_word returns the address of a slot's link in the retired
+ * list, which follow the free hint's words: the next slot plus one, 0 for the
+ * list's last (bugs row 309).
+ */
+static pg_atomic_uint32 *
+shmem_delta_link_word(uint32 slot)
+{
+ size_t slots = (size_t) shmem_delta_slots();
+ size_t words = (size_t) shmem_delta_blocks() * SHMEM_DELTA_BLOCK_WORDS + shmem_delta_hints(slots);
+ size_t links = SHMEM_DELTA_SLOTS + slots * sizeof(ShmemDeltaSlot) + words * sizeof(pg_atomic_uint64);
+
+ Assert(slot < slots);
+ return (pg_atomic_uint32 *) STD_PTR_AT(pg_calls_delta_directory(), links + (size_t) slot * sizeof(pg_atomic_uint32), 1);
+}
+
/*
* shmem_delta_hints returns the count of the free hint's words of a directory
* of a count of slots: one for each SHMEM_DELTA_HINTED of those below the
@@ -230,8 +255,8 @@ shmem_delta_hints(size_t slots)
/*
* shmem_delta_request asks Postgres for the delta's two regions, in the
* postmaster's request callback: the region of blocks, of its bytes, and the
- * directory, of its header, its slots, its blocks' words, and the free
- * hint's words.
+ * directory, of its header, its slots, its blocks' words, the free hint's
+ * words, and the retired list's links.
*/
bool
shmem_delta_request(const PgMayRaise *raise, size_t slots, size_t blocks, size_t bytes, PgPgError *error)
@@ -242,16 +267,17 @@ shmem_delta_request(const PgMayRaise *raise, size_t slots, size_t blocks, size_t
size_t tmp_3 = STD_MUL(usize, slots, sizeof(ShmemDeltaSlot), 147, 45);
size_t tmp_4 = STD_ADD(usize, SHMEM_DELTA_SLOTS, tmp_3, 147, 37);
size_t tmp_5 = STD_ADD(usize, tmp_4, words, 147, 37);
+ size_t links = std_mul_usize(slots, sizeof(pg_atomic_uint32), __FILE__, __LINE__, 0);
- return pg_calls_request_delta(raise, bytes, tmp_5, error);
+ return pg_calls_request_delta(raise, bytes, std_add_usize(tmp_5, links, __FILE__, __LINE__, 0), error);
}
/*
* shmem_delta_initialize sets the delta's directory up in the postmaster's
* initialization callback, after the fork's region: the header, every slot
- * free at its first generation, every block free and never taken, and every
- * bit of the free hint set, since every slot is free. The region's bodies are
- * zeroed as each is taken.
+ * free at its first generation, every block free and never taken, every bit
+ * of the free hint set, since every slot is free, and the retired list empty.
+ * The region's bodies are zeroed as each is taken.
*/
bool
shmem_delta_initialize(const PgMayRaise *raise, size_t slots, size_t blocks, PgPgError *error)
@@ -284,8 +310,10 @@ shmem_delta_initialize(const PgMayRaise *raise, size_t slots, size_t blocks, PgP
pg_calls_atomic_init_u64(&header->asked, 0);
pg_calls_atomic_init_u64(&header->launcher, 0);
pg_calls_atomic_init_u64(&header->refused, 0);
+ pg_calls_atomic_init_u64(&header->retired, 0);
pg_calls_lwlock_initialize(&header->lock, tranche);
pg_calls_lwlock_initialize(&header->region_lock, tranche);
+ pg_calls_lwlock_initialize(&header->pass_lock, tranche);
pg_calls_cv_init(&header->wait);
header->wait_event = wait_event;
header->slots = (uint32) slots;
@@ -299,6 +327,8 @@ shmem_delta_initialize(const PgMayRaise *raise, size_t slots, size_t blocks, PgP
tmp_5 = STD_ADD(usize, tmp_4, words, 185, 43);
std_ptr_write_bytes(STD_PTR_AT(shmem_delta_slot(0), tmp_5, 1), 0xff,
STD_MUL(usize, shmem_delta_hints(slots), 8, 185, 96), 1, 1);
+ for (uint32 slot = 0; slot < (uint32) slots; slot++)
+ pg_calls_atomic_init_u32(shmem_delta_link_word(slot), 0);
return true;
}
@@ -543,6 +573,51 @@ shmem_delta_hand(void)
return (uint32) STD_REM(u64, hand, tmp_1, 417, 6);
}
+/* shmem_delta_retired loads the retired list's head: its first slot plus one, 0 for none. */
+uint64
+shmem_delta_retired(void)
+{
+ return pg_calls_atomic_read_u64(&shmem_delta_header()->retired);
+}
+
+/* shmem_delta_retired_swap stores the retired list's head over the one loaded, reporting whether it was still there. */
+bool
+shmem_delta_retired_swap(uint64 loaded, uint64 word)
+{
+ uint64 seen = loaded;
+
+ pg_calls_atomic_exchange_u64(&shmem_delta_header()->retired, &seen, word);
+ return seen == loaded;
+}
+
+/* shmem_delta_link loads a slot's link in the retired list: the next slot plus one, 0 for the last. */
+uint32
+shmem_delta_link(uint32 slot)
+{
+ return pg_calls_atomic_read_u32(shmem_delta_link_word(slot));
+}
+
+/* shmem_delta_set_link stores a slot's link in the retired list. */
+void
+shmem_delta_set_link(uint32 slot, uint32 value)
+{
+ pg_calls_atomic_write_u32(shmem_delta_link_word(slot), value);
+}
+
+/* shmem_delta_lock_pass takes the free pass's lock exclusively, which can't fail. */
+void
+shmem_delta_lock_pass(void)
+{
+ pg_calls_lwlock_acquire_sure(&shmem_delta_header()->pass_lock, LW_EXCLUSIVE);
+}
+
+/* shmem_delta_unlock_pass releases the free pass's lock, which its caller holds. */
+void
+shmem_delta_unlock_pass(void)
+{
+ pg_calls_lwlock_release(&shmem_delta_header()->pass_lock);
+}
+
/* shmem_delta_lock takes the allocation lock exclusively. */
bool
shmem_delta_lock(const PgMayRaise *raise, PgPgError *error)
@@ -771,6 +846,9 @@ shmem_delta_body_word(uint64 pointer, uint64 index, ShmemRecordsOp op)
pg_calls_atomic_exchange_u64(word, &seen, op.u.exchange.f1);
return seen;
}
+ case SHMEM_RECORDS_OP_LOAD_ACQUIRE:
+ pg_calls_read_barrier();
+ return pg_calls_atomic_read_u64(word);
}
pg_unreachable();
}
diff --git a/src/doltgres/shmem/c/records.c b/src/doltgres/shmem/c/records.c
index 887fb9005ff5d..64569f43546f7 100644
--- a/src/doltgres/shmem/c/records.c
+++ b/src/doltgres/shmem/c/records.c
@@ -78,11 +78,16 @@ typedef struct ShmemRecordsSpot
ShmemRecordsForm form;
} ShmemRecordsSpot;
-/* ShmemRecordsPlaced is a kind in the region: its spot, and the hand its makes sweep from. */
+/*
+ * ShmemRecordsPlaced is a kind in the region: its spot, the hand its makes
+ * sweep from, and for an aged kind the head of its retired list, its first
+ * record plus one, 0 for none (bugs row 309).
+ */
typedef struct ShmemRecordsPlaced
{
ShmemRecordsSpot spot;
pg_atomic_uint64 hand;
+ pg_atomic_uint64 retired;
} ShmemRecordsPlaced;
/*
@@ -373,18 +378,26 @@ shmem_records_part(size_t offset, size_t index, size_t size)
return STD_PTR_AT(base, std_wrapping_add_usize(0, tmp_2), 1);
}
+/* ShmemRecordsResolved is a kind as a process resolves it: its place and counts, and its records' layout. */
+typedef struct ShmemRecordsResolved
+{
+ ShmemRecordsSpot spot;
+ ShmemRecordsLayout layout;
+} ShmemRecordsResolved;
+
/* ShmemRecordsMapped is a record in this process's mapping of the region, with its kind's counts and its layout. */
typedef struct ShmemRecordsMapped
{
uint8 *base;
- ShmemRecordsSpot spot;
- ShmemRecordsLayout layout;
+ const ShmemRecordsSpot *spot;
+ const ShmemRecordsLayout *layout;
} ShmemRecordsMapped;
/* shmem_records_Mapped_of returns the record at an index among a kind's records, refusing an index past the kind's count. */
static ShmemRecordsMapped
-shmem_records_Mapped_of(const ShmemRecordsSpot *spot, size_t index)
+shmem_records_Mapped_of(const ShmemRecordsResolved *resolved, size_t index)
{
+ const ShmemRecordsSpot *spot = &resolved->spot;
ShmemRecordsMapped result;
size_t tmp_1;
@@ -392,8 +405,8 @@ shmem_records_Mapped_of(const ShmemRecordsSpot *spot, size_t index)
STD_PANIC(419, 9, "a shared record's number is past its kind's count");
tmp_1 = STD_MUL(usize, index, (size_t) spot->size, 420, 53);
result.base = (uint8 *) shmem_records_part((size_t) spot->at, tmp_1, 1);
- result.spot = *spot;
- result.layout = shmem_records_layout(spot);
+ result.spot = spot;
+ result.layout = &resolved->layout;
return result;
}
@@ -419,41 +432,41 @@ shmem_records_Mapped_part(const ShmemRecordsMapped *self, size_t at, uint32 coun
static LWLock *
shmem_records_Mapped_lock(const ShmemRecordsMapped *self, size_t index)
{
- return shmem_records_Mapped_part(self, 0, self->spot.locks, index, sizeof(LWLock));
+ return shmem_records_Mapped_part(self, 0, self->spot->locks, index, sizeof(LWLock));
}
/* shmem_records_Mapped_flag returns a flag's address. */
static ShmemRecordsFlagSlot *
shmem_records_Mapped_flag(const ShmemRecordsMapped *self, size_t index)
{
- return shmem_records_Mapped_part(self, self->layout.flags, self->spot.flags, index, sizeof(ShmemRecordsFlagSlot));
+ return shmem_records_Mapped_part(self, self->layout->flags, self->spot->flags, index, sizeof(ShmemRecordsFlagSlot));
}
/* shmem_records_Mapped_atomic returns an atomic's address. */
static pg_atomic_uint64 *
shmem_records_Mapped_atomic(const ShmemRecordsMapped *self, size_t index)
{
- return shmem_records_Mapped_part(self, self->layout.atomics, self->spot.atomics, index, sizeof(pg_atomic_uint64));
+ return shmem_records_Mapped_part(self, self->layout->atomics, self->spot->atomics, index, sizeof(pg_atomic_uint64));
}
/* shmem_records_Mapped_blob returns the address of a blob's length, which its bytes follow. */
static uint64 *
shmem_records_Mapped_blob(const ShmemRecordsMapped *self, size_t index)
{
- if (!(index < (size_t) self->spot.blobs))
+ if (!(index < (size_t) self->spot->blobs))
STD_PANIC(447, 9, "a shared record's part index is past its shape's count");
std_index_check(index, SHMEM_RECORDS_BLOBS, RUST_FILE, 448, 32);
- return STD_PTR_AT(self->base, self->layout.blobs[index], 1);
+ return STD_PTR_AT(self->base, self->layout->blobs[index], 1);
}
/* shmem_records_Mapped_array returns the address of an array's count, which its name and then its entries follow. */
static pg_atomic_uint64 *
shmem_records_Mapped_array(const ShmemRecordsMapped *self, size_t index)
{
- if (!(index < (size_t) self->spot.arrays))
+ if (!(index < (size_t) self->spot->arrays))
STD_PANIC(453, 9, "a shared record's part index is past its shape's count");
std_index_check(index, SHMEM_RECORDS_ARRAYS, RUST_FILE, 454, 32);
- return STD_PTR_AT(self->base, self->layout.arrays[index], 1);
+ return STD_PTR_AT(self->base, self->layout->arrays[index], 1);
}
/* shmem_records_Mapped_entries returns the address of an array's first entry. */
@@ -470,12 +483,12 @@ shmem_records_Mapped_entry(const ShmemRecordsMapped *self, size_t array, uint64
uint8 *first = shmem_records_Mapped_entries(self, array);
std_index_check(array, SHMEM_RECORDS_ARRAYS, RUST_FILE, 465, 28);
- if (entry < (uint64) self->spot.entries[array])
+ if (entry < (uint64) self->spot->entries[array])
{
size_t tmp_1;
std_index_check(array, SHMEM_RECORDS_ARRAYS, RUST_FILE, 466, 58);
- tmp_1 = STD_MUL(usize, (size_t) entry, (size_t) self->spot.widths[array], 466, 41);
+ tmp_1 = STD_MUL(usize, (size_t) entry, (size_t) self->spot->widths[array], 466, 41);
return STD_PTR_AT(first, tmp_1, 1);
}
return NULL;
@@ -484,6 +497,58 @@ shmem_records_Mapped_entry(const ShmemRecordsMapped *self, size_t array, uint64
/* shmem_records_record returns the record at an index among a kind's records. */
static ShmemRecordsMapped shmem_records_record(size_t kind, uint32 index);
+/*
+ * SHMEM_RECORDS_RESOLVED holds, once this process first reaches a record, each
+ * kind's place, counts, and layout as offsets from the process's own mapping
+ * of the region, a Vec read-only after it is made: the postmaster
+ * fixes the places for its life, a process attaches once, and the region's
+ * initialize, which a crash restart runs again, clears it (bugs row 355).
+ */
+static StdVec SHMEM_RECORDS_RESOLVED = STD_VEC_NEW;
+
+/* shmem_records_unresolved clears this process's resolutions, for the region's initialize. */
+static void
+shmem_records_unresolved(void)
+{
+ std_vec_free_Checked(&SHMEM_RECORDS_RESOLVED, sizeof(ShmemRecordsResolved), _Alignof(ShmemRecordsResolved));
+ std_vec_new(&SHMEM_RECORDS_RESOLVED, sizeof(ShmemRecordsResolved), _Alignof(ShmemRecordsResolved));
+}
+
+/*
+ * shmem_records_resolved returns a kind as this process resolved it, the
+ * first call resolving every kind of the region from its places, refusing a
+ * kind the region doesn't hold, which only a caller that passes a kind this
+ * module didn't give it could ask for.
+ */
+static const ShmemRecordsResolved *
+shmem_records_resolved(size_t kind)
+{
+ if (SHMEM_RECORDS_RESOLVED.len == 0)
+ {
+ ShmemControl *control = shmem_records_control();
+ size_t kinds;
+
+ STD_CHECK_ALIGNED(control, _Alignof(ShmemControl), 321, 24);
+ STD_CHECK_NULL_DEREF(control, 321, 24);
+ kinds = (size_t) control->kinds;
+ std_vec_with_capacity_Checked(&SHMEM_RECORDS_RESOLVED, kinds, sizeof(ShmemRecordsResolved),
+ _Alignof(ShmemRecordsResolved));
+ for (size_t each = 0; each < kinds; each++)
+ {
+ ShmemRecordsResolved resolved;
+
+ resolved.spot = ((ShmemRecordsPlaced *) shmem_records_part(SHMEM_RECORDS_PLACED, each,
+ sizeof(ShmemRecordsPlaced)))->spot;
+ resolved.layout = shmem_records_layout(&resolved.spot);
+ std_vec_push_Checked(&SHMEM_RECORDS_RESOLVED, &resolved, sizeof(resolved),
+ _Alignof(ShmemRecordsResolved));
+ }
+ }
+ if (!(kind < SHMEM_RECORDS_RESOLVED.len))
+ STD_PANIC(321, 9, "a shared record's kind is past the region's kinds");
+ return &STD_VEC_AT(&SHMEM_RECORDS_RESOLVED, ShmemRecordsResolved, kind);
+}
+
/* shmem_records_initialize_frame_0_t is initialize's state across its pad: its arguments and the plan. */
typedef struct shmem_records_initialize_frame_0_t
{
@@ -507,6 +572,7 @@ shmem_records_initialize_body_0(void *frame_)
size_t procs = (size_t) pg_calls_max_procs();
uint32 flag_wait;
+ shmem_records_unresolved();
frame->plan = shmem_records_plan(frame->kinds, frame->kinds_len, procs);
frame->plan_live = true;
if (!pg_calls_wait_event_new(frame->raise, SHMEM_RECORDS_FLAG_WAIT, &flag_wait, frame->error))
@@ -535,16 +601,20 @@ shmem_records_initialize_body_0(void *frame_)
size_t kind = count;
const ShmemRecordsSpot *spot = (const ShmemRecordsSpot *) STD_PTR_AT(frame->plan.spots.ptr, i,
sizeof(ShmemRecordsSpot));
+ ShmemRecordsResolved planned;
ShmemRecordsPlaced *placed;
count = std_Enumerate_count_step(count);
+ planned.spot = *spot;
+ planned.layout = shmem_records_layout(spot);
placed = shmem_records_part(SHMEM_RECORDS_PLACED, kind, sizeof(ShmemRecordsPlaced));
placed->spot = *spot;
pg_calls_atomic_init_u64(&placed->hand, 0);
+ pg_calls_atomic_init_u64(&placed->retired, 0);
for (size_t index = 0, index_end = (size_t) spot->count; index < index_end;
index = std_forward_unchecked_usize_Checked(index, 1))
{
- ShmemRecordsMapped record = shmem_records_Mapped_of(spot, index);
+ ShmemRecordsMapped record = shmem_records_Mapped_of(&planned, index);
for (size_t lock = 0, lock_end = (size_t) spot->locks; lock < lock_end;
lock = std_forward_unchecked_usize_Checked(lock, 1))
@@ -617,13 +687,7 @@ shmem_records_initialize(const PgMayRaise *raise, ShmemControl *control, int32 t
static ShmemRecordsSpot
shmem_records_spot(size_t kind)
{
- ShmemControl *control = shmem_records_control();
-
- STD_CHECK_ALIGNED(control, _Alignof(ShmemControl), 321, 24);
- STD_CHECK_NULL_DEREF(control, 321, 24);
- if (!(kind < (size_t) control->kinds))
- STD_PANIC(321, 9, "a shared record's kind is past the region's kinds");
- return ((ShmemRecordsPlaced *) shmem_records_part(SHMEM_RECORDS_PLACED, kind, sizeof(ShmemRecordsPlaced)))->spot;
+ return shmem_records_resolved(kind)->spot;
}
/* shmem_records_kinds returns the number of the region's kinds. */
@@ -769,12 +833,30 @@ shmem_records_hand(size_t kind)
return (uint32) STD_REM(u64, hand, (uint64) spot.count, 406, 6);
}
+/* shmem_records_retired loads an aged kind's retired list's head: its first record plus one, 0 for none. */
+uint64
+shmem_records_retired(size_t kind)
+{
+ ShmemRecordsPlaced *placed = shmem_records_part(SHMEM_RECORDS_PLACED, kind, sizeof(ShmemRecordsPlaced));
+
+ return pg_calls_atomic_read_u64(&placed->retired);
+}
+
+/* shmem_records_retired_swap stores an aged kind's retired list's head over the one loaded, reporting whether it was still there. */
+bool
+shmem_records_retired_swap(size_t kind, uint64 loaded, uint64 word)
+{
+ ShmemRecordsPlaced *placed = shmem_records_part(SHMEM_RECORDS_PLACED, kind, sizeof(ShmemRecordsPlaced));
+ uint64 seen = loaded;
+
+ pg_calls_atomic_exchange_u64(&placed->retired, &seen, word);
+ return seen == loaded;
+}
+
static ShmemRecordsMapped
shmem_records_record(size_t kind, uint32 index)
{
- ShmemRecordsSpot spot = shmem_records_spot(kind);
-
- return shmem_records_Mapped_of(&spot, (size_t) index);
+ return shmem_records_Mapped_of(shmem_records_resolved(kind), (size_t) index);
}
/* SHMEM_RECORDS_READERS_LOCK is READERS' RwLock, which guards SHMEM_RECORDS_READERS. */
@@ -821,20 +903,20 @@ shmem_records_clear(size_t kind, uint32 index)
{
ShmemRecordsMapped record = shmem_records_record(kind, index);
- for (size_t atomic = 0, atomic_end = (size_t) record.spot.atomics; atomic < atomic_end;
+ for (size_t atomic = 0, atomic_end = (size_t) record.spot->atomics; atomic < atomic_end;
atomic = std_forward_unchecked_usize_Checked(atomic, 1))
pg_calls_atomic_write_u64(shmem_records_Mapped_atomic(&record, atomic), 0);
- for (size_t blob = 0, blob_end = (size_t) record.spot.blobs; blob < blob_end;
+ for (size_t blob = 0, blob_end = (size_t) record.spot->blobs; blob < blob_end;
blob = std_forward_unchecked_usize_Checked(blob, 1))
*shmem_records_Mapped_blob(&record, blob) = 0;
- for (size_t array = 0, array_end = (size_t) record.spot.arrays; array < array_end;
+ for (size_t array = 0, array_end = (size_t) record.spot->arrays; array < array_end;
array = std_forward_unchecked_usize_Checked(array, 1))
{
uint32 tmp_1;
std_index_check(array, SHMEM_RECORDS_ARRAYS, RUST_FILE, 506, 51);
std_index_check(array, SHMEM_RECORDS_ARRAYS, RUST_FILE, 506, 80);
- tmp_1 = STD_MUL(u32, record.spot.entries[array], record.spot.widths[array],
+ tmp_1 = STD_MUL(u32, record.spot->entries[array], record.spot->widths[array],
506, 51);
std_ptr_write_bytes(shmem_records_Mapped_entries(&record, array), 0, (size_t) tmp_1, 1, 1);
}
@@ -1065,6 +1147,9 @@ shmem_records_atomic(size_t kind, uint32 index, size_t atomic, ShmemRecordsOp op
pg_calls_atomic_exchange_u64(at, &seen, op.u.exchange.f1);
return seen;
}
+ case SHMEM_RECORDS_OP_LOAD_ACQUIRE:
+ pg_calls_read_barrier();
+ return pg_calls_atomic_read_u64(at);
}
pg_unreachable();
}
@@ -1097,7 +1182,7 @@ shmem_records_blob_write(size_t kind, uint32 index, size_t blob, const uint8 *by
uint64 *length = shmem_records_Mapped_blob(&record, blob);
std_index_check(blob, SHMEM_RECORDS_BLOBS, RUST_FILE, 626, 22);
- if (bytes_len > (size_t) record.spot.maxes[blob])
+ if (bytes_len > (size_t) record.spot->maxes[blob])
{
*error = SHMEM_RECORDS_INTERNAL("a shared blob's bytes are longer than its largest size");
return false;
@@ -1133,7 +1218,7 @@ shmem_records_entry_read(size_t kind, uint32 index, size_t array, size_t entry,
return false;
}
std_index_check(array, SHMEM_RECORDS_ARRAYS, RUST_FILE, 647, 49);
- width = (size_t) record.spot.widths[array];
+ width = (size_t) record.spot->widths[array];
std_slice_from_raw_parts_check(at, width, 1, 1, RUST_FILE, 647, 17);
std_slice_to_vec_Checked(result, at, width, 1, 1);
return true;
@@ -1166,7 +1251,7 @@ shmem_records_entry_write(size_t kind, uint32 index, size_t array, size_t entry,
return false;
}
std_index_check(array, SHMEM_RECORDS_ARRAYS, RUST_FILE, 656, 23);
- if (bytes_len != (size_t) record.spot.widths[array])
+ if (bytes_len != (size_t) record.spot->widths[array])
{
*error = SHMEM_RECORDS_INTERNAL("a shared array's entry is of another length than its bytes");
return false;
@@ -1255,10 +1340,10 @@ shmem_records_slots_claim(const PgMayRaise *raise, size_t kind, uint32 index, si
uint8 *at;
std_index_check(array, SHMEM_RECORDS_ARRAYS, RUST_FILE, 713, 24);
- if (!(bytes_len == (size_t) record.spot.widths[array]))
+ if (!(bytes_len == (size_t) record.spot->widths[array]))
STD_PANIC(712, 5, "a shared slot's bytes are of another length than its array's entries");
std_index_check(array, SHMEM_RECORDS_ARRAYS, RUST_FILE, 718, 59);
- if (pg_calls_atomic_read_u64(count) >= (uint64) record.spot.entries[array])
+ if (pg_calls_atomic_read_u64(count) >= (uint64) record.spot->entries[array])
{
*result = false;
return true;
@@ -1320,10 +1405,10 @@ shmem_records_slots_read_body_0(void *frame_)
size_t len;
std_index_check(frame->array, SHMEM_RECORDS_ARRAYS, RUST_FILE, 739, 50);
- width = (size_t) record.spot.widths[frame->array];
+ width = (size_t) record.spot->widths[frame->array];
count = pg_calls_atomic_read_u64(shmem_records_Mapped_array(&record, frame->array));
std_index_check(frame->array, SHMEM_RECORDS_ARRAYS, RUST_FILE, 742, 83);
- count = std_min_u64(count, (uint64) record.spot.entries[frame->array]);
+ count = std_min_u64(count, (uint64) record.spot->entries[frame->array]);
len = (size_t) count;
std_vec_with_capacity_Checked(&frame->entries, len, sizeof(StdVec), _Alignof(StdVec));
frame->entries_live = true;
diff --git a/src/doltgres/store/c/collect.c b/src/doltgres/store/c/collect.c
index ffb13fc021f0f..27c8f3e2ed907 100644
--- a/src/doltgres/store/c/collect.c
+++ b/src/doltgres/store/c/collect.c
@@ -30,6 +30,7 @@
#include "doltgres/chunks/file.h"
#include "doltgres/chunks/header.h"
#include "doltgres/chunks/pack.h"
+#include "doltgres/chunks/packs.h"
#include "doltgres/chunks/packs/backend.h"
#include "doltgres/chunks/packs/marking.h"
#include "doltgres/chunks/packs/open.h"
@@ -45,6 +46,7 @@
#include "doltgres/std/int128.h"
#include "doltgres/std/num.h"
#include "doltgres/std/slice.h"
+#include "doltgres/std/str.h"
#include "doltgres/std/sync.h"
#include "doltgres/std/time.h"
#include "doltgres/std/tuple.h"
@@ -58,7 +60,6 @@
#ifdef DOLTGRES_TEST_STORE
#include "doltgres/chunks/address.h"
#include "doltgres/simc/simc.h"
-#include "doltgres/std/str.h"
#include "doltgres/std/test.h"
#include "doltgres/store/fixtures.h"
#endif
@@ -1869,6 +1870,7 @@ typedef struct store_collect_marked_frame_0_t
uint64 newest;
uint64 limit;
uint64 percent;
+ uint32 emptied;
bool *has_result;
StoreCollectCollected *result;
HostError *error;
@@ -2131,6 +2133,8 @@ store_collect_marked_choose(store_collect_marked_frame_0_t *frame)
live = store_collect_bytes_get(&frame->marks.bytes, entry.f0);
if (live == 0)
std_vec_push_Checked(&frame->dead, &entry.f0, sizeof(uint64), _Alignof(uint64));
+ else if (entry.f1 == frame->emptied && frame->emptied != CHUNKS_PACKS_DEFAULT_SPACE)
+ store_collect_copied_push(&frame->copied, CHUNKS_PACKS_DEFAULT_SPACE, entry.f0);
else if (store_collect_over(chunks_pack_PackReader_records_len(std_arc_Deref_deref(reader,
_Alignof(ChunksPackPackReader))),
live, frame->percent))
@@ -2387,12 +2391,12 @@ store_collect_marked_body_0(void *frame_)
*/
static bool
store_collect_marked(HostIoIo *io, const RepoRepo *repo, const HostSharedReader *reading, uint32 database,
- const StdVec *old, uint64 newest, uint64 limit, uint64 percent, bool *has_result,
+ const StdVec *old, uint64 newest, uint64 limit, uint64 percent, uint32 emptied, bool *has_result,
StoreCollectCollected *result, HostError *error)
{
store_collect_marked_frame_0_t frame = {.io = io, .repo = repo, .reading = reading, .database = database,
- .old = old, .newest = newest, .limit = limit, .percent = percent, .has_result = has_result, .result = result,
- .error = error};
+ .old = old, .newest = newest, .limit = limit, .percent = percent, .emptied = emptied, .has_result = has_result,
+ .result = result, .error = error};
std_landing(&frame, store_collect_marked_body_0, store_collect_marked_pad_0);
return frame.ok;
@@ -2406,7 +2410,7 @@ store_collect_marked(HostIoIo *io, const RepoRepo *repo, const HostSharedReader
*/
static bool
store_collect_marking(HostIoIo *io, const RepoRepo *repo, const HostSharedReader *reading, uint32 database,
- const StdVec *old, uint64 limit, uint64 percent, bool *has_result,
+ const StdVec *old, uint64 limit, uint64 percent, uint32 emptied, bool *has_result,
StoreCollectCollected *result, HostError *error)
{
const ChunksPacksBackendPacks *packs = &repo->packs;
@@ -2421,7 +2425,7 @@ store_collect_marking(HostIoIo *io, const RepoRepo *repo, const HostSharedReader
if (!chunks_packs_marking_Packs_begin_marking(packs, io, reading, &newest, &failed))
goto raised;
- if (store_collect_marked(io, repo, reading, database, old, newest, limit, percent, has_result, result,
+ if (store_collect_marked(io, repo, reading, database, old, newest, limit, percent, emptied, has_result, result,
&collect_error))
{
if (*has_result)
@@ -2451,11 +2455,14 @@ store_collect_marking(HostIoIo *io, const RepoRepo *repo, const HostSharedReader
* base backup is counted in. Its marking's reads and its copies run at limit
* bytes a second, or unpaced at 0, and it copies out the live chunks of a
* pack that passes them by percent. It answers nothing when a base backup
- * kept it from finishing.
+ * kept it from finishing. A tablespace it empties, unless that is
+ * CHUNKS_PACKS_DEFAULT_SPACE, has the live chunks of each of its packs copied
+ * into the repository's own directory whatever their share, so that the
+ * packs retire, as DROP TABLESPACE needs (bugs row 99).
*/
bool
store_collect_collected(HostIoIo *io, const RepoRepo *repo, const HostSharedReader *reading, uint32 database,
- uint64 limit, uint64 percent, const StdVec *old, bool *has_result,
+ uint64 limit, uint64 percent, uint32 emptied, const StdVec *old, bool *has_result,
StoreCollectCollected *result, HostError *error)
{
bool done;
@@ -2465,7 +2472,8 @@ store_collect_collected(HostIoIo *io, const RepoRepo *repo, const HostSharedRead
return false;
if (!host_collection_begin())
return true;
- done = store_collect_marking(io, repo, reading, database, old, limit, percent, has_result, result, error);
+ done = store_collect_marking(io, repo, reading, database, old, limit, percent, emptied, has_result, result,
+ error);
host_collection_end();
return done;
}
@@ -2710,8 +2718,8 @@ store_collect_checked(HostIoIo *io, uint32 repository, const StoreCollectDatabas
old = held->old;
std_vec_new(&held->old, sizeof(uint64), _Alignof(uint64));
}
- if (!store_collect_collected(io, &repo, reading, database->id, limit, settings->target_percent, &old, &has_done,
- &done, error))
+ if (!store_collect_collected(io, &repo, reading, database->id, limit, settings->target_percent,
+ CHUNKS_PACKS_DEFAULT_SPACE, &old, &has_done, &done, error))
{
std_vec_free_Checked(&old, sizeof(uint64), _Alignof(uint64));
goto dropped;
@@ -2927,9 +2935,88 @@ store_collect_collect_due(HostIoIo *io, StoreCollectSchedule *schedule, const St
return frame.ok;
}
-/* store_collect_asked is an ask's full collection under its database's lock, the repository opened for it and closed after; its caller gives the lock back however it ends. */
+/*
+ * store_collect_unlisted takes an emptied tablespace out of a repository's
+ * lists once the repository lists none of its packs (bugs row 99).
+ */
+static bool
+store_collect_unlisted(HostIoIo *io, const RepoRepo *repo, const HostSharedReader *reading, uint32 emptied,
+ HostError *error)
+{
+ ChunksErrorError failed;
+ StdVec spaces;
+ bool listed = false;
+
+ if (!chunks_packs_backend_Packs_spaces(&repo->packs, io, reading, &spaces, &failed))
+ {
+ *error = chunks_packs_shared_raised(&failed);
+ return false;
+ }
+ for (size_t k = 0, k_len = spaces.len; k < k_len; k++)
+ listed |= STD_VEC_AT(&spaces, std_TupleU64U32_t, k).f1 == emptied;
+ std_vec_free_Checked(&spaces, sizeof(std_TupleU64U32_t), _Alignof(std_TupleU64U32_t));
+ if (listed)
+ return true;
+ if (!chunks_packs_backend_Packs_remove_space(&repo->packs, HOST_IO_IO_TAKES_SHORT(io), reading, emptied, &failed))
+ {
+ *error = chunks_packs_shared_raised(&failed);
+ return false;
+ }
+ return true;
+}
+
+/*
+ * store_collect_emptied removes the files due removal, and then a
+ * repository's directory in an emptied tablespace once no pack file is left
+ * in it, every one either copied out and retired with its readers ended or
+ * never listed; a pack file still there, a pack being written included,
+ * leaves the directory for a later ask. A writer that opens in the
+ * tablespace after the directory's listing finds it gone and its pack's
+ * write fails, which leaves its fold's delta for a retry that makes the
+ * directory again, and its relation's own file there makes Postgres refuse
+ * the DROP.
+ */
static bool
-store_collect_asked(HostIoIo *io, uint32 repository, const StoreCollectDatabase *database,
+store_collect_emptied(HostIoIo *io, uint32 database, uint32 emptied, HostError *error)
+{
+ ChunksErrorError failed;
+ StdPathBuf dir;
+ StdVec names;
+ bool has;
+ bool packed = false;
+ bool removed;
+ bool ok = true;
+
+ if (!chunks_packs_removal_remove_due(io, &failed))
+ {
+ *error = chunks_packs_shared_raised(&failed);
+ return false;
+ }
+ dir = host_tablespace_dir(database, emptied);
+ if (!host_list_dir_if_there(HOST_IO_IO_READ_DOORS(io), (const char *) dir.bytes.ptr, dir.bytes.len, &has, &names,
+ error))
+ ok = false;
+ else if (has)
+ {
+ for (size_t k = 0, k_len = names.len; k < k_len; k++)
+ {
+ const StdString *name = &STD_VEC_AT(&names, StdString, k);
+
+ packed |= (name->len >= 5 && memcmp((const char *) name->ptr + name->len - 5, ".pack", 5) == 0) ||
+ (name->len >= 9 && memcmp((const char *) name->ptr + name->len - 9, ".pack.tmp", 9) == 0);
+ }
+ std_vec_drop_Checked(&names, sizeof(StdString), _Alignof(StdString), std_String_drop);
+ if (!packed)
+ ok = host_remove_tree(HOST_IO_IO_IO_DOORS(io), (const char *) dir.bytes.ptr, dir.bytes.len, &removed,
+ error);
+ }
+ std_vec_free_Checked(&dir.bytes, 1, 1);
+ return ok;
+}
+
+/* store_collect_asked is an ask's full collection under its database's lock, emptying the tablespace it names, the repository opened for it and closed after; its caller gives the lock back however it ends. */
+static bool
+store_collect_asked(HostIoIo *io, uint32 repository, const StoreCollectDatabase *database, uint32 emptied,
const StoreCollectSettings *settings, bool *has_result, StoreCollectCollected *result,
HostError *error)
{
@@ -2947,8 +3034,14 @@ store_collect_asked(HostIoIo *io, uint32 repository, const StoreCollectDatabase
return false;
}
std_vec_new(&none, sizeof(uint64), _Alignof(uint64));
- ok = store_collect_collected(io, &repo, &reading, database->id, 0, settings->target_percent, &none, has_result,
- result, error);
+ ok = store_collect_collected(io, &repo, &reading, database->id, 0, settings->target_percent, emptied, &none,
+ has_result, result, error);
+ if (ok && *has_result && emptied != CHUNKS_PACKS_DEFAULT_SPACE &&
+ !store_collect_unlisted(io, &repo, &reading, emptied, error))
+ {
+ store_collect_Collected_drop(result);
+ ok = false;
+ }
if (!ok)
repo_Repo_drop(&repo);
else if (!repo_Repo_close(&repo, io, &reading, &failed))
@@ -2959,12 +3052,18 @@ store_collect_asked(HostIoIo *io, uint32 repository, const StoreCollectDatabase
ok = false;
}
host_shared_Reader_drop(&reading);
+ if (ok && *has_result && emptied != CHUNKS_PACKS_DEFAULT_SPACE && !store_collect_emptied(io, repository, emptied, error))
+ {
+ store_collect_Collected_drop(result);
+ ok = false;
+ }
return ok;
}
/*
* store_collect_collect_asked runs the full collection a backend asked of the
- * collector worker in a database's repository, recorded in the schedule as
+ * collector worker in a database's repository, emptying the tablespace the
+ * ask names (collected), recorded in the schedule as
* its own, under the database's lock as collect_due takes it, and returns its
* counts, a [u64; 3]: the packs it retired, those it copied out, and its live
* bytes, all 0 when the database may not be collected, a session holds its
@@ -2972,8 +3071,8 @@ store_collect_asked(HostIoIo *io, uint32 repository, const StoreCollectDatabase
*/
bool
store_collect_collect_asked(HostIoIo *io, StoreCollectSchedule *schedule, const StoreCollectDatabase *databases,
- size_t databases_len, uint32 id, const StoreCollectSettings *settings, uint64 *result,
- HostError *error)
+ size_t databases_len, uint32 id, uint32 emptied, const StoreCollectSettings *settings,
+ uint64 *result, HostError *error)
{
const StoreCollectDatabase *database = store_collect_find(databases, databases_len, id, true);
StoreCollectCollected done;
@@ -2996,7 +3095,7 @@ store_collect_collect_asked(HostIoIo *io, StoreCollectSchedule *schedule, const
return false;
if (!locked)
return true;
- ok = store_collect_asked(io, id, database, settings, &has, &done, error);
+ ok = store_collect_asked(io, id, database, emptied, settings, &has, &done, error);
host_database_unlocked(id);
if (!ok)
return false;
@@ -3238,8 +3337,8 @@ store_collect_tests_collected(HostIoIo *io, const RepoRepo *repo, const HostShar
bool has;
std_vec_new(&none, sizeof(uint64), _Alignof(uint64));
- store_collect_tests_ok(store_collect_collected(io, repo, reading, database, limit, 50, old == NULL ? &none : old,
- &has, &done, &error), &error, line, col);
+ store_collect_tests_ok(store_collect_collected(io, repo, reading, database, limit, 50, CHUNKS_PACKS_DEFAULT_SPACE,
+ old == NULL ? &none : old, &has, &done, &error), &error, line, col);
if (!has)
std_option_unwrap_failed(RUST_FILE, line, some_col);
return done;
@@ -3408,8 +3507,8 @@ test_store_collect_tests_a_collection_runs_nothing_while_a_base_backup_is_counte
store_collect_tests_ok(host_backup_begin(io, &error), &error, 924, 32);
STD_ASSERT(925, 9, host_backups_running(), "host::backups_running()");
std_vec_new(&none, sizeof(uint64), _Alignof(uint64));
- store_collect_tests_ok(store_collect_collected(io, &repo, &reading, 832, 0, 50, &none, &has, &collected, &error),
- &error, 926, 85);
+ store_collect_tests_ok(store_collect_collected(io, &repo, &reading, 832, 0, 50, CHUNKS_PACKS_DEFAULT_SPACE, &none,
+ &has, &collected, &error), &error, 926, 85);
if (!store_collect_tests_option_eq(has, &collected, false, NULL))
{
StdOptionRef left = {has, STD_ITEM(&collected, store_collect_Collected_Debug_fmt)};
diff --git a/src/doltgres/test/meson.build b/src/doltgres/test/meson.build
index 22c508c2ab66d..22833df2c5362 100644
--- a/src/doltgres/test/meson.build
+++ b/src/doltgres/test/meson.build
@@ -135,6 +135,14 @@ tests += {
] : []) + [
't/123_cport_fe_pg.pl',
't/120_cport_access.pl',
+ 't/071_spill_reads.pl',
+ 't/131_scan_costs.pl',
+ 't/132_heap_only_contrib.pl',
+ 't/072_standby_spills.pl',
+ 't/124_carry_unlisted.pl',
+ 't/135_moved_takes.pl',
+ 't/133_identity_spill.pl',
+ 't/134_saves.pl',
],
'deps': [doltgres_cport_test_fe_pg],
# The injection points are cluster-wide, so disable installcheck
diff --git a/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_chunks.c b/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_chunks.c
index 489c006381360..b76a2c0003891 100644
--- a/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_chunks.c
+++ b/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_chunks.c
@@ -227,6 +227,8 @@ static const CportTest CPORT_TESTS[] = {
test_chunks_packs_lists_tests_a_pack_added_is_listed_by_every_view},
{"packs::lists::tests::a_refresh_across_a_whole_write_lists_the_packs_before_it_or_after_it",
test_chunks_packs_lists_tests_a_refresh_across_a_whole_write_lists_the_packs_before_it_or_after_it},
+ {"packs::lists::tests::a_removed_tablespace_reads_a_runs_file_made_again_from_its_start",
+ test_chunks_packs_lists_tests_a_removed_tablespace_reads_a_runs_file_made_again_from_its_start},
{"packs::lists::tests::a_retirements_records_lead_every_view_to_the_copies",
test_chunks_packs_lists_tests_a_retirements_records_lead_every_view_to_the_copies},
{"packs::lists::tests::a_whole_write_that_fails_before_its_renames_leaves_the_count_even",
@@ -408,6 +410,8 @@ static const CportTest CPORT_TESTS[] = {
{"trailer::tests::internal_nodes_store_their_subtrees_highest_tuple_ids",
test_chunks_trailer_tests_internal_nodes_store_their_subtrees_highest_tuple_ids},
{"trailer::tests::round_trips", test_chunks_trailer_tests_round_trips},
+ {"trailer::tests::stamp_of_finds_each_copys_stamp_among_its_runs",
+ test_chunks_trailer_tests_stamp_of_finds_each_copys_stamp_among_its_runs},
{"trailer::tests::the_same_stamps_store_the_same_bytes_however_they_were_pushed",
test_chunks_trailer_tests_the_same_stamps_store_the_same_bytes_however_they_were_pushed},
{"trailer::tests::xxh3_digests_are_the_committed_table",
diff --git a/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_doltgres.c b/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_doltgres.c
index aef06e33606fb..7790c079731cb 100644
--- a/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_doltgres.c
+++ b/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_doltgres.c
@@ -118,8 +118,12 @@ static const CportTest CPORT_TESTS[] = {
test_doltgres_scan_tests_a_parallel_tid_ranges_blocks_are_heaps},
{"scan::tests::a_sample_of_many_blocks_walks_the_delta_once",
test_doltgres_scan_tests_a_sample_of_many_blocks_walks_the_delta_once},
+ {"scan::tests::a_scan_held_inside_its_delta_read_across_an_install_and_drop_gives_every_row_once",
+ test_doltgres_scan_tests_a_scan_held_inside_its_delta_read_across_an_install_and_drop_gives_every_row_once},
{"scan::tests::a_scan_merges_the_deltas_rows_with_the_bases_in_key_order",
test_doltgres_scan_tests_a_scan_merges_the_deltas_rows_with_the_bases_in_key_order},
+ {"scan::tests::a_scan_moves_to_a_newer_base_at_its_next_read_and_gives_every_row_once",
+ test_doltgres_scan_tests_a_scan_moves_to_a_newer_base_at_its_next_read_and_gives_every_row_once},
{"scan::tests::a_scan_reads_a_bases_rows_in_order_across_calls",
test_doltgres_scan_tests_a_scan_reads_a_bases_rows_in_order_across_calls},
{"scan::tests::a_scan_reads_backward_and_turns_as_heaps_does",
@@ -136,6 +140,8 @@ static const CportTest CPORT_TESTS[] = {
test_doltgres_scan_tests_a_tuple_id_is_valid_within_the_storages_blocks},
{"scan::tests::each_keyless_copy_fetches_by_its_own_tuple_id",
test_doltgres_scan_tests_each_keyless_copy_fetches_by_its_own_tuple_id},
+ {"scan::tests::fetches_through_one_memo_give_each_keyless_copys_own_stamp",
+ test_doltgres_scan_tests_fetches_through_one_memo_give_each_keyless_copys_own_stamp},
{"scan::tests::scans_of_one_base_keep_their_own_places",
test_doltgres_scan_tests_scans_of_one_base_keep_their_own_places},
{"sizes::tests::a_tables_blocks_are_its_rows_packed_and_at_least_its_ctid_blocks",
diff --git a/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_host.c b/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_host.c
index 121f8eac45146..24fbd5b436a23 100644
--- a/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_host.c
+++ b/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_host.c
@@ -82,6 +82,10 @@ static const CportTest CPORT_TESTS[] = {
test_std_vec_tests_an_over_aligned_element_stays_aligned},
{"delta::blocks::tests::a_body_takes_the_smallest_class_that_holds_it",
test_host_delta_blocks_tests_a_body_takes_the_smallest_class_that_holds_it},
+ {"delta::directory::tests::a_free_pass_loads_only_the_retirements",
+ test_host_delta_directory_tests_a_free_pass_loads_only_the_retirements},
+ {"delta::directory::tests::a_refused_allocations_pass_loads_only_the_retirements",
+ test_host_delta_directory_tests_a_refused_allocations_pass_loads_only_the_retirements},
{"delta::directory::tests::a_size_fits_its_word",
test_host_delta_directory_tests_a_size_fits_its_word},
{"delta::directory::tests::a_words_index_is_below_its_pages_count",
diff --git a/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_simc.c b/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_simc.c
index ec362f61e02e1..18b1876c17c57 100644
--- a/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_simc.c
+++ b/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_simc.c
@@ -2533,6 +2533,68 @@ test_simc_tests_a_scripted_run_runs_each_actor_until_its_point_and_terminates_on
simc_TestIo_drop(&test_io);
}
+/* test_simc_tests_trace_Debug_fmt is the trace case's Vec Debug. */
+static bool
+test_simc_tests_trace_Debug_fmt(const void *value, StdFormatter *f)
+{
+ const StdVec *trace = value;
+ StdDebugList list = std_fmt_debug_list(f);
+
+ for (size_t k = 0; k < trace->len; k++)
+ std_DebugList_entry(&list, &STD_VEC_AT(trace, size_t, k), std_usize_Debug_fmt);
+ return std_DebugList_finish(&list);
+}
+
+/*
+ * test_simc_tests_a_reports_trace_is_the_rusts:
+ * a finished run's trace is its report's, each yield point's actor in order
+ * as the Rust's Debug of the report shows it, the first actor's pause first.
+ */
+static void
+test_simc_tests_a_reports_trace_is_the_rusts(void)
+{
+ static const StdFmtPiece report_pieces[] = {STD_FMT_ARG(0)};
+ static const StdFmtPiece trace_pieces[] = {STD_FMT_LIT("trace: "), STD_FMT_ARG(0), STD_FMT_LIT(", exits")};
+ SimcTestIo test_io = simc_io();
+ SimcRun run = simc_Run_new(13);
+ SimcActor first = {test_simc_tests_pausing_actor, (void *) (intptr_t) 1};
+ SimcActor second = {test_simc_tests_pausing_actor, (void *) (intptr_t) 2};
+ SimcStep steps[] = {{"second", 6, PG_UINT64_MAX}, {"first", 5, PG_UINT64_MAX}};
+ SimcReport report;
+ StdVec trace;
+ StdString text;
+ StdString shown;
+ StdFmtArg report_args[1];
+ StdFmtArg trace_args[1];
+ StdFmtArguments report_formatted = {report_pieces, lengthof(report_pieces), report_args};
+ StdFmtArguments trace_formatted = {trace_pieces, lengthof(trace_pieces), trace_args};
+ bool found = false;
+
+ test_simc_tests_FINISHED = 0;
+ simc_Run_actor(&run, "first", 5, first);
+ simc_Run_actor(&run, "second", 6, second);
+ simc_Run_until(&run, "first", 5, "doltgres-simc-case-first");
+ simc_Run_script(&run, steps, lengthof(steps));
+ report = simc_Run_finish(run);
+ trace = simc_Report_trace(&report);
+ report_args[0].value = &report;
+ report_args[0].fmt = simc_Report_Debug_fmt;
+ std_fmt_format_Checked(&text, &report_formatted);
+ trace_args[0].value = &trace;
+ trace_args[0].fmt = test_simc_tests_trace_Debug_fmt;
+ std_fmt_format_Checked(&shown, &trace_formatted);
+ for (size_t at = 0; !found && at + shown.len <= text.len; at++)
+ found = memcmp((const uint8 *) text.ptr + at, shown.ptr, shown.len) == 0;
+ STD_ASSERT(__LINE__, 5, trace.len >= 2 && STD_VEC_AT(&trace, size_t, 0) == 0,
+ "the first actor's pause is the trace's first yield");
+ STD_ASSERT(__LINE__, 5, found, "the trace is the report's");
+ std_vec_free_Checked(&shown, 1, 1);
+ std_vec_free_Checked(&text, 1, 1);
+ std_vec_free_Checked(&trace, sizeof(size_t), _Alignof(size_t));
+ simc_Report_drop(&report);
+ simc_TestIo_drop(&test_io);
+}
+
/* test_simc_tests_acting is the acting case's body: it records that it ran. */
static void
test_simc_tests_acting(void *captures)
@@ -3307,6 +3369,211 @@ test_simc_tests_the_checkpoint_delay_doors_answer_as_the_simulations_host(void)
simc_TestIo_drop(&test_io);
}
+/* test_simc_tests_horizon is the horizon case's actor: a NonVacuumable snapshot's removable xids. */
+static void
+test_simc_tests_horizon(void *captures)
+{
+ HostHost host = host_current();
+ HostMayRaise raise = host_doors(HOST_IO_IO_READ_DOORS((HostIoIo *) captures));
+ HostXid horizon = {100};
+ HostXid below = {99};
+ SimcTaken taken = simc_non_vacuumable(horizon);
+ HostSnapshot snapshot = simc_Taken_lent(&taken);
+ HostError error;
+ bool removable;
+
+ STD_ASSERT(__LINE__, 5, snapshot.kind == HOST_SNAPSHOT_KIND_NON_VACUUMABLE, "the snapshot is NonVacuumable");
+ STD_ASSERT(__LINE__, 5, host.ops->removable_in(host.self, &raise, snapshot.of, below, &removable, &error) &&
+ removable, "an xid below the horizon is removable");
+ STD_ASSERT(__LINE__, 5, host.ops->removable_in(host.self, &raise, snapshot.of, horizon, &removable, &error) &&
+ !removable, "the horizon's own xid isn't");
+ simc_Taken_drop(&taken);
+}
+
+/*
+ * test_simc_tests_a_non_vacuumable_snapshot_finds_xids_below_its_horizon_removable:
+ * a NonVacuumable snapshot is of its kind, and through the removable_in door
+ * it finds an xid below its horizon removable and the horizon's own not.
+ */
+static void
+test_simc_tests_a_non_vacuumable_snapshot_finds_xids_below_its_horizon_removable(void)
+{
+ SimcTestIo test_io = simc_io();
+
+ simc_as_actor(55, test_simc_tests_horizon, &test_io.io);
+ simc_TestIo_drop(&test_io);
+}
+
+/* test_simc_tests_logging is the logged case's actor: it inserts a record and reads it back as the WAL took it. */
+static void
+test_simc_tests_logging(void *captures)
+{
+ static const uint8 body[] = {1, 2, 3};
+ HostHost host = host_current();
+ HostMayRaise raise = host_doors(HOST_IO_IO_READ_DOORS((HostIoIo *) captures));
+ HostError error;
+ HostLsn end;
+ StdVec logged;
+ const simc_TupleU8VecU8Lsn_t *last;
+
+ STD_ASSERT(__LINE__, 5, host.ops->wal_insert(host.self, &raise, HOST_WAL_KIND_CHUNK, body, sizeof(body), &end,
+ &error), "the record is inserted");
+ logged = simc_logged();
+ STD_ASSERT(__LINE__, 5, logged.len > 0, "the WAL took a record");
+ last = &STD_VEC_AT(&logged, simc_TupleU8VecU8Lsn_t, logged.len - 1);
+ STD_ASSERT(__LINE__, 5, last->f0 == host_WalKind_value(HOST_WAL_KIND_CHUNK) && last->f1.len == sizeof(body) &&
+ memcmp(last->f1.ptr, body, sizeof(body)) == 0 && last->f2 == end,
+ "the last record logged is the record, its kind, and its end");
+ for (size_t i = 0; i < logged.len; i++)
+ std_vec_free_Checked(&STD_VEC_AT(&logged, simc_TupleU8VecU8Lsn_t, i).f1, 1, 1);
+ std_vec_free_Checked(&logged, sizeof(simc_TupleU8VecU8Lsn_t), _Alignof(simc_TupleU8VecU8Lsn_t));
+}
+
+/*
+ * test_simc_tests_the_logged_records_are_the_records_the_wal_took: the
+ * records simc lists are those the WAL door took, each with its kind, its
+ * body, and its end.
+ */
+static void
+test_simc_tests_the_logged_records_are_the_records_the_wal_took(void)
+{
+ SimcTestIo test_io = simc_io();
+
+ simc_as_actor(56, test_simc_tests_logging, &test_io.io);
+ simc_TestIo_drop(&test_io);
+}
+
+/*
+ * test_simc_tests_a_delta_page_takes_one_of_the_directorys_free_slots: a page
+ * the delta takes holds one of its directory's free slots until it's given
+ * back.
+ */
+static void
+test_simc_tests_a_delta_page_takes_one_of_the_directorys_free_slots(void)
+{
+ SimcTestIo test_io = simc_io();
+ HostHost host = host_current();
+ HostMayRaise raise = host_doors(HOST_IO_IO_READ_DOORS(&test_io.io));
+ HostDeltaPageSize size = {.bytes = 512, .words = 4};
+ HostError error;
+ StdVec names;
+ bool refused;
+ uint64 frees;
+ size_t before = simc_free_slots();
+
+ std_vec_with_capacity_Checked(&names, 1, sizeof(HostDeltaPagePage), _Alignof(HostDeltaPagePage));
+ STD_ASSERT(__LINE__, 5, host.ops->delta_alloc(host.self, &raise, &size, 1, &names, &refused, &frees, &error) &&
+ !refused, "the page is taken");
+ STD_ASSERT(__LINE__, 5, simc_free_slots() == before - 1, "the page holds one free slot");
+ host.ops->delta_give_back(host.self, STD_VEC_AT(&names, HostDeltaPagePage, 0));
+ std_vec_free_Checked(&names, sizeof(HostDeltaPagePage), _Alignof(HostDeltaPagePage));
+ host.ops->delta_pass(host.self);
+ simc_TestIo_drop(&test_io);
+}
+
+/* test_simc_tests_new_ciding is the new cid case's actor: it logs a row's command ids and reads the record back. */
+static void
+test_simc_tests_new_ciding(void *captures)
+{
+ HostHost host = host_current();
+ HostMayRaise raise = host_doors(HOST_IO_IO_READ_DOORS((HostIoIo *) captures));
+ HostStorage storage = {HOST_PG_DEFAULT, 9301, 16384};
+ HostRow row = {UINT64CONST(1) << 16 | 2};
+ HostNewCid cid = {.has_cmin = true, .cmin = {3}, .has_combo = true, .combo = {5}};
+ HostError error;
+ HostLsn end;
+ StdVec cids;
+ const simc_TupleStorageRowNewCidLsn_t *last;
+
+ STD_ASSERT(__LINE__, 5, host.ops->wal_new_cid(host.self, &raise, &storage, row, cid, &end, &error),
+ "the new cid record is logged");
+ cids = simc_new_cids();
+ STD_ASSERT(__LINE__, 5, cids.len > 0, "the WAL took a new cid record");
+ last = &STD_VEC_AT(&cids, simc_TupleStorageRowNewCidLsn_t, cids.len - 1);
+ STD_ASSERT(__LINE__, 5, last->f0.database == 9301 && last->f0.relation == 16384 && last->f1.f0 == row.f0 &&
+ last->f2.has_cmin && last->f2.cmin.f0 == 3 && !last->f2.has_cmax && last->f2.has_combo &&
+ last->f2.combo.f0 == 5 && last->f3 == end, "the last record is the row's, its cids, and its end");
+ std_vec_free_Checked(&cids, sizeof(simc_TupleStorageRowNewCidLsn_t), _Alignof(simc_TupleStorageRowNewCidLsn_t));
+}
+
+/*
+ * test_simc_tests_the_new_cid_door_logs_its_record_as_the_simulations_host:
+ * the wal_new_cid door logs a row's command ids with its storage and row,
+ * which simc lists with the record's end.
+ */
+static void
+test_simc_tests_the_new_cid_door_logs_its_record_as_the_simulations_host(void)
+{
+ SimcTestIo test_io = simc_io();
+
+ simc_as_actor(57, test_simc_tests_new_ciding, &test_io.io);
+ simc_TestIo_drop(&test_io);
+}
+
+/*
+ * test_simc_tests_the_wal_flush_door_answers_as_the_simulations_host: a
+ * flush of the WAL up to a record's end answers, as the simulation's host
+ * flushes it.
+ */
+static void
+test_simc_tests_the_wal_flush_door_answers_as_the_simulations_host(void)
+{
+ static const uint8 body[] = {1, 2, 3};
+ SimcTestIo test_io = simc_io();
+ HostHost host = host_current();
+ HostMayRaise raise = host_doors(HOST_IO_IO_READ_DOORS(&test_io.io));
+ HostError error;
+ HostLsn end;
+
+ STD_ASSERT(__LINE__, 5, host.ops->wal_insert(host.self, &raise, HOST_WAL_KIND_CHUNK, body, sizeof(body), &end,
+ &error), "the record is inserted");
+ STD_ASSERT(__LINE__, 5, host.ops->wal_flush(host.self, &raise, end, &error), "the WAL is flushed to its end");
+ simc_TestIo_drop(&test_io);
+}
+
+/*
+ * test_simc_tests_the_injected_door_answers_as_the_simulations_host: the
+ * simulation attaches no injection point, so the injected door answers false
+ * for a point's name.
+ */
+static void
+test_simc_tests_the_injected_door_answers_as_the_simulations_host(void)
+{
+ SimcTestIo test_io = simc_io();
+ HostHost host = host_current();
+ HostMayRaise raise = host_doors(HOST_IO_IO_READ_DOORS(&test_io.io));
+ HostError error;
+ bool injected = true;
+
+ STD_ASSERT(__LINE__, 5, host.ops->injected(host.self, &raise, "doltgres-vacuum-found", &injected, &error) &&
+ !injected, "the simulation attaches no injection point");
+ simc_TestIo_drop(&test_io);
+}
+
+/*
+ * test_simc_tests_a_hold_of_the_test_process_keeps_its_data_directory_past_its_capability:
+ * a hold of the test process taken before a test's capability keeps the
+ * test's data directory once the capability drops, and its release ends the
+ * test.
+ */
+static void
+test_simc_tests_a_hold_of_the_test_process_keeps_its_data_directory_past_its_capability(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io = simc_io();
+ StdPathBuf before = simc_data_dir();
+ StdPathBuf after;
+
+ simc_TestIo_drop(&test_io);
+ after = simc_data_dir();
+ STD_ASSERT(__LINE__, 5, before.bytes.len == after.bytes.len &&
+ memcmp(before.bytes.ptr, after.bytes.ptr, before.bytes.len) == 0,
+ "the data directory outlives the capability");
+ std_vec_free_Checked(&after.bytes, 1, 1);
+ std_vec_free_Checked(&before.bytes, 1, 1);
+ simc_Process_drop(&process);
+}
+
static const CportTest CPORT_TESTS[] = {
{"simc::tests::a_body_as_an_actor_runs_with_that_actors_id_in_the_data_directory",
test_simc_tests_a_body_as_an_actor_runs_with_that_actors_id_in_the_data_directory},
@@ -3322,6 +3589,8 @@ static const CportTest CPORT_TESTS[] = {
test_simc_tests_a_crash_fails_each_call_until_a_recovery_takes_the_files_back_to_their_syncs},
{"simc::tests::a_delta_page_is_taken_written_and_given_back_as_the_simulations_host",
test_simc_tests_a_delta_page_is_taken_written_and_given_back_as_the_simulations_host},
+ {"simc::tests::a_delta_page_takes_one_of_the_directorys_free_slots",
+ test_simc_tests_a_delta_page_takes_one_of_the_directorys_free_slots},
{"simc::tests::a_dictionary_simc_prepares_compresses_and_decompresses_and_drops_in_the_c",
test_simc_tests_a_dictionary_simc_prepares_compresses_and_decompresses_and_drops_in_the_c},
{"simc::tests::a_failed_allocation_of_a_listings_names_frees_the_names_made_and_raises_out_of_memory",
@@ -3336,10 +3605,14 @@ static const CportTest CPORT_TESTS[] = {
test_simc_tests_a_hinted_c_error_crosses_to_sim_as_its_literals},
{"simc::tests::a_historic_snapshot_lends_its_bounds_command_and_sorted_xids",
test_simc_tests_a_historic_snapshot_lends_its_bounds_command_and_sorted_xids},
+ {"simc::tests::a_hold_of_the_test_process_keeps_its_data_directory_past_its_capability",
+ test_simc_tests_a_hold_of_the_test_process_keeps_its_data_directory_past_its_capability},
{"simc::tests::a_linked_delta_page_opens_reads_appends_seals_and_retires_as_the_simulations_host",
test_simc_tests_a_linked_delta_page_opens_reads_appends_seals_and_retires_as_the_simulations_host},
{"simc::tests::a_nap_is_counted_and_the_disk_space_set_is_answered",
test_simc_tests_a_nap_is_counted_and_the_disk_space_set_is_answered},
+ {"simc::tests::a_non_vacuumable_snapshot_finds_xids_below_its_horizon_removable",
+ test_simc_tests_a_non_vacuumable_snapshot_finds_xids_below_its_horizon_removable},
{"simc::tests::a_pause_a_warning_a_log_and_the_last_redo_answer_as_the_simulations_host",
test_simc_tests_a_pause_a_warning_a_log_and_the_last_redo_answer_as_the_simulations_host},
{"simc::tests::a_pooled_records_doors_answer_as_the_simulations_host",
@@ -3356,6 +3629,8 @@ static const CportTest CPORT_TESTS[] = {
test_simc_tests_a_refused_take_fails_once_and_refuse_nothing_takes_back_the_rest},
{"simc::tests::a_reports_debug_is_the_rusts",
test_simc_tests_a_reports_debug_is_the_rusts},
+ {"simc::tests::a_reports_trace_is_the_rusts",
+ test_simc_tests_a_reports_trace_is_the_rusts},
{"simc::tests::a_returning_actor_ends_ok_and_its_endings_compare_as_the_rusts",
test_simc_tests_a_returning_actor_ends_ok_and_its_endings_compare_as_the_rusts},
{"simc::tests::a_run_check_fails_the_run_at_a_yield_with_its_message",
@@ -3426,8 +3701,14 @@ static const CportTest CPORT_TESTS[] = {
test_simc_tests_the_delta_hold_and_budget_doors_answer_as_the_simulations_host},
{"simc::tests::the_epoch_doors_answer_as_the_simulations_host",
test_simc_tests_the_epoch_doors_answer_as_the_simulations_host},
+ {"simc::tests::the_injected_door_answers_as_the_simulations_host",
+ test_simc_tests_the_injected_door_answers_as_the_simulations_host},
{"simc::tests::the_key_doors_answer_as_the_simulations_host",
test_simc_tests_the_key_doors_answer_as_the_simulations_host},
+ {"simc::tests::the_logged_records_are_the_records_the_wal_took",
+ test_simc_tests_the_logged_records_are_the_records_the_wal_took},
+ {"simc::tests::the_new_cid_door_logs_its_record_as_the_simulations_host",
+ test_simc_tests_the_new_cid_door_logs_its_record_as_the_simulations_host},
{"simc::tests::the_next_decompression_runs_out_of_memory_when_asked",
test_simc_tests_the_next_decompression_runs_out_of_memory_when_asked},
{"simc::tests::the_node_cache_installs_finds_and_unpins_a_nodes_frame",
@@ -3444,6 +3725,8 @@ static const CportTest CPORT_TESTS[] = {
test_simc_tests_the_test_only_std_fs_writes_reads_appends_and_removes_a_file},
{"simc::tests::the_wal_a_checkpoint_and_a_databases_quiet_answer_as_the_simulations_host",
test_simc_tests_the_wal_a_checkpoint_and_a_databases_quiet_answer_as_the_simulations_host},
+ {"simc::tests::the_wal_flush_door_answers_as_the_simulations_host",
+ test_simc_tests_the_wal_flush_door_answers_as_the_simulations_host},
};
PG_FUNCTION_INFO_V1(doltgres_cport_test_simc);
diff --git a/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_txn.c b/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_txn.c
index 1079d9d6ab9d6..4dd6268d6effd 100644
--- a/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_txn.c
+++ b/src/doltgres/test/modules/doltgres_cport_test/doltgres_cport_test_txn.c
@@ -25,17 +25,25 @@
#include "fmgr.h"
#include "doltgres/txn/base.h"
+#include "doltgres/txn/blobs.h"
+#include "doltgres/txn/change.h"
#include "doltgres/txn/counted.h"
+#include "doltgres/txn/entry.h"
#include "doltgres/txn/free_space.h"
+#include "doltgres/txn/identity.h"
#include "doltgres/txn/memtable/branch.h"
#include "doltgres/txn/memtable/index.h"
#include "doltgres/txn/memtable/map.h"
#include "doltgres/txn/memtable/table.h"
#include "doltgres/txn/memtable/tuples.h"
+#include "doltgres/txn/outcome.h"
+#include "doltgres/txn/record.h"
#include "doltgres/txn/runs.h"
#include "doltgres/txn/spill.h"
#include "doltgres/txn/state.h"
#include "doltgres/txn/status.h"
+#include "doltgres/txn/vacuum.h"
+#include "doltgres/txn/values.h"
#include "doltgres/txn/visible.h"
#include "doltgres_cport_test.h"
@@ -57,6 +65,24 @@ static const CportTest CPORT_TESTS[] = {
test_txn_base_tests_planned_counts_follow_the_sizes_alone},
{"base::tests::planned_counts_of_entries_larger_than_a_node_are_those_of_the_tree_the_splitter_makes",
test_txn_base_tests_planned_counts_of_entries_larger_than_a_node_are_those_of_the_tree_the_splitter_makes},
+ {"blobs::tests::a_blob_in_the_delta_reads_back_through_blobs",
+ test_txn_blobs_tests_a_blob_in_the_delta_reads_back_through_blobs},
+ {"blobs::tests::a_chunk_another_running_transaction_holds_stays_after_that_ones_abort",
+ test_txn_blobs_tests_a_chunk_another_running_transaction_holds_stays_after_that_ones_abort},
+ {"blobs::tests::a_logged_write_logs_a_chunk_an_unlogged_one_left_and_redo_makes_it_durable",
+ test_txn_blobs_tests_a_logged_write_logs_a_chunk_an_unlogged_one_left_and_redo_makes_it_durable},
+ {"blobs::tests::a_rolled_back_chunk_takes_nothing_from_its_tops_count",
+ test_txn_blobs_tests_a_rolled_back_chunk_takes_nothing_from_its_tops_count},
+ {"blobs::tests::a_transactions_chunks_spill_read_back_and_leave_at_its_abort",
+ test_txn_blobs_tests_a_transactions_chunks_spill_read_back_and_leave_at_its_abort},
+ {"change::tests::a_change_of_a_spilled_row_reads_its_spilled_version",
+ test_txn_change_tests_a_change_of_a_spilled_row_reads_its_spilled_version},
+ {"change::tests::a_delete_is_logged_pushed_and_redone_once_and_a_lock_is_never_logged",
+ test_txn_change_tests_a_delete_is_logged_pushed_and_redone_once_and_a_lock_is_never_logged},
+ {"change::tests::a_keys_stripe_is_its_storage_and_bytes_or_its_storage_alone",
+ test_txn_change_tests_a_keys_stripe_is_its_storage_and_bytes_or_its_storage_alone},
+ {"change::tests::an_aborted_delete_and_lock_go_at_the_take_back",
+ test_txn_change_tests_an_aborted_delete_and_lock_go_at_the_take_back},
{"cport::txn::free_space::tests::an_entry_past_the_bitmap_panics_at_encode",
test_txn_free_space_tests_an_entry_past_the_bitmap_panics_at_encode, true,
"range end index 250 out of range for slice of length 147"},
@@ -76,12 +102,102 @@ static const CportTest CPORT_TESTS[] = {
test_txn_visible_tests_a_running_writers_version_leaves_its_ran_answer_as_it_was},
{"counted::tests::a_keyless_value_is_its_count_and_its_tuple",
test_txn_counted_tests_a_keyless_value_is_its_count_and_its_tuple},
+ {"entry::tests::a_counted_unique_entry_writes_nothing_for_its_own_copy_and_answers_another",
+ test_txn_entry_tests_a_counted_unique_entry_writes_nothing_for_its_own_copy_and_answers_another},
+ {"entry::tests::a_partial_unique_check_answers_a_running_write_without_waiting",
+ test_txn_entry_tests_a_partial_unique_check_answers_a_running_write_without_waiting},
+ {"entry::tests::a_unique_entry_takes_a_free_key_and_answers_an_existing_entrys_value",
+ test_txn_entry_tests_a_unique_entry_takes_a_free_key_and_answers_an_existing_entrys_value},
+ {"entry::tests::an_entry_and_its_record_change_together",
+ test_txn_entry_tests_an_entry_and_its_record_change_together},
+ {"entry::tests::an_index_base_entry_reads_back_and_refuses_a_keyed_stamp_and_an_uneven_count",
+ test_txn_entry_tests_an_index_base_entry_reads_back_and_refuses_a_keyed_stamp_and_an_uneven_count},
+ {"entry::tests::an_index_entrys_changes_are_logged_pushed_and_redone_once",
+ test_txn_entry_tests_an_index_entrys_changes_are_logged_pushed_and_redone_once},
+ {"entry::tests::an_index_storages_large_keys_spill_at_the_threshold",
+ test_txn_entry_tests_an_index_storages_large_keys_spill_at_the_threshold},
{"free_space::tests::a_free_space_entry_reads_back_and_refuses_every_other_form",
test_txn_free_space_tests_a_free_space_entry_reads_back_and_refuses_every_other_form},
{"free_space::tests::a_seek_finds_the_lowest_block_with_room_through_the_summaries",
test_txn_free_space_tests_a_seek_finds_the_lowest_block_with_room_through_the_summaries},
{"free_space::tests::an_insert_takes_the_lowest_free_offset_and_a_left_fresh_page_frees_its_counter_offsets",
test_txn_free_space_tests_an_insert_takes_the_lowest_free_offset_and_a_left_fresh_page_frees_its_counter_offsets},
+ {"identity::tests::a_columns_id_holds_its_types_category",
+ test_txn_identity_tests_a_columns_id_holds_its_types_category},
+ {"identity::tests::a_pairs_keys_find_it_from_either_side",
+ test_txn_identity_tests_a_pairs_keys_find_it_from_either_side},
+ {"identity::tests::a_saves_drop_takes_its_folded_pairs_out",
+ test_txn_identity_tests_a_saves_drop_takes_its_folded_pairs_out},
+ {"identity::tests::a_saves_fold_keeps_its_committed_pairs_and_leaves_running_ones",
+ test_txn_identity_tests_a_saves_fold_keeps_its_committed_pairs_and_leaves_running_ones},
+ {"identity::tests::a_tables_pairs_read_back_by_its_oid_and_its_id",
+ test_txn_identity_tests_a_tables_pairs_read_back_by_its_oid_and_its_id},
+ {"identity::tests::a_taken_id_moves_on_and_a_removed_pair_frees_it",
+ test_txn_identity_tests_a_taken_id_moves_on_and_a_removed_pair_frees_it},
+ {"identity::tests::an_aborted_transactions_pairs_go_at_the_take_back",
+ test_txn_identity_tests_an_aborted_transactions_pairs_go_at_the_take_back},
+ {"identity::tests::an_identity_change_reads_back_as_written",
+ test_txn_identity_tests_an_identity_change_reads_back_as_written},
+ {"identity::tests::an_objects_id_derives_from_its_kind_parent_and_name",
+ test_txn_identity_tests_an_objects_id_derives_from_its_kind_parent_and_name},
+ {"identity::tests::redone_identity_changes_give_the_same_pairs_once",
+ test_txn_identity_tests_redone_identity_changes_give_the_same_pairs_once},
+ {"insert::tests::a_checkpoint_before_or_after_an_update_leaves_its_kept_counts_once",
+ test_txn_insert_tests_a_checkpoint_before_or_after_an_update_leaves_its_kept_counts_once},
+ {"insert::tests::a_checkpoint_between_an_updates_count_adds_and_its_row_leaves_the_counts_once",
+ test_txn_insert_tests_a_checkpoint_between_an_updates_count_adds_and_its_row_leaves_the_counts_once},
+ {"insert::tests::a_checkpoints_runs_and_every_record_after_redo_as_the_primary",
+ test_txn_insert_tests_a_checkpoints_runs_and_every_record_after_redo_as_the_primary},
+ {"insert::tests::a_crash_after_a_key_change_whose_runs_hold_one_chain_redoes_both",
+ test_txn_insert_tests_a_crash_after_a_key_change_whose_runs_hold_one_chain_redoes_both},
+ {"insert::tests::a_frozen_inserts_row_change_replays_with_no_status_room",
+ test_txn_insert_tests_a_frozen_inserts_row_change_replays_with_no_status_room},
+ {"insert::tests::a_gathering_from_an_inserts_prepare_to_its_push_keeps_the_counter",
+ test_txn_insert_tests_a_gathering_from_an_inserts_prepare_to_its_push_keeps_the_counter},
+ {"insert::tests::a_kept_values_count_add_is_redone_once",
+ test_txn_insert_tests_a_kept_values_count_add_is_redone_once},
+ {"insert::tests::a_keyless_or_deferrable_update_names_its_rows_by_their_tuple_ids",
+ test_txn_insert_tests_a_keyless_or_deferrable_update_names_its_rows_by_their_tuple_ids},
+ {"insert::tests::a_load_moves_the_counter_past_every_loaded_tid_and_left_page",
+ test_txn_insert_tests_a_load_moves_the_counter_past_every_loaded_tid_and_left_page},
+ {"insert::tests::a_resolve_whose_first_target_is_refused_ends_in_bounded_turns",
+ test_txn_insert_tests_a_resolve_whose_first_target_is_refused_ends_in_bounded_turns},
+ {"insert::tests::a_resolve_whose_stripe_moves_three_times_holds_its_writers_back",
+ test_txn_insert_tests_a_resolve_whose_stripe_moves_three_times_holds_its_writers_back},
+ {"insert::tests::a_row_the_last_page_cant_hold_goes_to_the_lowest_page_left_with_room",
+ test_txn_insert_tests_a_row_the_last_page_cant_hold_goes_to_the_lowest_page_left_with_room},
+ {"insert::tests::a_row_whose_value_references_sit_in_an_overflow_chunk_redoes_its_map_entries",
+ test_txn_insert_tests_a_row_whose_value_references_sit_in_an_overflow_chunk_redoes_its_map_entries},
+ {"insert::tests::a_speculative_insert_holds_its_token_until_confirmed",
+ test_txn_insert_tests_a_speculative_insert_holds_its_token_until_confirmed},
+ {"insert::tests::a_take_redone_again_changes_nothing",
+ test_txn_insert_tests_a_take_redone_again_changes_nothing},
+ {"insert::tests::a_window_between_a_sweeps_rows_and_its_cut_leaves_its_take_without_its_row",
+ test_txn_insert_tests_a_window_between_a_sweeps_rows_and_its_cut_leaves_its_take_without_its_row},
+ {"insert::tests::an_aborted_inserts_value_map_versions_go_at_its_take_back_and_its_free_space_take_stays",
+ test_txn_insert_tests_an_aborted_inserts_value_map_versions_go_at_its_take_back_and_its_free_space_take_stays},
+ {"insert::tests::an_aborted_updates_new_row_and_end_go_at_the_take_back",
+ test_txn_insert_tests_an_aborted_updates_new_row_and_end_go_at_the_take_back},
+ {"insert::tests::an_insert_logical_decoding_reads_logs_heaps_record_of_its_command",
+ test_txn_insert_tests_an_insert_logical_decoding_reads_logs_heaps_record_of_its_command},
+ {"insert::tests::an_insert_maps_its_out_of_line_values_in_its_window",
+ test_txn_insert_tests_an_insert_maps_its_out_of_line_values_in_its_window},
+ {"insert::tests::an_insert_that_fails_after_its_chunks_maps_no_value",
+ test_txn_insert_tests_an_insert_that_fails_after_its_chunks_maps_no_value},
+ {"insert::tests::an_insert_that_leaves_the_fresh_page_and_fails_redoes_as_the_primary",
+ test_txn_insert_tests_an_insert_that_leaves_the_fresh_page_and_fails_redoes_as_the_primary},
+ {"insert::tests::an_update_keeps_its_key_or_moves_it_and_redoes_as_the_primary",
+ test_txn_insert_tests_an_update_keeps_its_key_or_moves_it_and_redoes_as_the_primary},
+ {"insert::tests::an_updates_combo_command_shows_on_the_row_it_ends",
+ test_txn_insert_tests_an_updates_combo_command_shows_on_the_row_it_ends},
+ {"insert::tests::inserts_take_heaps_tuple_ids_by_offsets_and_by_bytes",
+ test_txn_insert_tests_inserts_take_heaps_tuple_ids_by_offsets_and_by_bytes},
+ {"insert::tests::redo_from_any_point_gives_a_row_with_all_its_map_entries_or_none",
+ test_txn_insert_tests_redo_from_any_point_gives_a_row_with_all_its_map_entries_or_none},
+ {"insert::tests::redo_leaves_the_free_space_map_and_the_counter_as_the_primarys",
+ test_txn_insert_tests_redo_leaves_the_free_space_map_and_the_counter_as_the_primarys},
+ {"insert::tests::windows_after_a_sweeps_cut_are_in_its_runs_with_neither_their_rows_nor_their_takes",
+ test_txn_insert_tests_windows_after_a_sweeps_cut_are_in_its_runs_with_neither_their_rows_nor_their_takes},
{"memtable::branch::tests::a_chunk_is_held_by_its_records_lsn",
test_txn_memtable_branch_tests_a_chunk_is_held_by_its_records_lsn},
{"memtable::branch::tests::a_chunk_reads_back_by_its_address_until_its_repositorys_drop",
@@ -114,6 +230,8 @@ static const CportTest CPORT_TESTS[] = {
test_txn_memtable_map_tests_a_failure_after_the_chunks_and_before_the_window_leaves_the_chunk_unreferenced},
{"memtable::map::tests::a_map_only_prepare_takes_its_entry_and_makes_no_rows_memtable",
test_txn_memtable_map_tests_a_map_only_prepare_takes_its_entry_and_makes_no_rows_memtable},
+ {"memtable::map::tests::a_take_whose_read_moved_answers_moved_and_pushes_nothing",
+ test_txn_memtable_map_tests_a_take_whose_read_moved_answers_moved_and_pushes_nothing},
{"memtable::map::tests::a_maps_seek_goes_on_past_a_ranges_end",
test_txn_memtable_map_tests_a_maps_seek_goes_on_past_a_ranges_end},
{"memtable::map::tests::a_seek_reads_on_after_a_writer_follows_its_read_of_an_empty_map",
@@ -192,6 +310,10 @@ static const CportTest CPORT_TESTS[] = {
test_txn_memtable_table_tests_a_reclaim_keeps_a_state_whose_maps_are_linked_and_marks_it_due},
{"memtable::table::tests::a_reclaim_keeps_a_state_with_a_memtable",
test_txn_memtable_table_tests_a_reclaim_keeps_a_state_with_a_memtable},
+ {"memtable::table::tests::a_refused_try_leaves_no_memtable_range_or_page_behind",
+ test_txn_memtable_table_tests_a_refused_try_leaves_no_memtable_range_or_page_behind},
+ {"memtable::table::tests::a_refused_try_of_two_trees_leaves_neither_memtable_behind",
+ test_txn_memtable_table_tests_a_refused_try_of_two_trees_leaves_neither_memtable_behind},
{"memtable::table::tests::a_removal_closes_an_emptied_range_and_an_emptied_memtable",
test_txn_memtable_table_tests_a_removal_closes_an_emptied_range_and_an_emptied_memtable},
{"memtable::table::tests::a_removal_counts_its_records_and_its_keys_bytes_dead",
@@ -264,24 +386,74 @@ static const CportTest CPORT_TESTS[] = {
test_txn_memtable_table_tests_two_first_writers_make_one_memtable},
{"memtable::table::tests::two_writers_that_find_a_closed_memtable_clear_it_once",
test_txn_memtable_table_tests_two_writers_that_find_a_closed_memtable_clear_it_once},
+ {"memtable::tuples::tests::a_compaction_failed_at_any_step_leaves_every_tid_found",
+ test_txn_memtable_tuples_tests_a_compaction_failed_at_any_step_leaves_every_tid_found},
+ {"memtable::tuples::tests::a_compaction_with_a_reservation_outstanding_moves_no_byte_page",
+ test_txn_memtable_tuples_tests_a_compaction_with_a_reservation_outstanding_moves_no_byte_page},
{"memtable::tuples::tests::a_reader_held_across_a_growth_reads_from_the_next_table",
test_txn_memtable_tuples_tests_a_reader_held_across_a_growth_reads_from_the_next_table},
{"memtable::tuples::tests::a_reservation_no_finish_stored_is_given_back",
test_txn_memtable_tuples_tests_a_reservation_no_finish_stored_is_given_back},
+ {"memtable::tuples::tests::a_table_of_mostly_dropped_tids_is_compacted_at_its_load_factor",
+ test_txn_memtable_tuples_tests_a_table_of_mostly_dropped_tids_is_compacted_at_its_load_factor},
{"memtable::tuples::tests::a_tables_first_pages_are_the_smallest_that_hold_them",
test_txn_memtable_tuples_tests_a_tables_first_pages_are_the_smallest_that_hold_them},
{"memtable::tuples::tests::a_tables_tids_are_found_after_inserts_and_a_growth",
test_txn_memtable_tuples_tests_a_tables_tids_are_found_after_inserts_and_a_growth},
{"memtable::tuples::tests::a_tid_used_again_names_its_newest_key",
test_txn_memtable_tuples_tests_a_tid_used_again_names_its_newest_key},
+ {"memtable::tuples::tests::a_writer_waits_on_a_compactions_exclusive_lock",
+ test_txn_memtable_tuples_tests_a_writer_waits_on_a_compactions_exclusive_lock},
{"memtable::tuples::tests::two_prepares_at_the_bound_each_get_a_slot_through_one_growth",
test_txn_memtable_tuples_tests_two_prepares_at_the_bound_each_get_a_slot_through_one_growth},
{"memtable::tuples::tests::two_tids_of_one_first_slot_are_each_found",
test_txn_memtable_tuples_tests_two_tids_of_one_first_slot_are_each_found},
+ {"memtable::version::tests::a_hint_and_a_token_clear_racing_both_survive",
+ test_txn_memtable_version_tests_a_hint_and_a_token_clear_racing_both_survive},
+ {"memtable::version::tests::a_keys_versions_read_newest_first_with_their_deleters",
+ test_txn_memtable_version_tests_a_keys_versions_read_newest_first_with_their_deleters},
+ {"memtable::version::tests::a_link_holds_its_offset_and_older_handle_at_their_limits",
+ test_txn_memtable_version_tests_a_link_holds_its_offset_and_older_handle_at_their_limits},
+ {"memtable::version::tests::a_shown_combo_is_the_rows_raw_command_and_no_token",
+ test_txn_memtable_version_tests_a_shown_combo_is_the_rows_raw_command_and_no_token},
+ {"memtable::version::tests::a_stored_record_is_refused_unless_encode_writes_it",
+ test_txn_memtable_version_tests_a_stored_record_is_refused_unless_encode_writes_it},
+ {"memtable::version::tests::a_version_on_a_fresh_page_is_named_by_its_index_in_the_list",
+ test_txn_memtable_version_tests_a_version_on_a_fresh_page_is_named_by_its_index_in_the_list},
+ {"memtable::version::tests::every_ops_record_reads_back_as_written",
+ test_txn_memtable_version_tests_every_ops_record_reads_back_as_written},
+ {"outcome::tests::a_writers_outcome_answers_frozen_with_no_door_and_caches_an_ended_writers",
+ test_txn_outcome_tests_a_writers_outcome_answers_frozen_with_no_door_and_caches_an_ended_writers},
+ {"record::tests::a_built_bases_node_reads_back_with_its_stamps",
+ test_txn_record_tests_a_built_bases_node_reads_back_with_its_stamps},
+ {"record::tests::a_built_nodes_key_names_its_base_and_its_fields_come_together",
+ test_txn_record_tests_a_built_nodes_key_names_its_base_and_its_fields_come_together},
+ {"record::tests::a_chunk_reads_back_and_refuses_every_other_form",
+ test_txn_record_tests_a_chunk_reads_back_and_refuses_every_other_form},
+ {"record::tests::a_folds_record_reads_back_and_refuses_every_other_form",
+ test_txn_record_tests_a_folds_record_reads_back_and_refuses_every_other_form},
+ {"record::tests::a_free_reads_back_and_refuses_runs_out_of_bounds_or_order",
+ test_txn_record_tests_a_free_reads_back_and_refuses_runs_out_of_bounds_or_order},
+ {"record::tests::a_frees_taken_down_values_read_back_and_refuse_every_other_form",
+ test_txn_record_tests_a_frees_taken_down_values_read_back_and_refuse_every_other_form},
+ {"record::tests::a_moves_carried_free_space_reads_back_and_refuses_every_other_form",
+ test_txn_record_tests_a_moves_carried_free_space_reads_back_and_refuses_every_other_form},
+ {"record::tests::a_page_leave_reads_back_and_refuses_every_other_form",
+ test_txn_record_tests_a_page_leave_reads_back_and_refuses_every_other_form},
+ {"record::tests::a_row_change_reads_back_and_refuses_every_other_form",
+ test_txn_record_tests_a_row_change_reads_back_and_refuses_every_other_form},
+ {"record::tests::an_index_entrys_change_reads_back_and_refuses_every_field_its_op_doesnt_carry",
+ test_txn_record_tests_an_index_entrys_change_reads_back_and_refuses_every_field_its_op_doesnt_carry},
+ {"record::tests::an_updates_kept_values_read_back_in_their_values_order",
+ test_txn_record_tests_an_updates_kept_values_read_back_in_their_values_order},
+ {"record::tests::stamps_on_a_blob_or_of_another_count_are_refused",
+ test_txn_record_tests_stamps_on_a_blob_or_of_another_count_are_refused},
{"runs::tests::a_checkpoint_names_a_running_spill_and_a_start_links_it_back",
test_txn_runs_tests_a_checkpoint_names_a_running_spill_and_a_start_links_it_back},
{"runs::tests::a_checkpoint_saves_its_runs_and_removes_the_unnamed",
test_txn_runs_tests_a_checkpoint_saves_its_runs_and_removes_the_unnamed},
+ {"runs::tests::a_checkpoints_identity_run_loads_its_pairs_back",
+ test_txn_runs_tests_a_checkpoints_identity_run_loads_its_pairs_back},
{"runs::tests::a_checkpoints_runs_hold_each_trees_versions",
test_txn_runs_tests_a_checkpoints_runs_hold_each_trees_versions},
{"runs::tests::a_checkpoints_runs_load_back_into_the_delta",
@@ -290,8 +462,16 @@ static const CportTest CPORT_TESTS[] = {
test_txn_runs_tests_a_checkpoints_runs_survive_a_power_loss},
{"runs::tests::a_checkpoints_status_run_gives_the_loaded_writers_their_outcomes",
test_txn_runs_tests_a_checkpoints_status_run_gives_the_loaded_writers_their_outcomes},
+ {"runs::tests::a_deferred_spill_named_again_waits_for_two_new_sightings",
+ test_txn_runs_tests_a_deferred_spill_named_again_waits_for_two_new_sightings},
+ {"runs::tests::a_helpers_part_linked_after_its_transactions_abort_leaves_with_its_files",
+ test_txn_runs_tests_a_helpers_part_linked_after_its_transactions_abort_leaves_with_its_files},
{"runs::tests::a_large_status_run_keeps_every_entry_through_its_load",
test_txn_runs_tests_a_large_status_run_keeps_every_entry_through_its_load},
+ {"runs::tests::a_pair_the_save_folds_is_left_out_of_the_identity_run",
+ test_txn_runs_tests_a_pair_the_save_folds_is_left_out_of_the_identity_run},
+ {"runs::tests::a_reader_that_listed_a_merges_source_reads_it_across_two_checkpoints",
+ test_txn_runs_tests_a_reader_that_listed_a_merges_source_reads_it_across_two_checkpoints},
{"runs::tests::a_recoverys_cleanup_drops_an_unlogged_storages_loaded_rows",
test_txn_runs_tests_a_recoverys_cleanup_drops_an_unlogged_storages_loaded_rows},
{"runs::tests::a_recoverys_cleanup_resets_an_unlogged_storages_saved_base",
@@ -322,6 +502,8 @@ static const CportTest CPORT_TESTS[] = {
test_txn_runs_tests_a_start_skips_a_repository_whose_run_is_damaged_and_loads_the_others},
{"runs::tests::a_start_warns_of_a_manifest_it_cant_read_and_loads_the_others_runs",
test_txn_runs_tests_a_start_warns_of_a_manifest_it_cant_read_and_loads_the_others_runs},
+ {"runs::tests::a_starts_loaded_identity_pairs_count_at_their_writers_entry",
+ test_txn_runs_tests_a_starts_loaded_identity_pairs_count_at_their_writers_entry},
{"runs::tests::a_status_run_reads_back_its_outcomes_and_refuses_what_isnt_one",
test_txn_runs_tests_a_status_run_reads_back_its_outcomes_and_refuses_what_isnt_one},
{"runs::tests::a_takes_ender_loads_back_from_its_free_space_run",
@@ -330,10 +512,24 @@ static const CportTest CPORT_TESTS[] = {
test_txn_runs_tests_an_entries_run_reads_its_entries_and_refuses_heap_bytes_tuple_ids_and_other_ops},
{"runs::tests::an_entries_runs_header_holds_its_table_and_index",
test_txn_runs_tests_an_entries_runs_header_holds_its_table_and_index},
+ {"runs::tests::an_identity_run_reads_its_pairs_and_refuses_heap_bytes_tuple_ids_and_other_ops",
+ test_txn_runs_tests_an_identity_run_reads_its_pairs_and_refuses_heap_bytes_tuple_ids_and_other_ops},
+ {"runs::tests::an_identity_spill_reads_its_pairs_and_refuses_another_writer_tuple_ids_and_heap_bytes",
+ test_txn_runs_tests_an_identity_spill_reads_its_pairs_and_refuses_another_writer_tuple_ids_and_heap_bytes},
+ {"runs::tests::an_index_storages_entries_spill_and_load_back_as_entries",
+ test_txn_runs_tests_an_index_storages_entries_spill_and_load_back_as_entries},
{"runs::tests::an_index_storages_loaded_entries_stay_an_entries_run_through_a_second_checkpoint",
test_txn_runs_tests_an_index_storages_loaded_entries_stay_an_entries_run_through_a_second_checkpoint},
+ {"spill::tests::a_carry_that_fails_at_any_step_lists_nothing_and_leaves_every_count",
+ test_txn_spill_tests_a_carry_that_fails_at_any_step_lists_nothing_and_leaves_every_count},
+ {"spill::tests::a_drop_counts_a_crashed_transactions_files_in_either_order",
+ test_txn_spill_tests_a_drop_counts_a_crashed_transactions_files_in_either_order},
{"spill::tests::a_filter_hits_its_keys_and_few_others",
test_txn_spill_tests_a_filter_hits_its_keys_and_few_others},
+ {"spill::tests::a_free_space_spill_a_crash_ended_is_marked_with_the_next_checkpoints_redo_point",
+ test_txn_spill_tests_a_free_space_spill_a_crash_ended_is_marked_with_the_next_checkpoints_redo_point},
+ {"spill::tests::a_free_space_spills_merge_leaves_the_rows_tuple_ids_alone",
+ test_txn_spill_tests_a_free_space_spills_merge_leaves_the_rows_tuple_ids_alone},
{"spill::tests::a_large_filter_takes_several_pages",
test_txn_spill_tests_a_large_filter_takes_several_pages},
{"spill::tests::a_lookup_reads_an_evicted_filter_page_again",
@@ -348,12 +544,16 @@ static const CportTest CPORT_TESTS[] = {
test_txn_spill_tests_a_one_range_tree_spills_at_a_full_budget},
{"spill::tests::a_pending_spill_is_found_by_its_storages_readers_until_it_settles",
test_txn_spill_tests_a_pending_spill_is_found_by_its_storages_readers_until_it_settles},
+ {"spill::tests::a_prepared_transactions_spills_are_never_taken_out",
+ test_txn_spill_tests_a_prepared_transactions_spills_are_never_taken_out},
{"spill::tests::a_process_with_nothing_of_its_own_spills_a_paused_transactions_versions",
test_txn_spill_tests_a_process_with_nothing_of_its_own_spills_a_paused_transactions_versions},
{"spill::tests::a_reader_across_a_merges_unchain_reads_each_version_once",
test_txn_spill_tests_a_reader_across_a_merges_unchain_reads_each_version_once},
{"spill::tests::a_reader_paused_in_a_manifest_read_finds_each_version_once",
test_txn_spill_tests_a_reader_paused_in_a_manifest_read_finds_each_version_once},
+ {"spill::tests::a_residual_whose_listing_fails_lists_nothing_and_gives_its_count_back",
+ test_txn_spill_tests_a_residual_whose_listing_fails_lists_nothing_and_gives_its_count_back},
{"spill::tests::a_scan_merges_spilled_keys_across_chunks_in_order",
test_txn_spill_tests_a_scan_merges_spilled_keys_across_chunks_in_order},
{"spill::tests::a_second_spill_merges_with_the_first_keeping_its_versions_order",
@@ -364,6 +564,8 @@ static const CportTest CPORT_TESTS[] = {
test_txn_spill_tests_a_spill_of_many_keys_writes_one_file_whose_filter_takes_pages},
{"spill::tests::a_spill_takes_only_the_keys_its_transaction_alone_holds",
test_txn_spill_tests_a_spill_takes_only_the_keys_its_transaction_alone_holds},
+ {"spill::tests::a_spilled_free_space_maps_entries_are_read_below_the_memtable",
+ test_txn_spill_tests_a_spilled_free_space_maps_entries_are_read_below_the_memtable},
{"spill::tests::a_spilled_value_maps_entries_are_read_below_the_memtable",
test_txn_spill_tests_a_spilled_value_maps_entries_are_read_below_the_memtable},
{"spill::tests::a_spills_tuple_ids_still_find_their_rows_keys",
@@ -374,12 +576,18 @@ static const CportTest CPORT_TESTS[] = {
test_txn_spill_tests_a_transaction_spilling_more_storages_than_the_budget_holds_pages_completes},
{"spill::tests::an_abort_takes_its_spills_out_of_the_chain",
test_txn_spill_tests_an_abort_takes_its_spills_out_of_the_chain},
+ {"spill::tests::an_aborted_free_space_map_spill_stays_listed_until_a_fold",
+ test_txn_spill_tests_an_aborted_free_space_map_spill_stays_listed_until_a_fold},
{"spill::tests::an_owner_spills_nothing_while_a_helper_holds_its_transactions_turn",
test_txn_spill_tests_an_owner_spills_nothing_while_a_helper_holds_its_transactions_turn},
{"spill::tests::more_transactions_than_any_fixed_count_spill_one_storage_at_a_full_budget",
test_txn_spill_tests_more_transactions_than_any_fixed_count_spill_one_storage_at_a_full_budget},
{"spill::tests::redo_over_budget_spills_the_largest_replayed_transaction",
test_txn_spill_tests_redo_over_budget_spills_the_largest_replayed_transaction},
+ {"spill::tests::the_committed_arm_spills_a_keys_oldest_committed_versions_above_its_spills",
+ test_txn_spill_tests_the_committed_arm_spills_a_keys_oldest_committed_versions_above_its_spills},
+ {"spill::tests::the_committed_arms_spill_reads_back_in_order_after_a_restart",
+ test_txn_spill_tests_the_committed_arms_spill_reads_back_in_order_after_a_restart},
{"spill::tests::three_writers_of_small_trees_past_a_third_of_the_budget_each_spill_and_finish",
test_txn_spill_tests_three_writers_of_small_trees_past_a_third_of_the_budget_each_spill_and_finish},
{"spill::tests::three_writers_waiting_on_committed_versions_finish_once_a_fold_installs",
@@ -392,8 +600,12 @@ static const CportTest CPORT_TESTS[] = {
test_txn_state_tests_a_backend_records_its_key_in_a_state_and_redo_takes_none},
{"state::tests::a_linked_free_space_map_or_tuple_id_table_keeps_the_counter_from_falling_back",
test_txn_state_tests_a_linked_free_space_map_or_tuple_id_table_keeps_the_counter_from_falling_back},
+ {"state::tests::a_make_while_running_writers_hold_every_unsaved_state_gets_the_pools_error",
+ test_txn_state_tests_a_make_while_running_writers_hold_every_unsaved_state_gets_the_pools_error},
{"state::tests::a_maker_terminated_before_its_fill_leaves_the_fill_to_the_next_finder",
test_txn_state_tests_a_maker_terminated_before_its_fill_leaves_the_fill_to_the_next_finder},
+ {"state::tests::a_rechain_keeps_a_key_recorded_while_it_logs",
+ test_txn_state_tests_a_rechain_keeps_a_key_recorded_while_it_logs},
{"state::tests::a_reclaim_keeps_a_state_whose_manifest_lists_a_spill",
test_txn_state_tests_a_reclaim_keeps_a_state_whose_manifest_lists_a_spill},
{"state::tests::a_save_gathers_a_linked_states_counter_and_lets_an_idle_ones_fall_back",
@@ -406,6 +618,8 @@ static const CportTest CPORT_TESTS[] = {
test_txn_state_tests_a_state_opened_while_a_reclaim_looks_at_it_is_kept},
{"state::tests::a_state_saved_reclaimed_and_made_again_reads_the_same",
test_txn_state_tests_a_state_saved_reclaimed_and_made_again_reads_the_same},
+ {"state::tests::a_state_with_spills_keeps_the_counter_from_falling_back",
+ test_txn_state_tests_a_state_with_spills_keeps_the_counter_from_falling_back},
{"state::tests::a_write_between_a_gathering_and_its_saved_leaves_the_state_unsaved",
test_txn_state_tests_a_write_between_a_gathering_and_its_saved_leaves_the_state_unsaved},
{"state::tests::an_install_keeps_a_key_recorded_after_its_fold_read_the_base",
@@ -422,6 +636,136 @@ static const CportTest CPORT_TESTS[] = {
test_txn_state_tests_the_pool_holds_a_state_for_each_relation_lock_and_reclaims_idle_ones},
{"state::tests::two_backends_that_open_one_storage_make_one_state",
test_txn_state_tests_two_backends_that_open_one_storage_make_one_state},
+ {"status::tests::a_commit_registered_in_its_slot_reads_back_and_its_move_settles_the_entry",
+ test_txn_status_tests_a_commit_registered_in_its_slot_reads_back_and_its_move_settles_the_entry},
+ {"status::tests::a_folds_point_lies_below_a_commit_still_in_the_proc_array",
+ test_txn_status_tests_a_folds_point_lies_below_a_commit_still_in_the_proc_array},
+ {"status::tests::a_growth_holds_what_it_keeps_in_the_least_room",
+ test_txn_status_tests_a_growth_holds_what_it_keeps_in_the_least_room},
+ {"status::tests::a_growth_while_the_proc_array_cant_answer_keeps_an_ended_entry",
+ test_txn_status_tests_a_growth_while_the_proc_array_cant_answer_keeps_an_ended_entry},
+ {"status::tests::a_lookup_probes_every_place_and_neighbours_start_apart",
+ test_txn_status_tests_a_lookup_probes_every_place_and_neighbours_start_apart},
+ {"status::tests::a_prepared_commit_points_its_entry_at_the_committer_and_registers",
+ test_txn_status_tests_a_prepared_commit_points_its_entry_at_the_committer_and_registers},
+ {"status::tests::a_replayed_commit_stores_its_end_in_the_entry_its_replay_took",
+ test_txn_status_tests_a_replayed_commit_stores_its_end_in_the_entry_its_replay_took},
+ {"status::tests::a_slot_stays_its_entrys_across_a_growth_and_a_removed_ones_is_taken_again",
+ test_txn_status_tests_a_slot_stays_its_entrys_across_a_growth_and_a_removed_ones_is_taken_again},
+ {"status::tests::a_table_is_crowded_at_three_quarters",
+ test_txn_status_tests_a_table_is_crowded_at_three_quarters},
+ {"status::tests::a_take_of_room_moves_the_slots_pair_first",
+ test_txn_status_tests_a_take_of_room_moves_the_slots_pair_first},
+ {"status::tests::an_outcome_too_large_for_its_word_has_none",
+ test_txn_status_tests_an_outcome_too_large_for_its_word_has_none},
+ {"status::tests::an_outcomes_word_says_the_outcome",
+ test_txn_status_tests_an_outcomes_word_says_the_outcome},
+ {"status::tests::an_xids_word_is_no_empty_places",
+ test_txn_status_tests_an_xids_word_is_no_empty_places},
+ {"status::tests::churn_with_no_live_entries_keeps_one_segment",
+ test_txn_status_tests_churn_with_no_live_entries_keeps_one_segment},
+ {"status::tests::entries_are_taken_found_counted_removed_and_kept_by_a_growth",
+ test_txn_status_tests_entries_are_taken_found_counted_removed_and_kept_by_a_growth},
+ {"vacuum::tests::a_dead_tuple_id_is_claimed_once",
+ test_txn_vacuum_tests_a_dead_tuple_id_is_claimed_once},
+ {"vacuum::tests::a_dead_tuple_id_on_a_full_block_keeps_its_bytes_until_its_free",
+ test_txn_vacuum_tests_a_dead_tuple_id_on_a_full_block_keeps_its_bytes_until_its_free},
+ {"vacuum::tests::a_dropped_rows_tuple_id_keeps_its_enders_xid_for_its_frees_horizon",
+ test_txn_vacuum_tests_a_dropped_rows_tuple_id_keeps_its_enders_xid_for_its_frees_horizon},
+ {"vacuum::tests::a_dropped_rows_tuple_id_waits_for_the_horizon_to_pass_its_ender",
+ test_txn_vacuum_tests_a_dropped_rows_tuple_id_waits_for_the_horizon_to_pass_its_ender},
+ {"vacuum::tests::a_drops_ender_goes_on_its_rows_take_not_a_later_take_of_the_tuple_id",
+ test_txn_vacuum_tests_a_drops_ender_goes_on_its_rows_take_not_a_later_take_of_the_tuple_id},
+ {"vacuum::tests::a_fold_between_a_prunes_reads_leaves_what_a_prune_after_it_leaves",
+ test_txn_vacuum_tests_a_fold_between_a_prunes_reads_leaves_what_a_prune_after_it_leaves},
+ {"vacuum::tests::a_fold_between_the_finds_reads_leaves_its_rows_held",
+ test_txn_vacuum_tests_a_fold_between_the_finds_reads_leaves_its_rows_held},
+ {"vacuum::tests::a_fold_during_a_blocks_spent_count_counts_what_a_count_after_it_counts",
+ test_txn_vacuum_tests_a_fold_during_a_blocks_spent_count_counts_what_a_count_after_it_counts},
+ {"vacuum::tests::a_fold_during_a_find_finds_what_a_find_after_it_finds",
+ test_txn_vacuum_tests_a_fold_during_a_find_finds_what_a_find_after_it_finds},
+ {"vacuum::tests::a_fold_during_a_free_leaves_what_a_free_after_it_leaves",
+ test_txn_vacuum_tests_a_fold_during_a_free_leaves_what_a_free_after_it_leaves},
+ {"vacuum::tests::a_fold_during_a_frees_redo_leaves_what_a_redo_after_it_leaves",
+ test_txn_vacuum_tests_a_fold_during_a_frees_redo_leaves_what_a_redo_after_it_leaves},
+ {"vacuum::tests::a_fold_during_a_frees_redo_leaves_what_the_free_left",
+ test_txn_vacuum_tests_a_fold_during_a_frees_redo_leaves_what_the_free_left},
+ {"vacuum::tests::a_folded_rows_tuple_id_is_not_freed",
+ test_txn_vacuum_tests_a_folded_rows_tuple_id_is_not_freed},
+ {"vacuum::tests::a_free_of_a_block_whose_takes_all_spilled_starts_from_its_newest_spilled_entry",
+ test_txn_vacuum_tests_a_free_of_a_block_whose_takes_all_spilled_starts_from_its_newest_spilled_entry},
+ {"vacuum::tests::a_frees_enders_of_another_form_are_refused_as_corruption",
+ test_txn_vacuum_tests_a_frees_enders_of_another_form_are_refused_as_corruption},
+ {"vacuum::tests::a_frees_take_out_leaves_a_retake_on_its_key",
+ test_txn_vacuum_tests_a_frees_take_out_leaves_a_retake_on_its_key},
+ {"vacuum::tests::a_frees_take_out_takes_out_a_subtransactions_delete",
+ test_txn_vacuum_tests_a_frees_take_out_takes_out_a_subtransactions_delete},
+ {"vacuum::tests::a_frees_take_out_with_no_ender_leaves_any_retake_on_its_key",
+ test_txn_vacuum_tests_a_frees_take_out_with_no_ender_leaves_any_retake_on_its_key},
+ {"vacuum::tests::a_prune_over_an_aborted_unfolded_insert_claims_nothing",
+ test_txn_vacuum_tests_a_prune_over_an_aborted_unfolded_insert_claims_nothing},
+ {"vacuum::tests::a_prunes_take_out_of_an_aborted_writers_own_row_leaves_a_retake",
+ test_txn_vacuum_tests_a_prunes_take_out_of_an_aborted_writers_own_row_leaves_a_retake},
+ {"vacuum::tests::a_refused_markers_redo_keeps_the_version_as_the_primary_does",
+ test_txn_vacuum_tests_a_refused_markers_redo_keeps_the_version_as_the_primary_does},
+ {"vacuum::tests::a_row_its_own_transaction_inserted_and_deleted_waits_for_the_horizon",
+ test_txn_vacuum_tests_a_row_its_own_transaction_inserted_and_deleted_waits_for_the_horizon},
+ {"vacuum::tests::a_skipped_index_pass_gives_back_a_dead_tuple_ids_bytes_and_a_later_free_its_offset_alone",
+ test_txn_vacuum_tests_a_skipped_index_pass_gives_back_a_dead_tuple_ids_bytes_and_a_later_free_its_offset_alone},
+ {"vacuum::tests::a_take_leaves_a_version_an_open_snapshot_may_see",
+ test_txn_vacuum_tests_a_take_leaves_a_version_an_open_snapshot_may_see},
+ {"vacuum::tests::a_take_no_row_names_is_freed_only_once_its_taker_has_ended",
+ test_txn_vacuum_tests_a_take_no_row_names_is_freed_only_once_its_taker_has_ended},
+ {"vacuum::tests::a_take_prunes_a_full_blocks_dead_versions_and_its_update_stays_on_the_block",
+ test_txn_vacuum_tests_a_take_prunes_a_full_blocks_dead_versions_and_its_update_stays_on_the_block},
+ {"vacuum::tests::a_take_that_empties_its_entry_is_freed_with_all_the_bytes_the_entry_held",
+ test_txn_vacuum_tests_a_take_that_empties_its_entry_is_freed_with_all_the_bytes_the_entry_held},
+ {"vacuum::tests::a_takes_ender_widens_against_its_writer_across_an_epoch",
+ test_txn_vacuum_tests_a_takes_ender_widens_against_its_writer_across_an_epoch},
+ {"vacuum::tests::an_aborted_inserts_tuple_id_on_the_fresh_page_is_freed_with_its_own_bytes_and_taken_again",
+ test_txn_vacuum_tests_an_aborted_inserts_tuple_id_on_the_fresh_page_is_freed_with_its_own_bytes_and_taken_again},
+ {"vacuum::tests::an_aborted_leaves_old_offsets_and_its_new_tuple_id_are_handed_out_again",
+ test_txn_vacuum_tests_an_aborted_leaves_old_offsets_and_its_new_tuple_id_are_handed_out_again},
+ {"vacuum::tests::every_raise_of_a_blocks_room_lowers_its_categorys_hint",
+ test_txn_vacuum_tests_every_raise_of_a_blocks_room_lowers_its_categorys_hint},
+ {"values::tests::a_take_down_takes_one_from_a_count_and_removes_the_last",
+ test_txn_values_tests_a_take_down_takes_one_from_a_count_and_removes_the_last},
+ {"values::tests::a_value_counts_heaps_toast_chunks",
+ test_txn_values_tests_a_value_counts_heaps_toast_chunks},
+ {"values::tests::a_value_id_the_map_holds_is_skipped",
+ test_txn_values_tests_a_value_id_the_map_holds_is_skipped},
+ {"values::tests::a_value_ids_entry_reads_back",
+ test_txn_values_tests_a_value_ids_entry_reads_back},
+ {"values::tests::a_value_map_entry_reads_back_and_refuses_every_other_form",
+ test_txn_values_tests_a_value_map_entry_reads_back_and_refuses_every_other_form},
+ {"visible::tests::a_historic_snapshot_sees_its_transactions_rows_by_their_commands",
+ test_txn_visible_tests_a_historic_snapshot_sees_its_transactions_rows_by_their_commands},
+ {"visible::tests::a_key_read_gives_every_row_version_the_snapshots_rule_sees",
+ test_txn_visible_tests_a_key_read_gives_every_row_version_the_snapshots_rule_sees},
+ {"visible::tests::a_killed_speculative_insert_is_aborted_for_every_reader_at_once",
+ test_txn_visible_tests_a_killed_speculative_insert_is_aborted_for_every_reader_at_once},
+ {"visible::tests::a_row_named_by_its_tuple_id_is_ended_by_the_version_that_names_it",
+ test_txn_visible_tests_a_row_named_by_its_tuple_id_is_ended_by_the_version_that_names_it},
+ {"visible::tests::a_rows_change_is_answered_as_heaps_satisfies_update_answers",
+ test_txn_visible_tests_a_rows_change_is_answered_as_heaps_satisfies_update_answers},
+ {"visible::tests::a_rows_delete_is_seen_only_by_a_snapshot_taken_after_its_commit",
+ test_txn_visible_tests_a_rows_delete_is_seen_only_by_a_snapshot_taken_after_its_commit},
+ {"visible::tests::a_rows_slice_after_a_removal_under_its_head_answers_as_its_whole_chain",
+ test_txn_visible_tests_a_rows_slice_after_a_removal_under_its_head_answers_as_its_whole_chain},
+ {"visible::tests::a_rows_slice_answers_as_its_whole_chain",
+ test_txn_visible_tests_a_rows_slice_answers_as_its_whole_chain},
+ {"visible::tests::a_rows_stamp_shows_the_last_change_above_it_whatever_its_outcome",
+ test_txn_visible_tests_a_rows_stamp_shows_the_last_change_above_it_whatever_its_outcome},
+ {"visible::tests::a_serializable_read_conflicts_out_with_the_writers_heaps_rule_names",
+ test_txn_visible_tests_a_serializable_read_conflicts_out_with_the_writers_heaps_rule_names},
+ {"visible::tests::a_subtransactions_row_stays_invisible_to_a_snapshot_taken_while_its_transaction_ran",
+ test_txn_visible_tests_a_subtransactions_row_stays_invisible_to_a_snapshot_taken_while_its_transaction_ran},
+ {"visible::tests::a_transactions_own_row_is_seen_from_the_command_after_its_write",
+ test_txn_visible_tests_a_transactions_own_row_is_seen_from_the_command_after_its_write},
+ {"visible::tests::a_versions_epoch_counts_the_transforms_written_before_it",
+ test_txn_visible_tests_a_versions_epoch_counts_the_transforms_written_before_it},
+ {"visible::tests::each_snapshot_kind_sees_the_versions_heaps_rule_sees",
+ test_txn_visible_tests_each_snapshot_kind_sees_the_versions_heaps_rule_sees},
};
PG_FUNCTION_INFO_V1(doltgres_cport_test_txn);
diff --git a/src/doltgres/test/modules/doltgres_test/doltgres_test--1.0.sql b/src/doltgres/test/modules/doltgres_test/doltgres_test--1.0.sql
index 4c26ee5aeefa8..6bf378a4db871 100644
--- a/src/doltgres/test/modules/doltgres_test/doltgres_test--1.0.sql
+++ b/src/doltgres/test/modules/doltgres_test/doltgres_test--1.0.sql
@@ -332,3 +332,18 @@ AS 'MODULE_PATHNAME', 'dg_listed_unwritten' LANGUAGE C STRICT;
CREATE FUNCTION doltgres_test_step_unwritten() RETURNS bool
AS 'MODULE_PATHNAME', 'dg_step_unwritten' LANGUAGE C STRICT;
+
+CREATE FUNCTION doltgres_test_scan_shape(target regclass) RETURNS text
+AS 'MODULE_PATHNAME', 'dg_scan_shape' LANGUAGE C STRICT;
+
+CREATE FUNCTION doltgres_test_index_move(index regclass, tablespace text) RETURNS void
+AS 'MODULE_PATHNAME', 'dg_index_move' LANGUAGE C STRICT;
+
+CREATE FUNCTION doltgres_test_base_points(target regclass) RETURNS text
+AS 'MODULE_PATHNAME', 'dg_base_points' LANGUAGE C STRICT;
+
+CREATE FUNCTION doltgres_test_identity_versions(kind int4, number oid) RETURNS text
+AS 'MODULE_PATHNAME', 'dg_identity_versions' LANGUAGE C STRICT;
+
+CREATE FUNCTION doltgres_test_save() RETURNS text
+AS 'MODULE_PATHNAME', 'dg_save' LANGUAGE C STRICT;
diff --git a/src/doltgres/test/modules/doltgres_test/doltgres_test.c b/src/doltgres/test/modules/doltgres_test/doltgres_test.c
index 56587bb2f658d..6759ea72ecf08 100644
--- a/src/doltgres/test/modules/doltgres_test/doltgres_test.c
+++ b/src/doltgres/test/modules/doltgres_test/doltgres_test.c
@@ -44,11 +44,13 @@
#include "catalog/pg_class.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type_d.h"
+#include "catalog/storage.h"
#include "commands/progress.h"
#include "commands/repack.h"
#include "commands/tablecmds.h"
#include "commands/tablespace.h"
#include "commands/vacuum.h"
+#include "doltgres/hooks.h"
#include "executor/executor.h"
#include "executor/spi.h"
#include "executor/tuptable.h"
@@ -124,7 +126,11 @@ extern void doltgres_test_index_fetch_key(struct IndexFetchTableData *fetch, Rel
extern bool doltgres_test_fold(Relation target);
extern Datum doltgres_test_base_sizes(Relation target);
extern Datum doltgres_test_totals(Relation target);
+extern Datum doltgres_test_scan_shape(Relation target);
extern Datum doltgres_test_count(Relation target);
+extern Datum doltgres_test_base_points(Relation target);
+extern Datum doltgres_test_identity_versions(int32 kind, Oid number);
+extern Datum doltgres_test_save(void);
extern Datum doltgres_test_index_entries(Relation target);
extern bool doltgres_test_held(Relation target, uint64 tid);
extern Datum doltgres_test_index_base(Relation target);
@@ -252,6 +258,11 @@ PG_FUNCTION_INFO_V1(dg_totals);
PG_FUNCTION_INFO_V1(dg_count);
PG_FUNCTION_INFO_V1(dg_drop_constraint);
PG_FUNCTION_INFO_V1(dg_unchain);
+PG_FUNCTION_INFO_V1(dg_scan_shape);
+PG_FUNCTION_INFO_V1(dg_index_move);
+PG_FUNCTION_INFO_V1(dg_base_points);
+PG_FUNCTION_INFO_V1(dg_identity_versions);
+PG_FUNCTION_INFO_V1(dg_save);
/* dg_store writes the shared area's root record under the fork's lock. */
Datum
@@ -2709,3 +2720,77 @@ dg_unchain(PG_FUNCTION_ARGS)
ProcessUtility_hook = unchained_utility;
PG_RETURN_VOID();
}
+
+/* dg_scan_shape returns a fork table's shape as its scans' costs read it, as doltgres_test_scan_shape reads it. */
+Datum
+dg_scan_shape(PG_FUNCTION_ARGS)
+{
+ Relation rel = table_open(PG_GETARG_OID(0), AccessShareLock);
+ Datum shape = doltgres_test_scan_shape(rel);
+
+ table_close(rel, AccessShareLock);
+ PG_RETURN_DATUM(shape);
+}
+
+/*
+ * dg_index_move moves an index into a tablespace as ALTER INDEX SET TABLESPACE's index_copy_data does: a new
+ * relfilenode with its main fork's pages copied, the fork's move of its storage, the old file dropped at commit, and
+ * its pg_class row.
+ */
+Datum
+dg_index_move(PG_FUNCTION_ARGS)
+{
+ Relation rel = index_open(PG_GETARG_OID(0), AccessExclusiveLock);
+ Oid space = get_tablespace_oid(text_to_cstring(PG_GETARG_TEXT_PP(1)), false);
+ RelFileNumber number = GetNewRelFileNumber(space, NULL, rel->rd_rel->relpersistence);
+ RelFileLocator locator = rel->rd_locator;
+ SMgrRelation dstrel;
+
+ locator.relNumber = number;
+ locator.spcOid = space;
+ dstrel = RelationCreateStorage(locator, rel->rd_rel->relpersistence, true);
+ RelationCopyStorage(RelationGetSmgr(rel), dstrel, MAIN_FORKNUM, rel->rd_rel->relpersistence);
+ doltgres_index_copy_data(rel, &locator);
+ RelationDropStorage(rel);
+ smgrclose(dstrel);
+ SetRelationTableSpace(rel, space, number);
+ RelationAssumeNewRelfilelocator(rel);
+ index_close(rel, NoLock);
+ CommandCounterIncrement();
+ PG_RETURN_VOID();
+}
+
+/*
+ * dg_base_points returns a fork table's newest base's root, fold point, and read point, as doltgres_test_base_points
+ * reads them.
+ */
+Datum
+dg_base_points(PG_FUNCTION_ARGS)
+{
+ Relation rel = table_open(PG_GETARG_OID(0), AccessShareLock);
+ Datum points = doltgres_test_base_points(rel);
+
+ table_close(rel, AccessShareLock);
+ PG_RETURN_DATUM(points);
+}
+
+/*
+ * dg_identity_versions returns how many versions the delta and its identity
+ * spills hold of an object's identity key by its number, as
+ * doltgres_test_identity_versions counts them.
+ */
+Datum
+dg_identity_versions(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_DATUM(doltgres_test_identity_versions(PG_GETARG_INT32(0), PG_GETARG_OID(1)));
+}
+
+/*
+ * dg_save saves the database's repository outside a checkpoint, as
+ * doltgres_test_save does.
+ */
+Datum
+dg_save(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_DATUM(doltgres_test_save());
+}
diff --git a/src/doltgres/test/t/006_wal.pl b/src/doltgres/test/t/006_wal.pl
index 1345a3dc07acc..f8b21eed755fd 100644
--- a/src/doltgres/test/t/006_wal.pl
+++ b/src/doltgres/test/t/006_wal.pl
@@ -129,14 +129,14 @@
"\\set VERBOSITY verbose\nSELECT doltgres_test_wal_unknown(0)");
like(
$stderr,
- qr/ERROR: XX000: versioned WAL record of kind 14 holds no data/,
+ qr/ERROR: XX000: versioned WAL record of kind \d+ holds no data/,
'a record without data is refused');
$node->safe_psql('postgres', 'SELECT doltgres_test_wal_unknown(1)');
$node->stop('immediate');
ok(!$node->start(fail_ok => 1), 'a record of an unknown kind stops recovery');
like(
slurp_file($node->logfile),
- qr/PANIC: versioned_redo: unknown op code 224/,
+ qr/PANIC: versioned_redo: unknown op code \d+/,
'it names the unknown op code');
# Redo, outside any transaction, is refused a checkpoint delay, which a
diff --git a/src/doltgres/test/t/017_pages.pl b/src/doltgres/test/t/017_pages.pl
index 94019f01ed4a6..8215ee75e3cfd 100644
--- a/src/doltgres/test/t/017_pages.pl
+++ b/src/doltgres/test/t/017_pages.pl
@@ -273,15 +273,16 @@ sub restamped
# The region is listed at its blocks, the setting's sixteen and one more for
# the copies compactions take past the budget (bugs rows 110 and 116), and the
-# directory at its header, ten cache lines, a cache line for each 1 KiB page
+# directory at its header, eleven cache lines, a cache line for each 1 KiB page
# the region's blocks can hold, that block's included, three words for each
-# block, and a word of the free hint for each 64 slots below the block past
-# the budget's (bugs row 150).
+# block, a word of the free hint for each 64 slots below the block past the
+# budget's (bugs row 150), and a slot's four-byte link in the retired list for
+# each slot (bugs row 309).
is( $node->safe_psql(
'postgres',
"SELECT name, size FROM pg_shmem_allocations WHERE name LIKE 'versioned delta%' ORDER BY name"
),
- "versioned delta|1114112\nversioned delta directory|70808",
+ "versioned delta|1114112\nversioned delta directory|75224",
"the delta's region and its directory are listed with their sizes");
# On the fresh server the region gives sixteen pages of a block at once, the
@@ -566,20 +567,31 @@ sub restamped
"$used|$free",
'a backend terminated while it owns a page gives the page back');
-# A free pass paused after its claim of a slot: a second pass frees nothing,
-# and the count is right once the first goes on.
+# A free pass paused after its claim of a slot holds the pass's lock: a
+# second pass waits at it and frees nothing meanwhile, and the count is right
+# once the first goes on (bugs row 309's one pass at a time).
$kept = paged($LARGE);
$used = delta(0, 0, 0);
-hold('doltgres-delta-claimed');
my $passer = $node->background_psql('postgres', on_error_stop => 0);
+$passer->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-delta-claimed', 'wait')"
+);
start($passer, "SELECT doltgres_test_delta(4, $kept, 0)");
$node->wait_for_event('client backend', 'doltgres-delta-claimed');
-delta(23, 0, 0);
+my $second_pass = $node->background_psql('postgres', on_error_stop => 0);
+start($second_pass, 'SELECT doltgres_test_delta(23, 0, 0)');
+$node->poll_query_until('postgres',
+ "SELECT wait_event_type = 'LWLock' FROM pg_stat_activity WHERE pid = $second_pass->{pid}"
+) or die 'the second free pass never waited at the first';
is(delta(0, 0, 0),
- $used, "a second free pass at a pass's pause frees nothing");
-release('doltgres-delta-claimed');
+ $used, "a second free pass at a pass's pause waits and frees nothing");
+$node->safe_psql('postgres',
+ "SELECT injection_points_wakeup('doltgres-delta-claimed')");
is(ended($passer), '0', 'a paused free pass goes on when woken');
+isnt(ended($second_pass), undef,
+ 'the waiting free pass goes on once the first ends');
leave($passer);
+leave($second_pass);
is( delta(0, 0, 0),
$used - $BLOCK,
'a free pass that was paused frees what it claimed');
@@ -1350,4 +1362,87 @@ sub raised_records
ok($below->($unlinking, $before_abort + int(($aborted_filled - $before_abort) / 2)),
"an aborted table's storage gives its delta's pages back at the abort");
+
+# Bugs row 229's wait: a writer whose first write into a table finds room for
+# its memtable's first page and none for its version waits asleep at the
+# budget, and a cancel ends it with Postgres' 57014. Spill's relieved spills
+# nothing for it, and its walk closes the memtable the refused try left, a
+# free that ended the wait at once, so the writer tried again forever and
+# never reached the sleep a cancel ends. A write to another table first
+# gives the commit status table room for the writer's entry; then the delta is
+# filled with test pages of every size but one free 1 KB slot, in a block
+# other pages keep.
+$node->safe_psql('postgres', 'DROP TABLE kilobytes');
+$node->append_conf('postgresql.conf', 'delta_memory = 1MB');
+$node->restart;
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE first_write (k int4, v text);
+CREATE TABLE status_warmed (k int4);
+SELECT doltgres_test_write('status_warmed', true, 'INSERT INTO status_warmed VALUES (1)');
+});
+my $slot = paged($PAGE);
+my @sizes = ($LARGE, map { (8 << 32) | ((1024 << $_) - 64) } 0 .. 5);
+my @filled;
+for (1 .. 2)
+{
+ delta(23, 0, 0);
+ for my $size (@sizes)
+ {
+ my $record = record();
+ push @filled, $record if delta(20, $record, $size) > 0;
+ }
+}
+delta(4, $slot, 0);
+ok($node->poll_query_until('postgres', "SELECT doltgres_test_delta(10, $PAGE, 1) = 1"),
+ "the delta frees the one slot a test page left");
+is(join(',', map { delta(10, $_, $_ == $PAGE ? 2 : 1) } @sizes),
+ '0,0,0,0,0,0,0', 'the delta has no other free slot');
+my $first_writer = $node->background_psql('postgres', on_error_stop => 0);
+$first_writer->query_safe('\set VERBOSITY verbose');
+start($first_writer,
+ "SELECT doltgres_test_write('first_write', true, 'INSERT INTO first_write VALUES (1, repeat(''x'', 2000))')");
+ok( $node->poll_query_until(
+ 'postgres',
+ "SELECT count(*) = 1 FROM pg_stat_activity WHERE wait_event = 'versioned delta budget'"
+ ),
+ "a first write with room for its memtable's first page alone waits asleep at the budget");
+$node->safe_psql('postgres', "SELECT pg_cancel_backend($first_writer->{pid})");
+awaited($first_writer, \$first_writer->{stderr}, qr/ERROR:/);
+like($first_writer->{stderr},
+ qr/ERROR: 57014: canceling statement due to user request/,
+ 'a cancel ends a wait at a full delta with 57014');
+delta(21, $_, 0) for @filled;
+leave($first_writer);
+
+# Bugs row 355: a process answers a repeat of an open the directory admitted
+# from its own table, for the same name at the same epoch only. A repeat
+# reads what the first open read; once the page is retired and its slot freed
+# and taken again, the old name is refused and the new one reads the new page.
+my $cached = record();
+my $cached_name = delta(1, $cached, $PAGE);
+delta(2, $cached, 41);
+my $cacher = $node->background_psql('postgres', on_error_stop => 0);
+is( $cacher->query_safe("SELECT doltgres_test_delta(27, 0, $cached_name)")
+ . ' '
+ . $cacher->query_safe("SELECT doltgres_test_delta(27, 0, $cached_name)"),
+ '41 41',
+ 'a repeat of an admitted open in one process reads what the first open read');
+my $free_before = delta(17, 0, 0);
+delta(4, $cached, 0);
+delta(23, 0, 0);
+is(delta(17, 0, 0), $free_before + 1, "the cached page's slot is freed");
+$cacher->query("SELECT doltgres_test_delta(27, 0, $cached_name)");
+like(
+ $cacher->{stderr},
+ qr/a name of the delta names no page its hold reaches/,
+ "the process's open of the freed page's name is refused");
+$cacher->{stderr} = '';
+my $taker = record();
+my $taker_name = delta(1, $taker, $PAGE);
+delta(2, $taker, 42);
+is( $cacher->query_safe("SELECT doltgres_test_delta(27, 0, $taker_name)"),
+ '42', 'and its open of the page that took the slot reads the new page');
+$cacher->quit;
+
done_testing();
diff --git a/src/doltgres/test/t/036_parallel.pl b/src/doltgres/test/t/036_parallel.pl
index 1c986957163c5..8bcfcdd92661a 100644
--- a/src/doltgres/test/t/036_parallel.pl
+++ b/src/doltgres/test/t/036_parallel.pl
@@ -149,5 +149,63 @@ sub parallel
);
$before->quit;
+# Bugs row 309's parallel units: a parallel query's scan of a table whose rows
+# are all in the delta, run by the leader and two workers, reads the delta's
+# run at most once in each of them, at 2,000 rows and at 5,000, where each
+# unit read and sorted the whole run; it gives every row once; and the scan
+# rescanned on a nested loop's inner side for each of three outer rows gives
+# every row each time.
+my $parallel_on = q{
+SET parallel_setup_cost = 0;
+SET parallel_tuple_cost = 0;
+SET min_parallel_table_scan_size = 0;
+SET max_parallel_workers_per_gather = 2;
+SET enable_hashjoin = off;
+SET enable_mergejoin = off;
+SET enable_material = off;
+};
+$node->safe_psql(
+ 'postgres', q{
+CREATE EXTENSION injection_points;
+});
+foreach my $rows (2000, 5000)
+{
+ my ($code, $out, $err) = $node->psql(
+ 'postgres', qq{
+BEGIN;
+$parallel_on
+CREATE TABLE units_$rows (a int4, b text) USING heap;
+ALTER TABLE units_$rows SET (parallel_workers = 2);
+INSERT INTO units_$rows SELECT n, repeat('u', 34) FROM generate_series(1, $rows) n;
+ANALYZE units_$rows;
+SELECT injection_points_attach('doltgres-scan-run', 'notice');
+SELECT count(*) FROM units_$rows;
+SELECT injection_points_detach('doltgres-scan-run');
+\\echo ONCE
+SELECT count(*), count(DISTINCT a), sum(a) FROM units_$rows;
+\\echo PLAN
+EXPLAIN (COSTS OFF) SELECT count(*) FROM (SELECT n FROM generate_series(1, 3) n WHERE random() >= 0) o, units_$rows u WHERE u.a > 0;
+\\echo RESCANNED
+SELECT count(*) FROM (SELECT n FROM generate_series(1, 3) n WHERE random() >= 0) o, units_$rows u WHERE u.a > 0;
+ROLLBACK;
+});
+ my $runs = () = $err =~ /doltgres-scan-run/g;
+ ok( $runs >= 1 && $runs <= 3,
+ "a parallel scan of $rows delta rows reads the delta's run at most once in each process ($runs)"
+ );
+ is( ($out =~ /ONCE\n(\S+)\n/)[0],
+ "$rows|$rows|" . ($rows * ($rows + 1) / 2),
+ "a parallel scan of $rows delta rows gives every row once");
+ like(
+ ($out =~ /PLAN\n(.*)RESCANNED/s)[0],
+ qr/Nested Loop.*Gather.*Parallel Seq Scan on units_$rows/s,
+ "the rescanned plan has the parallel scan of $rows rows on a nested loop's inner side"
+ );
+ is( ($out =~ /RESCANNED\n(\d+)/)[0],
+ 3 * $rows,
+ "a parallel scan of $rows delta rows rescanned for each outer row gives every row each time"
+ );
+}
+
$node->stop;
done_testing();
diff --git a/src/doltgres/test/t/041_database.pl b/src/doltgres/test/t/041_database.pl
index bbe71897a2845..d72ef8eadae81 100644
--- a/src/doltgres/test/t/041_database.pl
+++ b/src/doltgres/test/t/041_database.pl
@@ -431,5 +431,265 @@ sub files
}
$rekeyed->stop;
+# Bugs row 99: DROP TABLESPACE of a location a fork table left, moved out by
+# SET TABLESPACE or dropped, empties the fork's packs out of it into the
+# database's own directory and removes the tablespace's directory of them, so
+# the tablespace drops as in Postgres, after the checkpoint that removes a
+# dropped table's files, and the moved table reads every row.
+$node->safe_psql('postgres', 'CREATE EXTENSION IF NOT EXISTS doltgres_test');
+for my $left ('moved', 'dropped')
+{
+ $node->safe_psql('postgres', "CREATE TABLESPACE left_$left LOCATION ''");
+ my $space = $node->safe_psql('postgres',
+ "SELECT oid FROM pg_tablespace WHERE spcname = 'left_$left'");
+ $node->safe_psql('postgres',
+ "CREATE TABLE in_$left (k int4 PRIMARY KEY, v text) TABLESPACE left_$left; INSERT INTO in_$left SELECT g, repeat('v', 40) || g FROM generate_series(1, 500) g"
+ );
+ $node->safe_psql('postgres',
+ "SELECT doltgres_test_fold('in_$left'); CHECKPOINT");
+ my @packs = glob("$pgdata/pg_tblspc/$space/PG_*/pg_versioned/*/*.pack");
+ ok(@packs > 0,
+ "a fold of a table in tablespace left_$left writes its packs there");
+ $node->safe_psql('postgres',
+ $left eq 'moved'
+ ? "ALTER TABLE in_$left SET TABLESPACE pg_default"
+ : "DROP TABLE in_$left");
+ my ($ret, $out, $err) =
+ $node->psql('postgres', "DROP TABLESPACE left_$left");
+ is("$ret|$err", '0|',
+ $left eq 'moved'
+ ? 'DROP TABLESPACE of a location a fork table moved out of empties the fork\'s packs out of it'
+ : 'DROP TABLESPACE of a location a fork table left empties the fork\'s packs out of it'
+ );
+ ok(!-e "$pgdata/pg_tblspc/$space",
+ "the dropped tablespace left_$left leaves no directory");
+}
+is( $node->safe_psql(
+ 'postgres', 'SELECT count(*) || \' \' || sum(k) FROM in_moved'),
+ '500 125250',
+ "the moved table reads every row after its old tablespace drops");
+
+# Bugs row 99: a base backup keeps every collection from running, so DROP
+# TABLESPACE of a location that holds the fork's packs waits for the backup's
+# end, where Postgres drops it at once: a cancel ends the wait with Postgres'
+# error and keeps the tablespace, and a DROP waiting at pg_backup_stop then
+# drops it.
+$node->safe_psql('postgres',
+ "CREATE TABLESPACE backed LOCATION ''; CREATE TABLE in_backed (k int4) TABLESPACE backed; INSERT INTO in_backed SELECT generate_series(1, 100)"
+);
+$node->safe_psql('postgres',
+ "SELECT doltgres_test_fold('in_backed'); ALTER TABLE in_backed SET TABLESPACE pg_default; CHECKPOINT"
+);
+my $backed = $node->safe_psql('postgres',
+ "SELECT oid FROM pg_tablespace WHERE spcname = 'backed'");
+my $backup = $node->background_psql('postgres');
+$backup->query_safe("SELECT pg_backup_start('row99', true)");
+my $waiting =
+ q{SELECT EXISTS (SELECT 1 FROM pg_stat_activity WHERE query = 'DROP TABLESPACE backed;' AND wait_event_type = 'Extension')};
+my $dropper = $node->background_psql('postgres', on_error_stop => 0);
+$dropper->query_safe('\\set VERBOSITY verbose');
+$dropper->query_until(qr/start/, "\\echo start\nDROP TABLESPACE backed;\n");
+$node->poll_query_until('postgres', $waiting);
+$node->safe_psql('postgres',
+ "SELECT pg_cancel_backend(pid) FROM pg_stat_activity WHERE query = 'DROP TABLESPACE backed;'"
+);
+$dropper->query('SELECT 1');
+like(
+ $dropper->{stderr},
+ qr/ERROR: 57014: canceling statement due to user request/,
+ "a cancel ends DROP TABLESPACE's wait for a base backup with Postgres' error"
+);
+is( $node->safe_psql(
+ 'postgres',
+ "SELECT count(*) FROM pg_tablespace WHERE spcname = 'backed'"),
+ '1',
+ "a canceled DROP TABLESPACE keeps the tablespace");
+$dropper->{stderr} = '';
+$dropper->query_until(qr/again/, "\\echo again\nDROP TABLESPACE backed;\n");
+$node->poll_query_until('postgres', $waiting);
+$backup->query('SELECT 1 FROM pg_backup_stop()');
+$dropper->query('SELECT 1');
+is( $dropper->{stderr}
+ . (
+ $node->safe_psql(
+ 'postgres',
+ "SELECT count(*) FROM pg_tablespace WHERE spcname = 'backed'")),
+ '0',
+ "DROP TABLESPACE during a base backup drops the tablespace once pg_backup_stop ends the backup"
+);
+ok(!-e "$pgdata/pg_tblspc/$backed",
+ "the tablespace dropped after the backup leaves no directory");
+$_->quit for $backup, $dropper;
+
+# Bugs row 99 on a hot standby: its replay of DROP TABLESPACE empties its own
+# packs out of the tablespace, so the replay removes the tablespace's
+# directories and the moved table reads every row; and when a reader holds
+# the packs through the replay, Postgres' replay logs what it couldn't
+# remove, and the collector's sweep removes the fork's directory of packs
+# once the reader ends, with no restart.
+$node->backup('spaced');
+my $spaced = PostgreSQL::Test::Cluster->new('spaced_standby');
+$spaced->init_from_backup($node, 'spaced', has_streaming => 1);
+$spaced->append_conf('postgresql.conf', 'max_standby_streaming_delay = 1s');
+$spaced->start;
+my $spaced_dir = $spaced->data_dir;
+# standby_space makes a tablespace with a table whose fold the standby
+# replays into packs of its own there, and returns the tablespace's id.
+sub standby_space
+{
+ my ($held) = @_;
+ $node->safe_psql('postgres',
+ "CREATE TABLESPACE standby_$held LOCATION ''; CREATE TABLE on_$held (k int4 PRIMARY KEY, v text) TABLESPACE standby_$held; INSERT INTO on_$held SELECT g, repeat('w', 40) || g FROM generate_series(1, 300) g"
+ );
+ $node->safe_psql('postgres',
+ "SELECT doltgres_test_fold('on_$held'); CHECKPOINT");
+ $node->wait_for_replay_catchup($spaced);
+ my $space = $node->safe_psql('postgres',
+ "SELECT oid FROM pg_tablespace WHERE spcname = 'standby_$held'");
+ my $made = time + $PostgreSQL::Test::Utils::timeout_default;
+ sleep 1
+ while !(() =
+ glob("$spaced_dir/pg_tblspc/$space/PG_*/pg_versioned/*/*.pack"))
+ && time < $made;
+ return $space;
+}
+
+# standby_moved moves a tablespace's table out to pg_default and drops the
+# tablespace, each replayed on the standby.
+sub standby_moved
+{
+ my ($held) = @_;
+ $node->safe_psql('postgres',
+ "ALTER TABLE on_$held SET TABLESPACE pg_default; CHECKPOINT");
+ $node->wait_for_replay_catchup($spaced);
+ $node->safe_psql('postgres', "DROP TABLESPACE standby_$held");
+ $node->wait_for_replay_catchup($spaced);
+}
+my $free = standby_space('free');
+standby_moved('free');
+ok( !-e "$spaced_dir/pg_tblspc/$free",
+ "a hot standby's replay of DROP TABLESPACE removes the tablespace's directories with the fork's packs"
+);
+is( $spaced->safe_psql(
+ 'postgres', 'SELECT count(*) || \' \' || sum(k) FROM on_free'),
+ '300 45150',
+ "the standby reads every row of the table moved out of the dropped tablespace"
+);
+my $read = standby_space('read');
+$spaced->safe_psql('postgres',
+ "SELECT injection_points_attach('doltgres-shared-reading', 'wait')");
+my $holder = $spaced->background_psql('postgres', on_error_stop => 0);
+$holder->query_until(qr/reading/,
+ "\\echo reading\nSELECT doltgres_test_shared(11, 0, 0);\n");
+$spaced->wait_for_event('client backend', 'doltgres-shared-reading');
+standby_moved('read');
+$spaced->psql('postgres',
+ "SELECT injection_points_wakeup('doltgres-shared-reading')");
+$holder->query('SELECT 1');
+like(
+ $holder->{stderr},
+ qr/ERROR: canceling statement due to conflict with recovery/,
+ "a standby reader that holds the packs through the replay of DROP TABLESPACE is canceled as Postgres' replay cancels a tablespace's users"
+);
+ok( !-e "$spaced_dir/pg_tblspc/$read",
+ "and the replay's retry after the cancel empties the fork's packs out of the tablespace and removes its directories, with no restart"
+);
+$spaced->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-shared-reading')");
+$holder->quit;
+is( $spaced->safe_psql(
+ 'postgres', 'SELECT count(*) || \' \' || sum(k) FROM on_read'),
+ '300 45150',
+ "the standby reads every row of the table moved out of that tablespace");
+$spaced->stop;
+
+# Bugs row 99 on a standby without hot standby: no collector worker runs to
+# empty its packs at the replay of DROP TABLESPACE, so Postgres' replay logs
+# what it couldn't remove after leaving the dropped mark, and once the
+# standby starts again with hot standby, after a crash, the collector's
+# sweep empties the fork's packs out of the marked tablespace.
+$node->backup('cold');
+my $cold = PostgreSQL::Test::Cluster->new('cold_standby');
+$cold->init_from_backup($node, 'cold', has_streaming => 1);
+$cold->append_conf('postgresql.conf', 'hot_standby = off');
+$cold->start;
+my $cold_dir = $cold->data_dir;
+# cold_replayed waits until the standby without hot standby has replayed the
+# primary's WAL to its current end, read from the primary's view of it.
+sub cold_replayed
+{
+ my $lsn = $node->lsn('insert');
+ $node->poll_query_until('postgres',
+ "SELECT replay_lsn >= '$lsn' FROM pg_stat_replication WHERE application_name = 'cold_standby'"
+ ) or die "the standby without hot standby didn't replay to $lsn";
+}
+$node->safe_psql('postgres',
+ "CREATE TABLESPACE cold_kept LOCATION ''; CREATE TABLE on_cold (k int4 PRIMARY KEY, v text); INSERT INTO on_cold SELECT g, repeat('c', 40) || g FROM generate_series(1, 300) g"
+);
+$node->safe_psql('postgres',
+ "SELECT doltgres_test_fold('on_cold'); ALTER TABLE on_cold SET TABLESPACE cold_kept; CHECKPOINT"
+);
+my $cold_space = $node->safe_psql('postgres',
+ "SELECT oid FROM pg_tablespace WHERE spcname = 'cold_kept'");
+cold_replayed();
+my @cold_packs =
+ glob("$cold_dir/pg_tblspc/$cold_space/PG_*/pg_versioned/*/*.pack");
+ok( @cold_packs > 0,
+ "a standby without hot standby replays a move into a tablespace as packs of its own there"
+);
+$node->safe_psql('postgres',
+ 'ALTER TABLE on_cold SET TABLESPACE pg_default; CHECKPOINT');
+$node->safe_psql('postgres', 'DROP TABLESPACE cold_kept');
+cold_replayed();
+my @cold_left = glob("$cold_dir/pg_tblspc/$cold_space/PG_*/pg_versioned");
+ok( @cold_left == 1,
+ "a standby without hot standby keeps the fork's packs through its replay of DROP TABLESPACE"
+);
+ok(-e "$cold_left[0]/dropped",
+ "and its replay leaves the dropped mark on them");
+$cold->stop('immediate');
+$cold->append_conf('postgresql.conf', 'hot_standby = on');
+$cold->start;
+my $cold_swept = time + $PostgreSQL::Test::Utils::timeout_default;
+sleep 1 while -e $cold_left[0] && time < $cold_swept;
+ok( !-e $cold_left[0],
+ "the collector's sweep empties the fork's packs out of a tablespace whose catalog row is gone"
+);
+is( $cold->safe_psql(
+ 'postgres', 'SELECT count(*) || \' \' || sum(k) FROM on_cold'),
+ '300 45150',
+ "the swept standby reads every row of the table moved out of the tablespace"
+);
+$cold->stop;
+
+# Bugs row 99: a table another session creates in the tablespace while DROP
+# TABLESPACE waits for a base backup stops the DROP's emptying at its next
+# round, before any of the new table's packs could be moved, so Postgres
+# refuses the DROP as not empty, as it would with no wait.
+$node->safe_psql('postgres',
+ "CREATE TABLESPACE raced LOCATION ''; CREATE TABLE in_raced (k int4) TABLESPACE raced; INSERT INTO in_raced SELECT generate_series(1, 100)"
+);
+$node->safe_psql('postgres',
+ "SELECT doltgres_test_fold('in_raced'); ALTER TABLE in_raced SET TABLESPACE pg_default; CHECKPOINT"
+);
+my $raced_backup = $node->background_psql('postgres');
+$raced_backup->query_safe("SELECT pg_backup_start('raced', true)");
+my $racing = $node->background_psql('postgres', on_error_stop => 0);
+$racing->query_safe('\\set VERBOSITY verbose');
+$racing->query_until(qr/raced/, "\\echo raced\nDROP TABLESPACE raced;\n");
+$node->poll_query_until('postgres',
+ q{SELECT EXISTS (SELECT 1 FROM pg_stat_activity WHERE query = 'DROP TABLESPACE raced;' AND wait_event_type = 'Extension')}
+);
+$node->safe_psql('postgres',
+ 'CREATE TABLE in_raced_late (k int4) TABLESPACE raced');
+$racing->query('SELECT 1');
+like(
+ $racing->{stderr},
+ qr/ERROR: 55000: tablespace "raced" is not empty/,
+ "a table created in the tablespace while DROP TABLESPACE waits stops its emptying, and Postgres refuses the DROP"
+);
+$raced_backup->query('SELECT 1 FROM pg_backup_stop()');
+$_->quit for $racing, $raced_backup;
+
$node->stop;
done_testing();
diff --git a/src/doltgres/test/t/044_removal.pl b/src/doltgres/test/t/044_removal.pl
index c9e03f0709693..756bff95195cc 100644
--- a/src/doltgres/test/t/044_removal.pl
+++ b/src/doltgres/test/t/044_removal.pl
@@ -83,5 +83,31 @@
"a backend terminated inside its transaction takes its rows back at its exit's abort"
);
+# Bugs row 309: an abort's take-back of twenty rows of one table finds the
+# table's state once, not once for each row it wrote.
+SKIP:
+{
+ skip 'injection points are not enabled', 1
+ if $ENV{enable_injection_points} ne 'yes'
+ || !$node->check_extension('injection_points');
+ $node->safe_psql(
+ 'postgres', q{
+CREATE EXTENSION injection_points;
+CREATE TABLE source_twenty AS SELECT generate_series(1, 20) AS a;
+CREATE TABLE found_twenty (a int4);
+});
+ my ($code, $out, $err) = $node->psql(
+ 'postgres', q{
+SELECT injection_points_set_local();
+SELECT injection_points_attach('doltgres-state-found', 'notice');
+BEGIN;
+SELECT doltgres_test_insert('found_twenty', 'source_twenty', false, 0);
+ROLLBACK;
+});
+ my $found = () = $err =~ /doltgres-state-found/g;
+ is($found, 1,
+ "an abort's take-back of twenty rows finds their table's state once");
+}
+
$node->stop;
done_testing();
diff --git a/src/doltgres/test/t/045_btree.pl b/src/doltgres/test/t/045_btree.pl
index 6d538ff4b6933..824af99f90287 100644
--- a/src/doltgres/test/t/045_btree.pl
+++ b/src/doltgres/test/t/045_btree.pl
@@ -2481,5 +2481,130 @@ sub parallel_ordered
't|t',
"an answer of none leaves the caller's handle and datum as they were");
+# Bugs row 313: a concurrent build's validation forms no key of a row its
+# build's snapshot saw, which the build indexed, so a REINDEX CONCURRENTLY runs
+# an indexed row's expression once, as heap's validation does: the regress
+# suite's concur_reindex_ddl sequence gives the reference's one NOTICE and its
+# two errors.
+my (undef, undef, $ddl_err) = $node->psql(
+ 'postgres', q{
+CREATE TABLE concur_reindex_ddl_tab (i int);
+INSERT INTO concur_reindex_ddl_tab VALUES (1);
+CREATE FUNCTION concur_reindex_ddl_f() RETURNS void LANGUAGE plpgsql AS
+ 'begin DROP INDEX IF EXISTS public.concur_reindex_ddl_ind2; end';
+CREATE FUNCTION concur_reindex_ddl_im(int) RETURNS int
+ LANGUAGE plpgsql IMMUTABLE AS
+ 'begin perform public.concur_reindex_ddl_f(); return $1; end';
+CREATE INDEX concur_reindex_ddl_ind1 ON
+ concur_reindex_ddl_tab (concur_reindex_ddl_im(i));
+CREATE INDEX concur_reindex_ddl_ind2 ON concur_reindex_ddl_tab (i);
+REINDEX TABLE CONCURRENTLY concur_reindex_ddl_tab;
+REINDEX INDEX CONCURRENTLY concur_reindex_ddl_ind2;
+REINDEX INDEX CONCURRENTLY concur_reindex_ddl_ind1;
+REINDEX INDEX CONCURRENTLY concur_reindex_ddl_ind2;
+},
+ on_error_stop => 0,
+ extra_params => [ '-v', 'VERBOSITY=verbose' ]);
+is( join("\n", $ddl_err =~ /((?:NOTICE|ERROR): \w{5}: [^\n]*)/g),
+ join("\n",
+ 'NOTICE: 00000: index "concur_reindex_ddl_ind2" does not exist, skipping',
+ 'ERROR: 55006: cannot DROP INDEX "concur_reindex_ddl_ind2" because it is being used by active queries in this session',
+ 'ERROR: 42P01: relation "concur_reindex_ddl_ind2" does not exist'),
+ "a REINDEX CONCURRENTLY computes an indexed row's expression once, as heap's validation does"
+);
+
+# Bugs row 313: CREATE INDEX CONCURRENTLY and REINDEX CONCURRENTLY of a fork
+# btree expression index while another session, which held its lock across the
+# build, inserts while the command waits for writers before its validation:
+# the index is valid and holds every row once. The index's expression waits on
+# an advisory lock at one row, so the writer's lock comes after the build's
+# first wait.
+$node->safe_psql(
+ 'postgres', q{
+CREATE FUNCTION validated_at(k int) RETURNS int IMMUTABLE LANGUAGE plpgsql
+ AS $$ BEGIN IF k = 2 THEN PERFORM pg_advisory_xact_lock_shared(45); END IF; RETURN k; END $$;
+CREATE TABLE validated (k int);
+INSERT INTO validated SELECT generate_series(1, 3);
+});
+my $validated_locker = $node->background_psql('postgres');
+my $validated_builder = $node->background_psql('postgres');
+my $validated_writer = $node->background_psql('postgres');
+my $validated_first = 4;
+foreach my $statement (
+ 'CREATE INDEX CONCURRENTLY validated_k ON validated ((validated_at(k)))',
+ 'REINDEX INDEX CONCURRENTLY validated_k')
+{
+ $validated_locker->query_safe('SELECT pg_advisory_lock(45)');
+ $validated_builder->query_until(qr/start/, "\\echo start\n$statement;\n");
+ $node->poll_query_until('postgres',
+ q{SELECT EXISTS (SELECT 1 FROM pg_stat_activity WHERE wait_event = 'advisory' AND query ~ 'CONCURRENTLY')}
+ );
+ $validated_writer->query_safe(
+ 'BEGIN; LOCK TABLE validated IN ROW EXCLUSIVE MODE');
+ $validated_locker->query_safe('SELECT pg_advisory_unlock(45)');
+ $node->poll_query_until('postgres',
+ q{SELECT EXISTS (SELECT 1 FROM pg_stat_progress_create_index WHERE phase = 'waiting for writers before validation')}
+ );
+ my $last = $validated_first + 1;
+ $validated_writer->query_safe(
+ "INSERT INTO validated SELECT generate_series($validated_first, $last); COMMIT"
+ );
+ $validated_builder->query_safe('SELECT 1');
+ is( $node->safe_psql(
+ 'postgres',
+ "SET enable_seqscan = off; SET enable_bitmapscan = off; SELECT (SELECT indisvalid FROM pg_index WHERE indexrelid = 'validated_k'::regclass) || ' ' || string_agg(k::text, ',' ORDER BY k) FROM validated WHERE validated_at(k) > 0"
+ ),
+ 'true ' . join(',', 1 .. $last),
+ "$statement while another session inserts before its validation leaves a valid index holding every row once"
+ );
+ $validated_first = $last + 1;
+}
+$_->quit for $validated_locker, $validated_builder, $validated_writer;
+
$node->stop;
+
+# P2's index build and index move: a build whose snapshot predates another
+# transaction's committed insert of rows past a 4 MB delta_memory carries their
+# late entries to spill files under the writer's xid and completes; held
+# unfolded by an older snapshot, the index then moves into a tablespace with
+# those entries carried again, never waiting at the budget, and its storage
+# holds an entry for every row after the move and after a crash.
+my $moving_index = PostgreSQL::Test::Cluster->new('index_move');
+$moving_index->init;
+$moving_index->append_conf('postgresql.conf',
+ "delta_memory = 4MB\nfold_threshold = 1GB\nfold_timeout = 3600s\ncollector_naptime = 3600s\nallow_in_place_tablespaces = on"
+);
+$moving_index->start;
+$moving_index->safe_psql(
+ 'postgres', q{
+ CREATE EXTENSION doltgres_test;
+ CREATE TABLESPACE index_moved LOCATION '';
+ CREATE TABLE indexed (id int PRIMARY KEY, name text);
+ CREATE INDEX indexed_name ON indexed (name);
+});
+my $entries = sub {
+ scalar(() = $moving_index->safe_psql('postgres', q{SELECT doltgres_test_index_entries('indexed_name')}) =~ /^put$/mg);
+};
+my $index_older = $moving_index->background_psql('postgres');
+$index_older->query_safe('BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1');
+my $index_builder = $moving_index->background_psql('postgres');
+$index_builder->query_safe('BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1');
+$moving_index->safe_psql('postgres',
+ q{SELECT doltgres_test_write('indexed', true, 'INSERT INTO indexed SELECT g, repeat(md5(g::text), 31) FROM generate_series(1, 10000) g')},
+ timeout => 600);
+$index_builder->query_safe(q{SELECT doltgres_test_index_build('indexed', 'indexed_name')});
+$index_builder->query_safe('COMMIT');
+$index_builder->quit;
+is($entries->(), 10000, "a build at a 4 MB delta_memory carries every late entry of another transaction's rows");
+$moving_index->safe_psql('postgres',
+ q{SELECT doltgres_test_index_routine('indexed_name'); SELECT doltgres_test_index_move('indexed_name', 'index_moved')},
+ timeout => 600);
+is($entries->(), 10000, "an index move held unfolded carries every entry of another transaction past delta_memory");
+$index_older->query_safe('COMMIT');
+$index_older->quit;
+$moving_index->stop('immediate');
+$moving_index->start;
+is($entries->(), 10000, 'and the moved entries survive a crash');
+$moving_index->stop;
+
done_testing();
diff --git a/src/doltgres/test/t/046_update.pl b/src/doltgres/test/t/046_update.pl
index ae0999047d526..128170d9e5dc5 100644
--- a/src/doltgres/test/t/046_update.pl
+++ b/src/doltgres/test/t/046_update.pl
@@ -1073,4 +1073,200 @@ sub both
"an update carries its old row's lock to the new row's xmax as heap's does, with columns ($columns)");
}
+
+# A primary key WITHOUT OVERLAPS is an exclusion constraint's, so an insert or
+# an update that writes an existing row's key is refused with the exclusion
+# violation, 23P01, as heap's exclusion check refuses it (bugs row 317), not
+# with a unique index's 23505, the existing row folded into the base. On a
+# node of its own, with the fold launcher held, no fold worker left running,
+# and no other process's xmin or xid holding the horizon, the case's own fold
+# installs that base.
+my $overlap = PostgreSQL::Test::Cluster->new('overlap');
+$overlap->init;
+$overlap->start;
+$overlap->safe_psql('postgres',
+ "CREATE EXTENSION injection_points; CREATE EXTENSION doltgres_test; SELECT injection_points_attach('doltgres-fold-launch', 'wait')"
+);
+$overlap->poll_query_until('postgres',
+ "SELECT count(*) = 1 FROM pg_stat_activity WHERE wait_event = 'doltgres-fold-launch'"
+) or die 'the fold launcher is not held';
+$overlap->safe_psql(
+ 'postgres', q{
+CREATE TABLE overlapped (id int4range, valid_at daterange, CONSTRAINT overlapped_pkey PRIMARY KEY (id, valid_at WITHOUT OVERLAPS));
+INSERT INTO overlapped VALUES ('[1,2)', daterange('2000-01-01', '2000-02-15')), ('[1,2)', daterange('2001-01-01', '2002-01-01'));
+});
+$overlap->poll_query_until('postgres',
+ "SELECT count(*) = 0 FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND (backend_type = 'fold worker' OR backend_xmin IS NOT NULL OR backend_xid IS NOT NULL)"
+) or die 'a process holds the fold back';
+is( $overlap->safe_psql(
+ 'postgres', "SELECT doltgres_test_fold('overlapped')"),
+ 't',
+ 'a WITHOUT OVERLAPS primary key\'s table folds its rows into its base');
+my @refused;
+for my $statement (
+ "INSERT INTO overlapped VALUES ('[1,2)', daterange('2000-01-01', '2000-02-15'))",
+ "UPDATE overlapped SET valid_at = daterange('2000-01-01', '2000-04-01') WHERE id = '[1,2)'"
+ )
+{
+ my (undef, undef, $err) =
+ $overlap->psql('postgres', "\\set VERBOSITY verbose\n$statement");
+ push @refused,
+ join("\n",
+ grep { /^(?:psql::\d+: )?(?:ERROR|DETAIL):/ } split /\n/,
+ $err);
+}
+is_deeply(
+ \@refused,
+ [
+ qq{psql::2: ERROR: 23P01: conflicting key value violates exclusion constraint "overlapped_pkey"\nDETAIL: Key (id, valid_at)=([1,2), [2000-01-01,2000-02-15)) conflicts with existing key (id, valid_at)=([1,2), [2000-01-01,2000-02-15)).},
+ qq{psql::2: ERROR: 23P01: conflicting key value violates exclusion constraint "overlapped_pkey"\nDETAIL: Key (id, valid_at)=([1,2), [2000-01-01,2000-04-01)) conflicts with existing key (id, valid_at)=([1,2), [2000-01-01,2000-04-01)).}
+ ],
+ "an insert or an update of a WITHOUT OVERLAPS primary key's existing key is refused as heap's exclusion check refuses it"
+);
+$overlap->stop;
+
+# An update's flagged take of its replaced row's block, whose read a spill of the block's running writer moves
+# after it, finishes with the block's spilled entry, and an error at the block's read again fails only the update
+# (bugs row 342).
+my $flagging = PostgreSQL::Test::Cluster->new('flagging');
+$flagging->init(allows_streaming => 1);
+$flagging->append_conf('postgresql.conf',
+ "fold_threshold = 1GB\nfold_timeout = 3600s\nautovacuum = off");
+$flagging->start;
+$flagging->safe_psql('postgres',
+ 'CREATE EXTENSION injection_points; CREATE EXTENSION doltgres_test');
+$flagging->backup('flagging_backup');
+my $flagging_standby = PostgreSQL::Test::Cluster->new('flagging_standby');
+$flagging_standby->init_from_backup($flagging, 'flagging_backup',
+ has_streaming => 1);
+$flagging_standby->start;
+
+# flagging_made makes a table whose block 0, nearly full and left for block 1, holds a folded row, so an update to
+# 3000 bytes moves it to block 1.
+sub flagging_made
+{
+ my ($table) = @_;
+ $flagging->safe_psql(
+ 'postgres', qq{
+CREATE TABLE $table (k int4, v text);
+ALTER TABLE $table ALTER COLUMN v SET STORAGE PLAIN;
+INSERT INTO $table VALUES (1, repeat('v', 2500));
+INSERT INTO $table VALUES (4, repeat('v', 4500));
+INSERT INTO $table VALUES (3, repeat('v', 1500));
+SELECT doltgres_test_fold('$table');
+});
+}
+
+# flagging_held runs the update of a table's row 1 held after its flagged read, while the writer spills its takes on
+# block 0, its second turn's flagged read erring when error is set; it returns the update's errors.
+sub flagging_held
+{
+ my ($table, $error) = @_;
+ my $writer = $flagging->background_psql('postgres');
+ $writer->query_safe(
+ "BEGIN; INSERT INTO $table VALUES (2, repeat('v', 600))");
+ my $updater = $flagging->background_psql('postgres', on_error_stop => 0);
+ $updater->query_safe('SELECT injection_points_set_local()');
+ $updater->query_safe(
+ "SELECT injection_points_attach('doltgres-insert-flagged-read', 'wait')"
+ );
+ $updater->query_until(qr/started/,
+ "\\echo started\nUPDATE $table SET v = repeat('w', 3000) WHERE k = 1;\n"
+ );
+ $flagging->wait_for_event('client backend',
+ 'doltgres-insert-flagged-read');
+ $writer->query_safe('SELECT doltgres_test_delta(61, 0, 0)') for 1 .. 4;
+ $flagging->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-insert-flagged-read')");
+ $flagging->safe_psql('postgres',
+ "SELECT injection_points_attach('doltgres-insert-flagged-read', 'error')"
+ ) if $error;
+ $flagging->safe_psql('postgres',
+ "SELECT injection_points_wakeup('doltgres-insert-flagged-read')");
+ $updater->query('SELECT 1');
+ $flagging->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-insert-flagged-read')")
+ if $error;
+ my $errors = $updater->{stderr};
+ $updater->quit;
+ $writer->query_safe('COMMIT');
+ $writer->quit;
+ return $errors;
+}
+my $flagging_entry = q{SELECT doltgres_test_free_space('%s', 0)};
+
+flagging_made('flagged_spilled');
+flagging_held('flagged_spilled', 0);
+flagging_made('flagged_plain');
+$flagging->safe_psql(
+ 'postgres', q{
+INSERT INTO flagged_plain VALUES (2, repeat('v', 600));
+UPDATE flagged_plain SET v = repeat('w', 3000) WHERE k = 1;
+});
+is( $flagging->safe_psql(
+ 'postgres', sprintf($flagging_entry, 'flagged_spilled')),
+ $flagging->safe_psql(
+ 'postgres', sprintf($flagging_entry, 'flagged_plain')),
+ "an update's flagged take after a spill moved its block's takes leaves the entry an unspilled update leaves"
+);
+
+flagging_made('flagged_erred');
+my $erred = flagging_held('flagged_erred', 1);
+like(
+ $erred,
+ qr/doltgres-insert-flagged-read/,
+ "an error at the flagged block's read again fails the update");
+is( $flagging->safe_psql(
+ 'postgres', q{SELECT length(v) FROM flagged_erred WHERE k = 1}),
+ '2500',
+ "the failed update leaves its row as it was");
+$flagging->safe_psql('postgres',
+ q{UPDATE flagged_erred SET v = repeat('w', 3000) WHERE k = 1});
+flagging_made('flagged_undone');
+$flagging->safe_psql(
+ 'postgres', q{
+INSERT INTO flagged_undone VALUES (2, repeat('v', 600));
+BEGIN;
+UPDATE flagged_undone SET v = repeat('w', 3000) WHERE k = 1;
+ROLLBACK;
+UPDATE flagged_undone SET v = repeat('w', 3000) WHERE k = 1;
+});
+is( $flagging->safe_psql(
+ 'postgres', sprintf($flagging_entry, 'flagged_erred')),
+ $flagging->safe_psql(
+ 'postgres', sprintf($flagging_entry, 'flagged_undone')),
+ "a later update after the failed one leaves the entry a rolled-back update leaves"
+);
+
+$flagging->wait_for_catchup($flagging_standby);
+foreach my $table ('flagged_spilled', 'flagged_plain', 'flagged_erred',
+ 'flagged_undone')
+{
+ is( $flagging_standby->safe_psql(
+ 'postgres', sprintf($flagging_entry, $table)),
+ $flagging->safe_psql('postgres', sprintf($flagging_entry, $table)),
+ "the standby's entry of $table matches the primary's");
+}
+$flagging->safe_psql('postgres', 'CHECKPOINT');
+$flagging->safe_psql('postgres',
+ q{UPDATE flagged_plain SET v = repeat('x', 10) WHERE k = 2});
+$flagging->safe_psql('postgres',
+ q{UPDATE flagged_spilled SET v = repeat('x', 10) WHERE k = 2});
+$flagging->stop('immediate');
+$flagging->start;
+is( $flagging->safe_psql(
+ 'postgres', sprintf($flagging_entry, 'flagged_spilled')),
+ $flagging->safe_psql(
+ 'postgres', sprintf($flagging_entry, 'flagged_plain')),
+ "after a checkpoint and a crash the spilled update's entry matches the unspilled one's"
+);
+$flagging->wait_for_catchup($flagging_standby);
+is( $flagging_standby->safe_psql(
+ 'postgres', sprintf($flagging_entry, 'flagged_spilled')),
+ $flagging->safe_psql(
+ 'postgres', sprintf($flagging_entry, 'flagged_spilled')),
+ "and the standby's matches the primary's after the crash");
+$flagging_standby->stop;
+$flagging->stop;
+
done_testing();
diff --git a/src/doltgres/test/t/047_rewrite.pl b/src/doltgres/test/t/047_rewrite.pl
index 1e44f5a0242a1..eb616595c8cfe 100644
--- a/src/doltgres/test/t/047_rewrite.pl
+++ b/src/doltgres/test/t/047_rewrite.pl
@@ -1251,7 +1251,8 @@ sub vacuumed
"a new standby snapshot reads $table as heap's twin");
is($node->safe_psql('postgres', $stamp_read),
$stamp, "the move keeps the stamps of a row of $table updated to its own value");
- eval { $standby_older->query('COMMIT'); $standby_older->quit };
+ eval { $standby_older->query('COMMIT') };
+ $standby_older->quit;
}
$relocating->stop;
@@ -1904,4 +1905,148 @@ sub vacuumed
}
$node->stop;
+
+# P2 (bugs row 229): a rewrite's carry never waits on other writers' versions.
+# Session A holds a REPEATABLE READ snapshot from before B, so no fold applies
+# B's commit; B commits about 10 MB of rows past a 4 MB delta_memory, which its
+# own spills relieve; C's VACUUM FULL of the table carries B's versions to spill
+# files under B's xid and completes. A still reads the table as it was, a new
+# snapshot reads B's rows, and a crash keeps them.
+my $budgeted = PostgreSQL::Test::Cluster->new('carry_budget');
+$budgeted->init;
+# A count's parallel workers would read the table through heap, since the fork's
+# routine stands in the leader alone.
+$budgeted->append_conf('postgresql.conf',
+ "delta_memory = 4MB\nfold_threshold = 1GB\nfold_timeout = 3600s\ncollector_naptime = 3600s\nmax_parallel_workers_per_gather = 0"
+);
+$budgeted->start;
+$budgeted->safe_psql('postgres',
+ 'CREATE EXTENSION injection_points; CREATE EXTENSION doltgres_test; CREATE TABLE carried (k int4 PRIMARY KEY, v text)'
+);
+my $carried_count = rows('carried', 'SELECT count(*) FROM %');
+my $before_carry = $budgeted->background_psql('postgres');
+$before_carry->query_safe('BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1');
+$budgeted->safe_psql('postgres',
+ written('carried', "INSERT INTO % SELECT g, repeat(md5(g::text), 31) FROM generate_series(1, 10000) g"));
+$budgeted->safe_psql('postgres', rewritten('carried', undef, 'false'), timeout => 600);
+pass('a VACUUM FULL over another transaction\'s committed rows past delta_memory, held unfolded by a snapshot, completes');
+is($before_carry->query_safe($carried_count), '0', 'the older snapshot still reads none of the committed rows');
+$before_carry->query_safe('COMMIT');
+$before_carry->quit;
+is($budgeted->safe_psql('postgres', $carried_count), '10000', 'a new snapshot reads every row the carry spilled');
+$budgeted->stop('immediate');
+$budgeted->start;
+is($budgeted->safe_psql('postgres', $carried_count), '10000', 'the carried rows survive a crash');
+
+# A carry the server crashes in, after its files are written and before they
+# are listed: after the restart, whose redo replays the carry's records into
+# the new relfilenode its transaction left, the table reads as it was before
+# the rewrite, and a rewrite then carries every row.
+$before_carry = $budgeted->background_psql('postgres');
+$before_carry->query_safe('BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1');
+$budgeted->safe_psql('postgres',
+ written('carried', "INSERT INTO % SELECT g, repeat(md5(g::text), 31) FROM generate_series(10001, 20000) g"));
+$budgeted->safe_psql('postgres', "SELECT injection_points_attach('doltgres-carry-written', 'wait')");
+my $cut_short = $budgeted->background_psql('postgres', on_error_stop => 0);
+$cut_short->query_until(qr/start/, "\\echo start\n" . rewritten('carried', undef, 'false') . ";\n");
+$budgeted->wait_for_event('client backend', 'doltgres-carry-written');
+$budgeted->stop('immediate');
+eval { $cut_short->quit };
+eval { $before_carry->quit };
+$budgeted->start;
+is($budgeted->safe_psql('postgres', $carried_count), '20000', 'the table reads as it was before the rewrite');
+$budgeted->safe_psql('postgres', rewritten('carried', undef, 'false'), timeout => 600);
+is($budgeted->safe_psql('postgres', $carried_count), '20000', 'a rewrite after the restart carries every row');
+$budgeted->stop;
+
+# Ruling (b)'s case (iii): a moved table's base on a standby is its primary's,
+# root, fold point, and read point alike, its read point no higher than its fold
+# point.
+my $points = PostgreSQL::Test::Cluster->new('moved_points');
+$points->init(allows_streaming => 1);
+$points->append_conf('postgresql.conf',
+ "fold_threshold = 1GB\nfold_timeout = 3600s\ncollector_naptime = 3600s\nallow_in_place_tablespaces = on");
+$points->start;
+$points->safe_psql('postgres',
+ q{CREATE EXTENSION doltgres_test; CREATE TABLESPACE points_moved LOCATION ''; CREATE TABLE pointed (k int4 PRIMARY KEY, v text)});
+$points->safe_psql('postgres', written('pointed', "INSERT INTO % SELECT g, 'r' || g FROM generate_series(1, 2000) g"));
+$points->safe_psql('postgres', "SELECT doltgres_test_fold('pointed')");
+$points->backup('points_backup');
+my $points_standby = PostgreSQL::Test::Cluster->new('moved_points_standby');
+$points_standby->init_from_backup($points, 'points_backup', has_streaming => 1);
+$points_standby->append_conf('postgresql.conf', 'allow_in_place_tablespaces = on');
+$points_standby->start;
+$points->safe_psql('postgres', written('pointed', "INSERT INTO % SELECT g, 'r' || g FROM generate_series(2001, 2100) g"));
+$points->safe_psql('postgres', "SELECT doltgres_test_move('pointed', 'points_moved')");
+$points->wait_for_catchup($points_standby);
+my $primary_points = $points->safe_psql('postgres', "SELECT doltgres_test_base_points('pointed')");
+my ($root, $folded, $read) = split / /, $primary_points;
+ok($root ne '' && $read <= $folded, "a moved base's read point is no higher than its fold point");
+is($points_standby->safe_psql('postgres', "SELECT doltgres_test_base_points('pointed')"),
+ $primary_points, "a standby's base of a moved table has its primary's root, fold point, and read point");
+$points_standby->stop;
+$points->stop;
+
+# RevA's Q2 guard: a committed transaction's free space takes sit above those
+# of a writer begun after it, which commits while a snapshot taken before its
+# commit holds the table's horizon, so the move's fold keeps the takes above the
+# later writer's with its rows' out-of-line values; the move lowers the moved
+# base's read point below its fold point, and a rewrite before any fold carries
+# every row, so the table's rows and values equal heap's twin's, and no tuple id
+# is given twice.
+my $guarded = PostgreSQL::Test::Cluster->new('rewrite_guard');
+$guarded->init;
+$guarded->append_conf('postgresql.conf',
+ "fold_threshold = 1GB\nfold_timeout = 3600s\ncollector_naptime = 3600s\nallow_in_place_tablespaces = on\nmax_parallel_workers_per_gather = 0"
+);
+$guarded->start;
+$guarded->safe_psql(
+ 'postgres', q{
+CREATE EXTENSION doltgres_test;
+CREATE TABLESPACE guard_moved LOCATION '';
+CREATE TABLE guarded (k int4 PRIMARY KEY, v text);
+CREATE TABLE heap_guarded (k int4 PRIMARY KEY, v text);
+});
+my $long = "string_agg(md5(g::text || ':' || n::text), '') FROM generate_series(1, 120) n";
+for my $table ('guarded', 'heap_guarded')
+{
+ $guarded->safe_psql('postgres',
+ written($table, "INSERT INTO % SELECT g, 'r' || g FROM generate_series(1, 400) g"));
+}
+$guarded->safe_psql('postgres', "SELECT doltgres_test_fold('guarded')");
+my ($kept, $running) = map { $guarded->background_psql('postgres') } 1 .. 2;
+$kept->query_safe('BEGIN; SELECT pg_current_xact_id()');
+$running->query_safe('BEGIN');
+for my $table ('guarded', 'heap_guarded')
+{
+ $running->query_safe(written($table, "INSERT INTO % SELECT g, 'o' || g FROM generate_series(401, 405) g"));
+}
+for my $table ('guarded', 'heap_guarded')
+{
+ $kept->query_safe(written($table, "INSERT INTO % SELECT g, (SELECT $long) FROM generate_series(406, 430) g"));
+}
+$kept->query_safe('COMMIT');
+$kept->quit;
+my $holder = $guarded->background_psql('postgres');
+$holder->query_safe('BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1');
+$running->query_safe('COMMIT');
+$running->quit;
+$guarded->safe_psql('postgres', "SELECT doltgres_test_move('guarded', 'guard_moved')");
+$holder->query_safe('COMMIT');
+$holder->quit;
+my ($guard_root, $guard_folded, $guard_read) =
+ split / /, $guarded->safe_psql('postgres', "SELECT doltgres_test_base_points('guarded')");
+cmp_ok($guard_read, '<', $guard_folded,
+ "a move over takes its fold kept above a later writer's lowers the moved base's read point");
+$guarded->safe_psql('postgres', rewritten('guarded', undef, 'false'));
+my $guard_rows = "SELECT count(*) || ' ' || md5(string_agg(k || ':' || v, ',' ORDER BY k)) FROM %";
+my $unindexed = 'SET enable_indexscan = off; SET enable_indexonlyscan = off; SET enable_bitmapscan = off;';
+is($guarded->safe_psql('postgres', "$unindexed " . rows('guarded', $guard_rows)),
+ $guarded->safe_psql('postgres', rows('heap_guarded', $guard_rows)),
+ "a rewrite of the moved table before any fold carries every row and value as heap's twin holds them");
+is( $guarded->safe_psql('postgres',
+ q{SELECT count(*) = count(DISTINCT substring(r from '^(\S+) ')) FROM doltgres_test_scan('guarded', false, -1) r}),
+ 't', 'and gives no tuple id twice');
+$guarded->stop;
+
done_testing();
diff --git a/src/doltgres/test/t/050_fold.pl b/src/doltgres/test/t/050_fold.pl
index 6b7aea45adbe2..c723b55b40eb7 100644
--- a/src/doltgres/test/t/050_fold.pl
+++ b/src/doltgres/test/t/050_fold.pl
@@ -577,4 +577,87 @@ BEGIN
'the base and a read hold the eleven rows, the loaded ten not applied twice'
);
$raced->stop;
+
+# Bugs row 295: a temporary table's fold and index build are bounded by the
+# backend's own snapshots as well as by Postgres' temp horizon, which counts
+# no other session's transaction, so with an older transaction open elsewhere
+# no base of the table is folded past the snapshot that reads it: a read
+# through a new index returns the table's rows, and a fold across an open
+# cursor's snapshot leaves the rows the cursor's transaction began after.
+my $temped = PostgreSQL::Test::Cluster->new('temped');
+$temped->init;
+$temped->start;
+$temped->safe_psql('postgres', 'CREATE EXTENSION doltgres_test');
+my $older = $temped->background_psql('postgres');
+$older->query_safe('BEGIN; SELECT pg_current_xact_id()');
+my ($indexed_code, $indexed, $indexed_error) = $temped->psql(
+ 'postgres', q{
+CREATE TEMP TABLE temp_indexed (a int4);
+INSERT INTO temp_indexed VALUES (3), (1), (2);
+CREATE INDEX ON temp_indexed (a);
+SET enable_seqscan = off;
+SET enable_sort = off;
+SELECT a FROM temp_indexed ORDER BY a;
+});
+is($indexed . $indexed_error, "1\n2\n3",
+ "a read through a temporary table's new index returns its rows while an older transaction runs elsewhere");
+my ($cursored_code, $cursored, $cursored_error) = $temped->psql(
+ 'postgres', q{
+CREATE TEMP TABLE temp_cursored (a int4);
+INSERT INTO temp_cursored VALUES (3), (1), (2);
+CREATE INDEX ON temp_cursored (a);
+SET enable_seqscan = off;
+SET enable_sort = off;
+BEGIN;
+DECLARE c CURSOR FOR SELECT a FROM temp_cursored ORDER BY a;
+FETCH 1 FROM c;
+SELECT doltgres_test_fold('temp_cursored');
+FETCH ALL FROM c;
+COMMIT;
+SELECT a FROM temp_cursored ORDER BY a;
+});
+is($cursored . $cursored_error, "1\nf\n2\n3\n1\n2\n3",
+ "a fold across an open cursor on a temporary table leaves the rows its snapshot may not read as folded");
+$older->query_safe('COMMIT');
+$older->quit;
+
+# Bugs row 328: a fold of a keyless table whose ALTER TABLE ... SET STORAGE
+# EXTERNAL is pending applies its chain, keeping each entry's copies, those
+# of equal rows written before the ALTER and so re-keyed at its commit and
+# those written after it, and the table reads every copy back, where the
+# re-form refused an entry of more than one copy; the base's sizes count
+# every copy's row and heap bytes, as a fold with no chain to apply counts
+# the same rows written after the same ALTER.
+$temped->safe_psql(
+ 'postgres', q{
+CREATE TABLE stored_copies (k int4, v text);
+INSERT INTO stored_copies SELECT g % 3, repeat(chr(65 + g % 3), 3000) FROM generate_series(1, 6) g;
+ALTER TABLE stored_copies ALTER COLUMN v SET STORAGE EXTERNAL;
+INSERT INTO stored_copies SELECT g % 3, repeat(chr(65 + g % 3), 3000) FROM generate_series(7, 12) g;
+CREATE TABLE stored_after (k int4, v text);
+ALTER TABLE stored_after ALTER COLUMN v SET STORAGE EXTERNAL;
+SELECT doltgres_test_fold('stored_after');
+INSERT INTO stored_after SELECT g % 3, repeat(chr(65 + g % 3), 3000) FROM generate_series(1, 12) g;
+SELECT doltgres_test_fold('stored_after');
+});
+is( $temped->safe_psql(
+ 'postgres', q{SELECT doltgres_test_fold('stored_copies')}),
+ 't',
+ 'a fold of a keyless table with a pending SET STORAGE and equal rows installs a base'
+);
+is( $temped->safe_psql(
+ 'postgres',
+ q{SELECT k, count(*), min(length(v)), max(length(v)) FROM stored_copies GROUP BY k ORDER BY k}
+ ),
+ "0|4|3000|3000\n1|4|3000|3000\n2|4|3000|3000",
+ 'and the table reads every copy of its equal rows back');
+my $base_counts = sub {
+ return $temped->safe_psql('postgres',
+ "SELECT doltgres_test_base_sizes('$_[0]')") =~
+ s/^(\S+ \S+ \S+).*/$1/r;
+};
+is($base_counts->('stored_copies'), $base_counts->('stored_after'),
+ "and the base's heap bytes, rows, and entries count every copy, as a fold with no chain to apply counts them"
+);
+$temped->stop;
done_testing();
diff --git a/src/doltgres/test/t/053_spill.pl b/src/doltgres/test/t/053_spill.pl
index 8585e06cb9b87..bbdc1a4016ecf 100644
--- a/src/doltgres/test/t/053_spill.pl
+++ b/src/doltgres/test/t/053_spill.pl
@@ -267,11 +267,24 @@ sub spill_files
});
ok(spill_files($aborted) > 0,
"an aborted transaction's spill files stay until a checkpoint");
+# The versioned collector holds a reader while it sleeps (bugs row 255), which
+# keeps every deferred removal waiting, so the case ends it before its
+# checkpoints, the postmaster starting it again ten seconds later; this step
+# goes when row 255 is fixed.
+$node->safe_psql('postgres',
+ q{SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE backend_type = 'versioned collector'});
+$node->poll_query_until('postgres',
+ q{SELECT count(*) = 0 FROM pg_stat_activity WHERE backend_type = 'versioned collector'})
+ or die 'the versioned collector stayed';
+$node->safe_psql('postgres', 'CHECKPOINT') for 1 .. 2;
+isnt(spill_files($aborted), '0', "an aborted transaction's spill files stay through the second checkpoint after it");
+# A fold worker's reader, which the third checkpoint's removal rightly waits
+# for, ends with its fold first.
+$node->poll_query_until('postgres',
+ q{SELECT count(*) = 0 FROM pg_stat_activity WHERE backend_type = 'fold worker'})
+ or die 'a fold worker stayed';
$node->safe_psql('postgres', 'CHECKPOINT');
-$node->safe_psql('postgres', 'CHECKPOINT');
-is(spill_files($aborted), '0',
- "an aborted transaction's spill files go at the second checkpoint after it"
-);
+is(spill_files($aborted), '0', "and go at the third");
my $crashed = $node->safe_psql(
'postgres', q{
BEGIN;
@@ -281,9 +294,9 @@ sub spill_files
});
$node->stop('immediate');
$node->start;
-$node->safe_psql('postgres', 'CHECKPOINT');
+$node->safe_psql('postgres', 'CHECKPOINT') for 1 .. 2;
is(spill_files($crashed), '0',
- "a crash between an abort and a checkpoint leaves none of its spill files once a checkpoint after the start ran"
+ "a crash between an abort and a checkpoint leaves none of its spill files once two checkpoints after the start ran"
);
folded('swept');
@@ -355,7 +368,7 @@ sub spilled_in_savepoint
# A spill whose write fails between its slot and the end of its removal, in a
# savepoint: the versions it took out stay in the memtable too, another
-# session's conflict check still finds the key, and the commit's link of the
+# session's conflict check still finds the key, and the rollback's link of the
# pending file takes the memtable's twins out, so each row reads once.
my ($waited, $count) =
spilled_in_savepoint('pended', 'doltgres-spill-pended');
@@ -365,7 +378,7 @@ sub spilled_in_savepoint
"a spill failed after its slot leaves its key to another writer's conflict check"
);
is($count, '3000',
- "the commit links a spill that failed after its slot, its memtable twins taken out"
+ "the rollback links a spill that failed after its slot, its memtable twins taken out"
);
folded('pended');
@@ -655,6 +668,73 @@ sub chunk_listed
'a test insert of a source past shared_buffers completes on a node made for streaming');
$pinned->stop;
+# Commit 4's case: a statement inserting 300,000 rows at a 4 MB delta_memory
+# completes, its spills dropping their rows' tuple ids from the storage's tuple
+# id table, whose compaction leaves those slots and their keys' bytes behind,
+# and every row reads back.
+my $compacted = PostgreSQL::Test::Cluster->new('spill_compacted');
+$compacted->init;
+$compacted->append_conf('postgresql.conf',
+ "delta_memory = 4MB\nfold_threshold = 1GB\nfold_timeout = 3600s\nmax_parallel_workers_per_gather = 0");
+$compacted->start;
+$compacted->safe_psql(
+ 'postgres', q{
+CREATE EXTENSION doltgres_test;
+CREATE TABLE many_source (v text);
+INSERT INTO many_source SELECT 'r' || i FROM generate_series(1, 300000) i;
+CREATE TABLE many (v text);
+});
+$compacted->safe_psql('postgres',
+ q{SELECT count(*) FROM doltgres_test_insert('many', 'many_source', true, 0)}, timeout => 1200);
+is($compacted->safe_psql('postgres', q{SELECT count(*) FROM doltgres_test_scan('many', false, -1)}),
+ '300000', 'a statement inserting 300,000 rows at a 4 MB delta_memory completes and every row reads back');
+$compacted->stop;
+
+# A standby's replay of an aborted transaction's spills: at a 4 MB delta_memory
+# a standby replays a transaction whose rows and free space map takes pass its
+# budget, which its redo spills, and then the transaction's abort, which keeps
+# the free space map spill marked with the abort's end until a fold takes it;
+# the standby reads the table as its primary does after a committed insert,
+# after the primary's fold, and after the standby's restart, once it folded at
+# its primary's point and its primary's checkpoint gave it a restartpoint, which
+# no fold point a storage holds unfolded lets it pass (bugs row 158).
+my $aborting = PostgreSQL::Test::Cluster->new('spill_aborting');
+$aborting->init(allows_streaming => 1);
+$aborting->append_conf('postgresql.conf',
+ "delta_memory = 4MB\nfold_threshold = 1GB\nfold_timeout = 3600s\nmax_parallel_workers_per_gather = 0");
+$aborting->start;
+$aborting->safe_psql(
+ 'postgres', q{
+CREATE EXTENSION doltgres_test;
+CREATE TABLE aborted_source (v text);
+INSERT INTO aborted_source SELECT 'r' || i FROM generate_series(1, 100000) i;
+CREATE TABLE aborted_rows (v text);
+});
+$aborting->backup('aborting_backup');
+my $aborting_standby = PostgreSQL::Test::Cluster->new('spill_aborting_standby');
+$aborting_standby->init_from_backup($aborting, 'aborting_backup', has_streaming => 1);
+$aborting_standby->append_conf('postgresql.conf', "delta_memory = 4MB\nmax_parallel_workers_per_gather = 0");
+$aborting_standby->start;
+$aborting->safe_psql('postgres',
+ q{BEGIN; SELECT count(*) FROM doltgres_test_insert('aborted_rows', 'aborted_source', true, 0); ROLLBACK;},
+ timeout => 1200);
+$aborting->safe_psql('postgres',
+ q{SELECT count(*) FROM doltgres_test_insert('aborted_rows', 'aborted_source', true, 0)}, timeout => 1200);
+$aborting->wait_for_catchup($aborting_standby);
+my $aborted_count = q{SELECT count(*) FROM doltgres_test_scan('aborted_rows', false, -1)};
+is($aborting_standby->safe_psql('postgres', $aborted_count),
+ '100000', "a standby that replayed an aborted transaction's spills reads only the committed rows");
+$aborting->safe_psql('postgres', q{SELECT doltgres_test_fold('aborted_rows')});
+$aborting->wait_for_catchup($aborting_standby);
+is($aborting_standby->safe_psql('postgres', $aborted_count), '100000', "and after the primary's fold");
+$aborting_standby->safe_psql('postgres', q{SELECT doltgres_test_fold('aborted_rows')});
+$aborting->safe_psql('postgres', 'CHECKPOINT');
+$aborting->wait_for_catchup($aborting_standby);
+$aborting_standby->restart;
+is($aborting_standby->safe_psql('postgres', $aborted_count), '100000', "and after the standby's restart");
+$aborting_standby->stop;
+$aborting->stop;
+
# Bugs row 74 through the real commands: one INSERT ... SELECT whose own
# versions pass the default 32 MB of delta_memory completes, its running
# transaction spilling them (the delta's drain), and every row reads back.
@@ -940,49 +1020,6 @@ sub written
$start, "the delta's used bytes are back at their start once both ended and folded");
$small->stop;
-# Bugs row 242: an UPDATE ... FROM through a nested loop whose outer scan reads
-# a source whose rows its writer spilled, held at its first read of the
-# source's spills while a fold of the source installs a base and drops the
-# spills, updates every row, as heap's twin's does.
-my %joined;
-for my $am ('', 'USING doltgres_twin')
-{
- my $p = $am eq '' ? '' : 'heap_';
- $node->safe_psql(
- 'postgres', qq{
-CREATE TABLE ${p}joined_source (k int4, v text) $am;
-INSERT INTO ${p}joined_source SELECT g, repeat('s', 100) FROM generate_series(1, 40000) g;
-CREATE TABLE ${p}joined_target (k int4 PRIMARY KEY, v text) $am;
-INSERT INTO ${p}joined_target SELECT g, 't' FROM generate_series(1, 40000) g;
-});
- my $plan = "SET enable_hashjoin = off; SET enable_mergejoin = off; SET enable_material = off;"
- . " SET max_parallel_workers_per_gather = 0;";
- my $update = "UPDATE ${p}joined_target t SET v = s.v || 'u' FROM ${p}joined_source s WHERE t.k = s.k";
- if ($am eq '')
- {
- my $updater = $node->background_psql('postgres');
- $updater->query_safe(
- "$plan SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-spill-slots-read', 'wait')"
- );
- $updater->query_until(qr/started/, "\\echo started\n$update;\n");
- $node->wait_for_event('client backend', 'doltgres-spill-slots-read');
- $node->safe_psql('postgres', "SELECT doltgres_test_fold('joined_source')");
- $node->safe_psql('postgres',
- "SELECT injection_points_detach('doltgres-spill-slots-read'); SELECT injection_points_wakeup('doltgres-spill-slots-read')"
- );
- $updater->query_safe('SELECT 1');
- $updater->quit;
- }
- else
- {
- $node->safe_psql('postgres', "$plan $update");
- }
- $joined{$p} = $node->safe_psql('postgres',
- "SELECT count(*) || ' ' || md5(string_agg(k || v, ',' ORDER BY k)) FROM ${p}joined_target");
-}
-is($joined{''}, $joined{heap_},
- "an UPDATE ... FROM whose source scan a fold's drop passes under updates every row as heap's twin's");
-
# Bugs row 286: a helper spilling an idle transaction's versions pauses after
# taking them out of its memtable and before listing its file, and the
# transaction commits; a second transaction's upsert of the same keys, which
@@ -1033,4 +1070,125 @@ sub written
"a helper's spill listed after the helped transaction's commit leaves a later transaction's spill of the same keys newer"
);
+# Bugs row 307: a spill's take-out of versions from the memtable pends the
+# spill, which readers read, before the versions leave the memtable. With the
+# budget's committed arm paused at doltgres-spill-pended, its spill of a
+# keyless table's committed inserts pended and every one still in the
+# memtable, a fold of the table waits for the take-out's end and applies each
+# insert once. Then, with a running transaction's inserts spilled so that
+# readers list the table's spills, a count paused at
+# doltgres-spill-slots-read, having read the first key's chain, across a
+# whole take-out of the table's newer committed inserts reads each row once.
+my $taken = PostgreSQL::Test::Cluster->new('taken');
+$taken->init;
+$taken->append_conf('postgresql.conf',
+ "fold_threshold = 1GB\nfold_timeout = 3600s\nautovacuum = off");
+$taken->start;
+$taken->safe_psql('postgres',
+ 'CREATE EXTENSION injection_points; CREATE EXTENSION doltgres_test; CREATE TABLE taken_once (k int4, v text)'
+);
+$taken->safe_psql('postgres',
+ "INSERT INTO taken_once SELECT n, repeat('t', 100) FROM generate_series(1, 1000) n"
+);
+my $spiller = $taken->background_psql('postgres');
+$spiller->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-spill-pended', 'wait')"
+);
+$spiller->query_until(qr/spilling/,
+ "\\echo spilling\nSELECT doltgres_test_delta(61, 0, 0);\n");
+$taken->wait_for_event('client backend', 'doltgres-spill-pended');
+my $folder = $taken->background_psql('postgres');
+my $folder_pid = $folder->query_safe('SELECT pg_backend_pid()');
+$folder->query_until(qr/folding/,
+ "\\echo folding\nSELECT doltgres_test_fold('taken_once');\n");
+$taken->poll_query_until('postgres',
+ "SELECT count(*) = 1 FROM pg_stat_activity WHERE pid = $folder_pid AND (state = 'idle' OR wait_event = 'Extension')"
+) or die 'the fold neither ended nor waited';
+$taken->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-spill-pended'); SELECT injection_points_wakeup('doltgres-spill-pended')"
+);
+$spiller->query_safe('SELECT 1');
+$folder->query_safe('SELECT 1');
+is( $taken->safe_psql(
+ 'postgres',
+ q{SELECT count(*) || ' ' || count(DISTINCT k) || ' ' || (split_part(doltgres_test_count('taken_once'), ' ', 2) = split_part(doltgres_test_count('taken_once'), ' ', 6)) FROM taken_once}
+ ),
+ '1000 1000 true',
+ 'a fold that read a keyless table while a take-out of its inserts was pended applies each insert once'
+);
+$taken->safe_psql('postgres',
+ "INSERT INTO taken_once SELECT n, repeat('t', 100) FROM generate_series(1001, 2000) n"
+);
+my $running = $taken->background_psql('postgres');
+$running->query_safe('BEGIN');
+$running->query_safe(
+ "INSERT INTO taken_once SELECT n, repeat('t', 100) FROM generate_series(2001, 2100) n"
+);
+is($taken->safe_psql('postgres', 'SELECT doltgres_test_delta(61, 0, 0)'),
+ '1', "a helper spills the running transaction's inserts, so the table's spills are listed");
+my $counter = $taken->background_psql('postgres');
+$counter->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-spill-slots-read', 'wait')"
+);
+$counter->query_until(qr/counting/,
+ "\\echo counting\nSELECT count(*) FROM taken_once;\n");
+$taken->wait_for_event('client backend', 'doltgres-spill-slots-read');
+is($taken->safe_psql('postgres', 'SELECT doltgres_test_delta(61, 0, 0)'),
+ '1', "the committed arm takes the newer inserts out while the count waits");
+$taken->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-spill-slots-read'); SELECT injection_points_wakeup('doltgres-spill-slots-read')"
+);
+like($counter->query_safe('SELECT 1'),
+ qr/^2000$/m,
+ 'a count that read a key before a whole take-out of it and listed the spills after reads it once');
+$running->query_safe('ROLLBACK');
+$running->quit;
+$spiller->quit;
+$folder->quit;
+$counter->quit;
+
+# A take-out that a top-level error stops between its spill's pend and its
+# versions' removal ends at the transaction's abort, and one whose backend is
+# terminated there ends at its exit, so another session's read of the table
+# finishes and reads each row once.
+$taken->safe_psql('postgres', "SELECT doltgres_test_fold('taken_once')");
+$taken->safe_psql('postgres',
+ "CREATE TABLE taken_ended (k int4, v text); INSERT INTO taken_ended SELECT n, repeat('t', 100) FROM generate_series(1, 1000) n"
+);
+my ($code, $out, $err) = $taken->psql('postgres',
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-spill-pended', 'error'); SELECT doltgres_test_delta(61, 0, 0)"
+);
+like($err, qr/error triggered for injection point doltgres-spill-pended/,
+ "the committed arm's take-out fails between its pend and its removal");
+is( $taken->safe_psql(
+ 'postgres',
+ "SET statement_timeout = '60s'; SELECT count(*) FROM taken_ended"),
+ '1000',
+ 'a read after a take-out a top-level error stopped finishes and reads each row once'
+);
+$taken->safe_psql('postgres', "SELECT doltgres_test_fold('taken_ended')");
+$taken->safe_psql('postgres',
+ "CREATE TABLE taken_exited (k int4, v text); INSERT INTO taken_exited SELECT n, repeat('t', 100) FROM generate_series(1, 1000) n"
+);
+my $exiting = $taken->background_psql('postgres', on_error_stop => 0);
+my $exiting_pid = $exiting->query_safe('SELECT pg_backend_pid()');
+$exiting->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-spill-pended', 'wait')"
+);
+$exiting->query_until(qr/exiting/,
+ "\\echo exiting\nSELECT doltgres_test_delta(61, 0, 0);\n");
+$taken->wait_for_event('client backend', 'doltgres-spill-pended');
+$taken->safe_psql('postgres', "SELECT pg_terminate_backend($exiting_pid)");
+$taken->poll_query_until('postgres',
+ "SELECT count(*) = 0 FROM pg_stat_activity WHERE pid = $exiting_pid")
+ or die 'the terminated backend never exited';
+$exiting->quit;
+is( $taken->safe_psql(
+ 'postgres',
+ "SET statement_timeout = '60s'; SELECT count(*) FROM taken_exited"),
+ '1000',
+ 'a read after a take-out whose backend was terminated finishes and reads each row once'
+);
+$taken->stop;
+
done_testing();
diff --git a/src/doltgres/test/t/057_identity.pl b/src/doltgres/test/t/057_identity.pl
index dc82ccaed41f6..1d34a082c038a 100644
--- a/src/doltgres/test/t/057_identity.pl
+++ b/src/doltgres/test/t/057_identity.pl
@@ -387,4 +387,41 @@ sub forked
"a rewrite moves the table's TOAST pair to its new TOAST relation");
$node->stop;
+
+# Bugs row 338: a DROP TABLE of a table whose row type grew by 200 columns
+# removes its 404 identity entries in groups the budget can give, at 8MB and
+# at 1MB, where one prepare asked more pages than the whole budget holds and
+# waited forever.
+foreach my $memory ('8MB', '1MB')
+{
+ my $grown = PostgreSQL::Test::Cluster->new("grown_$memory");
+ $grown->init;
+ $grown->append_conf('postgresql.conf', "delta_memory = $memory");
+ $grown->start;
+ $grown->safe_psql(
+ 'postgres', q{
+CREATE TABLE compositetable_type (a int);
+CREATE TABLE compositetable_holder (id int, c compositetable_type);
+INSERT INTO compositetable_holder VALUES (1, ROW(1)::compositetable_type);
+DO $$
+BEGIN
+ FOR i IN 1..200 LOOP
+ EXECUTE format('ALTER TABLE compositetable_type ADD COLUMN x%s bigint', i);
+ END LOOP;
+END $$;
+DROP TABLE compositetable_holder;
+});
+ my ($code, $out, $err) = $grown->psql(
+ 'postgres', q{
+SET statement_timeout = '120s';
+DROP TABLE compositetable_type;
+SELECT count(*) FROM pg_class WHERE relname = 'compositetable_type';
+});
+ is( $err . $out,
+ '0',
+ "a DROP TABLE of a table grown by 200 columns finishes at delta_memory $memory"
+ );
+ $grown->stop;
+}
+
done_testing();
diff --git a/src/doltgres/test/t/062_fold_trigger.pl b/src/doltgres/test/t/062_fold_trigger.pl
index 53ff367d1e2b8..c4766d864dcb1 100644
--- a/src/doltgres/test/t/062_fold_trigger.pl
+++ b/src/doltgres/test/t/062_fold_trigger.pl
@@ -767,13 +767,21 @@ BEGIN
# 062's budget case (bugs rows 111 and 160) through the real commands: with
# delta_memory at 1 MB and neither of the fold's thresholds met, committed
# writes far past it finish, a writer refused at the budget asking for folds
-# whose drops give the delta back and wake it.
+# whose drops give the delta back and wake it. A count right after the last
+# commit, held after it read the table's spill slots while a fold installs a
+# base that applies the spilled rows and drops them from the delta, moves to
+# that base at its next read and counts every row (bugs row 241); the fold
+# launcher is held from before the count, so no fold but the case's own
+# installs a base.
my $budget = PostgreSQL::Test::Cluster->new('budget');
$budget->init;
$budget->append_conf('postgresql.conf',
- "delta_memory = 1MB\nfold_threshold = '1GB'\nfold_timeout = 3600s");
+ "delta_memory = 1MB\nfold_threshold = '1GB'\nfold_timeout = 3600s\nautovacuum = off"
+);
$budget->start;
-$budget->safe_psql('postgres', 'CREATE TABLE committed (k int4, v text)');
+$budget->safe_psql('postgres',
+ 'CREATE EXTENSION injection_points; CREATE EXTENSION doltgres_test; CREATE TABLE committed (k int4, v text)'
+);
my $refused = 0;
for my $batch (0 .. 49)
{
@@ -785,8 +793,185 @@ BEGIN
}
is($refused, 0,
"fifty committed writes past a 1 MB delta_memory finish with neither of the fold's thresholds met");
+$budget->safe_psql('postgres',
+ "SELECT injection_points_attach('doltgres-fold-launch', 'wait')");
+$budget->poll_query_until('postgres',
+ q{SELECT count(*) = 1 FROM pg_stat_activity WHERE wait_event = 'doltgres-fold-launch'}
+) or die 'the fold launcher never waited';
+$budget->poll_query_until('postgres',
+ q{SELECT count(*) = 0 FROM pg_stat_activity WHERE backend_type = 'fold worker'}
+) or die 'a fold worker kept running';
+my $counter = $budget->background_psql('postgres');
+$counter->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-spill-slots-read', 'wait')"
+);
+$counter->query_until(qr/held/,
+ "\\echo held\nSELECT count(*) FROM committed;\n");
+$budget->wait_for_event('client backend', 'doltgres-spill-slots-read');
+is( $budget->safe_psql('postgres', "SELECT doltgres_test_fold('committed')"),
+ 't',
+ 'a fold under the held count installs a base');
+$budget->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-fold-launch'); SELECT injection_points_wakeup('doltgres-fold-launch')"
+);
+$budget->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-spill-slots-read'); SELECT injection_points_wakeup('doltgres-spill-slots-read')"
+);
+my $counted = $counter->query_safe('SELECT 1');
+$counter->quit;
+like($counted, qr/^50000$/m,
+ 'a count right after the last commit, held across a fold that applied its spilled rows, counts every row'
+);
is($budget->safe_psql('postgres', 'SELECT count(*) FROM committed'),
'50000', 'and every row reads back');
$budget->stop;
+# Bugs row 306: a fold worker held inside its fold, its snapshot taken, keeps
+# neither CREATE INDEX CONCURRENTLY nor REINDEX TABLE CONCURRENTLY of other
+# tables waiting for it, as a VACUUM doesn't.
+my $built = PostgreSQL::Test::Cluster->new('built');
+$built->init;
+$built->append_conf('postgresql.conf',
+ "fold_threshold = 16kB\nfold_timeout = 3600s\nautovacuum = off");
+$built->start;
+$built->safe_psql(
+ 'postgres', q{
+CREATE EXTENSION injection_points;
+CREATE EXTENSION doltgres_test;
+CREATE TABLE indexed (k int4);
+INSERT INTO indexed SELECT generate_series(1, 100);
+CREATE TABLE reindexed (k int4);
+INSERT INTO reindexed SELECT generate_series(1, 100);
+CREATE INDEX reindexed_k ON reindexed (k);
+CREATE TABLE held (k int4, v text);
+SELECT injection_points_attach('doltgres-fold-relation', 'wait');
+});
+$built->safe_psql('postgres',
+ "SELECT doltgres_test_write('held', true, 'INSERT INTO held SELECT n, repeat(''h'', 200) FROM generate_series(1, 200) n')"
+);
+$built->wait_for_event('fold worker', 'doltgres-fold-relation');
+is( $built->safe_psql(
+ 'postgres',
+ q{SELECT count(*) FROM pg_stat_activity WHERE backend_type = 'fold worker' AND wait_event = 'doltgres-fold-relation' AND backend_xmin IS NOT NULL}
+ ),
+ '1',
+ 'the held fold worker has its snapshot');
+my ($built_ret, $built_out, $built_err) = $built->psql('postgres',
+ "SET lock_timeout = '30s'; CREATE INDEX CONCURRENTLY indexed_k ON indexed (k)"
+);
+is($built_ret, 0,
+ "CREATE INDEX CONCURRENTLY doesn't wait for a fold worker held inside its fold"
+);
+($built_ret, $built_out, $built_err) = $built->psql('postgres',
+ "SET lock_timeout = '30s'; REINDEX TABLE CONCURRENTLY reindexed");
+is($built_ret, 0,
+ "REINDEX TABLE CONCURRENTLY doesn't wait for a fold worker held inside its fold"
+);
+$built->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-fold-relation'); SELECT injection_points_wakeup('doltgres-fold-relation')"
+);
+ok( $built->poll_query_until(
+ 'postgres',
+ "SELECT split_part(doltgres_test_base_sizes('held'), ' ', 2) = '200'"
+ ),
+ 'the released fold installs its base');
+is( $built->safe_psql(
+ 'postgres',
+ 'SET enable_seqscan = off; SELECT count(*) FROM indexed WHERE k <= 50; SELECT count(*) FROM reindexed WHERE k <= 50'
+ ),
+ "50\n50",
+ 'and the built and the rebuilt index read their tables');
+
+# A CREATE INDEX CONCURRENTLY of the very table a held fold worker folds, its
+# snapshot older than the deletes and inserts made before the build, doesn't
+# wait for it, and once the fold installs its base the new index gives every
+# row a sequential scan gives.
+$built->safe_psql(
+ 'postgres', q{
+CREATE TABLE own (k int4, v text);
+INSERT INTO own SELECT n, 'o' FROM generate_series(1, 100) n;
+SELECT injection_points_attach('doltgres-fold-relation', 'wait');
+});
+$built->safe_psql('postgres',
+ "SELECT doltgres_test_write('own', true, 'INSERT INTO own SELECT n, repeat(''o'', 200) FROM generate_series(101, 300) n')"
+);
+$built->wait_for_event('fold worker', 'doltgres-fold-relation');
+$built->safe_psql('postgres',
+ q{DELETE FROM own WHERE k <= 20; INSERT INTO own SELECT n, 'n' FROM generate_series(301, 350) n}
+);
+($built_ret, $built_out, $built_err) = $built->psql('postgres',
+ "SET lock_timeout = '30s'; CREATE INDEX CONCURRENTLY own_k ON own (k)");
+is($built_ret, 0,
+ "CREATE INDEX CONCURRENTLY of a table a held fold worker folds doesn't wait for it"
+);
+$built->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-fold-relation'); SELECT injection_points_wakeup('doltgres-fold-relation')"
+);
+ok( $built->poll_query_until(
+ 'postgres',
+ q{SELECT count(*) = 0 FROM pg_stat_activity WHERE backend_type = 'fold worker'}
+ ),
+ "the table's released fold ends");
+my $counted_rows = q{SELECT count(*) || ' ' || sum(k) FROM own WHERE k > 0};
+my $by_index =
+ "SET enable_seqscan = off; SET enable_bitmapscan = off; $counted_rows";
+my $by_scan =
+ "SET enable_indexscan = off; SET enable_bitmapscan = off; SET enable_indexonlyscan = off; $counted_rows";
+is($built->safe_psql('postgres', $by_index),
+ '330 61215', 'and the new index gives every row');
+is($built->safe_psql('postgres', $by_scan),
+ '330 61215', 'as a sequential scan does');
+$built->safe_psql('postgres',
+ "SELECT doltgres_test_fold('own'); SELECT doltgres_test_index_fold('own_k')"
+);
+is($built->safe_psql('postgres', $by_index),
+ '330 61215',
+ 'and still does once the table and the index fold every row');
+$built->stop;
+
+# A replayed free's take-out passes over a later take of its tuple id (bugs
+# row 320): with a standby's launcher held, the primary folds three rounds of
+# a comparator-keyed table, each deleting a row and freeing its tuple id at a
+# VACUUM, the next round's insert taking that tuple id again on another key,
+# and a fourth round's take of it stays live. Once the launcher runs, the
+# standby folds each point and does each free's deferred take-outs past it,
+# which meet the later takes; every root matches its primary's at each fold,
+# and after a last VACUUM every row reads back as on the primary.
+$node->safe_psql('postgres',
+ "ALTER SYSTEM SET fold_threshold = '1GB'; ALTER SYSTEM SET fold_timeout = '3600s'; SELECT pg_reload_conf()"
+);
+$node->backup('retaken');
+my $retaken = PostgreSQL::Test::Cluster->new('retaken');
+$retaken->init_from_backup($node, 'retaken', has_streaming => 1);
+$retaken->start;
+ok($held_launcher->($retaken), "the retaken standby's launcher is held");
+$node->safe_psql('postgres',
+ 'CREATE TABLE retaken_keys (k ranged PRIMARY KEY, v text)');
+$folds->('retaken_keys', 3);
+$node->safe_psql('postgres',
+ "SELECT doltgres_test_write('retaken_keys', true, 'INSERT INTO retaken_keys SELECT ranged(n, n + 1), ''r'' FROM generate_series(31, 40) n')"
+);
+$node->safe_psql('postgres', "SELECT doltgres_test_fold('retaken_keys')");
+$node->safe_psql('postgres',
+ "SELECT doltgres_test_vacuum('retaken_keys', 'auto')");
+$node->wait_for_replay_catchup($retaken);
+$offset = -s $retaken->logfile;
+$run_launcher->($retaken);
+ok( $same_base->($retaken, 'retaken_keys'),
+ "a lagging standby whose deferred take-outs meet later takes of their tuple ids folds each point to its primary's roots"
+);
+ok( !$retaken->log_contains(qr/does not match its primary's/, $offset),
+ "the retaken standby's rows, tuple id, free space, and pending tuple id roots match each record's"
+);
+$node->safe_psql('postgres',
+ "SELECT doltgres_test_vacuum('retaken_keys', 'auto')");
+$node->wait_for_replay_catchup($retaken);
+my $retaken_rows =
+ q{SELECT doltgres_test_standing('retaken_keys', 'SELECT string_agg(lower(k)::text, '' '' ORDER BY lower(k)) FROM retaken_keys')};
+is( $retaken->safe_psql('postgres', $retaken_rows),
+ $node->safe_psql('postgres', $retaken_rows),
+ "after a last VACUUM every row, the live retake's included, reads back on the standby as on its primary"
+);
+$retaken->stop;
+
done_testing();
diff --git a/src/doltgres/test/t/062_size_sites.pl b/src/doltgres/test/t/062_size_sites.pl
index e5968b0afcdf5..c306916ea1880 100644
--- a/src/doltgres/test/t/062_size_sites.pl
+++ b/src/doltgres/test/t/062_size_sites.pl
@@ -55,9 +55,12 @@
);
is( $node->safe_psql(
'postgres', q{
-SELECT pg_relation_size('sized', 'fsm'), pg_relation_size('sized', 'vm'), pg_relation_size('sized', 'init')}),
- '0|0|0',
- 'a fork table\'s other forks have no size');
+SELECT pg_relation_size('sized', 'fsm')
+ = coalesce((pg_stat_file(pg_relation_filepath('sized') || '_fsm', true)).size, 0),
+ pg_relation_size('sized', 'vm'), pg_relation_size('sized', 'init')}),
+ 't|0|0',
+ 'a fork table\'s free space map fork is its real pages, and its other forks have no size'
+);
is( $node->safe_psql(
'postgres', q{
SELECT pg_relation_size('sized_k') % 8192 = 0 AND pg_relation_size('sized_k') >= 16384,
@@ -420,5 +423,223 @@ sub sized
is(sized($standby, 'stood'), $primary_sizes, 'a promoted standby\'s sizes equal its primary\'s');
$standby->stop;
+# Bugs rows 240 and 316: a commit's totals, held where the commit reads its
+# storage's base before a fold installs a base holding another transaction's
+# rows and drops their versions from the delta, move by what the commit's
+# rows change, so a DELETE of those rows leaves a keyless table's totals, and
+# an UPDATE that keeps their values leaves a TOAST relation's, equal to a fresh
+# count; and a reader's count of totals a rewrite left unknown, held where
+# the count reads the base before such a fold, counts them again to a fresh
+# count.
+sub launcher_held
+{
+ $node->safe_psql('postgres',
+ "SELECT injection_points_attach('doltgres-fold-launch', 'wait')");
+ $node->poll_query_until('postgres',
+ q{SELECT count(*) = 1 FROM pg_stat_activity WHERE wait_event = 'doltgres-fold-launch'}
+ ) or die 'the fold launcher never waited';
+ $node->poll_query_until('postgres',
+ q{SELECT count(*) = 0 FROM pg_stat_activity WHERE backend_type = 'fold worker'}
+ ) or die 'a fold worker kept running';
+}
+
+sub launcher_freed
+{
+ $node->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-fold-launch'); SELECT injection_points_wakeup('doltgres-fold-launch')"
+ );
+}
+
+sub held_fold
+{
+ my ($table, $point, $before, $held) = @_;
+ launcher_held();
+ $node->safe_psql('postgres', $before);
+ my $session = $node->background_psql('postgres', on_error_stop => 0);
+ $session->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('$point', 'wait')"
+ );
+ $session->query_until(qr/held/, "\\echo held\n$held;\n");
+ $node->wait_for_event('client backend', $point);
+ is($node->safe_psql('postgres', "SELECT doltgres_test_fold('$table')"),
+ 't', "a fold under a backend held at $point installs a base");
+ launcher_freed();
+ $node->safe_psql('postgres',
+ "SELECT injection_points_detach('$point'); SELECT injection_points_wakeup('$point')"
+ );
+ $session->query('SELECT 1');
+ $session->quit;
+}
+held_fold(
+ 'moved_rows',
+ 'doltgres-commit-based',
+ q{CREATE TABLE moved_rows (k int4, v text);
+INSERT INTO moved_rows SELECT g, 'v' || g FROM generate_series(1, 100) g},
+ 'BEGIN; DELETE FROM moved_rows WHERE k <= 50; COMMIT');
+is(totals('moved_rows'), count('moved_rows'),
+ 'a commit held across a fold that applied the rows it deletes moves a keyless table\'s totals to a fresh count'
+);
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE moved_values (k int4, v text);
+ALTER TABLE moved_values ALTER COLUMN v SET STORAGE EXTERNAL;
+});
+my $moved_toast = $node->safe_psql('postgres',
+ q{SELECT reltoastrelid::regclass FROM pg_class WHERE relname = 'moved_values'}
+);
+held_fold(
+ $moved_toast,
+ 'doltgres-commit-valued',
+ q{INSERT INTO moved_values SELECT g, repeat(chr(64 + g), 3000) FROM generate_series(1, 20) g},
+ 'BEGIN; UPDATE moved_values SET k = k + 100 WHERE k <= 10; COMMIT');
+is(totals($moved_toast), count($moved_toast),
+ 'a commit held across a fold that applied the values its update keeps moves a TOAST relation\'s totals to a fresh count'
+);
+held_fold(
+ 'recounted',
+ 'doltgres-recount-based',
+ q{CREATE TABLE recounted (k int4);
+INSERT INTO recounted SELECT g FROM generate_series(1, 10) g;
+ALTER TABLE recounted ALTER COLUMN k TYPE int8;
+INSERT INTO recounted SELECT g FROM generate_series(11, 20) g},
+ q{SELECT pg_relation_size('recounted')});
+is(totals('recounted'), count('recounted'),
+ 'a count held across a fold counts the totals again to a fresh count');
+
+# Bugs row 316: a DELETE after a count that a fold's install met, as merge's
+# DELETE and ANALYZE met one, takes its rows from totals the count left right:
+# after a crash's restart leaves a folded table's totals unknown, a reader's
+# count held across a fold of rows inserted since is followed by a DELETE of
+# more rows than the older base holds, and the table's size still reads and
+# its totals equal a fresh count, where totals counted over the older base
+# wrapped past zero.
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE restarted_count (k int4);
+INSERT INTO restarted_count SELECT g FROM generate_series(1, 10) g;
+SELECT doltgres_test_fold('restarted_count');
+});
+$node->stop('immediate');
+$node->start;
+held_fold(
+ 'restarted_count',
+ 'doltgres-recount-based',
+ q{INSERT INTO restarted_count SELECT g FROM generate_series(11, 20) g},
+ q{SELECT pg_relation_size('restarted_count')});
+$node->safe_psql('postgres', 'DELETE FROM restarted_count WHERE k <= 15');
+$node->psql(
+ 'postgres', q{SELECT pg_relation_size('restarted_count')},
+ stdout => \my $restarted_size,
+ stderr => \my $restarted_error);
+is($restarted_error, '',
+ 'a DELETE after a count held across a fold leaves the table\'s size readable'
+);
+is(totals('restarted_count'), count('restarted_count'),
+ 'a DELETE after a count held across a fold moves the totals to a fresh count'
+);
+
+# Bugs row 316's carriers: a rewrite that carries versions into a new storage,
+# which an older snapshot makes it do by holding the fold horizon below their
+# rows, leaves that storage's totals unknown, since its base's sizes count
+# only the rows folded into it, so after a DELETE and a VACUUM the relation's
+# size reads and equals a twin's that no snapshot held, and a table's totals
+# equal a fresh count, where totals of the base alone wrapped past zero:
+# merge's primary key added to a keyless table, an index's build, and a
+# table's move to another tablespace.
+my $spaced = $node->basedir . '/carried_space';
+mkdir $spaced or die "mkdir $spaced: $!";
+$node->safe_psql('postgres',
+ "CREATE TABLESPACE carried_space LOCATION '$spaced'");
+my %carriers = (
+ rekeyed => [ '%T', 'ALTER TABLE %T ADD PRIMARY KEY (tid)' ],
+ indexed => [ '%T_tid', 'CREATE INDEX %T_tid ON %T (tid)' ],
+ moved => [ '%T', 'ALTER TABLE %T SET TABLESPACE carried_space' ],);
+my %carrier_named = (
+ rekeyed => 'a key change',
+ indexed => 'an index build',
+ moved => 'a move to another tablespace');
+for my $carrier ('rekeyed', 'indexed', 'moved')
+{
+ my $named = $carrier_named{$carrier};
+ my %size;
+ for my $table ($carrier, "${carrier}_twin")
+ {
+ my ($sized, $carry) = @{ $carriers{$carrier} };
+ s/%T/$table/g for ($sized, $carry);
+ my $horizon = $node->background_psql('postgres');
+ $horizon->query_safe(
+ 'BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1')
+ if $table eq $carrier;
+ $node->safe_psql('postgres',
+ "CREATE TABLE $table (tid int4, balance int4); INSERT INTO $table SELECT g, 0 FROM generate_series(1, 20) g; $carry"
+ );
+ $horizon->query_safe('COMMIT') if $table eq $carrier;
+ $horizon->quit;
+ $node->safe_psql('postgres', "DELETE FROM $table WHERE tid > 5");
+ $node->safe_psql('postgres', "VACUUM $table");
+ $node->psql(
+ 'postgres', "SELECT pg_relation_size('$sized')",
+ stdout => \$size{$table},
+ stderr => \my $carried_error);
+ is($carried_error, '',
+ "a DELETE and VACUUM after $named leave the size readable")
+ if $table eq $carrier;
+ is(totals($sized), count($sized),
+ "a DELETE and VACUUM after $named that carried its rows move the totals to a fresh count"
+ ) if $table eq $carrier && $carrier ne 'indexed';
+ }
+ is($size{$carrier}, $size{"${carrier}_twin"},
+ "after $named that carried its rows, the size is a twin's that no snapshot held"
+ );
+}
+
+# Bugs row 323: a commit's moves count only the versions its storage's base
+# didn't apply. After a fold's install is made durable and a crash's restart
+# replays the versions that fold applied, a reader's count makes the totals
+# known, and an UPDATE's commit moves a keyless table's totals to a fresh
+# count, where counting the replayed versions again added the folded rows
+# twice.
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE replayed (k int4, v text);
+INSERT INTO replayed SELECT g % 3, 'v' FROM generate_series(1, 10) g;
+SELECT doltgres_test_fold('replayed');
+CREATE TABLE replayed_flushed (k int4);
+});
+$node->stop('immediate');
+$node->start;
+launcher_held();
+$node->safe_psql('postgres', q{SELECT pg_relation_size('replayed')});
+$node->safe_psql('postgres',
+ q{BEGIN; UPDATE replayed SET v = 's' WHERE k = 0; COMMIT});
+launcher_freed();
+is(totals('replayed'), count('replayed'),
+ 'a commit after a restart replayed what a fold applied moves a keyless table\'s totals to a fresh count'
+);
+
+# Bugs row 312: every block of a fork table is all-visible and all-frozen
+# (Differences row 10), so CREATE INDEX, ANALYZE, and REINDEX, which recount
+# through visibilitymap_count, write relallvisible and relallfrozen equal to
+# relpages, as the fork's VACUUM does, on a table no VACUUM has touched.
+my $all_visible = q{
+SELECT relpages > 0, relallvisible = relpages, relallfrozen = relpages FROM pg_class WHERE relname = '%T'};
+$node->safe_psql('postgres',
+ 'CREATE TABLE counted_indexed (id int4); INSERT INTO counted_indexed SELECT g FROM generate_series(1, 3000) g; CREATE INDEX ON counted_indexed (id)'
+);
+is($node->safe_psql('postgres', $all_visible =~ s/%T/counted_indexed/r),
+ 't|t|t', 'CREATE INDEX counts a fork table\'s every block all-visible');
+$node->safe_psql('postgres',
+ 'CREATE TABLE counted_analyzed (id int4); INSERT INTO counted_analyzed SELECT g FROM generate_series(1, 3000) g; ANALYZE counted_analyzed'
+);
+is($node->safe_psql('postgres', $all_visible =~ s/%T/counted_analyzed/r),
+ 't|t|t', 'ANALYZE counts a fork table\'s every block all-visible');
+$node->safe_psql('postgres',
+ 'INSERT INTO counted_indexed SELECT g FROM generate_series(3001, 9000) g; REINDEX TABLE counted_indexed'
+);
+is( $node->safe_psql('postgres', $all_visible =~ s/%T/counted_indexed/r),
+ 't|t|t',
+ 'REINDEX counts a fork table\'s every block all-visible after more writes'
+);
+
$node->stop;
done_testing();
diff --git a/src/doltgres/test/t/065_map_counts.pl b/src/doltgres/test/t/065_map_counts.pl
index 7792efff1136a..1fe7e3f5e965a 100644
--- a/src/doltgres/test/t/065_map_counts.pl
+++ b/src/doltgres/test/t/065_map_counts.pl
@@ -37,8 +37,11 @@
# entry, so the entry stays while the version does. A drop as the status
# table's move makes one, of an ended entry nothing counts, then can't
# take the entry of a writer whose values or takes are still in a delta: a
-# TOAST relation's fold after its table's fold, and a fold that meets a take
-# whose row ended before an earlier fold, both read the writer's commit.
+# TOAST relation's fold after its table's fold reads the writer's commit. A
+# take whose row ended before a fold leaves the delta with its block's run,
+# its tuple id pending in the base with its ender's full xid and its value
+# ids, so its writer's entry counts nothing, and a later fold after the
+# entry's drop asks nothing of it (bugs row 347).
sub dropped_if_uncounted
{
my ($xid) = @_;
@@ -71,12 +74,14 @@ sub dropped_if_uncounted
q{BEGIN; SELECT doltgres_test_insert('spent', 'source_spent', false, 0); SELECT txid_current(); COMMIT}))[-1];
$node->safe_psql('postgres',
q{SELECT doltgres_test_write('spent', true, 'DELETE FROM spent WHERE a = 1'); SELECT doltgres_test_fold('spent')});
-ok(dropped_if_uncounted($taker) > 0,
- 'a writer\'s entry still counts its take of a row ended before the fold');
+is( $node->safe_psql('postgres', q{SELECT doltgres_test_mapped('spent')}) . ' '
+ . dropped_if_uncounted($taker),
+ 'f -1',
+ 'a take of a row ended before the fold leaves the delta, and its writer\'s entry counts nothing');
$node->safe_psql('postgres',
q{SELECT doltgres_test_insert('spent', 'source_spent', false, 0)});
(undef, $folded) = $node->psql('postgres', q{SELECT doltgres_test_fold('spent')});
-is($folded, 't', 'a later fold that meets the take reads its writer\'s commit');
+is($folded, 't', 'a later fold after the writer\'s entry is dropped completes');
# An abort and a rollback to a savepoint give back the counts of the versions
@@ -136,5 +141,78 @@ sub dropped_if_uncounted
is($node->safe_psql('postgres', "SELECT doltgres_test_status_table(1, $locker, 0)"),
'0', 'a row lock rolled back to a savepoint leaves its committed transaction\'s entry counting nothing');
+
+# Every free space map version a write pushes counts at its writer's entry,
+# and a fold gives back only what was counted (bugs row 305). An UPDATE of 40
+# rows on full pages flags a block for each row it moves off one, 40 flags;
+# an INSERT of 20 rows into another table by the same transaction pushes 20
+# rows and their 20 takes, 40 versions, as many as the flags. The first
+# table's fold gives back the UPDATE's versions, flags included, and leaves
+# the entry counting the INSERT's 40, so a drop of an ended entry that counts
+# nothing passes it by, and the second table's fold reads the writer's
+# commit. With the fold launcher held and no fold worker it started before
+# its hold left running, no fold but the case's own folds or drops a version
+# while the case counts, and each fold waits until no other process holds an
+# xmin or an xid at or below the writer, such as an autovacuum worker's,
+# which would keep the fold's horizon below it.
+sub horizon_past
+{
+ my ($xid) = @_;
+ $node->poll_query_until('postgres',
+ "SELECT count(*) = 0 FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND (age(backend_xmin) >= age('$xid'::xid) OR age(backend_xid) >= age('$xid'::xid))"
+ ) or die "a process holds the horizon at or below $xid";
+ return;
+}
+$node->safe_psql('postgres', 'CREATE EXTENSION injection_points');
+$node->safe_psql('postgres',
+ "SELECT injection_points_attach('doltgres-fold-launch', 'wait')");
+$node->poll_query_until('postgres',
+ "SELECT count(*) = 1 FROM pg_stat_activity WHERE wait_event = 'doltgres-fold-launch'"
+) or die 'the fold launcher is not held';
+$node->poll_query_until('postgres',
+ "SELECT count(*) = 0 FROM pg_stat_activity WHERE backend_type = 'fold worker'"
+) or die 'a fold worker still runs';
+my $filler = (
+ split /\n/,
+ $node->safe_psql(
+ 'postgres', q{
+CREATE TABLE flagged (id int4 PRIMARY KEY, v text);
+BEGIN;
+INSERT INTO flagged SELECT g, repeat('x', 900) FROM generate_series(1, 400) g;
+SELECT txid_current();
+COMMIT;
+CREATE TABLE inserted (id int4, v text);
+}))[-1];
+horizon_past($filler);
+$node->safe_psql('postgres', q{SELECT doltgres_test_fold('flagged')});
+my $flagger = (
+ split /\n/,
+ $node->safe_psql(
+ 'postgres', q{
+BEGIN;
+UPDATE flagged SET v = repeat('y', 900) WHERE id <= 40;
+INSERT INTO inserted SELECT g, 'b' FROM generate_series(1, 20) g;
+SELECT txid_current();
+COMMIT;
+}))[-1];
+horizon_past($flagger);
+$node->safe_psql('postgres', q{SELECT doltgres_test_fold('flagged')});
+is(dropped_if_uncounted($flagger), '40',
+ 'a writer\'s entry counts its other table\'s versions after its first table\'s fold gives back its flags'
+);
+(undef, $folded) =
+ $node->psql('postgres', q{SELECT doltgres_test_fold('inserted')});
+is($folded, 't', 'the other table\'s fold reads its writer\'s commit');
+is( $node->safe_psql(
+ 'postgres', "SELECT doltgres_test_status_table(1, $flagger, 0)"),
+ '0',
+ 'the writer\'s entry counts nothing once both tables fold');
+is( $node->safe_psql(
+ 'postgres',
+ q{SELECT (SELECT count(*) FROM flagged WHERE v LIKE 'y%'), (SELECT count(*) FROM inserted)}
+ ),
+ '40|20',
+ 'both tables read the transaction\'s rows');
+
$node->stop;
done_testing();
diff --git a/src/doltgres/test/t/071_spill_reads.pl b/src/doltgres/test/t/071_spill_reads.pl
new file mode 100644
index 0000000000000..42ed2224f6507
--- /dev/null
+++ b/src/doltgres/test/t/071_spill_reads.pl
@@ -0,0 +1,487 @@
+# Copyright 2026 Dolthub, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Reads held across a fold that applies a committed transaction's spilled
+# versions and drops them from the delta (bugs rows 240, 241, and 242): each
+# read, held after it read its storage's spill slots, moves to the fold's base
+# at its next read and reads every row once. The node's delta_memory is 4 MB,
+# so a batch of a few thousand rows spills and stays inside the budget.
+
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+if ($ENV{enable_injection_points} ne 'yes')
+{
+ plan skip_all => 'Injection points not supported by this build';
+}
+
+my $node = PostgreSQL::Test::Cluster->new('spill_reads');
+$node->init;
+$node->append_conf('postgresql.conf',
+ "delta_memory = 4MB\nfold_threshold = 1GB\nfold_timeout = 3600s\nmax_parallel_workers_per_gather = 0\nautovacuum = off"
+);
+$node->start;
+if (!$node->check_extension('injection_points'))
+{
+ plan skip_all => 'Extension injection_points not installed';
+}
+$node->safe_psql(
+ 'postgres', q{
+CREATE EXTENSION injection_points;
+CREATE EXTENSION doltgres_test;
+});
+
+# launcher_held holds the fold launcher at its injection point and waits for
+# its fold workers to end, so no fold but a case's own installs a base under
+# the case's held read before that fold; launcher_freed lets the launcher go
+# on. The node runs without autovacuum, whose workers' snapshots would hold
+# the fold horizon below a case's batch.
+sub launcher_held
+{
+ $node->safe_psql('postgres',
+ "SELECT injection_points_attach('doltgres-fold-launch', 'wait')");
+ $node->poll_query_until('postgres',
+ q{SELECT count(*) = 1 FROM pg_stat_activity WHERE wait_event = 'doltgres-fold-launch'}
+ ) or die 'the fold launcher never waited';
+ $node->poll_query_until('postgres',
+ q{SELECT count(*) = 0 FROM pg_stat_activity WHERE backend_type = 'fold worker'}
+ ) or die 'a fold worker kept running';
+}
+
+sub launcher_freed
+{
+ $node->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-fold-launch'); SELECT injection_points_wakeup('doltgres-fold-launch')"
+ );
+}
+
+# Bugs row 242: an UPDATE ... FROM through a nested loop whose outer scan reads
+# a source whose rows its writer spilled, held at its first read of the
+# source's spills while a fold of the source installs a base and drops the
+# spills, updates every row, as heap's twin's does.
+my %joined;
+for my $am ('', 'USING doltgres_twin')
+{
+ my $p = $am eq '' ? '' : 'heap_';
+ launcher_held() if $am eq '';
+ $node->safe_psql(
+ 'postgres', qq{
+CREATE TABLE ${p}joined_source (k int4, v text) $am;
+INSERT INTO ${p}joined_source SELECT g, repeat('s', 400) FROM generate_series(1, 3000) g;
+CREATE TABLE ${p}joined_target (k int4 PRIMARY KEY, v text) $am;
+INSERT INTO ${p}joined_target SELECT g, 't' FROM generate_series(1, 3000) g;
+});
+ my $plan =
+ "SET enable_hashjoin = off; SET enable_mergejoin = off; SET enable_material = off;";
+ my $update =
+ "UPDATE ${p}joined_target t SET v = s.v || 'u' FROM ${p}joined_source s WHERE t.k = s.k";
+ if ($am eq '')
+ {
+ my $updater = $node->background_psql('postgres');
+ $updater->query_safe(
+ "$plan SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-spill-slots-read', 'wait')"
+ );
+ $updater->query_until(qr/started/, "\\echo started\n$update;\n");
+ $node->wait_for_event('client backend', 'doltgres-spill-slots-read');
+ is( $node->safe_psql(
+ 'postgres', "SELECT doltgres_test_fold('joined_source')"),
+ 't',
+ "a fold under the held UPDATE ... FROM's source scan installs a base"
+ );
+ launcher_freed();
+ $node->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-spill-slots-read'); SELECT injection_points_wakeup('doltgres-spill-slots-read')"
+ );
+ $updater->query_safe('SELECT 1');
+ $updater->quit;
+ }
+ else
+ {
+ $node->safe_psql('postgres', "$plan $update");
+ }
+ $joined{$p} = $node->safe_psql('postgres',
+ "SELECT count(*) || ' ' || md5(string_agg(k || v, ',' ORDER BY k)) FROM ${p}joined_target"
+ );
+}
+is($joined{''}, $joined{heap_},
+ "an UPDATE ... FROM whose source scan a fold's drop passes under updates every row as heap's twin's"
+);
+
+# Bugs rows 240 and 241: a scan held after it read its storage's spill slots,
+# while a fold installs a base that applies a committed transaction's spilled
+# rows and drops them from the delta, moves to that base at its next read and
+# reads every row once.
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE read_source (v text) USING doltgres_twin;
+INSERT INTO read_source SELECT 'r' || i || repeat('x', 400) FROM generate_series(1, 3000) i;
+CREATE TABLE read_rows (v text);
+});
+my $read_count =
+ q{SELECT count(*) FROM doltgres_test_scan('read_rows', false, -1)};
+
+# held_scan starts a scan of read_rows in a session of its own: a cursor
+# that has fetched its first 1,000 rows, held where its next fetch reads the
+# storage's spill slots, and returns the session.
+sub held_scan
+{
+ my $scan = $node->background_psql('postgres');
+ $scan->query_safe('BEGIN; DECLARE c CURSOR FOR SELECT v FROM read_rows');
+ $scan->query_safe('FETCH 1000 FROM c');
+ $scan->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-spill-slots-read', 'wait')"
+ );
+ $scan->query_until(qr/held/, "\\echo held\nFETCH ALL FROM c;\n");
+ $node->wait_for_event('client backend', 'doltgres-spill-slots-read');
+ return $scan;
+}
+
+# released lets a held scan go on and returns the count of the rows it read,
+# its first 1,000 with the rest.
+sub released
+{
+ my ($scan) = @_;
+ $node->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-spill-slots-read'); SELECT injection_points_wakeup('doltgres-spill-slots-read')"
+ );
+ my @rest = split /\n/, $scan->query_safe('SELECT 1');
+ $scan->query_safe('COMMIT');
+ $scan->quit;
+ return 1000 + @rest - 1;
+}
+launcher_held();
+$node->safe_psql('postgres',
+ q{SELECT count(*) FROM doltgres_test_insert('read_rows', 'read_source', true, 0)}
+);
+my $scan = held_scan();
+is($node->safe_psql('postgres', q{SELECT doltgres_test_fold('read_rows')}),
+ 't', 'a fold under the held scan installs a base');
+launcher_freed();
+is(released($scan), 3000,
+ 'a scan held across a fold that applied its spilled rows reads every row'
+);
+is($node->safe_psql('postgres', $read_count),
+ '3000', 'and a scan after it reads every row');
+
+# Bugs rows 240 and 241's point reads: a fetch of a spilled row by its tuple id,
+# held inside its read of the delta between its tuple id's lookup and its
+# versions' read, and an index fetch of it by its row key, held inside its
+# read of the delta, each while a fold installs a base holding the row and
+# drops its spill, read the row, as the same fetch after the fold does. Each
+# held case holds the fold launcher from before its batch's writes, so no fold
+# worker the writes ask for applies the batch, or holds the horizon below it,
+# before the case's own fold.
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE point_source USING doltgres_twin AS SELECT 'p' || i || repeat('x', 400) AS v FROM generate_series(1, 3000) i;
+CREATE TABLE point_rows (v text);
+CREATE TABLE point_one AS SELECT 'p1' || repeat('x', 400) AS v;
+});
+# copied writes the point table's batch of spilled rows and returns its first
+# row's tuple id, which its first spill holds, and that id as a number.
+sub copied
+{
+ my $tid = $node->safe_psql('postgres',
+ q{SELECT min(t) FROM doltgres_test_insert('point_rows', 'point_source', true, 0) t}
+ );
+ return (
+ $tid,
+ $node->safe_psql(
+ 'postgres',
+ "SELECT ((('$tid'::tid::text::point)[0])::int8 << 16) | (('$tid'::tid::text::point)[1])::int8"
+ ));
+}
+for my $name ('by tuple id', 'by row key')
+{
+ launcher_held();
+ my ($tid, $copy) = copied();
+ my $fetch =
+ $name eq 'by tuple id'
+ ? "SELECT doltgres_test_fetch('point_rows', '$tid')"
+ : "SELECT string_agg(r, ' ') FROM doltgres_test_index_fetch_key('point_rows', 'point_one', $copy) r";
+ my $at =
+ $name eq 'by tuple id'
+ ? 'doltgres-fetch-keyed'
+ : 'doltgres-spill-slots-read';
+ my $point = $node->background_psql('postgres');
+ $point->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('$at', 'wait')"
+ );
+ $point->query_until(qr/held/, "\\echo held\n$fetch;\n");
+ $node->wait_for_event('client backend', $at);
+ is( $node->safe_psql(
+ 'postgres', q{SELECT doltgres_test_fold('point_rows')}),
+ 't',
+ "a fold under the held fetch $name installs a base");
+ launcher_freed();
+ $node->safe_psql('postgres',
+ "SELECT injection_points_detach('$at'); SELECT injection_points_wakeup('$at')"
+ );
+ my $held = $point->query_safe('SELECT 1');
+ $point->quit;
+ $held =~ s/\n1$//;
+ my $after = $node->safe_psql('postgres', $fetch);
+ like(
+ $after,
+ qr/^\(\d+,\d+\) \d+ p1x/,
+ "a fetch $name reads the row after the fold");
+ is($held, $after,
+ "a fetch $name held across a fold that applied its spilled row reads it"
+ );
+}
+
+# Bugs rows 240 and 241's position kinds: each kind of read the test module
+# reaches, held after it read its storage's spill slots while a fold installs a
+# base that applies a committed transaction's spilled rows and drops them from
+# the delta, moves to that base at its next read and reads every row once, as a
+# read after the fold does and as many rows as heap's twin holds: a scan forward
+# and backward over keyless copies, a tid range read forward and backward, a
+# parallel scan's workers, and a scan of a table whose key a collation compares.
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE kinds_source (v text) USING doltgres_twin;
+INSERT INTO kinds_source SELECT repeat('r', 400) || (i % 500) FROM generate_series(1, 3000) i;
+CREATE TABLE kinds (v text);
+CREATE TABLE heap_kinds (v text) USING doltgres_twin;
+CREATE TABLE compared (k text COLLATE "und-x-icu" PRIMARY KEY, v text);
+CREATE TABLE heap_compared (k text COLLATE "und-x-icu" PRIMARY KEY, v text) USING doltgres_twin;
+});
+
+# held_kind starts a read in a session of its own, held at the point where it
+# reads its storage's spill slots, and returns the session.
+sub held_kind
+{
+ my ($query) = @_;
+ my $read = $node->background_psql('postgres');
+ $read->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-spill-slots-read', 'wait')"
+ );
+ $read->query_until(qr/held/, "\\echo held\n$query;\n");
+ $node->wait_for_event('client backend', 'doltgres-spill-slots-read');
+ return $read;
+}
+
+# across_fold writes a batch of a table's spilled rows, holds a read of them,
+# folds the table, lets the read go on, and returns its answer with the same
+# read's answer after the fold, the twin's count, and whether the fold
+# installed a base.
+sub across_fold
+{
+ my ($table, $batch, $query, $twin) = @_;
+ launcher_held();
+ $node->safe_psql('postgres', $batch);
+ my $read = held_kind($query);
+ my $folded =
+ $node->safe_psql('postgres', "SELECT doltgres_test_fold('$table')");
+ launcher_freed();
+ $node->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-spill-slots-read'); SELECT injection_points_wakeup('doltgres-spill-slots-read')"
+ );
+ my $held = $read->query_safe('SELECT 1');
+ $read->quit;
+ $held =~ s/\n1$//;
+ return (
+ $held,
+ $node->safe_psql('postgres', $query),
+ $node->safe_psql('postgres', $twin), $folded);
+}
+
+my $insert =
+ q{SELECT count(*) FROM doltgres_test_insert('kinds', 'kinds_source', true, 0);
+ INSERT INTO heap_kinds SELECT * FROM kinds_source};
+my $twin = q{SELECT count(*) FROM heap_kinds};
+my %kind = (
+ forward =>
+ q{SELECT count(*) || ' ' || md5(string_agg(substring(r from '^\S+ \S+ (.*)$'), ',' ORDER BY r)) FROM doltgres_test_scan('kinds', false, -1) r},
+ backward =>
+ q{SELECT count(*) || ' ' || md5(string_agg(substring(r from '^\S+ \S+ (.*)$'), ',' ORDER BY r)) FROM doltgres_test_scan('kinds', true, -1) r},
+ 'tid range forward' =>
+ q{SELECT count(*) || ' ' || md5(string_agg(substring(r from '^\S+ \S+ (.*)$'), ',' ORDER BY r)) FROM doltgres_test_tidrange('kinds', '(0,1)', '(4294967294,0)', false) r},
+ 'tid range backward' =>
+ q{SELECT count(*) || ' ' || md5(string_agg(substring(r from '^\S+ \S+ (.*)$'), ',' ORDER BY r)) FROM doltgres_test_tidrange('kinds', '(0,1)', '(4294967294,0)', true) r},
+ parallel =>
+ q{SELECT count(*) || ' ' || md5(string_agg(substring(r from '^\S+ \S+ (.*)$'), ',' ORDER BY r)) FROM doltgres_test_parallel('kinds', 3, false, 0, NULL, NULL) r},
+);
+for my $name (
+ 'forward', 'backward',
+ 'tid range forward', 'tid range backward',
+ 'parallel')
+{
+ my ($held, $after, $count, $folded) =
+ across_fold('kinds', $insert, $kind{$name}, $twin);
+ is($folded, 't', "a fold under the held $name read installs a base");
+ is($held, $after, "a $name read held across a fold reads every row once");
+ like($held, qr/^$count /, "and as many rows as heap's twin holds");
+}
+my @compared = across_fold(
+ 'compared',
+ q{SELECT doltgres_test_write('compared', true, 'INSERT INTO compared SELECT ''k'' || i, repeat(''v'', 400) FROM generate_series(1, 3000) i');
+ INSERT INTO heap_compared SELECT 'k' || i, repeat('v', 400) FROM generate_series(1, 3000) i},
+ q{SELECT count(*) || ' ' || md5(string_agg(substring(r from '^\S+ \S+ (.*)$'), ',' ORDER BY r)) FROM doltgres_test_scan('compared', false, -1) r},
+ q{SELECT count(*) FROM heap_compared});
+is($compared[3], 't',
+ "a fold under the held read of a table whose key a collation compares installs a base"
+);
+is($compared[0], $compared[1],
+ "a read of a table whose key a collation compares, held across a fold, reads every row once"
+);
+like(
+ $compared[0],
+ qr/^$compared[2] /,
+ "and as many rows as heap's twin holds");
+
+# Bugs rows 240 and 241's position kinds through a fork btree index: an index
+# scan forward and backward, an index-only scan, and a bitmap scan, each held
+# after it read a storage's spill slots while a fold of the index installs a
+# base that applies a committed transaction's spilled entries, read every row
+# once, as heap's twin gives them, with many rows of one key spanning the fold.
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE ix (id int4, t text);
+CREATE INDEX ix_t ON ix (t);
+CREATE TABLE heap_ix (id int4, t text) USING doltgres_twin;
+CREATE INDEX heap_ix_t ON heap_ix (t);
+CREATE TABLE ix_source USING doltgres_twin AS SELECT i AS id, repeat('v', 200) || (i % 41) AS t FROM generate_series(1, 2000) i;
+INSERT INTO heap_ix SELECT * FROM ix_source;
+SELECT doltgres_test_insert('ix', 'ix_source', false, 0);
+SELECT doltgres_test_fold('ix');
+SELECT doltgres_test_index_build('ix', 'ix_t');
+ANALYZE heap_ix;
+});
+my $routine =
+ "DO \$\$ BEGIN PERFORM doltgres_test_index_routine('ix_t'); END \$\$;";
+my $indexes =
+ 'SET enable_seqscan = off; SET enable_bitmapscan = off; SET enable_indexonlyscan = off;';
+my %plans = (
+ 'index forward' =>
+ [ $indexes, q{SELECT id FROM %T WHERE t >= 'v' ORDER BY t} ],
+ 'index backward' =>
+ [ $indexes, q{SELECT id FROM %T WHERE t >= 'v' ORDER BY t DESC} ],
+ 'index-only' => [
+ 'SET enable_seqscan = off; SET enable_bitmapscan = off;',
+ q{SELECT t FROM %T WHERE t >= 'v' ORDER BY t}
+ ],
+ bitmap => [
+ 'SET enable_seqscan = off; SET enable_indexscan = off; SET enable_indexonlyscan = off;',
+ q{SELECT id FROM %T WHERE t >= 'v'}
+ ],);
+my $next = 2001;
+for my $name ('index forward', 'index backward', 'index-only', 'bitmap')
+{
+ my ($settings, $query) = @{ $plans{$name} };
+ my ($from, $to) = ($next, $next + 3999);
+ $next = $to + 1;
+ launcher_held();
+ $node->safe_psql(
+ 'postgres',
+ "$routine SELECT doltgres_test_write('ix', true, 'INSERT INTO ix SELECT i, repeat(''v'', 200) || (i % 41) FROM generate_series($from, $to) i');
+ INSERT INTO heap_ix SELECT i, repeat('v', 200) || (i % 41) FROM generate_series($from, $to) i"
+ );
+ (my $fork = $query) =~ s/%T/ix/g;
+ $fork =~ s/'/''/g;
+ (my $heap = $query) =~ s/%T/heap_ix/g;
+ my $read = $node->background_psql('postgres');
+ $read->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-spill-slots-read', 'wait')"
+ );
+ $read->query_until(qr/held/,
+ "\\echo held\n$settings $routine SELECT doltgres_test_standing('ix', '$fork');\n"
+ );
+ $node->wait_for_event('client backend', 'doltgres-spill-slots-read');
+ my $entries =
+ $node->safe_psql('postgres', "SELECT doltgres_test_index_base('ix_t')");
+ isnt(
+ $node->safe_psql(
+ 'postgres', "SELECT doltgres_test_index_fold('ix_t')"),
+ $entries,
+ "a fold of the index under the held $name read installs a base");
+ is($node->safe_psql('postgres', "SELECT doltgres_test_fold('ix')"),
+ 't', "a fold of the table under the held $name read installs a base");
+ launcher_freed();
+ $node->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-spill-slots-read'); SELECT injection_points_wakeup('doltgres-spill-slots-read')"
+ );
+ my $held = $read->query_safe('SELECT 1');
+ $read->quit;
+ $held =~ s/\n1$//;
+ my $twin = $node->safe_psql('postgres', "$settings $heap");
+ is( join(',', sort split /\n/, $held),
+ join(',', sort split /\n/, $twin),
+ "a fork btree's $name read held across a fold of its index reads every row once"
+ );
+}
+
+# Bugs rows 240 and 315: a change by tuple id, held where it looks up its
+# row's key, before a fold installs a base holding the row and drops the
+# row's versions from the delta, finds the row's key and changes it, as the
+# same change does with no fold: an UPDATE after an INSERT in its own
+# transaction, a DELETE, and a row lock.
+my %changes = (
+ update => [
+ q{BEGIN; INSERT INTO %T SELECT g, 'y' FROM generate_series(2001, 5000) g},
+ q{WITH c AS (UPDATE %T SET v = 'z' WHERE id <= 1000 RETURNING 1) SELECT count(*) FROM c},
+ 'COMMIT'
+ ],
+ delete => [
+ 'BEGIN',
+ 'WITH c AS (DELETE FROM %T WHERE id <= 1000 RETURNING 1) SELECT count(*) FROM c',
+ 'COMMIT'
+ ],
+ lock => [
+ 'BEGIN',
+ 'SELECT count(*) FROM (SELECT id FROM %T WHERE id <= 1000 FOR UPDATE) l',
+ 'COMMIT'
+ ],);
+my %change_named = (
+ update => 'an UPDATE',
+ delete => 'a DELETE',
+ lock => 'a row lock');
+for my $name ('update', 'delete', 'lock')
+{
+ my ($before, $change, $after) = @{ $changes{$name} };
+ my $named = $change_named{$name};
+ my $table = "changed_$name";
+ s/%T/$table/g for ($before, $change, $after);
+ launcher_held();
+ $node->safe_psql('postgres',
+ "CREATE TABLE $table (id int4 PRIMARY KEY, v text); INSERT INTO $table SELECT g, repeat('x', 400) FROM generate_series(1, 2000) g"
+ );
+ my $changer = $node->background_psql('postgres', on_error_stop => 0);
+ $changer->query_safe($before);
+ $changer->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-tuple-keyed', 'wait')"
+ );
+ $changer->query_until(qr/held/, "\\echo held\n$change;\n");
+ $node->wait_for_event('client backend', 'doltgres-tuple-keyed');
+ is($node->safe_psql('postgres', "SELECT doltgres_test_fold('$table')"),
+ 't', "a fold under $named by tuple id, held, installs a base");
+ launcher_freed();
+ $node->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-tuple-keyed'); SELECT injection_points_wakeup('doltgres-tuple-keyed')"
+ );
+ my $changed = $changer->query('SELECT 1');
+ $changed =~ s/\n1$//;
+ $changed .= " $changer->{stderr}" if $changer->{stderr} =~ /ERROR/;
+ $changer->query($after);
+ $changer->quit;
+ is($changed, '1000',
+ "$named by tuple id held across a fold that applied its rows changes every row"
+ );
+}
+$node->stop;
+
+done_testing();
diff --git a/src/doltgres/test/t/072_standby_spills.pl b/src/doltgres/test/t/072_standby_spills.pl
new file mode 100644
index 0000000000000..31f310dcbf3b8
--- /dev/null
+++ b/src/doltgres/test/t/072_standby_spills.pl
@@ -0,0 +1,205 @@
+# Copyright 2026 Dolthub, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# A streaming standby's replay of spilled batches (bugs row 298): a primary
+# writes a committed batch of rows past its delta_memory, which it spills, and
+# folds it while a scan of it is held at its read of the spill slots; writes
+# two batches into another table, each folded while a point fetch of a spilled
+# row is held the same way; and writes a second batch into the first table,
+# which the standby replays, folds at its primary's point while its own scan is
+# held, and reads whole. Before, the standby's redo of the second batch's row
+# changes died with "a location of the delta names no record its hold
+# reaches".
+
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+if ($ENV{enable_injection_points} ne 'yes')
+{
+ plan skip_all => 'Injection points not supported by this build';
+}
+
+my $reading = PostgreSQL::Test::Cluster->new('spill_reading');
+$reading->init(allows_streaming => 1);
+$reading->append_conf('postgresql.conf',
+ "fold_threshold = 1GB\nfold_timeout = 3600s\nmax_parallel_workers_per_gather = 0"
+);
+$reading->start;
+$reading->safe_psql(
+ 'postgres', q{
+CREATE EXTENSION injection_points;
+CREATE EXTENSION doltgres_test;
+CREATE TABLE read_source (v text) USING doltgres_twin;
+INSERT INTO read_source SELECT 'r' || i FROM generate_series(1, 100000) i;
+CREATE TABLE read_rows (v text);
+});
+$reading->backup('reading_backup');
+my $reading_standby = PostgreSQL::Test::Cluster->new('spill_reading_standby');
+$reading_standby->init_from_backup($reading, 'reading_backup',
+ has_streaming => 1);
+$reading_standby->append_conf('postgresql.conf',
+ "max_parallel_workers_per_gather = 0");
+$reading_standby->start;
+my $read_count =
+ q{SELECT count(*) FROM doltgres_test_scan('read_rows', false, -1)};
+
+# held_scan starts a scan of read_rows on a node in a session of its own, held at
+# the point where it reads the storage's spill slots, and returns the session.
+sub held_scan
+{
+ my ($on) = @_;
+ my $scan = $on->background_psql('postgres');
+ $scan->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-spill-slots-read', 'wait')"
+ );
+ $scan->query_until(qr/held/, "\\echo held\n$read_count;\n");
+ $on->wait_for_event('client backend', 'doltgres-spill-slots-read');
+ return $scan;
+}
+
+# released lets a node's held scan go on and returns its count.
+sub released
+{
+ my ($on, $scan) = @_;
+ $on->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-spill-slots-read'); SELECT injection_points_wakeup('doltgres-spill-slots-read')"
+ );
+ my $count = $scan->query_safe('SELECT 1');
+ $scan->quit;
+ return $count;
+}
+$reading->safe_psql(
+ 'postgres',
+ q{SELECT count(*) FROM doltgres_test_insert('read_rows', 'read_source', true, 0)},
+ timeout => 1200);
+$reading->wait_for_catchup($reading_standby);
+my $scan = held_scan($reading);
+$reading->safe_psql('postgres', q{SELECT doltgres_test_fold('read_rows')});
+released($reading, $scan);
+is($reading->safe_psql('postgres', $read_count),
+ '100000', 'and a scan after it reads every row');
+
+# A fetch of a spilled row by its tuple id and an index fetch of it by its row
+# key, each held inside its read of the delta while a fold installs a base
+# holding the row and drops its spill, then read again.
+$reading->safe_psql(
+ 'postgres', q{
+CREATE TABLE point_source USING doltgres_twin AS SELECT 'p' || i AS v FROM generate_series(1, 100000) i;
+CREATE TABLE point_rows (v text);
+CREATE TABLE point_one AS SELECT 'p1' AS v;
+});
+# copied writes the point table's batch of spilled rows and returns its first
+# row's tuple id, which its first spill holds, and that id as a number.
+sub copied
+{
+ my $tid = $reading->safe_psql(
+ 'postgres',
+ q{SELECT min(t) FROM doltgres_test_insert('point_rows', 'point_source', true, 0) t},
+ timeout => 1200);
+ return (
+ $tid,
+ $reading->safe_psql(
+ 'postgres',
+ "SELECT ((('$tid'::tid::text::point)[0])::int8 << 16) | (('$tid'::tid::text::point)[1])::int8"
+ ));
+}
+for my $name ('by tuple id', 'by row key')
+{
+ my ($tid, $copy) = copied();
+ my $fetch =
+ $name eq 'by tuple id'
+ ? "SELECT doltgres_test_fetch('point_rows', '$tid')"
+ : "SELECT string_agg(r, ' ') FROM doltgres_test_index_fetch_key('point_rows', 'point_one', $copy) r";
+ my $point = $reading->background_psql('postgres');
+ $point->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-spill-slots-read', 'wait')"
+ );
+ $point->query_until(qr/held/, "\\echo held\n$fetch;\n");
+ $reading->wait_for_event('client backend', 'doltgres-spill-slots-read');
+ $reading->safe_psql('postgres',
+ q{SELECT doltgres_test_fold('point_rows')});
+ $reading->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-spill-slots-read'); SELECT injection_points_wakeup('doltgres-spill-slots-read')"
+ );
+ $point->query_safe('SELECT 1');
+ $point->quit;
+ isnt($reading->safe_psql('postgres', $fetch),
+ '', "a fetch $name reads the row after the fold");
+}
+$reading->safe_psql(
+ 'postgres',
+ q{SELECT count(*) FROM doltgres_test_insert('read_rows', 'read_source', true, 0)},
+ timeout => 1200);
+$reading->wait_for_catchup($reading_standby);
+$scan = held_scan($reading_standby);
+$reading->safe_psql('postgres', q{SELECT doltgres_test_fold('read_rows')});
+# A standby folds a byte-ordered table's reached point only as the next record
+# arrives (bugs row 334, whose fix takes this record out), so one follows the
+# fold, and the standby's own fold runs until its base is its primary's, since
+# one run after its catch-up isn't always enough (bugs row 337, whose fix takes
+# the retry out).
+$reading->safe_psql('postgres', 'SELECT txid_current()');
+$reading->wait_for_catchup($reading_standby);
+my $points = q{SELECT doltgres_test_base_sizes('read_rows')};
+my $primary_points = $reading->safe_psql('postgres', $points);
+ok( $reading_standby->poll_query_until(
+ 'postgres',
+ "SELECT doltgres_test_base_sizes('read_rows') = '$primary_points' FROM (SELECT doltgres_test_fold('read_rows')) f"
+ ),
+ "the standby folded at its primary's point while its scan was held");
+released($reading_standby, $scan);
+is($reading_standby->safe_psql('postgres', $read_count),
+ '200000', "and the standby's scan after it reads every row");
+$reading_standby->stop;
+$reading->stop;
+
+# Bugs row 339: one INSERT past a 1MB delta_memory finishes, a wide one and a
+# narrow one, and the 1MB standby replays each; before, the primary's writer
+# waited at the budget for good.
+my $small = PostgreSQL::Test::Cluster->new('spill_small');
+$small->init(allows_streaming => 1);
+$small->append_conf('postgresql.conf',
+ "delta_memory = 1MB\nfold_threshold = 1GB\nfold_timeout = 3600s");
+$small->start;
+$small->backup('small_backup');
+my $small_standby = PostgreSQL::Test::Cluster->new('spill_small_standby');
+$small_standby->init_from_backup($small, 'small_backup', has_streaming => 1);
+$small_standby->start;
+
+foreach my $case ([ 'wide_rows', 3000, q{repeat('w', 600)} ],
+ [ 'narrow_rows', 20000, q{'n'} ])
+{
+ my ($table, $rows, $value) = @$case;
+ my (undef, $out) = $small->psql(
+ 'postgres', qq{
+SET statement_timeout = '180s';
+CREATE TABLE $table (k int4 PRIMARY KEY, v text) WITH (fillfactor = 50);
+INSERT INTO $table SELECT g, $value FROM generate_series(1, $rows) g;
+SELECT count(*) FROM $table;
+});
+ is($out, $rows,
+ "an INSERT of $rows rows into $table past a 1MB delta_memory finishes"
+ );
+ $small->wait_for_catchup($small_standby);
+ is( $small_standby->safe_psql('postgres', "SELECT count(*) FROM $table"),
+ $rows,
+ "and the 1MB standby replays it");
+}
+$small_standby->stop;
+$small->stop;
+
+done_testing();
diff --git a/src/doltgres/test/t/106_cport_simc.pl b/src/doltgres/test/t/106_cport_simc.pl
index b6d8a20e7049e..91eaac8f6f312 100644
--- a/src/doltgres/test/t/106_cport_simc.pl
+++ b/src/doltgres/test/t/106_cport_simc.pl
@@ -37,6 +37,7 @@
'simc::tests::a_checkpoint_sets_the_redo_point_the_host_answers',
'simc::tests::a_crash_fails_each_call_until_a_recovery_takes_the_files_back_to_their_syncs',
'simc::tests::a_delta_page_is_taken_written_and_given_back_as_the_simulations_host',
+ 'simc::tests::a_delta_page_takes_one_of_the_directorys_free_slots',
'simc::tests::a_dictionary_simc_prepares_compresses_and_decompresses_and_drops_in_the_c',
'simc::tests::a_failed_allocation_of_a_listings_names_frees_the_names_made_and_raises_out_of_memory',
'simc::tests::a_failed_allocation_of_a_panics_payload_frees_its_message_and_raises_out_of_memory',
@@ -44,8 +45,10 @@
'simc::tests::a_free_pass_is_counted',
'simc::tests::a_hinted_c_error_crosses_to_sim_as_its_literals',
'simc::tests::a_historic_snapshot_lends_its_bounds_command_and_sorted_xids',
+ 'simc::tests::a_hold_of_the_test_process_keeps_its_data_directory_past_its_capability',
'simc::tests::a_linked_delta_page_opens_reads_appends_seals_and_retires_as_the_simulations_host',
'simc::tests::a_nap_is_counted_and_the_disk_space_set_is_answered',
+ 'simc::tests::a_non_vacuumable_snapshot_finds_xids_below_its_horizon_removable',
'simc::tests::a_pause_a_warning_a_log_and_the_last_redo_answer_as_the_simulations_host',
'simc::tests::a_pooled_records_doors_answer_as_the_simulations_host',
'simc::tests::a_processs_flags_are_cleared_before_each_exit_work',
@@ -54,6 +57,7 @@
'simc::tests::a_refused_open_and_a_refused_record_fail_once_with_the_refusal',
'simc::tests::a_refused_take_fails_once_and_refuse_nothing_takes_back_the_rest',
'simc::tests::a_reports_debug_is_the_rusts',
+ 'simc::tests::a_reports_trace_is_the_rusts',
'simc::tests::a_returning_actor_ends_ok_and_its_endings_compare_as_the_rusts',
'simc::tests::a_run_check_fails_the_run_at_a_yield_with_its_message',
'simc::tests::a_run_checks_c_panic_fails_the_run_with_its_message',
@@ -89,7 +93,10 @@
'simc::tests::the_delta_advance_seed_and_pass_doors_answer_as_the_simulations_host',
'simc::tests::the_delta_hold_and_budget_doors_answer_as_the_simulations_host',
'simc::tests::the_epoch_doors_answer_as_the_simulations_host',
+ 'simc::tests::the_injected_door_answers_as_the_simulations_host',
'simc::tests::the_key_doors_answer_as_the_simulations_host',
+ 'simc::tests::the_logged_records_are_the_records_the_wal_took',
+ 'simc::tests::the_new_cid_door_logs_its_record_as_the_simulations_host',
'simc::tests::the_next_decompression_runs_out_of_memory_when_asked',
'simc::tests::the_node_cache_installs_finds_and_unpins_a_nodes_frame',
'simc::tests::the_test_only_std_fs_lists_a_directorys_names',
@@ -98,6 +105,7 @@
'simc::tests::the_test_only_std_fs_removes_a_directory_with_its_contents',
'simc::tests::the_test_only_std_fs_writes_reads_appends_and_removes_a_file',
'simc::tests::the_wal_a_checkpoint_and_a_databases_quiet_answer_as_the_simulations_host',
+ 'simc::tests::the_wal_flush_door_answers_as_the_simulations_host',
);
my @tests = split /\n/, $rows;
is_deeply([ map { (split /\t/, $_, 2)[0] } @tests ],
diff --git a/src/doltgres/test/t/124_carry_unlisted.pl b/src/doltgres/test/t/124_carry_unlisted.pl
new file mode 100644
index 0000000000000..c8c6c42d6c0a4
--- /dev/null
+++ b/src/doltgres/test/t/124_carry_unlisted.pl
@@ -0,0 +1,332 @@
+# Copyright 2026 Dolthub, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# A carry whose count's add fails takes its files back out (bugs row 291's
+# review R2): a VACUUM FULL carries three writers' recently dead versions,
+# which an older snapshot keeps, into spill files under each writer, and the
+# add of the second file's count fails; the carry takes its files back out and
+# gives back the first add, so every writer's count is where it was, and a
+# restart replays the carry's records and its unlist to nothing. A failure
+# inside the unlist's window stops the server, and the restart's recount
+# leaves every count at the versions its storages hold.
+
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+if ($ENV{enable_injection_points} ne 'yes')
+{
+ plan skip_all => 'Injection points not supported by this build';
+}
+
+my $node = PostgreSQL::Test::Cluster->new('carrier');
+$node->init(allows_streaming => 1);
+$node->append_conf('postgresql.conf',
+ "fold_threshold = 1GB\nfold_timeout = 3600s\nrestart_after_crash = on\ncheckpoint_timeout = 1h"
+);
+$node->start;
+$node->safe_psql(
+ 'postgres', q{
+CREATE EXTENSION injection_points;
+CREATE EXTENSION doltgres_test;
+CREATE TABLE carried (k int4 PRIMARY KEY, v text) WITH (fillfactor = 50);
+INSERT INTO carried SELECT g, repeat('c', 20) FROM generate_series(1, 30) g;
+SELECT doltgres_test_fold('carried');
+});
+
+# Every server takes no checkpoint or restartpoint by time: a standby's
+# restartpoint moves the writers' counts while the rounds compare them.
+# counts returns each writer's entry's count on a server, the primary when
+# none is given, -1 for one with none.
+sub counts
+{
+ my ($writers, $on) = @_;
+ $on //= $node;
+ return join ' ', map {
+ $on->safe_psql('postgres',
+ "SELECT doltgres_test_status_table(1, $_, 0)")
+ } @$writers;
+}
+
+# updated runs three updates, each its own transaction, while an older
+# snapshot holds their older versions, and returns the writers' xids.
+sub updated
+{
+ my ($holder) = @_;
+ my @writers;
+ for my $n (1 .. 3)
+ {
+ push @writers,
+ $node->safe_psql('postgres',
+ "BEGIN; UPDATE carried SET v = v || 'u' WHERE mod(k, 3) = "
+ . ($n - 1)
+ . "; SELECT txid_current(); COMMIT;");
+ }
+ return \@writers;
+}
+
+my $holder = $node->background_psql('postgres');
+$holder->query_safe(
+ 'BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT count(*) FROM pg_class');
+my $writers = updated($holder);
+my $before = counts($writers);
+
+$node->safe_psql('postgres',
+ "SELECT injection_points_attach('doltgres-carry-counting', 'wait')");
+my $rewriter = $node->background_psql('postgres', on_error_stop => 0);
+$rewriter->query_until(qr/rewriting/,
+ "\\echo rewriting\nVACUUM FULL carried;\n");
+$node->wait_for_event('client backend', 'doltgres-carry-counting');
+$node->safe_psql(
+ 'postgres', q{
+SELECT injection_points_detach('doltgres-carry-counting');
+SELECT injection_points_attach('doltgres-carry-counting', 'error');
+SELECT injection_points_wakeup('doltgres-carry-counting');
+});
+$rewriter->quit;
+$node->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-carry-counting')");
+is(counts($writers), $before,
+ "a carry whose second count's add fails gives back the first, every writer's count where it was"
+);
+$holder->query_safe('COMMIT');
+$holder->quit;
+
+$node->restart;
+is(counts($writers), $before,
+ "a restart replays the carry's records and its unlist to nothing");
+is($node->safe_psql('postgres', 'SELECT count(*) FROM carried'),
+ '30',
+ "the table's rows read back after the failed carry and the restart");
+
+# A concurrent build's late entries (bugs row 291's review R2): CREATE INDEX
+# CONCURRENTLY carries the entries of rows whose writers are past its horizon,
+# which an older snapshot holds, into spill files under each writer, in an
+# index whose storage an earlier transaction made, so the build's abort
+# drops nothing. With the second count's add failed, the build takes its
+# files back out and logs the unlist, and a standby that spilled the carried
+# entries, too large for its delta_memory, before the unlist arrived takes
+# them out of its spill files too: its delta of the index holds its primary's
+# entries, and every writer's count is where it was.
+$node->backup('late');
+my $standby = PostgreSQL::Test::Cluster->new('late_standby');
+$standby->init_from_backup($node, 'late', has_streaming => 1);
+$standby->append_conf('postgresql.conf',
+ "delta_memory = 1MB\nfold_threshold = 1GB\nfold_timeout = 3600s\ncheckpoint_timeout = 1h"
+);
+$standby->start;
+$node->safe_psql('postgres', 'CREATE TABLE late (k int4, v text)');
+$holder = $node->background_psql('postgres');
+$holder->query_safe(
+ 'BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT count(*) FROM pg_class');
+my @late_writers;
+
+for my $n (0 .. 2)
+{
+ my $from = 3000 * $n + 1;
+ push @late_writers,
+ $node->safe_psql('postgres',
+ "BEGIN; INSERT INTO late SELECT g, md5(g::text) || repeat('l', 200) FROM generate_series($from, $from + 2999) g; SELECT txid_current(); COMMIT;"
+ );
+}
+$before = counts(\@late_writers);
+$node->safe_psql('postgres',
+ "SELECT injection_points_attach('doltgres-carry-counting', 'wait')");
+my $builder = $node->background_psql('postgres', on_error_stop => 0);
+$builder->query_until(qr/building/,
+ "\\echo building\nCREATE INDEX CONCURRENTLY late_v ON late (v COLLATE \"C\");\n"
+);
+$node->wait_for_event('client backend', 'doltgres-carry-counting');
+$node->safe_psql(
+ 'postgres', q{
+SELECT injection_points_detach('doltgres-carry-counting');
+SELECT injection_points_attach('doltgres-carry-counting', 'error');
+SELECT injection_points_wakeup('doltgres-carry-counting');
+});
+$builder->quit;
+$node->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-carry-counting')");
+$holder->query_safe('COMMIT');
+$holder->quit;
+is(counts(\@late_writers), $before,
+ "a concurrent build whose second count's add fails gives back the first, every writer's count where it was"
+);
+$node->wait_for_replay_catchup($standby);
+is(counts(\@late_writers, $standby),
+ $before,
+ "the standby replays the unlist, every writer's count where it was");
+is( $standby->safe_psql(
+ 'postgres', "SELECT md5(doltgres_test_index_entries('late_v'))"),
+ $node->safe_psql(
+ 'postgres', "SELECT md5(doltgres_test_index_entries('late_v'))"),
+ "the standby's delta of the index holds its primary's entries, the carried ones out"
+);
+is($standby->safe_psql('postgres', 'SELECT count(*) FROM late'),
+ '9000', "the standby reads the table whole");
+$standby->stop;
+
+# A plain rewrite's failed carry on a standby (bugs row 291's review G1): a
+# VACUUM FULL carries three writers' recently dead rows, with their tuple ids
+# and their puts' takes, which an older snapshot holds, and the add of the
+# second file's count fails; a standby that replayed the carried rows takes
+# them back out at the unlist, so every writer's count there is where it was.
+# The new storage itself goes with the rewrite's abort, on the standby too.
+# The standby has the default delta_memory: at 1MB its replay of so wide a
+# carry stalls at the budget (bugs rows 332 and 304).
+$node->backup('wide');
+my $wide_standby = PostgreSQL::Test::Cluster->new('wide_standby');
+$wide_standby->init_from_backup($node, 'wide', has_streaming => 1);
+$wide_standby->append_conf('postgresql.conf',
+ "fold_threshold = 1GB\nfold_timeout = 3600s\nallow_in_place_tablespaces = on\ncheckpoint_timeout = 1h"
+);
+$wide_standby->start;
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE carried_wide (k int4 PRIMARY KEY, v text) WITH (fillfactor = 50);
+INSERT INTO carried_wide SELECT g, repeat('w', 600) FROM generate_series(1, 3000) g;
+SELECT doltgres_test_fold('carried_wide');
+});
+$node->wait_for_replay_catchup($wide_standby);
+$holder = $node->background_psql('postgres');
+$holder->query_safe(
+ 'BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT count(*) FROM pg_class');
+my @wide_writers;
+
+for my $n (0 .. 2)
+{
+ push @wide_writers,
+ $node->safe_psql('postgres',
+ "BEGIN; UPDATE carried_wide SET v = v || 'u' WHERE mod(k, 3) = $n; SELECT txid_current(); COMMIT;"
+ );
+}
+$before = counts(\@wide_writers);
+$node->safe_psql('postgres',
+ "SELECT injection_points_attach('doltgres-carry-counting', 'wait')");
+$rewriter = $node->background_psql('postgres', on_error_stop => 0);
+$rewriter->query_until(qr/widening/,
+ "\\echo widening\nVACUUM FULL carried_wide;\n");
+$node->wait_for_event('client backend', 'doltgres-carry-counting');
+$node->safe_psql(
+ 'postgres', q{
+SELECT injection_points_detach('doltgres-carry-counting');
+SELECT injection_points_attach('doltgres-carry-counting', 'error');
+SELECT injection_points_wakeup('doltgres-carry-counting');
+});
+$rewriter->quit;
+$node->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-carry-counting')");
+$holder->query_safe('COMMIT');
+$holder->quit;
+is(counts(\@wide_writers), $before,
+ "a wide table's carry whose second count's add fails gives back the first, every writer's count where it was"
+);
+$node->wait_for_replay_catchup($wide_standby);
+is( counts(\@wide_writers, $wide_standby),
+ $before,
+ "a standby that replayed the carried rows takes them out at the unlist, every writer's count where it was"
+);
+is( $wide_standby->safe_psql(
+ 'postgres', 'SELECT count(*), sum(length(v)) FROM carried_wide'),
+ $node->safe_psql(
+ 'postgres', 'SELECT count(*), sum(length(v)) FROM carried_wide'),
+ "the standby reads the wide table as its primary does");
+
+# A move's failed carry on a standby (bugs row 291's review G2): ALTER TABLE
+# SET TABLESPACE carries its old storage's versions, with the free space map
+# versions three writers' updates made, which an older snapshot holds, and
+# the add of the second file's count fails; the standby takes out each
+# carried map version by its record's end, as well as the carried rows'
+# takes, so every writer's count there is where it was.
+$node->safe_psql(
+ 'postgres', q{
+SET allow_in_place_tablespaces = on;
+CREATE TABLESPACE moved_space LOCATION '';
+CREATE TABLE carried_moved (k int4 PRIMARY KEY, v text) WITH (fillfactor = 50);
+INSERT INTO carried_moved SELECT g, repeat('m', 600) FROM generate_series(1, 3000) g;
+SELECT doltgres_test_fold('carried_moved');
+});
+$holder = $node->background_psql('postgres');
+$holder->query_safe(
+ 'BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT count(*) FROM pg_class');
+my @moved_writers;
+for my $n (0 .. 2)
+{
+ push @moved_writers,
+ $node->safe_psql('postgres',
+ "BEGIN; UPDATE carried_moved SET v = v || 'u' WHERE mod(k, 3) = $n; SELECT txid_current(); COMMIT;"
+ );
+}
+$before = counts(\@moved_writers);
+$node->safe_psql('postgres',
+ "SELECT injection_points_attach('doltgres-carry-counting', 'wait')");
+$rewriter = $node->background_psql('postgres', on_error_stop => 0);
+$rewriter->query_until(qr/moving/,
+ "\\echo moving\nALTER TABLE carried_moved SET TABLESPACE moved_space;\n");
+$node->wait_for_event('client backend', 'doltgres-carry-counting');
+$node->safe_psql(
+ 'postgres', q{
+SELECT injection_points_detach('doltgres-carry-counting');
+SELECT injection_points_attach('doltgres-carry-counting', 'error');
+SELECT injection_points_wakeup('doltgres-carry-counting');
+});
+$rewriter->quit;
+$node->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-carry-counting')");
+$holder->query_safe('COMMIT');
+$holder->quit;
+is(counts(\@moved_writers), $before,
+ "a move's carry whose second count's add fails gives back the first, every writer's count where it was"
+);
+$node->wait_for_replay_catchup($wide_standby);
+is( counts(\@moved_writers, $wide_standby),
+ $before,
+ "a standby that replayed the move's carried map versions takes them out at the unlist, every writer's count where it was"
+);
+$wide_standby->stop;
+
+# A failure inside the unlist's window, Postgres' critical section, stops the
+# server after the unlist's record is flushed, and its restart recounts what
+# its storages hold.
+$holder = $node->background_psql('postgres', on_error_stop => 0);
+$holder->query_safe(
+ 'BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT count(*) FROM pg_class');
+$writers = updated($holder);
+$before = counts($writers);
+$node->safe_psql(
+ 'postgres', q{
+SELECT injection_points_attach('doltgres-carry-counting', 'error');
+SELECT injection_points_attach('doltgres-carry-unlisting', 'error');
+});
+my ($ret, $stdout, $stderr) = $node->psql('postgres', 'VACUUM FULL carried');
+like(
+ $stderr,
+ qr/server closed the connection unexpectedly|terminating connection/,
+ "a failure inside the unlist's window stops the server, as a PANIC does");
+ok($node->poll_query_until('postgres', 'SELECT 1', '1'),
+ "the server restarts after the PANIC");
+# The stopped rewrite's new storage is an orphan its crashed transaction left,
+# as Postgres leaves an orphaned relation file, whose drop with its counts is
+# bugs row 322's; the recount is what the storages list, so a second restart
+# recounts the same.
+my $recounted = counts($writers);
+$node->restart;
+is(counts($writers), $recounted,
+ "the restart after a stop inside the window recounts the versions its storages list, as a second restart does"
+);
+is($node->safe_psql('postgres', 'SELECT count(*) FROM carried'),
+ '30', "the table's rows read back after the stop and the restart");
+
+done_testing();
diff --git a/src/doltgres/test/t/131_scan_costs.pl b/src/doltgres/test/t/131_scan_costs.pl
new file mode 100644
index 0000000000000..fbd6b39129951
--- /dev/null
+++ b/src/doltgres/test/t/131_scan_costs.pl
@@ -0,0 +1,331 @@
+# Copyright 2026 Dolthub, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# The table's part of a fork table's index and bitmap scan costs (design item
+# 16; bugs row 312), each term read back through EXPLAIN's costs: an
+# index-only scan reads no row of the table; the primary key's own index reads
+# its leaves, which the node cache holds here; a secondary index's entry costs
+# a descent of the table's tree, a comparison a level above the leaves, and,
+# past the node cache, Mackert and Lohman's leaves at random; a keyless
+# table's entry descends by the row's hash the same way; and a bitmap scan's
+# row costs a tuple id resolution and a descent.
+
+use strict;
+use warnings FATAL => 'all';
+use POSIX qw(ceil);
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+if ($ENV{enable_injection_points} ne 'yes')
+{
+ plan skip_all => 'Injection points not supported by this build';
+}
+
+my $node = PostgreSQL::Test::Cluster->new('costs');
+$node->init;
+$node->start;
+$node->safe_psql('postgres', 'CREATE EXTENSION doltgres_test');
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE costed (id int4 PRIMARY KEY, v int4, pad text);
+INSERT INTO costed SELECT g, g, repeat('x', 40) FROM generate_series(1, 20000) g;
+CREATE INDEX costed_v ON costed (v);
+CREATE TABLE keyless (v int4, pad text);
+INSERT INTO keyless SELECT g, repeat('x', 40) FROM generate_series(1, 20000) g;
+CREATE INDEX keyless_v ON keyless (v);
+ANALYZE costed, keyless});
+
+# planned returns a statement's plan's total cost and its scan's rows, under
+# settings that leave the scan of the kind asked for.
+sub planned
+{
+ my ($sql, $settings) = @_;
+ my $json = $node->safe_psql('postgres',
+ "SET enable_seqscan = off; $settings; EXPLAIN (FORMAT JSON) $sql");
+ my ($total) = $json =~ /"Total Cost": ([0-9.]+)/;
+ my ($rows) = $json =~ /"Plan Rows": ([0-9]+)/;
+ return ($total, $rows);
+}
+
+# shape returns a fork table's tree's leaves and height, and its tuple id
+# tree's, as its scans' costs read them.
+sub shape
+{
+ my ($table) = @_;
+ return split / /,
+ $node->safe_psql('postgres',
+ "SELECT doltgres_test_scan_shape('$table')");
+}
+
+# close_to says whether two costs agree within the 0.01 EXPLAIN rounds to.
+sub close_to
+{
+ my ($got, $wanted) = @_;
+ return abs($got - $wanted) < 0.015;
+}
+
+my $cpu_operator = 0.0025;
+my $cpu_tuple = 0.01;
+my $index_only = 'SET enable_bitmapscan = off';
+my $index_scan =
+ 'SET enable_bitmapscan = off; SET enable_indexonlyscan = off';
+
+# A secondary index whose table's leaves the node cache holds: the index scan
+# costs the index-only scan's plus a descent's comparisons a row.
+my ($leaves, $height, $tid_leaves, $tid_height) = shape('costed');
+my ($only, $rows) =
+ planned('SELECT v FROM costed WHERE v < 1000', $index_only);
+my ($scan) = planned('SELECT v FROM costed WHERE v < 1000', $index_scan);
+ok( close_to($scan - $only, $rows * $height * $cpu_operator),
+ "a secondary index scan in the node cache costs a descent a row ($scan - $only, $rows rows, height $height)"
+);
+
+# The primary key's own index, whose leaves are the table's rows and fit the
+# node cache, reads no more of the table than its index-only scan.
+my ($key_only) =
+ planned('SELECT id FROM costed WHERE id < 1000', $index_only);
+my ($key_scan) =
+ planned('SELECT id FROM costed WHERE id < 1000', $index_scan);
+ok( close_to($key_scan, $key_only),
+ "the primary key's own index scan in the node cache costs its index-only scan's ($key_scan, $key_only)"
+);
+
+# A bitmap heap scan's rows each cost a tuple id resolution and a descent,
+# beside the recheck's CPU and cost_bitmap_tree_node's tenth of an operator
+# for handling the bitmap, over its bitmap index scan's cost.
+my $bitmap = $node->safe_psql('postgres',
+ 'SET enable_seqscan = off; SET enable_indexscan = off; EXPLAIN (FORMAT JSON) SELECT pad FROM costed WHERE v < 1000'
+);
+my ($heap_total, $index_total) =
+ $bitmap =~ /"Total Cost": ([0-9.]+).*"Total Cost": ([0-9.]+)/s;
+my ($bitmap_rows) = $bitmap =~ /"Plan Rows": ([0-9]+)/;
+ok( close_to(
+ $heap_total - $index_total,
+ $bitmap_rows * (
+ ($tid_height + $height) * $cpu_operator +
+ $cpu_tuple +
+ $cpu_operator +
+ 0.1 * $cpu_operator)),
+ "a bitmap heap scan costs a tuple id resolution and a descent a row ($heap_total - $index_total, $bitmap_rows rows)"
+);
+
+# A keyless table's secondary index descends the table's tree by the row's
+# hash, a descent a row as a keyed table's.
+my ($keyless_leaves, $keyless_height) = shape('keyless');
+my ($keyless_only, $keyless_rows) =
+ planned('SELECT v FROM keyless WHERE v < 1000', $index_only);
+my ($keyless_scan) =
+ planned('SELECT v FROM keyless WHERE v < 1000', $index_scan);
+ok( close_to(
+ $keyless_scan - $keyless_only,
+ $keyless_rows * $keyless_height * $cpu_operator),
+ "a keyless table's secondary index scan costs a descent by the row's hash a row"
+);
+
+# An index-only scan reads no row of the table, so a VACUUM, which heap's
+# visibility map would change it by, leaves its cost as it was.
+$node->safe_psql('postgres', 'VACUUM costed');
+my ($vacuumed) = planned('SELECT v FROM costed WHERE v < 1000', $index_only);
+ok( close_to($vacuumed, $only),
+ "an index-only scan's cost is the same before and after a VACUUM ($only, $vacuumed)"
+);
+
+# default_cost returns a statement's plan's total cost at default settings.
+sub default_cost
+{
+ my ($sql) = @_;
+ my ($total) =
+ $node->safe_psql('postgres', "EXPLAIN (FORMAT JSON) $sql") =~
+ /"Total Cost": ([0-9.]+)/;
+ return $total;
+}
+
+# Item 16's plan choices at default settings, as the fork's own expected
+# plans: before any VACUUM an index-only scan, whose table part is nothing;
+# an index scan over a single-index bitmap scan, whose rows each cost a tuple
+# id resolution beside the descent; and the primary key's range scan, its
+# leaves the rows in order.
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE fresh (id int4 PRIMARY KEY, v int4, pad text);
+INSERT INTO fresh SELECT g, g, repeat('z', 40) FROM generate_series(1, 20000) g;
+CREATE INDEX fresh_v ON fresh (v);
+ANALYZE fresh});
+my ($fresh_leaves, $fresh_height) = shape('fresh');
+is( $node->safe_psql(
+ 'postgres', 'EXPLAIN (COSTS OFF) SELECT v FROM fresh WHERE v < 100'),
+ "Index Only Scan using fresh_v on fresh\n Index Cond: (v < 100)",
+ 'an index-only scan is chosen before any VACUUM');
+my ($fresh_scan, $fresh_rows) =
+ planned('SELECT v FROM fresh WHERE v < 100', $index_scan);
+ok( close_to(
+ default_cost('SELECT v FROM fresh WHERE v < 100'),
+ $fresh_scan - $fresh_rows * $fresh_height * $cpu_operator),
+ 'the chosen index-only scan costs the index scan less its descents');
+is( $node->safe_psql(
+ 'postgres',
+ 'EXPLAIN (COSTS OFF) SELECT pad FROM fresh WHERE v < 1000'),
+ "Index Scan using fresh_v on fresh\n Index Cond: (v < 1000)",
+ 'an index scan is chosen over a single-index bitmap scan');
+my ($fresh_index) =
+ planned('SELECT pad FROM fresh WHERE v < 1000', $index_scan);
+my ($fresh_bitmap) = planned('SELECT pad FROM fresh WHERE v < 1000',
+ 'SET enable_indexscan = off');
+ok( close_to(default_cost('SELECT pad FROM fresh WHERE v < 1000'),
+ $fresh_index)
+ && $fresh_index < $fresh_bitmap,
+ "the chosen index scan costs less than the bitmap scan ($fresh_index, $fresh_bitmap)"
+);
+is( $node->safe_psql(
+ 'postgres',
+ 'EXPLAIN (COSTS OFF) SELECT * FROM fresh WHERE id BETWEEN 100 AND 300'
+ ),
+ "Index Scan using fresh_pkey on fresh\n Index Cond: ((id >= 100) AND (id <= 300))",
+ 'the primary key\'s range scan is chosen');
+my ($fresh_key_only) =
+ planned('SELECT id FROM fresh WHERE id BETWEEN 100 AND 300', $index_only);
+ok( close_to(
+ default_cost('SELECT * FROM fresh WHERE id BETWEEN 100 AND 300'),
+ $fresh_key_only),
+ 'the chosen key range scan in the node cache costs its index-only scan\'s'
+);
+
+# leaves_read is Mackert and Lohman's count of a tree's leaves that fetches
+# read past a cache of so many leaves, as the fork's cost counts them.
+sub leaves_read
+{
+ my ($fetches, $leaves, $cached) = @_;
+ return 0 if $leaves <= $cached;
+ my $limit = 2 * $leaves * $cached / (2 * $leaves - $cached);
+ return $fetches <= $limit
+ ? ceil(2 * $leaves * $fetches / (2 * $leaves + $fetches))
+ : ceil($cached + ($fetches - $limit) * ($leaves - $cached) / $leaves);
+}
+
+# Past the node cache, a secondary index scan also reads Mackert and Lohman's
+# count of the table's leaves at random, with the node cache in
+# effective_cache_size's place.
+$node->append_conf('postgresql.conf', 'node_cache_size = 1024kB');
+$node->restart;
+$node->safe_psql(
+ 'postgres', q{
+CREATE TABLE wide (id int4 PRIMARY KEY, v int4, pad text);
+INSERT INTO wide SELECT g, (g * 7919) % 60000, repeat('y', 400) FROM generate_series(1, 60000) g;
+CREATE INDEX wide_v ON wide (v);
+ANALYZE wide});
+my ($wide_leaves, $wide_height) = shape('wide');
+my $cached = 1024 * 1024 / 8192;
+ok( $wide_leaves > $cached,
+ "the wide table's leaves ($wide_leaves) exceed the node cache's $cached");
+my ($wide_only, $wide_rows) =
+ planned('SELECT v FROM wide WHERE v < 3000', $index_only);
+my ($wide_scan) = planned('SELECT v FROM wide WHERE v < 3000', $index_scan);
+my $read = leaves_read($wide_rows, $wide_leaves, $cached);
+ok( close_to(
+ $wide_scan - $wide_only,
+ $wide_rows * $wide_height * $cpu_operator + $read * 4),
+ "a secondary index scan past the node cache reads Mackert and Lohman's leaves at random ($wide_scan - $wide_only, $wide_rows rows, $read leaves)"
+);
+
+# The primary key's own index past the node cache reads its leaves in key
+# order, the first at random and the rest in sequence: the selectivity's
+# share of the leaves, which the plan's rows give to within a row.
+my ($wide_key_only, $wide_key_rows) =
+ planned('SELECT id FROM wide WHERE id <= 3000', $index_only);
+my ($wide_key_scan) =
+ planned('SELECT id FROM wide WHERE id <= 3000', $index_scan);
+my $wide_tuples = $node->safe_psql('postgres',
+ "SELECT reltuples FROM pg_class WHERE relname = 'wide'");
+my @key_reads = map { ceil($_ / $wide_tuples * $wide_leaves) }
+ ($wide_key_rows - 0.5, $wide_key_rows + 0.5);
+ok( ( grep { close_to($wide_key_scan - $wide_key_only, 4 + ($_ - 1)) }
+ @key_reads),
+ "the primary key's own index scan past the node cache reads its leaves in order ($wide_key_scan - $wide_key_only, reads @key_reads)"
+);
+
+# A repeated inner index scan, a nested loop's parameterized one, costs each
+# loop its share of Mackert and Lohman's leaves over every loop's fetches: a
+# secondary index's rows and the primary key's one leaf a probe. Enough
+# probes pass the node cache's share of the leaves that the count differs
+# from heap's over effective_cache_size.
+$node->safe_psql('postgres',
+ 'CREATE TABLE probes (x int4); INSERT INTO probes SELECT g * 13 FROM generate_series(1, 4000) g; ANALYZE probes'
+);
+my $nested =
+ 'SET enable_hashjoin = off; SET enable_mergejoin = off; SET enable_material = off; SET enable_memoize = off; SET enable_bitmapscan = off';
+
+# inner returns a nested loop's inner index scan's cost a loop, its rows a
+# loop, and the loops, its outer scan's rows.
+sub inner
+{
+ my ($sql, $settings) = @_;
+ my $json = $node->safe_psql('postgres',
+ "$nested; $settings; EXPLAIN (FORMAT JSON) $sql");
+ my ($loops) = $json =~ /"Node Type": "Seq Scan".*?"Plan Rows": ([0-9]+)/s;
+ my ($cost, $rows) = $json =~
+ /"Node Type": "Index (?:Only )?Scan".*?"Total Cost": ([0-9.]+),\s*"Plan Rows": ([0-9]+)/s;
+ return ($cost, $rows, $loops);
+}
+my ($loop_only, $loop_rows, $loops) =
+ inner('SELECT w.v FROM probes p JOIN wide w ON w.v = p.x', '');
+my ($loop_scan) = inner('SELECT w.pad FROM probes p JOIN wide w ON w.v = p.x',
+ 'SET enable_indexonlyscan = off');
+ok( close_to(
+ $loop_scan - $loop_only,
+ $loop_rows *
+ $wide_height *
+ $cpu_operator +
+ leaves_read($loop_rows * $loops, $wide_leaves, $cached) * 4 / $loops
+ ),
+ "a repeated secondary index scan costs its loop's share of the leaves read ($loop_scan - $loop_only, $loops loops)"
+);
+my ($key_loop_only) =
+ inner('SELECT w.id FROM probes p JOIN wide w ON w.id = p.x', '');
+my ($key_loop_scan) =
+ inner('SELECT w.pad FROM probes p JOIN wide w ON w.id = p.x',
+ 'SET enable_indexonlyscan = off');
+ok( close_to(
+ $key_loop_scan - $key_loop_only,
+ leaves_read($loops, $wide_leaves, $cached) * 4 / $loops),
+ "a repeated primary key index scan costs its loop's share of the leaves read ($key_loop_scan - $key_loop_only, $loops loops)"
+);
+
+# A lossy bitmap: with 64kB of work_mem the bitmap holds 1024 entries, the
+# size of a page's entry and two pointers, so of the pages compute_bitmap_pages
+# fetches all but half the entries are lossy, and each costs a range scan of
+# the tuple id tree: a descent, and its leaf read at random past the node
+# cache. A scan of every row keeps the rows fetched the same either way.
+my ($wide_tid_leaves, $wide_tid_height) = (shape('wide'))[ 2, 3 ];
+my $lossy_sql = 'SELECT pad FROM wide WHERE v >= 0';
+my ($lossy_small) =
+ planned($lossy_sql, "SET enable_indexscan = off; SET work_mem = '64kB'");
+my ($lossy_large) =
+ planned($lossy_sql, "SET enable_indexscan = off; SET work_mem = '64MB'");
+my $wide_pages = $node->safe_psql('postgres',
+ "SELECT relpages FROM pg_class WHERE relname = 'wide'");
+my $fetched =
+ 2 * $wide_pages * $wide_tuples / (2 * $wide_pages + $wide_tuples);
+$fetched = $fetched >= $wide_pages ? $wide_pages : ceil($fetched);
+my $lossy = $fetched - 1024 / 2;
+ok( $lossy > 0 && close_to(
+ $lossy_small - $lossy_large,
+ $lossy * (
+ $wide_tid_height * $cpu_operator +
+ ($wide_tid_leaves > $cached ? 4 : 0))),
+ "a lossy bitmap's blocks each cost a tuple id range scan ($lossy_small - $lossy_large, $lossy lossy blocks)"
+);
+
+$node->stop;
+done_testing();
diff --git a/src/doltgres/test/t/132_heap_only_contrib.pl b/src/doltgres/test/t/132_heap_only_contrib.pl
new file mode 100644
index 0000000000000..8ac8a5a963dee
--- /dev/null
+++ b/src/doltgres/test/t/132_heap_only_contrib.pl
@@ -0,0 +1,175 @@
+# Copyright 2026 Dolthub, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# The heap-only contrib modules refuse the fork's relations (design item 5;
+# bugs row 330): each function of pageinspect, pg_surgery,
+# pgstattuple, and amcheck that reads a heap's or an index's pages refuses a
+# relation the fork stores with its own access method or relation kind
+# check's SQLSTATE and message, as the reference raises them for a table of
+# heap2 (heap's handler under another name), an index of another method, or
+# a view, while an index of a method the fork doesn't store reads as
+# Postgres' own; and pg_prewarm, whose reading of the fork's trees is bugs row
+# 331's, returns without failing the server.
+
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my $node = PostgreSQL::Test::Cluster->new('contrib');
+$node->init;
+$node->start;
+$node->safe_psql(
+ 'postgres', q{
+CREATE EXTENSION pageinspect;
+CREATE EXTENSION pg_visibility;
+CREATE EXTENSION pg_surgery;
+CREATE EXTENSION pgstattuple;
+CREATE EXTENSION amcheck;
+CREATE EXTENSION pg_prewarm;
+CREATE TABLE forked (id int4 PRIMARY KEY, v int4);
+INSERT INTO forked SELECT g, g FROM generate_series(1, 100) g;
+CREATE INDEX forked_v ON forked (v);
+CREATE INDEX forked_hash ON forked USING hash (v)});
+
+# refused returns a statement's error as its SQLSTATE and message.
+sub refused
+{
+ my ($sql) = @_;
+ my ($ret, $out, $err) =
+ $node->psql('postgres', "\\set VERBOSITY verbose\n$sql");
+ my ($error) = $err =~ /ERROR: ([0-9A-Z]{5}: [^\n]*)/;
+ return $error // "no error: $out";
+}
+
+# Each case: the call on a fork relation, and the reference's error for the
+# same check, with the fork relation's name.
+my @cases = (
+ [
+ q{SELECT tuple_data_split('forked'::regclass, '\x00'::bytea, 0, 1, NULL)},
+ '0A000: only heap AM is supported'
+ ],
+ [
+ q{SELECT get_raw_page('forked', 0)},
+ '42809: cannot get raw page from relation "forked"'
+ ],
+ [
+ q{SELECT bt_page_items('forked_v', 1)},
+ '42809: "forked_v" is not a btree index'
+ ],
+ [
+ q{SELECT bt_metap('forked_v')},
+ '42809: "forked_v" is not a btree index'
+ ],
+ [
+ q{SELECT heap_force_kill('forked', ARRAY['(0,1)']::tid[])},
+ '0A000: only heap AM is supported'
+ ],
+ [ q{SELECT pgstattuple('forked')}, '0A000: only heap AM is supported' ],
+ [
+ q{SELECT pgstattuple('forked_v')},
+ '0A000: index "forked_v" (unknown index) is not supported'
+ ],
+ [
+ q{SELECT pgstattuple_approx('forked')},
+ '0A000: only heap AM is supported'
+ ],
+ [
+ q{SELECT pgstatindex('forked_v')},
+ '42809: relation "forked_v" is not a btree index'
+ ],
+ [ q{SELECT verify_heapam('forked')}, '0A000: only heap AM is supported' ],
+ [
+ q{SELECT bt_index_check('forked_v')},
+ '0A000: expected "btree" index as targets for verification'
+ ],);
+for my $case (@cases)
+{
+ my ($sql, $wanted) = @$case;
+ is(refused($sql), $wanted, "$sql refuses a fork relation");
+}
+
+# An index of a method whose entries the fork doesn't store yet, hash here,
+# keeps Postgres' own pages, which the modules read as they read heap's: the
+# hash, GiST, BRIN, and GIN functions' checks refuse such an index once the
+# fork stores its method's entries, each method in its own piece after the
+# switch (PLAN's Order 2d).
+like(
+ $node->safe_psql('postgres', q{SELECT pgstathashindex('forked_hash')}),
+ qr/^\([0-9]+,/,
+ "an index of a method the fork doesn't store reads as Postgres' own");
+
+# get_raw_page refuses a fork table's main fork alone: its visibility map
+# fork, which the fork never writes, has no blocks, as the reference's error
+# for a fork with none says. Its free space map fork's page, Postgres' own
+# (bugs row 87), is bugs row 333's case.
+is( refused(q{SELECT get_raw_page('forked', 'vm', 0)}),
+ '22023: block number 0 is out of range for relation "forked"',
+ 'a fork table\'s visibility map fork has no blocks');
+
+# pg_prewarm doesn't yet read a fork relation's trees (bugs row 331), but its
+# call returns, whatever it answers, and the server stays up.
+my ($ret) = $node->psql('postgres', q{SELECT pg_prewarm('forked')});
+ok(defined $ret, 'pg_prewarm of a fork table returns');
+is($node->safe_psql('postgres', 'SELECT count(*) FROM forked'),
+ '100', 'the server reads the table after pg_prewarm');
+
+# Bugs row 348: pg_visibility works on a fork table, which keeps no
+# visibility map since every block is all-visible and all-frozen (design
+# item 5): each block within the table reads all-visible and all-frozen, in
+# the map and on its page, and a block past it neither; the summary counts
+# every block; pg_check_visible and pg_check_frozen find no tuple against
+# that, a running insert's and a deleted row's included; and truncating the
+# map changes none of it.
+$node->safe_psql('postgres',
+ q{CREATE TABLE visible (k int4, v text); INSERT INTO visible SELECT g, repeat('v', 200) FROM generate_series(1, 2000) g; DELETE FROM visible WHERE k <= 10}
+);
+my $running = $node->background_psql('postgres');
+$running->query_safe(
+ q{BEGIN; INSERT INTO visible SELECT g, 'running' FROM generate_series(2001, 2100) g}
+);
+my $mapped = q{SELECT count(*) || ' ' || count(*) FILTER (WHERE all_visible AND all_frozen) FROM pg_visibility_map('visible')};
+my $blocks = $node->safe_psql('postgres', q{SELECT count(*) FROM pg_visibility_map('visible')});
+ok($blocks > 1, 'a fork table of 2,000 rows maps more than one block');
+is($node->safe_psql('postgres', $mapped),
+ "$blocks $blocks", 'every block of a fork table is all-visible and all-frozen in its map');
+is( $node->safe_psql(
+ 'postgres',
+ q{SELECT count(*) FILTER (WHERE all_visible AND all_frozen AND pd_all_visible) FROM pg_visibility('visible')}
+ ),
+ $blocks,
+ 'and on its page');
+is( $node->safe_psql(
+ 'postgres',
+ q{SELECT pg_visibility_map('visible', 0) || ' ' || pg_visibility('visible', 0) || ' ' || pg_visibility_map('visible', 1000000) || ' ' || pg_visibility('visible', 1000000)}
+ ),
+ '(t,t) (t,t,t) (f,f) (f,f,f)',
+ 'a block of the table reads all-visible and all-frozen, and a block past it neither');
+is( $node->safe_psql('postgres', q{SELECT pg_visibility_map_summary('visible')}),
+ "($blocks,$blocks)", 'the summary counts every block both');
+is( $node->safe_psql(
+ 'postgres',
+ q{SELECT (SELECT count(*) FROM pg_check_visible('visible')) || ' ' || (SELECT count(*) FROM pg_check_frozen('visible'))}
+ ),
+ '0 0',
+ 'no tuple of a fork table contradicts its blocks, with a running insert and a deleted row');
+$running->query_safe('ROLLBACK');
+$running->quit;
+$node->safe_psql('postgres', q{SELECT pg_truncate_visibility_map('visible')});
+is($node->safe_psql('postgres', $mapped),
+ "$blocks $blocks", 'truncating the map leaves every block all-visible and all-frozen');
+
+$node->stop;
+done_testing();
diff --git a/src/doltgres/test/t/133_identity_spill.pl b/src/doltgres/test/t/133_identity_spill.pl
new file mode 100644
index 0000000000000..aedcc9ede3398
--- /dev/null
+++ b/src/doltgres/test/t/133_identity_spill.pl
@@ -0,0 +1,218 @@
+# Copyright 2026 Dolthub, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Bugs row 291: a transaction's own identity tree versions spill past the
+# spill threshold, as its other trees' do. At delta_memory 1MB a transaction
+# making hundreds of tables spills its identity pairs, reads them back in its
+# own transaction and, once it commits, in another; an abort leaves no pair
+# and its files go; a restart after a checkpoint that listed a running
+# transaction's identity spill finds every pair it committed; and a 1MB
+# standby replays a transaction of a thousand tables.
+
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my $settings =
+ "delta_memory = 1MB\nfold_threshold = 1GB\nfold_timeout = 3600s\ncheckpoint_timeout = 1h";
+my $node = PostgreSQL::Test::Cluster->new('identity');
+$node->init(allows_streaming => 1);
+$node->append_conf('postgresql.conf', $settings);
+$node->start;
+$node->safe_psql('postgres', 'CREATE EXTENSION doltgres_test');
+$node->backup('replayed');
+my $standby = PostgreSQL::Test::Cluster->new('replayed');
+$standby->init_from_backup($node, 'replayed', has_streaming => 1);
+$standby->append_conf('postgresql.conf', $settings);
+$standby->start;
+
+# made returns a DO block that makes count tables of a prefix, a key and a
+# text column each.
+sub made
+{
+ my ($prefix, $count) = @_;
+ return
+ "DO \$\$ BEGIN FOR i IN 1 .. $count LOOP EXECUTE format('CREATE TABLE ${prefix}_%s (k int4, v text)', i); END LOOP; END \$\$";
+}
+
+# identity_files returns the count of a transaction's identity spill files.
+sub identity_files
+{
+ my ($xid) = @_;
+ return $node->safe_psql('postgres',
+ "SELECT count(*) FROM pg_ls_dir('pg_versioned/' || (SELECT oid FROM pg_database WHERE datname = 'postgres') || '/delta/spill', true, false) f WHERE f LIKE 'identity-$xid-%'"
+ );
+}
+
+# A transaction's spilled pairs read back in it and, after its commit, in
+# another session.
+my $own = $node->background_psql('postgres');
+$own->query_safe("SET statement_timeout = '300s'");
+my $owner = $own->query_safe('BEGIN; SELECT txid_current()');
+$own->query_safe(made('own', 400));
+cmp_ok(identity_files($owner), '>', 0,
+ "a transaction making 400 tables at 1MB spills its identity pairs");
+my $first = $own->query_safe("SELECT 'own_1'::regclass::oid");
+my $found = $own->query_safe("SELECT doltgres_test_identity(2, $first, 0)");
+isnt($found, 'none', "its own transaction finds a spilled table's pair");
+$own->query_safe('INSERT INTO own_1 VALUES (1, \'a\')');
+is($own->query_safe('SELECT count(*) FROM own_1'),
+ '1', "and writes and reads that table");
+$own->query_safe('COMMIT');
+$own->quit;
+is( $node->safe_psql(
+ 'postgres', "SELECT doltgres_test_identity(2, $first, 0)"),
+ $found,
+ "after its commit another session finds the same pair");
+is($node->safe_psql('postgres', 'SELECT count(*) FROM own_1'),
+ '1', "and reads the table's row");
+
+# An abort after a spill leaves no pair, and a later checkpoint removes its
+# files.
+my $abort = $node->background_psql('postgres');
+$abort->query_safe("SET statement_timeout = '300s'");
+my $aborted = $abort->query_safe('BEGIN; SELECT txid_current()');
+$abort->query_safe(made('aborted', 400));
+my $gone = $abort->query_safe("SELECT 'aborted_1'::regclass::oid");
+cmp_ok(identity_files($aborted),
+ '>', 0, "an aborting transaction's identity pairs spill");
+$abort->query_safe('ROLLBACK');
+$abort->quit;
+is( $node->safe_psql(
+ 'postgres', "SELECT doltgres_test_identity(2, $gone, 0)"),
+ 'none',
+ "after its abort no pair names its table");
+$node->safe_psql('postgres', 'CHECKPOINT') for 1 .. 3;
+is(identity_files($aborted),
+ '0', "and its identity spill files are gone after three checkpoints");
+my ($code, $out, $err) = $node->psql('postgres',
+ "SELECT count(*) FROM pg_class WHERE relname LIKE 'own\\_%' AND doltgres_test_identity(2, oid, 0) <> 'none'"
+);
+is( $code . $err . ',' . $out,
+ '0,400',
+ "and once those checkpoints' saves fold the first transaction's spill, every table it made has its pair"
+);
+
+# A checkpoint lists a running transaction's identity spill; after its commit
+# and a crash, the restart finds every pair from the listed file.
+my $listed = $node->background_psql('postgres');
+$listed->query_safe("SET statement_timeout = '300s'");
+my $lister = $listed->query_safe('BEGIN; SELECT txid_current()');
+$listed->query_safe(made('listed', 400));
+cmp_ok(identity_files($lister), '>', 0,
+ "a third transaction's identity pairs spill");
+my $kept = $listed->query_safe("SELECT 'listed_1'::regclass::oid");
+my $kept_id =
+ $listed->query_safe("SELECT doltgres_test_identity(2, $kept, 0)");
+$node->safe_psql('postgres', 'CHECKPOINT');
+$listed->query_safe('COMMIT');
+$listed->quit;
+$node->stop('immediate');
+$node->start;
+is( $node->safe_psql(
+ 'postgres', "SELECT doltgres_test_identity(2, $kept, 0)"),
+ $kept_id,
+ "after a crash the restart finds a pair the checkpoint's listed spill holds"
+);
+is( $node->safe_psql(
+ 'postgres',
+ "SELECT count(*) FROM pg_class WHERE relname LIKE 'listed\\_%' AND doltgres_test_identity(2, oid, 0) <> 'none'"
+ ),
+ '400',
+ "and every table the transaction made has its pair");
+$node->safe_psql('postgres', 'CHECKPOINT');
+is( $node->safe_psql(
+ 'postgres', "SELECT doltgres_test_identity(2, $kept, 0)"),
+ $kept_id,
+ "and still does once a checkpoint's save folds them");
+
+for my $round (1 .. 3)
+{
+ $node->safe_psql('postgres',
+ "INSERT INTO own_1 VALUES ($round + 1, 'b'); CHECKPOINT");
+}
+is(identity_files($lister), '0',
+ "and the save's folded identity spill files are gone two checkpoints later"
+);
+
+# A 1MB standby replays one transaction of a thousand tables.
+$node->safe_psql('postgres',
+ "SET statement_timeout = '300s'; BEGIN; "
+ . made('standby', 1000)
+ . "; COMMIT;");
+$node->wait_for_replay_catchup($standby);
+my $pairs =
+ q{SELECT string_agg(relname || '=' || doltgres_test_identity(2, oid, 0), ' ' ORDER BY relname) FROM pg_class WHERE relname LIKE 'standby\_%'};
+is( $standby->safe_psql('postgres', $pairs),
+ $node->safe_psql('postgres', $pairs),
+ "a 1MB standby replays a transaction of a thousand tables, each pair as its primary's"
+);
+
+# Another transaction's lookup of a running transaction's spilled pair waits
+# for it, as it waits for an unspilled one, and then reads the pair.
+my $holder = $node->background_psql('postgres');
+$holder->query_safe("SET statement_timeout = '300s'");
+my $holding = $holder->query_safe('BEGIN; SELECT txid_current()');
+$holder->query_safe(made('held', 400));
+cmp_ok(identity_files($holding),
+ '>', 0, "a running transaction's identity pairs spill");
+my $held = $holder->query_safe("SELECT 'held_1'::regclass::oid");
+my $held_id =
+ $holder->query_safe("SELECT doltgres_test_identity(2, $held, 0)");
+my (undef, undef, $waited) = $node->psql('postgres',
+ "SET statement_timeout = '2s'; SELECT doltgres_test_identity(2, $held, 0)"
+);
+like(
+ $waited,
+ qr/canceling statement due to statement timeout/,
+ "another transaction's lookup of its spilled pair waits for it");
+$holder->query_safe('COMMIT');
+$holder->quit;
+is( $node->safe_psql(
+ 'postgres', "SELECT doltgres_test_identity(2, $held, 0)"),
+ $held_id,
+ "and reads the pair once it commits");
+
+# A 1MB standby that restarts at a restartpoint, while a running transaction's
+# replayed identity versions sit in its listed spills on both sides of the
+# restartpoint's redo point, pushes none of them again as it replays.
+my $replaying = $node->background_psql('postgres');
+$replaying->query_safe("SET statement_timeout = '300s'");
+$replaying->query_safe('BEGIN; SELECT txid_current()');
+$replaying->query_safe(made('early', 400));
+$node->safe_psql('postgres', 'CHECKPOINT');
+$replaying->query_safe(made('late', 400));
+my $oids = $replaying->query_safe(
+ "SELECT string_agg(oid::text, ',') FROM pg_class WHERE relname LIKE 'early\\_%' OR relname LIKE 'late\\_%'"
+);
+$node->wait_for_replay_catchup($standby);
+$standby->safe_psql('postgres', 'CHECKPOINT');
+$standby->stop('immediate');
+$standby->start;
+$node->wait_for_replay_catchup($standby);
+is( $standby->safe_psql(
+ 'postgres',
+ "SELECT count(*) FROM unnest('{$oids}'::oid[]) o WHERE doltgres_test_identity_versions(2, o) <> '1'"
+ ),
+ '0',
+ "a restarted 1MB standby's redo pushes no version its listed spills hold"
+);
+$replaying->query_safe('COMMIT');
+$replaying->quit;
+
+$standby->stop;
+$node->stop;
+done_testing();
diff --git a/src/doltgres/test/t/134_saves.pl b/src/doltgres/test/t/134_saves.pl
new file mode 100644
index 0000000000000..9b2f2a0adafc5
--- /dev/null
+++ b/src/doltgres/test/t/134_saves.pl
@@ -0,0 +1,396 @@
+# Copyright 2026 Dolthub, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Bugs row 340: item 6's saves outside a checkpoint. At delta_memory 1MB,
+# committed transactions' identity versions, which only a save frees, don't
+# stop later writers: a refused writer runs the save for them, so many
+# committed CREATE TABLEs finish with no checkpoint; a states' pool at its
+# smallest, filled with unsaved states, makes room by a save with no
+# checkpoint; and crash recovery replays a fold exactly.
+
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my $node = PostgreSQL::Test::Cluster->new('saves');
+$node->init;
+$node->append_conf('postgresql.conf',
+ "delta_memory = 1MB\nfold_threshold = 1GB\nfold_timeout = 3600s\ncheckpoint_timeout = 1h"
+);
+$node->start;
+$node->safe_psql('postgres', 'CREATE EXTENSION doltgres_test');
+
+my $checkpoints =
+ 'SELECT num_timed + num_requested FROM pg_stat_checkpointer';
+
+# Many committed CREATE TABLEs, each its own transaction, finish at 1MB with
+# no checkpoint: the writers the budget refuses save the committed identity
+# versions.
+my $before = $node->safe_psql('postgres', $checkpoints);
+my ($code, $out, $err) = $node->psql('postgres',
+ "SET statement_timeout = '300s'; DO \$\$ BEGIN FOR i IN 1 .. 2000 LOOP EXECUTE format('CREATE TABLE committed_%s (k int4, v text)', i); COMMIT; END LOOP; END \$\$"
+);
+is($code . $err,
+ '0',
+ "2,000 committed CREATE TABLEs at 1MB finish with no checkpoint run");
+is($node->safe_psql('postgres', $checkpoints),
+ $before, "and no checkpoint ran meanwhile");
+
+$node->stop;
+
+# A states' pool at its smallest, filled with storages' unsaved states, makes
+# room for another by saving with no checkpoint.
+my $pooled = PostgreSQL::Test::Cluster->new('pooled');
+$pooled->init;
+$pooled->append_conf('postgresql.conf',
+ "max_connections = 10\nmax_locks_per_transaction = 10\nfold_threshold = 1GB\nfold_timeout = 3600s\ncheckpoint_timeout = 1h"
+);
+$pooled->start;
+$pooled->safe_psql('postgres', 'CREATE EXTENSION doltgres_test');
+
+# Coverage of a restart after a non-checkpoint save (no failing-before: redo
+# replays every identity change after the last checkpoint's redo point, so a
+# run's older version never outlives a newer one): a table dropped and made
+# again under its name, whose id pairs with its new OID, and a table made and
+# dropped, which no pair names, both before the saves the pool's makes run.
+$pooled->safe_psql('postgres', 'CREATE TABLE changed (k int4); CHECKPOINT');
+my $old = $pooled->safe_psql('postgres', "SELECT 'changed'::regclass::oid");
+my $id =
+ $pooled->safe_psql('postgres', "SELECT doltgres_test_identity(2, $old, 0)");
+$pooled->safe_psql('postgres', 'DROP TABLE changed');
+$pooled->safe_psql('postgres', 'CREATE TABLE changed (k int4)');
+my $new = $pooled->safe_psql('postgres', "SELECT 'changed'::regclass::oid");
+$pooled->safe_psql('postgres', 'CREATE TABLE gone (k int4)');
+my $gone = $pooled->safe_psql('postgres', "SELECT 'gone'::regclass::oid");
+$pooled->safe_psql('postgres', 'DROP TABLE gone');
+my $slots =
+ $pooled->safe_psql('postgres', 'SELECT doltgres_test_relation_locks()');
+my $pool_before = $pooled->safe_psql('postgres', $checkpoints);
+my $made = $slots + 50;
+($code, $out, $err) = $pooled->psql('postgres',
+ "SET statement_timeout = '300s'; DO \$\$ BEGIN FOR i IN 1 .. $made LOOP EXECUTE format('CREATE TABLE pooled_%s (k int4)', i); EXECUTE format('INSERT INTO pooled_%s VALUES (1)', i); COMMIT; END LOOP; END \$\$"
+);
+is($code . $err,
+ '0', "writing into more storages than the states' pool holds finishes");
+is($pooled->safe_psql('postgres', $checkpoints),
+ $pool_before, "and no checkpoint ran meanwhile");
+is( $pooled->safe_psql(
+ 'postgres',
+ "SELECT count(*) FROM pg_class WHERE relname LIKE 'pooled\\_%' AND relkind = 'r'"
+ ),
+ $made,
+ "and every table it made is there");
+$pooled->stop('immediate');
+$pooled->start;
+is( $pooled->safe_psql(
+ 'postgres', "SELECT doltgres_test_identity(2, $new, 0)"),
+ $id,
+ "after a crash past the pool's saves, a table made again under its name pairs its id with its new OID"
+);
+is( $pooled->safe_psql(
+ 'postgres', "SELECT doltgres_test_identity(2, $old, 0)"),
+ 'none',
+ "and its old OID with nothing");
+is( $pooled->safe_psql(
+ 'postgres', "SELECT doltgres_test_identity(2, $gone, 0)"),
+ 'none',
+ "and a table made and dropped before the saves has no pair");
+$pooled->stop;
+
+# Crash recovery replays a fold after its rows' commit records exactly: the
+# replayed fold takes out the committed row version the live fold took out,
+# since a transaction whose commit redo replayed reads as committed there.
+my $replayed = PostgreSQL::Test::Cluster->new('replayed');
+$replayed->init;
+$replayed->append_conf('postgresql.conf',
+ "fold_threshold = 1GB\nfold_timeout = 3600s\ncheckpoint_timeout = 1h");
+$replayed->start;
+$replayed->safe_psql('postgres', 'CREATE EXTENSION doltgres_test');
+$replayed->safe_psql('postgres',
+ 'CREATE TABLE folded (k int4 PRIMARY KEY, v text); CHECKPOINT');
+$replayed->safe_psql('postgres', "INSERT INTO folded VALUES (1, 'a')");
+$replayed->safe_psql('postgres', "SELECT doltgres_test_fold('folded')");
+is( $replayed->safe_psql(
+ 'postgres', "SELECT doltgres_test_held('folded', '(0,1)')"),
+ 'f',
+ "a fold takes out a committed row's version");
+$replayed->safe_psql('postgres', 'SELECT pg_switch_wal()');
+$replayed->stop('immediate');
+$replayed->start;
+is( $replayed->safe_psql(
+ 'postgres', "SELECT doltgres_test_held('folded', '(0,1)')"),
+ 'f',
+ "and after a crash its replay after the row's commit record takes it out too"
+);
+is($replayed->safe_psql('postgres', 'SELECT v FROM folded WHERE k = 1'),
+ 'a', "and the row reads back from the base");
+$replayed->stop;
+
+# Bugs row 350: a durable manifest names only what is durable. A save waits
+# for the WAL through the record of each base it gathers, lists every pack
+# whose add its repository defers, and waits for the WAL through its fold
+# point when it folds an identity pair. The walwriter waits ten seconds
+# between flushes, so nothing but the save flushes the WAL in these cases.
+my $logged = PostgreSQL::Test::Cluster->new('logged');
+$logged->init;
+$logged->append_conf('postgresql.conf',
+ "autovacuum = off\nfold_threshold = 1GB\nfold_timeout = 3600s\ncheckpoint_timeout = 1h\nwal_writer_delay = 10s\nrestart_after_crash = on"
+);
+$logged->start;
+$logged->safe_psql('postgres',
+ 'CREATE EXTENSION doltgres_test; CREATE EXTENSION injection_points; CREATE EXTENSION pg_walinspect'
+);
+
+# landed runs a save in a session of its own, paused once its manifest is
+# written, and returns the session.
+my $landed = sub {
+ my $saver = $logged->background_psql('postgres', on_error_stop => 0);
+ $saver->query_safe('SELECT injection_points_set_local()');
+ $saver->query_safe(
+ "SELECT injection_points_attach('doltgres-repo-save-landed', 'wait')"
+ );
+ $saver->query_until(qr/start/,
+ "\\echo start\nSELECT doltgres_test_save();\n");
+ $logged->wait_for_event('client backend', 'doltgres-repo-save-landed');
+ return $saver;
+};
+
+# A fold paused after its install, a save that gathers its base, and a crash:
+# the save flushed the fold's record, so the restart reads the row once. The
+# tables of the row-once cases have no key, so a row both a base and the delta
+# hold reads twice.
+$logged->safe_psql('postgres',
+ 'CREATE TABLE logged_fold (k int4, v text); CHECKPOINT');
+my $start =
+ $logged->safe_psql('postgres', 'SELECT pg_current_wal_insert_lsn()');
+$logged->safe_psql('postgres', "INSERT INTO logged_fold VALUES (1, 'a')");
+my $folder = $logged->background_psql('postgres', on_error_stop => 0);
+$folder->query_safe('SELECT injection_points_set_local()');
+$folder->query_safe(
+ "SELECT injection_points_attach('doltgres-fold-installed', 'wait')");
+$folder->query_until(qr/start/,
+ "\\echo start\nSELECT doltgres_test_fold('logged_fold');\n");
+$logged->wait_for_event('client backend', 'doltgres-fold-installed');
+my $saver = $landed->();
+is( $logged->safe_psql(
+ 'postgres',
+ "SELECT count(*) FROM pg_get_wal_records_info('$start', pg_current_wal_flush_lsn()) WHERE record_type = 'BaseAdvanced'"
+ ),
+ '1',
+ "a save's manifest names a fold's base only once the base's record is flushed"
+);
+$logged->stop('immediate');
+eval { $folder->quit };
+eval { $saver->quit };
+$logged->start;
+is($logged->safe_psql('postgres', 'SELECT count(*) FROM logged_fold'),
+ '1', "and after a crash the restart reads the folded row once");
+is( $logged->safe_psql(
+ 'postgres', "SELECT doltgres_test_held('logged_fold', '(0,1)')"),
+ 'f',
+ "and the replay of the fold's record takes its version out of the delta");
+
+# A save whose own base tree's pack has a deferred add, and a crash: the save
+# listed the pack before its manifest named it, so the restart finds every
+# root the manifest names.
+$logged->safe_psql('postgres',
+ "CREATE TABLE deferred_pack (k int4 PRIMARY KEY, v text); INSERT INTO deferred_pack SELECT g, 'd' || g FROM generate_series(1, 100) g"
+);
+$logged->safe_psql('postgres', "SELECT doltgres_test_fold('deferred_pack')");
+$saver = $landed->();
+$logged->stop('immediate');
+eval { $saver->quit };
+$logged->start;
+is( $logged->safe_psql('postgres', 'SELECT count(*) FROM deferred_pack'),
+ '100',
+ "after a crash past a save, the restart finds every root its manifest names"
+);
+
+# A table made with an asynchronous commit, a save that folds its identity
+# pair, and a crash: the save flushed the commit's record, so the restart has
+# the table with its pair.
+my $async = $logged->background_psql('postgres');
+$async->query_safe(
+ "SET synchronous_commit = off; CREATE TABLE async_made (k int4)");
+my $made_oid =
+ $logged->safe_psql('postgres', "SELECT 'async_made'::regclass::oid");
+my $made_id = $logged->safe_psql('postgres',
+ "SELECT doltgres_test_identity(2, $made_oid, 0)");
+$saver = $landed->();
+$logged->stop('immediate');
+eval { $async->quit };
+eval { $saver->quit };
+$logged->start;
+is( $logged->safe_psql(
+ 'postgres', "SELECT count(*) FROM pg_class WHERE oid = $made_oid"),
+ '1',
+ "after a crash past a save that folded an asynchronous commit's identity pair, the table is there"
+);
+is( $logged->safe_psql(
+ 'postgres', "SELECT doltgres_test_identity(2, $made_oid, 0)"),
+ $made_id,
+ "with its pair");
+
+# A save past its fold point and a fold paused between its record's insertion
+# and its base's publish (a fold point waits out the install's checkpoint
+# delay, so only a save that took its point first gathers in the window): a
+# reader reads the row once across the save and the publish, and the save
+# names the base the fold replaces, so after a crash the restart reads each
+# row once.
+my $pointed = sub {
+ my $saver = $logged->background_psql('postgres', on_error_stop => 0);
+ $saver->query_safe('SELECT injection_points_set_local()');
+ $saver->query_safe(
+ "SELECT injection_points_attach('doltgres-runs-save-pointed', 'wait')"
+ );
+ $saver->query_until(qr/start/,
+ "\\echo start\nSELECT doltgres_test_save();\n");
+ $logged->wait_for_event('client backend', 'doltgres-runs-save-pointed');
+ return $saver;
+};
+my $logging = sub {
+ my ($table) = @_;
+ my $logger = $logged->background_psql('postgres', on_error_stop => 0);
+ $logger->query_safe('SELECT injection_points_set_local()');
+ $logger->query_safe(
+ "SELECT injection_points_attach('doltgres-state-logging', 'wait')");
+ $logger->query_until(qr/start/,
+ "\\echo start\nSELECT doltgres_test_fold('$table');\n");
+ $logged->wait_for_event('client backend', 'doltgres-state-logging');
+ return $logger;
+};
+my $saved = sub {
+ my ($saver) = @_;
+ $logged->safe_psql('postgres',
+ "SELECT injection_points_wakeup('doltgres-runs-save-pointed')");
+ $logged->poll_query_until('postgres',
+ "SELECT count(*) = 0 FROM pg_stat_activity WHERE query LIKE 'SELECT doltgres_test_save()%' AND state = 'active'"
+ ) or die "the save never finished";
+ $saver->quit;
+};
+$logged->safe_psql('postgres',
+ 'CREATE TABLE logging_fold (k int4, v text); CHECKPOINT');
+$logged->safe_psql('postgres', "INSERT INTO logging_fold VALUES (1, 'p')");
+$saver = $pointed->();
+$folder = $logging->('logging_fold');
+my $reader = $logged->background_psql('postgres');
+is( $reader->query_safe(
+ 'BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT count(*) FROM logging_fold'
+ ),
+ '1',
+ "a reader while a fold's record is in and its base unpublished reads the row once"
+);
+$saved->($saver);
+$logged->safe_psql('postgres',
+ "SELECT injection_points_wakeup('doltgres-state-logging')");
+$folder->quit;
+is($reader->query_safe('SELECT count(*) FROM logging_fold'),
+ '1', "and still once after a save in the window and the base's publish");
+$reader->query_safe('COMMIT');
+$reader->quit;
+my $second = $logged->safe_psql('postgres',
+ "SELECT pg_current_wal_insert_lsn() - '0/0'::pg_lsn");
+$logged->safe_psql('postgres', "INSERT INTO logging_fold VALUES (2, 'q')");
+$saver = $pointed->();
+$folder = $logging->('logging_fold');
+$saved->($saver);
+$logged->stop('immediate');
+eval { $folder->quit };
+$logged->start;
+my (undef, $folded) = split / /,
+ $logged->safe_psql('postgres',
+ "SELECT doltgres_test_base_points('logging_fold')");
+is( ($folded < $second ? 'below' : 'past') . ' '
+ . $logged->safe_psql(
+ 'postgres', "SELECT doltgres_test_held('logging_fold', '(0,2)')"),
+ 'below t',
+ "after a crash past a save that gathered before a base was published, the base is the one the save named and the delta holds the later row"
+);
+is($logged->safe_psql('postgres', 'SELECT count(*) FROM logging_fold'),
+ '2', "and the restart reads each row once");
+
+# An error inside a logging install's window stops the server, and recovery
+# replays the records before it, so the row reads once.
+$logged->safe_psql('postgres',
+ "CREATE TABLE stopped_fold (k int4, v text); INSERT INTO stopped_fold VALUES (1, 's')"
+);
+$folder = $logged->background_psql('postgres', on_error_stop => 0);
+$folder->query_safe('SELECT injection_points_set_local()');
+$folder->query_safe(
+ "SELECT injection_points_attach('doltgres-state-logging', 'error')");
+eval { $folder->query("SELECT doltgres_test_fold('stopped_fold')") };
+eval { $folder->quit };
+ok( $logged->log_contains(
+ 'PANIC: error triggered for injection point doltgres-state-logging'),
+ "an error inside a logging install's window stops the server");
+$logged->poll_query_until('postgres', 'SELECT true')
+ or die "the server never came back after the stop";
+is($logged->safe_psql('postgres', 'SELECT count(*) FROM stopped_fold'),
+ '1', "and recovery reads the row once");
+
+# Bugs row 354: no record a process logs from a base's content lands before
+# the base's own record, since a base is published only after its record is
+# in. VACUUM reading the previous base while a fold's record is in and its
+# base unpublished logs its frees after the fold's record, and a standby that
+# replays both reads every row at the tuple id its primary does.
+my $vacuuming = PostgreSQL::Test::Cluster->new('vacuuming');
+$vacuuming->init(allows_streaming => 1);
+$vacuuming->append_conf('postgresql.conf',
+ "autovacuum = off\nfold_threshold = 1GB\nfold_timeout = 3600s\ncheckpoint_timeout = 1h"
+);
+$vacuuming->start;
+$vacuuming->safe_psql('postgres',
+ 'CREATE EXTENSION doltgres_test; CREATE EXTENSION injection_points');
+$vacuuming->backup('vacuuming_backup');
+my $vacuumed = PostgreSQL::Test::Cluster->new('vacuumed');
+$vacuumed->init_from_backup($vacuuming, 'vacuuming_backup',
+ has_streaming => 1);
+$vacuumed->start;
+$vacuuming->safe_psql('postgres',
+ "CREATE TABLE vacuumed (k int4, v text); INSERT INTO vacuumed SELECT g, 'v' || g FROM generate_series(1, 20) g"
+);
+$vacuuming->safe_psql('postgres', "SELECT doltgres_test_fold('vacuumed')");
+$vacuuming->safe_psql('postgres', 'DELETE FROM vacuumed WHERE k <= 5');
+$vacuuming->safe_psql('postgres', "SELECT doltgres_test_fold('vacuumed')");
+$vacuuming->safe_psql('postgres', "INSERT INTO vacuumed VALUES (21, 'w')");
+my $windowed = $vacuuming->background_psql('postgres', on_error_stop => 0);
+$windowed->query_safe('SELECT injection_points_set_local()');
+$windowed->query_safe(
+ "SELECT injection_points_attach('doltgres-state-logging', 'wait')");
+$windowed->query_until(qr/start/,
+ "\\echo start\nSELECT doltgres_test_fold('vacuumed');\n");
+$vacuuming->wait_for_event('client backend', 'doltgres-state-logging');
+$vacuuming->safe_psql('postgres',
+ "SELECT doltgres_test_vacuum('vacuumed', 'on')");
+$vacuuming->safe_psql('postgres',
+ "SELECT injection_points_wakeup('doltgres-state-logging')");
+$windowed->quit;
+$vacuuming->safe_psql('postgres',
+ "INSERT INTO vacuumed SELECT g, 'x' || g FROM generate_series(22, 30) g");
+$vacuuming->safe_psql('postgres', "SELECT doltgres_test_fold('vacuumed')");
+$vacuuming->wait_for_replay_catchup($vacuumed);
+my $placed =
+ "SELECT string_agg(ctid::text || ':' || k, ' ' ORDER BY ctid) FROM vacuumed";
+is( $vacuumed->safe_psql('postgres', $placed),
+ $vacuuming->safe_psql('postgres', $placed),
+ "a standby replaying VACUUM's frees logged in a fold's window reads every row at its primary's tuple id"
+);
+$vacuumed->stop;
+$vacuuming->stop;
+
+$logged->stop;
+
+done_testing();
diff --git a/src/doltgres/test/t/135_moved_takes.pl b/src/doltgres/test/t/135_moved_takes.pl
new file mode 100644
index 0000000000000..a65694eec14ae
--- /dev/null
+++ b/src/doltgres/test/t/135_moved_takes.pl
@@ -0,0 +1,258 @@
+# Copyright 2026 Dolthub, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# A free space take whose block's entry a spill moved after its caller read
+# it finishes on the entry the spill left, not on the one under the memtable
+# it read before (bugs row 342), which lost the spilled takes: a VACUUM whose
+# block's running writer spills, which checks the entry under its block's
+# range, kept locked through its window, before it logs its record and reads
+# again outside the window when the entry moved; and a standby's replayed
+# leave of the fresh page whose prepare the full budget refuses, the
+# startup's relief spilling the replayed writer's takes of that page.
+
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+if ($ENV{enable_injection_points} ne 'yes')
+{
+ plan skip_all => 'Injection points not supported by this build';
+}
+
+my $spilling = PostgreSQL::Test::Cluster->new('vacuum_spilling');
+$spilling->init;
+$spilling->append_conf('postgresql.conf',
+ "fold_threshold = 1GB\nfold_timeout = 3600s\nautovacuum = off");
+$spilling->start;
+$spilling->safe_psql('postgres',
+ 'CREATE EXTENSION injection_points; CREATE EXTENSION doltgres_test');
+
+# freed makes a table whose block 0 holds a folded delete VACUUM frees.
+sub freed
+{
+ my ($table) = @_;
+ $spilling->safe_psql(
+ 'postgres', qq{
+CREATE TABLE $table (k int4, v text) WITH (fillfactor = 50);
+INSERT INTO $table VALUES (1, repeat('v', 600)), (2, repeat('v', 600));
+DELETE FROM $table WHERE k = 1;
+SELECT doltgres_test_fold('$table');
+});
+}
+freed('spilled_block');
+my $spilling_writer = $spilling->background_psql('postgres');
+$spilling_writer->query_safe(
+ "BEGIN; INSERT INTO spilled_block VALUES (3, repeat('v', 600))");
+my $spilled_vacuum =
+ $spilling->background_psql('postgres', on_error_stop => 0);
+$spilled_vacuum->query_safe('SELECT injection_points_set_local()');
+$spilled_vacuum->query_safe(
+ "SELECT injection_points_attach('doltgres-vacuum-read', 'wait')");
+$spilled_vacuum->query_until(qr/started/,
+ "\\echo started\nSELECT doltgres_test_vacuum('spilled_block', 'auto');\n"
+);
+$spilling->wait_for_event('client backend', 'doltgres-vacuum-read');
+is( join(
+ ' ',
+ map {
+ $spilling_writer->query_safe(
+ 'SELECT doltgres_test_delta(61, 0, 0)')
+ } 1 .. 3),
+ '1 1 0',
+ "the block's writer spills its rows and then its free space takes while VACUUM waits after its read"
+);
+$spilling->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-vacuum-read'); SELECT injection_points_wakeup('doltgres-vacuum-read')"
+);
+$spilled_vacuum->query_safe('SELECT 1');
+$spilled_vacuum->quit;
+$spilling_writer->query_safe('COMMIT');
+$spilling_writer->quit;
+freed('unspilled_block');
+$spilling->safe_psql(
+ 'postgres', q{
+INSERT INTO unspilled_block VALUES (3, repeat('v', 600));
+SELECT doltgres_test_vacuum('unspilled_block', 'auto');
+});
+my $spilled_entry = q{SELECT doltgres_test_free_space('%s', 0)};
+is( $spilling->safe_psql(
+ 'postgres', sprintf($spilled_entry, 'spilled_block')),
+ $spilling->safe_psql(
+ 'postgres', sprintf($spilled_entry, 'unspilled_block')),
+ "VACUUM's give-back after a spill moved its block's takes leaves the entry an unspilled VACUUM leaves"
+);
+
+# The entry's move found before VACUUM's window sends it to read the block
+# again outside the window, where an error is an ordinary one that fails the
+# VACUUM alone, and a later VACUUM gives the place back as an unspilled one.
+freed('erred_block');
+$spilling_writer = $spilling->background_psql('postgres');
+$spilling_writer->query_safe(
+ "BEGIN; INSERT INTO erred_block VALUES (3, repeat('v', 600))");
+$spilled_vacuum = $spilling->background_psql('postgres', on_error_stop => 0);
+$spilled_vacuum->query_safe('SELECT injection_points_set_local()');
+$spilled_vacuum->query_safe(
+ "SELECT injection_points_attach('doltgres-vacuum-read', 'wait')");
+$spilled_vacuum->query_until(qr/started/,
+ "\\echo started\nSELECT doltgres_test_vacuum('erred_block', 'auto');\n");
+$spilling->wait_for_event('client backend', 'doltgres-vacuum-read');
+$spilling_writer->query_safe('SELECT doltgres_test_delta(61, 0, 0)')
+ for 1 .. 3;
+$spilling->safe_psql(
+ 'postgres', q{
+SELECT injection_points_detach('doltgres-vacuum-read');
+SELECT injection_points_attach('doltgres-vacuum-read', 'error');
+SELECT injection_points_wakeup('doltgres-vacuum-read');
+});
+$spilled_vacuum->query('SELECT 1');
+$spilling->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-vacuum-read')");
+like(
+ $spilled_vacuum->{stderr},
+ qr/ERROR: error triggered for injection point doltgres-vacuum-read/,
+ "an error at VACUUM's read again after its block's entry moved fails the VACUUM as an ordinary error"
+);
+$spilled_vacuum->quit;
+$spilling_writer->query_safe('COMMIT');
+$spilling_writer->quit;
+$spilling->safe_psql('postgres',
+ "SELECT doltgres_test_vacuum('erred_block', 'auto')");
+is( $spilling->safe_psql('postgres', sprintf($spilled_entry, 'erred_block')),
+ $spilling->safe_psql(
+ 'postgres', sprintf($spilled_entry, 'unspilled_block')),
+ "a VACUUM after the failed one leaves the entry an unspilled VACUUM leaves"
+);
+
+# VACUUM keeps its block's range locked from before its window to its last
+# take there, so a spill's take-out of the block's free space takes that
+# begins while VACUUM is in its window waits on that lock, which VACUUM
+# releases waiting on nothing, and both finish.
+# toasted makes a table whose block 0 holds a folded delete of a row with a
+# TOAST value, whose take-down VACUUM pauses in its window before.
+sub toasted
+{
+ my ($table) = @_;
+ $spilling->safe_psql(
+ 'postgres', qq{
+CREATE TABLE $table (k int4, v text);
+ALTER TABLE $table ALTER COLUMN v SET STORAGE EXTERNAL;
+INSERT INTO $table VALUES (1, repeat('t', 3000)), (2, repeat('t', 3000));
+DELETE FROM $table WHERE k = 1;
+SELECT doltgres_test_fold('$table');
+});
+}
+toasted('locked_block');
+$spilling_writer = $spilling->background_psql('postgres');
+$spilling_writer->query_safe(
+ "BEGIN; INSERT INTO locked_block VALUES (3, 'short')");
+$spilling_writer->query_safe('SELECT doltgres_test_delta(61, 0, 0)');
+$spilled_vacuum = $spilling->background_psql('postgres');
+$spilled_vacuum->query_safe(
+ "SELECT injection_points_set_local(); SELECT injection_points_attach('doltgres-vacuum-logged', 'wait')"
+);
+$spilled_vacuum->query_until(qr/started/,
+ "\\echo started\nSELECT doltgres_test_vacuum('locked_block', 'auto');\n");
+$spilling->wait_for_event('client backend', 'doltgres-vacuum-logged');
+my $writer_pid = $spilling_writer->query_safe('SELECT pg_backend_pid()');
+$spilling_writer->query_until(qr/spilling/,
+ "\\echo spilling\nSELECT doltgres_test_delta(61, 0, 0);\n");
+ok( $spilling->poll_query_until(
+ 'postgres',
+ "SELECT wait_event_type = 'LWLock' FROM pg_stat_activity WHERE pid = $writer_pid"
+ ),
+ "a spill's take-out of the block's takes waits on the range VACUUM keeps through its window"
+);
+$spilling->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-vacuum-logged'); SELECT injection_points_wakeup('doltgres-vacuum-logged')"
+);
+like($spilled_vacuum->query_safe('SELECT 1'),
+ qr/^1\z/m, "VACUUM finishes its window");
+is($spilling_writer->query_safe('SELECT 1'),
+ "1\n1", "and then the spill finishes, having spilled");
+$spilled_vacuum->quit;
+$spilling_writer->query_safe('COMMIT');
+$spilling_writer->quit;
+toasted('unlocked_block');
+$spilling->safe_psql(
+ 'postgres', q{
+INSERT INTO unlocked_block VALUES (3, 'short');
+SELECT doltgres_test_vacuum('unlocked_block', 'auto');
+});
+is( $spilling->safe_psql('postgres', sprintf($spilled_entry, 'locked_block')),
+ $spilling->safe_psql(
+ 'postgres', sprintf($spilled_entry, 'unlocked_block')),
+ "VACUUM and the spill that waited on its range leave the entry an unspilled VACUUM leaves"
+);
+$spilling->stop;
+
+
+# A 1MB standby's replay of the row that leaves the fresh page holds its jump
+# after it read the page's entry, the budget filled, so the jump's refused
+# prepare relieves by spilling the replayed writer's free space takes of the
+# page; the jump reads the entry the spill left, and the page's entry is its
+# primary's.
+my $PAGE = (8 << 32) | (1024 - 64);
+my $jumping = PostgreSQL::Test::Cluster->new('jumping');
+$jumping->init(allows_streaming => 1);
+$jumping->append_conf('postgresql.conf',
+ "fold_threshold = 1GB\nfold_timeout = 3600s\nautovacuum = off\ncheckpoint_timeout = 1h"
+);
+$jumping->start;
+$jumping->safe_psql('postgres',
+ 'CREATE EXTENSION injection_points; CREATE EXTENSION doltgres_test');
+$jumping->backup('jumping_backup');
+my $jumping_standby = PostgreSQL::Test::Cluster->new('jumping_standby');
+$jumping_standby->init_from_backup($jumping, 'jumping_backup',
+ has_streaming => 1);
+$jumping_standby->append_conf('postgresql.conf',
+ "delta_memory = 1MB\nfold_threshold = 1GB\nfold_timeout = 3600s\ncheckpoint_timeout = 1h"
+);
+$jumping_standby->start;
+$jumping->safe_psql('postgres', 'CREATE TABLE jumped (k int4)');
+$jumping->wait_for_catchup($jumping_standby);
+$jumping_standby->safe_psql('postgres',
+ "SELECT injection_points_attach('doltgres-insert-jump-seen', 'wait')");
+$jumping->safe_psql('postgres',
+ 'INSERT INTO jumped SELECT g FROM generate_series(1, 300) g');
+my $jumped =
+ $jumping->safe_psql('postgres', 'SELECT pg_current_wal_insert_lsn()');
+$jumping_standby->wait_for_event('startup', 'doltgres-insert-jump-seen');
+my $full = $jumping_standby->safe_psql('postgres',
+ 'SELECT doltgres_test_shared(0, 0, 0)');
+$jumping_standby->safe_psql('postgres',
+ "SELECT doltgres_test_delta(20, $full, $PAGE)");
+$jumping_standby->safe_psql('postgres',
+ "SELECT injection_points_detach('doltgres-insert-jump-seen'); SELECT injection_points_wakeup('doltgres-insert-jump-seen')"
+);
+ok( $jumping_standby->poll_query_until(
+ 'postgres',
+ "SELECT pg_last_wal_replay_lsn() >= '$jumped'::pg_lsn OR (SELECT count(*) = 1 FROM pg_stat_activity WHERE backend_type = 'startup' AND wait_event = 'versioned delta budget')"
+ ),
+ "the standby's startup goes past its refused jump, or waits at its full budget with its relief spent"
+);
+$jumping_standby->safe_psql('postgres',
+ "SELECT doltgres_test_delta(21, $full, 0)");
+$jumping->wait_for_catchup($jumping_standby);
+my $jumped_entries =
+ q{SELECT string_agg(b || ':' || doltgres_test_free_space('jumped', b), ' ' ORDER BY b) FROM generate_series(0, (pg_relation_size('jumped') / 8192 - 1)::int4) b};
+is( $jumping_standby->safe_psql('postgres', $jumped_entries),
+ $jumping->safe_psql('postgres', $jumped_entries),
+ "a 1MB standby whose replayed leave's read a spill at its refused prepare moved holds its primary's free space entries"
+);
+$jumping_standby->stop;
+$jumping->stop;
+
+done_testing();
diff --git a/src/doltgres/trees/c/cursor.c b/src/doltgres/trees/c/cursor.c
index 24aa134797077..de4783430fc6b 100644
--- a/src/doltgres/trees/c/cursor.c
+++ b/src/doltgres/trees/c/cursor.c
@@ -1070,6 +1070,34 @@ trees_cursor_Cursor_index(const TreesCursorCursor *self)
return trees_cursor_Cursor_bottom(self)->idx;
}
+/*
+ * trees_cursor_Cursor_find_in_node moves the cursor to the entry of its own
+ * node whose key is the key, by a search of that node alone, and answers
+ * whether the node holds one, leaving the cursor where it was otherwise: a
+ * tree's keys are unique, so an entry found is the tree's entry of the key.
+ */
+bool
+trees_cursor_Cursor_find_in_node(TreesCursorCursor *self, HostIoIo *io, const uint8 *key, size_t key_len,
+ const TreesKeyOrder *order, bool *result, ChunksErrorError *error)
+{
+ TreesNodeNode node = trees_cursor_Cursor_node(self);
+ size_t at;
+ StdBytes found;
+
+ *result = false;
+ if (!trees_node_Node_search(&node, io, key, key_len, order, &at, error))
+ return false;
+ if (at >= trees_node_Node_len(&node))
+ return true;
+ found = trees_node_Node_key(&node, at);
+ if (found.len != key_len || memcmp(found.ptr, key, key_len) != 0)
+ return true;
+ STD_VEC_AT(&self->path, TreesCursorFrame, std_wrapping_sub_usize(self->path.len, 1)).idx = at;
+ self->valid = true;
+ *result = true;
+ return true;
+}
+
/* trees_cursor_Cursor_key returns the key of the entry the cursor is at. */
StdBytes
trees_cursor_Cursor_key(const TreesCursorCursor *self)
@@ -1690,7 +1718,7 @@ trees_cursor_LeafStamps_Default_default(void)
return result;
}
-/* trees_cursor_LeafStamps_of_frame_0_t is of's state across its pad: the pair it took from the cache, while live. */
+/* trees_cursor_LeafStamps_of_frame_0_t is of's and stamp_of's state across their pad: the pair taken from the cache, while live, and their answers. */
typedef struct trees_cursor_LeafStamps_of_frame_0_t
{
TreesCursorLeafStamps *self;
@@ -1700,22 +1728,23 @@ typedef struct trees_cursor_LeafStamps_of_frame_0_t
trees_TupleTreeArcStamps_t taken;
bool taken_live;
StdVec *result;
+ uint64 tuple;
+ uint64 *copies;
+ bool *some;
+ ChunksTrailerStamp *stamp;
ChunksErrorError *error;
bool ok;
} trees_cursor_LeafStamps_of_frame_0_t;
/*
- * trees_cursor_LeafStamps_of_body_0 is of's body: the cached pair taken, put
- * back when it is the cursor's leaf's, or else the leaf's stamps read and
- * stored, the taken pair dropped after, at the end of the let.
+ * trees_cursor_LeafStamps_read is of's and stamp_of's read: the cached pair
+ * taken, put back when it is the cursor's leaf's, or else the leaf's stamps
+ * read and stored, the taken pair dropped after, at the end of the let.
*/
-static void
-trees_cursor_LeafStamps_of_body_0(void *frame_)
+static bool
+trees_cursor_LeafStamps_read(trees_cursor_LeafStamps_of_frame_0_t *frame)
{
- trees_cursor_LeafStamps_of_frame_0_t *frame = frame_;
TreesStoreTree node = trees_cursor_Cursor_node_tree(frame->cursor);
- const ChunksTrailerStamps *stamps;
- size_t index;
frame->taken_live = frame->self->has_f0;
frame->taken = frame->self->f0;
@@ -1737,8 +1766,7 @@ trees_cursor_LeafStamps_of_body_0(void *frame_)
frame->taken_live = false;
trees_cursor_ArcStamps_drop(frame->taken.f1);
}
- frame->ok = false;
- return;
+ return false;
}
frame->self->f0.f0 = node;
frame->self->f0.f1 = read;
@@ -1749,9 +1777,40 @@ trees_cursor_LeafStamps_of_body_0(void *frame_)
trees_cursor_ArcStamps_drop(frame->taken.f1);
}
}
+ return true;
+}
+
+/* trees_cursor_LeafStamps_of_body_0 is of's body. */
+static void
+trees_cursor_LeafStamps_of_body_0(void *frame_)
+{
+ trees_cursor_LeafStamps_of_frame_0_t *frame = frame_;
+
+ if (!trees_cursor_LeafStamps_read(frame))
+ return;
+ chunks_trailer_Stamps_entry(std_arc_Deref_deref(frame->self->f0.f1, _Alignof(ChunksTrailerStamps)),
+ trees_cursor_Cursor_index(frame->cursor), frame->result);
+ frame->ok = true;
+}
+
+/* trees_cursor_LeafStamps_stamp_of_body_0 is stamp_of's body. */
+static void
+trees_cursor_LeafStamps_stamp_of_body_0(void *frame_)
+{
+ trees_cursor_LeafStamps_of_frame_0_t *frame = frame_;
+ const ChunksTrailerStamps *stamps;
+ size_t index;
+ const uint32 *values;
+ size_t values_len;
+
+ if (!trees_cursor_LeafStamps_read(frame))
+ return;
stamps = std_arc_Deref_deref(frame->self->f0.f1, _Alignof(ChunksTrailerStamps));
index = trees_cursor_Cursor_index(frame->cursor);
- chunks_trailer_Stamps_entry(stamps, index, frame->result);
+ *frame->copies = chunks_trailer_Stamps_entry_len(stamps, index);
+ *frame->some = chunks_trailer_Stamps_stamp_of(stamps, index, frame->tuple, frame->stamp);
+ values = chunks_trailer_Stamps_values(stamps, index, &values_len);
+ std_slice_to_vec_Checked(frame->result, values, values_len, sizeof(uint32), _Alignof(uint32));
frame->ok = true;
}
@@ -1781,6 +1840,25 @@ trees_cursor_LeafStamps_of(TreesCursorLeafStamps *self, HostIoIo *io, TreesPinPi
return frame.ok;
}
+/*
+ * trees_cursor_LeafStamps_stamp_of returns the number of row stamps of the
+ * leaf entry a cursor is at, one for each copy of a row it counts, its stamp
+ * of a tuple id, or nothing when it has none, by a search of the leaf's
+ * stamps, and its row's value ids as Cursor's values gives them, the leaf's
+ * stamps read only when the cursor is in another leaf than the last.
+ */
+bool
+trees_cursor_LeafStamps_stamp_of(TreesCursorLeafStamps *self, HostIoIo *io, TreesPinPin *pin,
+ const TreesCursorCursor *cursor, uint64 tuple, uint64 *copies, bool *some,
+ ChunksTrailerStamp *result, StdVec *values, ChunksErrorError *error)
+{
+ trees_cursor_LeafStamps_of_frame_0_t frame = {.self = self, .io = io, .pin = pin, .cursor = cursor,
+ .result = values, .tuple = tuple, .copies = copies, .some = some, .stamp = result, .error = error};
+
+ std_landing(&frame, trees_cursor_LeafStamps_stamp_of_body_0, trees_cursor_LeafStamps_of_pad_0);
+ return frame.ok;
+}
+
/* trees_cursor_LeafStamps_drop is LeafStamps' drop glue: its leaf's stamps, when it holds them. */
void
trees_cursor_LeafStamps_drop(TreesCursorLeafStamps *self)
diff --git a/src/doltgres/txn/c/base.c b/src/doltgres/txn/c/base.c
index 940ef83a33151..62c1c53d9b8aa 100644
--- a/src/doltgres/txn/c/base.c
+++ b/src/doltgres/txn/c/base.c
@@ -45,9 +45,9 @@
#define RUST_FILE "txn" STD_SEP "src" STD_SEP "base.rs"
/*
- * The record's field numbers, in the format's order, TABLE, MAX_ENDED, and
- * CHAIN always present, CHAIN the highest-numbered and last, so a record cut
- * short anywhere is refused.
+ * The record's field numbers, in the format's order, TABLE, MAX_ENDED, CHAIN,
+ * and READ always present, READ the highest-numbered and last, so a record
+ * cut short anywhere is refused.
*/
#define TXN_BASE_ROOT 1
#define TXN_BASE_FOLDED 2
@@ -64,6 +64,7 @@
#define TXN_BASE_PENDING 13
#define TXN_BASE_MAX_ENDED 14
#define TXN_BASE_CHAIN 16
+#define TXN_BASE_READ 18
/* TXN_BASE_ATTNUM_MAX is Postgres' MaxHeapAttributeNumber, the largest attribute number a key's column has. */
#define TXN_BASE_ATTNUM_MAX UINT64CONST(1600)
@@ -316,7 +317,7 @@ txn_base_Base_PartialEq_eq(const TxnBaseBase *self, const TxnBaseBase *other)
txn_base_Key_PartialEq_eq(&self->key, &other->key) &&
txn_base_tree_eq(self->has_pending, &self->pending, other->has_pending, &other->pending) &&
self->max_ended == other->max_ended &&
- txn_base_tree_eq(self->has_chain, &self->chain, other->has_chain, &other->chain);
+ txn_base_tree_eq(self->has_chain, &self->chain, other->has_chain, &other->chain) && self->read == other->read;
}
/* txn_base_Base_Debug_fmt is Base's derived Debug. */
@@ -347,6 +348,7 @@ txn_base_Base_Debug_fmt(const void *value, StdFormatter *f)
std_DebugStruct_field(&s, "pending", 7, &pending, std_option_Debug_fmt);
std_DebugStruct_field(&s, "max_ended", 9, &self->max_ended, std_u64_Debug_fmt);
std_DebugStruct_field(&s, "chain", 5, &chain, std_option_Debug_fmt);
+ std_DebugStruct_field(&s, "read", 4, &self->read, std_u64_Debug_fmt);
return std_DebugStruct_finish(&s);
}
@@ -905,7 +907,9 @@ txn_base_pair_value(const uint8 *key, size_t key_len, uint32 heap_bytes, StdVec
/*
* txn_base_pending_value returns a pending tuple id's entry's value in the
* base's pending tree: its row's ender's full xid, 0 when the ender aborted,
- * then its heap bytes, then the value ids its row names, each little-endian.
+ * with TXN_BASE_OWN set when the ender is the row's own writer, then its heap
+ * bytes, 0 when a prune already gave them back, then the value ids its row
+ * names, each little-endian.
*/
void
txn_base_pending_value(uint64 ender, uint32 heap_bytes, const uint32 *ids, size_t ids_len, StdVec *result)
@@ -924,7 +928,7 @@ txn_base_pending_value(uint64 ender, uint32 heap_bytes, const uint32 *ids, size_
/*
* txn_base_pending_read reads a pending tuple id's entry's value as
* pending_value writes it, refusing a length that isn't 12 bytes and a whole
- * number of value ids, and heap bytes of 0, as data corruption.
+ * number of value ids as data corruption.
*/
bool
txn_base_pending_read(const uint8 *value, size_t value_len, uint64 *result_0, uint32 *result_1, StdVec *result_2,
@@ -932,7 +936,7 @@ txn_base_pending_read(const uint8 *value, size_t value_len, uint64 *result_0, ui
{
size_t ids;
- if (value_len < 12 || (value_len - 12) % 4 != 0 || std_u32_from_le_bytes(value + 8) == 0)
+ if (value_len < 12 || (value_len - 12) % 4 != 0)
{
*error = TXN_BASE_FORK("XX001", "a pending tuple id's entry is corrupt");
return false;
@@ -1148,6 +1152,7 @@ txn_base_encode_body(void *frame_)
txn_base_address(&frame->writer, TXN_BASE_CHAIN, true, &self->chain);
else
chunks_record_RecordWriter_field(&frame->writer, TXN_BASE_CHAIN, TXN_BASE_NO_BYTES, 0);
+ chunks_record_RecordWriter_uint(&frame->writer, TXN_BASE_READ, self->read);
frame->writer_live = false;
chunks_record_RecordWriter_finish_value(&frame->writer, frame->result_0, frame->result_1);
}
@@ -1230,13 +1235,13 @@ txn_base_decode_fields_free(txn_base_decode_frame_t *frame)
* outside a range and isn't another number.
*/
static bool
-txn_base_decode_outside(const txn_base_decode_frame_t *frame, uint32 least, uint32 most, uint32 other)
+txn_base_decode_outside(const txn_base_decode_frame_t *frame, uint32 least, uint32 most, uint32 other, uint32 last)
{
for (size_t i = 0; i < frame->fields.len; i++)
{
uint32 number = STD_VEC_AT(&frame->fields, ChunksRecordField, i).number;
- if ((number < least || number > most) && number != other)
+ if ((number < least || number > most) && number != other && number != last)
return true;
}
return false;
@@ -1416,7 +1421,7 @@ txn_base_decode_body(void *frame_)
frame->record_live = true;
if (!txn_base_decode_fields(frame, frame->bytes, frame->bytes_len))
return;
- if (txn_base_decode_outside(frame, TXN_BASE_ROOT, TXN_BASE_MAX_ENDED, TXN_BASE_CHAIN))
+ if (txn_base_decode_outside(frame, TXN_BASE_ROOT, TXN_BASE_MAX_ENDED, TXN_BASE_CHAIN, TXN_BASE_READ))
{
*frame->error = txn_base_corrupt();
return;
@@ -1431,7 +1436,7 @@ txn_base_decode_body(void *frame_)
if (!txn_base_decode_nested(frame, &frame->record, sizes, &frame->held, &frame->held_live) ||
!txn_base_decode_fields(frame, sizes->bytes, sizes->bytes_len))
return;
- if (txn_base_decode_outside(frame, 1, 5, 1))
+ if (txn_base_decode_outside(frame, 1, 5, 1, 1))
{
*frame->error = txn_base_corrupt();
return;
@@ -1483,6 +1488,8 @@ txn_base_decode_body(void *frame_)
}
if (chain->bytes_len != 0 && !txn_base_decode_tree(frame, TXN_BASE_CHAIN, &base.has_chain, &base.chain))
return;
+ if (!txn_base_record_uint(&frame->record, TXN_BASE_READ, 0, base.folded, &base.read, frame->error))
+ return;
frame->columns_live = false;
*frame->result = base;
frame->ok = true;
@@ -1572,6 +1579,7 @@ txn_base_tests_full(void)
.has_pending = true, .pending = txn_base_tests_tree(4),
.max_ended = PG_UINT64_MAX,
.has_chain = true, .chain = txn_base_tests_tree(5),
+ .read = PG_UINT64_MAX,
};
return base;
@@ -1672,8 +1680,8 @@ txn_base_tests_round_trips(uint32 line, const TxnBaseBase *base)
void
test_txn_base_tests_a_base_record_round_trips_at_its_limits(void)
{
- TxnBaseBase bases[4] = {txn_base_tests_full(), txn_base_tests_empty(), txn_base_tests_full(),
- txn_base_tests_empty()};
+ TxnBaseBase bases[5] = {txn_base_tests_full(), txn_base_tests_empty(), txn_base_tests_full(),
+ txn_base_tests_empty(), txn_base_tests_empty()};
bases[2].has_index = false;
bases[2].index = 0;
@@ -1681,7 +1689,9 @@ test_txn_base_tests_a_base_record_round_trips_at_its_limits(void)
bases[3].index = 0;
bases[3].has_root = true;
bases[3].root = txn_base_tests_tree(9);
- for (int i = 0; i < 4; i++)
+ bases[4].folded = 200;
+ bases[4].read = 149;
+ for (int i = 0; i < lengthof(bases); i++)
txn_base_tests_round_trips(605, &bases[i]);
}
@@ -1765,6 +1775,7 @@ txn_base_tests_write_plain(ChunksRecordRecordWriter *writer, uint32 number)
case TXN_BASE_INDEX:
case TXN_BASE_KEY:
case 15:
+ case 17:
break;
case TXN_BASE_CHAIN:
chunks_record_RecordWriter_field(writer, TXN_BASE_CHAIN, TXN_BASE_NO_BYTES, 0);
@@ -1800,13 +1811,13 @@ txn_base_tests_write_changed(ChunksRecordRecordWriter *writer, const TxnBaseTest
txn_base_tests_write_plain(writer, number);
}
-/* txn_base_tests_write_form writes every field from ROOT to CHAIN in a form, but a skipped one, as a value. */
+/* txn_base_tests_write_form writes every field from ROOT to READ in a form, but a skipped one, as a value. */
static void
txn_base_tests_write_form(uint32 skip, const TxnBaseTestsForm *form, StdVec *bytes, StdVec *addresses)
{
ChunksRecordRecordWriter writer = chunks_record_RecordWriter_new();
- for (uint32 number = TXN_BASE_ROOT; number <= TXN_BASE_CHAIN; number++)
+ for (uint32 number = TXN_BASE_ROOT; number <= TXN_BASE_READ; number++)
if (number != skip)
txn_base_tests_write_changed(&writer, form, number);
chunks_record_RecordWriter_finish_value(&writer, bytes, addresses);
@@ -1822,9 +1833,9 @@ txn_base_tests_write_form(uint32 skip, const TxnBaseTestsForm *form, StdVec *byt
void
test_txn_base_tests_every_other_form_of_a_base_record_is_corrupt(void)
{
- static const uint32 required[] = {TXN_BASE_FOLDED, TXN_BASE_MAX_FOLDED, TXN_BASE_EPOCH, TXN_BASE_COUNTER, TXN_BASE_FRESH_END, TXN_BASE_SIZES, TXN_BASE_TABLE, TXN_BASE_MAX_ENDED, TXN_BASE_CHAIN};
+ static const uint32 required[] = {TXN_BASE_FOLDED, TXN_BASE_MAX_FOLDED, TXN_BASE_EPOCH, TXN_BASE_COUNTER, TXN_BASE_FRESH_END, TXN_BASE_SIZES, TXN_BASE_TABLE, TXN_BASE_MAX_ENDED, TXN_BASE_CHAIN, TXN_BASE_READ};
static const TxnBaseTestsForm past[] = {{TXN_BASE_TESTS_FORM_PAST, TXN_BASE_EPOCH, TXN_BASE_U32_MAX}, {TXN_BASE_TESTS_FORM_PAST, TXN_BASE_COUNTER, TXN_BASE_TUPLE_ID_MAX},
- {TXN_BASE_TESTS_FORM_PAST, TXN_BASE_FRESH_END, TXN_BASE_TUPLE_ID_MAX}, {TXN_BASE_TESTS_FORM_PAST, TXN_BASE_TABLE, TXN_BASE_U32_MAX}, {TXN_BASE_TESTS_FORM_PAST, TXN_BASE_INDEX, TXN_BASE_U32_MAX}};
+ {TXN_BASE_TESTS_FORM_PAST, TXN_BASE_FRESH_END, TXN_BASE_TUPLE_ID_MAX}, {TXN_BASE_TESTS_FORM_PAST, TXN_BASE_TABLE, TXN_BASE_U32_MAX}, {TXN_BASE_TESTS_FORM_PAST, TXN_BASE_INDEX, TXN_BASE_U32_MAX}, {TXN_BASE_TESTS_FORM_PAST, TXN_BASE_READ, 0}};
static const TxnBaseTestsForm plain = {TXN_BASE_TESTS_FORM_PLAIN};
StdVec bytes;
StdVec addresses;
@@ -1925,6 +1936,7 @@ txn_base_tests_keyed(const TxnBaseTestsKeyedCase *keyed_case, StdVec *bytes, Std
chunks_record_RecordWriter_uint(&writer, TXN_BASE_TABLE, 0);
chunks_record_RecordWriter_uint(&writer, TXN_BASE_MAX_ENDED, 0);
chunks_record_RecordWriter_field(&writer, TXN_BASE_CHAIN, TXN_BASE_NO_BYTES, 0);
+ chunks_record_RecordWriter_uint(&writer, TXN_BASE_READ, 0);
chunks_record_RecordWriter_finish_value(&writer, bytes, addresses);
}
diff --git a/src/doltgres/txn/c/blobs.c b/src/doltgres/txn/c/blobs.c
index 6083729a13310..b25e7048ce765 100644
--- a/src/doltgres/txn/c/blobs.c
+++ b/src/doltgres/txn/c/blobs.c
@@ -41,6 +41,18 @@
#include "doltgres/txn/spill.h"
#include "doltgres/txn/written.h"
+#ifdef DOLTGRES_TEST_TXN
+#include "doltgres/repo/lib.h"
+#include "doltgres/simc/simc.h"
+#include "doltgres/std/test.h"
+#include "doltgres/trees/blob.h"
+#include "doltgres/trees/chunker.h"
+#include "doltgres/txn/status.h"
+
+/* RUST_FILE is the file the tests' asserts name, blobs.rs. */
+#define RUST_FILE "txn" STD_SEP "src" STD_SEP "blobs.rs"
+#endif
+
/* txn_blobs_unread returns the error of a read of a cut, which a blob's write never makes. */
static ChunksErrorError
txn_blobs_unread(const ChunksAddressAddress *address)
@@ -768,3 +780,506 @@ txn_blobs_redo(HostIoIo *io, const HostSharedHeld *deltas, TxnRecordChunkWrite c
std_landing(&frame, txn_blobs_redo_body_0, txn_blobs_redo_pad_0);
return frame.ok;
}
+
+#ifdef DOLTGRES_TEST_TXN
+/* TXN_BLOBS_TESTS_UNWRAP is Result::unwrap's panic text. */
+#define TXN_BLOBS_TESTS_UNWRAP "called `Result::unwrap()` on an `Err` value"
+
+/* txn_blobs_tests_ok unwraps a host result at a line and column. */
+static void
+txn_blobs_tests_ok(bool ok, HostError *error, uint32 line, uint32 col)
+{
+ if (!ok)
+ STD_RESULT_EXPECT_FAILED(line, col, TXN_BLOBS_TESTS_UNWRAP, error, host_Error_Debug_fmt);
+}
+
+/* txn_blobs_tests_unpacked is the tests' packed closure: the packs hold no chunk. */
+static bool
+txn_blobs_tests_unpacked(void *captures, HostIoIo *io, const ChunksAddressAddress *address, bool *result,
+ HostError *error)
+{
+ (void) captures;
+ (void) io;
+ (void) address;
+ (void) error;
+ *result = false;
+ return true;
+}
+
+/* txn_blobs_tests_current returns the running actor's transaction as a chunk's writer, its xid assigned. */
+static TxnMemtableVersionWriter
+txn_blobs_tests_current(HostIoIo *io)
+{
+ HostXid sub;
+ HostXid top;
+ HostError error;
+ TxnMemtableVersionWriter writer = {0};
+
+ txn_blobs_tests_ok(host_current_writer(HOST_IO_IO_IO_DOORS(io), &sub, &top, &error), &error, 289, 51);
+ writer.xid = sub.f0;
+ writer.has_top = sub.f0 != top.f0;
+ writer.top = top.f0;
+ return writer;
+}
+
+/* txn_blobs_tests_chunk returns a test's one chunk of some bytes, by its address. */
+static StdVec
+txn_blobs_tests_chunk(const uint8 *bytes, size_t bytes_len)
+{
+ StdVec chunks;
+ txn_TupleAddressVecU8_t chunk = {.f0 = chunks_address_Address_of(bytes, bytes_len)};
+
+ std_vec_new(&chunks, sizeof(txn_TupleAddressVecU8_t), _Alignof(txn_TupleAddressVecU8_t));
+ std_slice_to_vec_Checked(&chunk.f1, bytes, bytes_len, 1, 1);
+ std_vec_push_Checked(&chunks, &chunk, sizeof(chunk), _Alignof(txn_TupleAddressVecU8_t));
+ return chunks;
+}
+
+/* txn_blobs_tests_chunked returns a test's chunks, each of 100 bytes of one value, by their addresses. */
+static StdVec
+txn_blobs_tests_chunked(uint8 first, uint8 end)
+{
+ StdVec chunks;
+
+ std_vec_new(&chunks, sizeof(txn_TupleAddressVecU8_t), _Alignof(txn_TupleAddressVecU8_t));
+ for (uint32 value = first; value < end; value++)
+ {
+ uint8 bytes[100];
+ StdVec one;
+
+ memset(bytes, (int) value, sizeof(bytes));
+ one = txn_blobs_tests_chunk(bytes, sizeof(bytes));
+ std_vec_push_Checked(&chunks, one.ptr, sizeof(txn_TupleAddressVecU8_t), _Alignof(txn_TupleAddressVecU8_t));
+ std_vec_free_Checked(&one, sizeof(txn_TupleAddressVecU8_t), _Alignof(txn_TupleAddressVecU8_t));
+ }
+ return chunks;
+}
+
+/* txn_blobs_tests_chunks_drop drops a test's chunks. */
+static void
+txn_blobs_tests_chunks_drop(StdVec *chunks)
+{
+ for (size_t k = 0; k < chunks->len; k++)
+ std_vec_free_Checked(&STD_VEC_AT(chunks, txn_TupleAddressVecU8_t, k).f1, 1, 1);
+ std_vec_free_Checked(chunks, sizeof(txn_TupleAddressVecU8_t), _Alignof(txn_TupleAddressVecU8_t));
+}
+
+/* txn_blobs_tests_deltas returns a repository's working state's deltas, which unwraps at a line and column. */
+static HostSharedHeld
+txn_blobs_tests_deltas(HostIoIo *io, uint32 repository, uint32 line, uint32 col)
+{
+ HostSharedHeld held;
+ HostError error;
+
+ txn_blobs_tests_ok(txn_memtable_branch_deltas(io, repository, HOST_PG_DEFAULT, &held, &error), &error, line, col);
+ return held;
+}
+
+/* txn_blobs_tests_written writes chunks under a writer, taking them, which unwraps at a line and column. */
+static void
+txn_blobs_tests_written(HostIoIo *io, const HostSharedHeld *deltas, uint32 repository, TxnMemtableVersionWriter writer,
+ StdVec chunks, bool logged, uint32 line, uint32 col)
+{
+ HostError error;
+
+ txn_blobs_tests_ok(txn_blobs_written(io, deltas, repository, writer, &chunks, logged, txn_blobs_tests_unpacked,
+ NULL, NULL, &error), &error, line, col);
+}
+
+/* txn_blobs_tests_got reads a chunk from the delta, answering whether it holds one, into bytes when given. */
+static bool
+txn_blobs_tests_got(HostIoIo *io, const HostSharedHeld *deltas, const ChunksAddressAddress *address, StdVec *bytes,
+ uint32 line, uint32 col)
+{
+ StdVec read;
+ bool some;
+ HostError error;
+
+ txn_blobs_tests_ok(txn_memtable_branch_get(io, host_shared_Held_record(deltas), address->f0, sizeof(address->f0),
+ &some, &read, &error), &error, line, col);
+ if (some && bytes != NULL)
+ *bytes = read;
+ else if (some)
+ std_vec_free_Checked(&read, 1, 1);
+ return some;
+}
+
+/* txn_blobs_tests_durable reports whether the delta's chunk is durable, which unwraps at a line and column. */
+static bool
+txn_blobs_tests_durable(HostIoIo *io, const HostSharedHeld *deltas, const ChunksAddressAddress *address, uint32 line,
+ uint32 col)
+{
+ bool durable;
+ HostError error;
+
+ txn_blobs_tests_ok(txn_memtable_branch_durable(io, host_shared_Held_record(deltas), address->f0,
+ sizeof(address->f0), &durable, &error), &error, line, col);
+ return durable;
+}
+
+/* txn_blobs_tests_spilled reads a chunk from its transaction's chunk spill, answering whether it holds one, into bytes when given. */
+static bool
+txn_blobs_tests_spilled(HostIoIo *io, const HostSharedHeld *deltas, const ChunksAddressAddress *address, StdVec *bytes,
+ uint32 line, uint32 col)
+{
+ StdVec read;
+ bool some;
+ HostError error;
+
+ txn_blobs_tests_ok(txn_spill_chunk(io, host_shared_Held_record(deltas), address->f0, sizeof(address->f0), &some,
+ &read, &error), &error, line, col);
+ if (some && bytes != NULL)
+ *bytes = read;
+ else if (some)
+ std_vec_free_Checked(&read, 1, 1);
+ return some;
+}
+
+/* txn_blobs_tests_logged_t is a logged chunk record of a repository with its end. */
+typedef struct txn_blobs_tests_logged_t
+{
+ TxnRecordChunkWrite chunk;
+ uint64 end;
+} txn_blobs_tests_logged_t;
+
+/* txn_blobs_tests_logged returns the chunk records the simulation logged for a repository, with their ends. */
+static StdVec
+txn_blobs_tests_logged(uint32 repository)
+{
+ StdVec logged = simc_logged();
+ StdVec records;
+
+ std_vec_new(&records, sizeof(txn_blobs_tests_logged_t), _Alignof(txn_blobs_tests_logged_t));
+ for (size_t k = 0; k < logged.len; k++)
+ {
+ simc_TupleU8VecU8Lsn_t *one = &STD_VEC_AT(&logged, simc_TupleU8VecU8Lsn_t, k);
+ txn_blobs_tests_logged_t record = {.end = one->f2};
+ HostWalKind kind;
+ HostError error;
+
+ if (host_WalKind_of(one->f0, &kind) && kind == HOST_WAL_KIND_CHUNK)
+ {
+ if (!txn_record_ChunkWrite_decode(one->f1.ptr, one->f1.len, &record.chunk, &error))
+ host_Error_drop(&error);
+ else if (record.chunk.repository != repository)
+ txn_record_ChunkWrite_drop(&record.chunk);
+ else
+ std_vec_push_Checked(&records, &record, sizeof(record), _Alignof(txn_blobs_tests_logged_t));
+ }
+ std_vec_free_Checked(&one->f1, 1, 1);
+ }
+ std_vec_free_Checked(&logged, sizeof(simc_TupleU8VecU8Lsn_t), _Alignof(simc_TupleU8VecU8Lsn_t));
+ return records;
+}
+
+/* txn_blobs_tests_logged_drop frees what logged returned. */
+static void
+txn_blobs_tests_logged_drop(StdVec *records)
+{
+ for (size_t k = 0; k < records->len; k++)
+ txn_record_ChunkWrite_drop(&STD_VEC_AT(records, txn_blobs_tests_logged_t, k).chunk);
+ std_vec_free_Checked(records, sizeof(txn_blobs_tests_logged_t), _Alignof(txn_blobs_tests_logged_t));
+}
+
+/* txn_blobs_tests_ended ends the running actor's transaction, committed or aborted, and its written list with it. */
+static void
+txn_blobs_tests_ended(HostIoIo *io, bool committed, uint32 line, uint32 col)
+{
+ HostError error;
+
+ simc_end(committed);
+ txn_blobs_tests_ok(committed ? txn_written_ended(io, &error) : txn_written_aborted(io, 0, &error), &error, line, col);
+}
+
+/*
+ * test_txn_blobs_tests_a_logged_write_logs_a_chunk_an_unlogged_one_left_and_redo_makes_it_durable:
+ * the out-of-line values text's V2, a chunk an unlogged write put into the
+ * working state's delta is not durable, so a logged write of the same chunk
+ * pushes and logs its own version, which makes it durable; a second logged
+ * write passes over it; and redo of the logged records on another working
+ * state makes the chunk durable there.
+ */
+void
+test_txn_blobs_tests_a_logged_write_logs_a_chunk_an_unlogged_one_left_and_redo_makes_it_durable(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ HostSharedHeld deltas = txn_blobs_tests_deltas(io, 9320, 228, 73);
+ HostSharedHeld replica;
+ ChunksAddressAddress address = chunks_address_Address_of((const uint8 *) "blob", 4);
+ TxnMemtableVersionWriter writer = txn_blobs_tests_current(io);
+ StdVec records;
+ StdVec bytes;
+ size_t count;
+ size_t one = 1;
+
+ txn_blobs_tests_written(io, &deltas, 9320, writer, txn_blobs_tests_chunk((const uint8 *) "blob", 4), false, 233,
+ 83);
+ STD_ASSERT(234, 9, txn_blobs_tests_got(io, &deltas, &address, NULL, 234, 62),
+ "branch::get(io, deltas.record(), &address.0).unwrap().is_some()");
+ STD_ASSERT(235, 9, !txn_blobs_tests_durable(io, &deltas, &address, 235, 67),
+ "!branch::durable(io, deltas.record(), &address.0).unwrap()");
+ records = txn_blobs_tests_logged(9320);
+ STD_ASSERT(244, 9, records.len == 0, "logged().is_empty()");
+ txn_blobs_tests_logged_drop(&records);
+ txn_blobs_tests_written(io, &deltas, 9320, writer, txn_blobs_tests_chunk((const uint8 *) "blob", 4), true, 245,
+ 82);
+ STD_ASSERT(246, 9, txn_blobs_tests_durable(io, &deltas, &address, 246, 66),
+ "branch::durable(io, deltas.record(), &address.0).unwrap()");
+ txn_blobs_tests_written(io, &deltas, 9320, writer, txn_blobs_tests_chunk((const uint8 *) "blob", 4), true, 247,
+ 82);
+ records = txn_blobs_tests_logged(9320);
+ count = records.len;
+ STD_ASSERT_EQ(249, 9, count == 1, &count, std_usize_Debug_fmt, &one, std_usize_Debug_fmt);
+ replica = txn_blobs_tests_deltas(io, 9321, 250, 94);
+ for (size_t k = 0; k < records.len; k++)
+ {
+ txn_blobs_tests_logged_t *record = &STD_VEC_AT(&records, txn_blobs_tests_logged_t, k);
+ HostError error;
+
+ txn_blobs_tests_ok(txn_blobs_redo(io, &replica, record->chunk, record->end, &error), &error, 252, 44);
+ }
+ std_vec_free_Checked(&records, sizeof(txn_blobs_tests_logged_t), _Alignof(txn_blobs_tests_logged_t));
+ STD_ASSERT(254, 9, txn_blobs_tests_durable(io, &replica, &address, 254, 67),
+ "branch::durable(io, replica.record(), &address.0).unwrap()");
+ STD_ASSERT(255, 9, txn_blobs_tests_got(io, &replica, &address, &bytes, 255, 66) && bytes.len == 4 &&
+ memcmp(bytes.ptr, "blob", 4) == 0,
+ "branch::get(io, replica.record(), &address.0).unwrap().as_deref() == Some(&b\"blob\"[..])");
+ std_vec_free_Checked(&bytes, 1, 1);
+ txn_blobs_tests_ended(io, true, 257, 35);
+ host_shared_Held_drop(&replica);
+ host_shared_Held_drop(&deltas);
+ simc_TestIo_drop(&test_io);
+ simc_Process_drop(&process);
+}
+
+/*
+ * test_txn_blobs_tests_a_blob_in_the_delta_reads_back_through_blobs: PLAN
+ * item 5's TOAST read, a blob tree whose chunks a write put into the working
+ * state's delta reads back whole and in slices through Blobs, and with no
+ * delta published its chunks aren't found.
+ */
+void
+test_txn_blobs_tests_a_blob_in_the_delta_reads_back_through_blobs(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ StdVec owned;
+ uint8 *data;
+ TxnBlobsCut cut;
+ TreesStoreTreeRef root;
+ TreesStoreMemoryStore packs = trees_store_MemoryStore_new();
+ TxnBlobsBlobs unread;
+ TxnBlobsBlobs blobs;
+ HostSharedHeld deltas;
+ TxnMemtableVersionWriter writer;
+ StdVec read;
+ uint8 slice[3000];
+ ChunksErrorError failed;
+ HostError error;
+
+ std_vec_from_elem_zeroed(&owned, 100000, 1, 1);
+ data = owned.ptr;
+ for (uint32 i = 0; i < 100000; i++)
+ data[i] = (uint8) ((i * UINT64CONST(2654435761) & PG_UINT32_MAX) >> 24);
+ txn_blobs_Cut_Default_default(&cut);
+ if (!trees_blob_write_blob(io, TXN_BLOBS_CUT_NODE_STORE(&cut), data, 100000, TREES_CHUNKER_DEFAULT_TARGET, &root,
+ &failed))
+ STD_RESULT_EXPECT_FAILED(270, 82, TXN_BLOBS_TESTS_UNWRAP, &failed, chunks_error_Error_Debug_fmt);
+ STD_ASSERT_MSG(271, 9, cut.chunks.len > 1, "the blob fit one chunk");
+ txn_blobs_tests_ok(txn_blobs_Blobs_new(io, 9322, TREES_STORE_MEMORY_STORE_NODE_STORE(&packs), &unread, &error),
+ &error, 273, 57);
+ if (trees_blob_read_blob(io, TXN_BLOBS_BLOBS_NODE_STORE(&unread), TREES_STORE_TREE_REF_NAMED(&root), &read,
+ &failed))
+ STD_ASSERT_MSG(274, 9, false, "a blob no delta holds was read");
+ chunks_error_Error_drop(&failed);
+ txn_blobs_Blobs_drop(&unread);
+ deltas = txn_blobs_tests_deltas(io, 9322, 275, 73);
+ writer = txn_blobs_tests_current(io);
+ txn_blobs_tests_written(io, &deltas, 9322, writer, cut.chunks, false, 277, 89);
+ txn_blobs_tests_ok(txn_blobs_Blobs_new(io, 9322, TREES_STORE_MEMORY_STORE_NODE_STORE(&packs), &blobs, &error),
+ &error, 278, 56);
+ if (!trees_blob_read_blob(io, TXN_BLOBS_BLOBS_NODE_STORE(&blobs), TREES_STORE_TREE_REF_NAMED(&root), &read,
+ &failed))
+ STD_RESULT_EXPECT_FAILED(279, 55, TXN_BLOBS_TESTS_UNWRAP, &failed, chunks_error_Error_Debug_fmt);
+ STD_ASSERT(279, 9, read.len == 100000 && memcmp(read.ptr, data, 100000) == 0,
+ "trees::read_blob(io, &blobs, root).unwrap() == data");
+ std_vec_free_Checked(&read, 1, 1);
+ if (!trees_blob_read_slice(io, TXN_BLOBS_BLOBS_NODE_STORE(&blobs), TREES_STORE_TREE_REF_NAMED(&root), 40000,
+ slice, sizeof(slice), &failed))
+ STD_RESULT_EXPECT_FAILED(281, 65, TXN_BLOBS_TESTS_UNWRAP, &failed, chunks_error_Error_Debug_fmt);
+ STD_ASSERT(282, 9, memcmp(slice, data + 40000, sizeof(slice)) == 0, "slice == data[40_000..43_000]");
+ txn_blobs_tests_ended(io, true, 284, 35);
+ txn_blobs_Blobs_drop(&blobs);
+ host_shared_Held_drop(&deltas);
+ trees_store_MemoryStore_drop(&packs);
+ std_vec_free_Checked(&owned, 1, 1);
+ simc_TestIo_drop(&test_io);
+ simc_Process_drop(&process);
+}
+
+/*
+ * test_txn_blobs_tests_a_transactions_chunks_spill_read_back_and_leave_at_its_abort:
+ * PLAN's chunk spill row, a transaction's chunks spill out of the working
+ * state's delta into its chunk spill, where spill::chunk reads them back, and
+ * its abort takes the spill out of the manifest, so none reads back after.
+ */
+void
+test_txn_blobs_tests_a_transactions_chunks_spill_read_back_and_leave_at_its_abort(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ RepoRepo repo;
+ HostSharedHeld deltas;
+ TxnMemtableVersionWriter writer;
+ HostStorage storage = txn_spill_chunks_storage(9324);
+ StdVec chunks;
+ bool spilled;
+ ChunksErrorError failed;
+ HostError error;
+
+ if (!repo_Repo_create(io, 9324, HOST_PG_DEFAULT, &repo, &failed))
+ STD_RESULT_EXPECT_FAILED(306, 74, TXN_BLOBS_TESTS_UNWRAP, &failed, chunks_error_Error_Debug_fmt);
+ deltas = txn_blobs_tests_deltas(io, 9324, 307, 73);
+ writer = txn_blobs_tests_current(io);
+ txn_blobs_tests_written(io, &deltas, 9324, writer, txn_blobs_tests_chunked(0, 3), true, 309, 91);
+ txn_blobs_tests_ok(txn_spill_spilled(io, &storage, TXN_WRITTEN_OF_CHUNKS, writer.xid, &spilled, &error), &error,
+ 310, 96);
+ STD_ASSERT(310, 9, spilled,
+ "spill::spilled(io, &spill::chunks_storage(repository), Of::Chunks, writer.xid).unwrap()");
+ chunks = txn_blobs_tests_chunked(0, 3);
+ for (size_t k = 0; k < chunks.len; k++)
+ {
+ txn_TupleAddressVecU8_t *chunk = &STD_VEC_AT(&chunks, txn_TupleAddressVecU8_t, k);
+ StdVec bytes;
+
+ STD_ASSERT_MSG(312, 13, !txn_blobs_tests_got(io, &deltas, &chunk->f0, NULL, 312, 69), "a spilled chunk stayed");
+ STD_ASSERT(313, 13, txn_blobs_tests_spilled(io, &deltas, &chunk->f0, &bytes, 313, 70) &&
+ bytes.len == chunk->f1.len && memcmp(bytes.ptr, chunk->f1.ptr, bytes.len) == 0,
+ "spill::chunk(io, deltas.record(), &address.0).unwrap() == Some(bytes)");
+ std_vec_free_Checked(&bytes, 1, 1);
+ }
+ txn_blobs_tests_ended(io, false, 316, 37);
+ for (size_t k = 0; k < chunks.len; k++)
+ STD_ASSERT_MSG(318, 13, !txn_blobs_tests_spilled(io, &deltas, &STD_VEC_AT(&chunks, txn_TupleAddressVecU8_t,
+ k).f0, NULL, 318, 70),
+ "an aborted spill's chunk read");
+ txn_blobs_tests_chunks_drop(&chunks);
+ host_shared_Held_drop(&deltas);
+ repo_Repo_drop(&repo);
+ simc_TestIo_drop(&test_io);
+ simc_Process_drop(&process);
+}
+
+/* txn_blobs_tests_holding_t is the holding test's actors' captures: the io, the deltas, and the end each actor's turn makes. */
+typedef struct txn_blobs_tests_holding_t
+{
+ HostIoIo *io;
+ const HostSharedHeld *deltas;
+ bool committed;
+} txn_blobs_tests_holding_t;
+
+/* txn_blobs_tests_holding_write is an actor's turn that writes the holding test's chunk under its own transaction. */
+static void
+txn_blobs_tests_holding_write(void *captures)
+{
+ txn_blobs_tests_holding_t *c = captures;
+
+ txn_blobs_tests_written(c->io, c->deltas, 9325, txn_blobs_tests_current(c->io), txn_blobs_tests_chunked(7, 8),
+ true, 334, 95);
+}
+
+/* txn_blobs_tests_holding_end is an actor's turn that ends its transaction as its captures say. */
+static void
+txn_blobs_tests_holding_end(void *captures)
+{
+ txn_blobs_tests_holding_t *c = captures;
+
+ txn_blobs_tests_ended(c->io, c->committed, c->committed ? 345 : 340, c->committed ? 39 : 41);
+}
+
+/*
+ * test_txn_blobs_tests_a_chunk_another_running_transaction_holds_stays_after_that_ones_abort:
+ * PLAN's chunk spill row, the writer's rule, a transaction writing a chunk
+ * another running transaction holds pushes its own version of it, so the
+ * other's abort, which takes its versions back, leaves the chunk the first
+ * one names.
+ */
+void
+test_txn_blobs_tests_a_chunk_another_running_transaction_holds_stays_after_that_ones_abort(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ HostSharedHeld deltas = txn_blobs_tests_deltas(io, 9325, 330, 73);
+ StdVec chunks = txn_blobs_tests_chunked(7, 8);
+ txn_TupleAddressVecU8_t *chunk = &STD_VEC_AT(&chunks, txn_TupleAddressVecU8_t, 0);
+ txn_blobs_tests_holding_t captures = {.io = io, .deltas = &deltas};
+ StdVec bytes;
+
+ simc_as_actor(1, txn_blobs_tests_holding_write, &captures);
+ simc_as_actor(2, txn_blobs_tests_holding_write, &captures);
+ captures.committed = false;
+ simc_as_actor(1, txn_blobs_tests_holding_end, &captures);
+ STD_ASSERT(342, 9, txn_blobs_tests_spilled(io, &deltas, &chunk->f0, &bytes, 342, 66) &&
+ bytes.len == chunk->f1.len && memcmp(bytes.ptr, chunk->f1.ptr, bytes.len) == 0,
+ "spill::chunk(io, deltas.record(), &address.0).unwrap() == Some(bytes)");
+ std_vec_free_Checked(&bytes, 1, 1);
+ captures.committed = true;
+ simc_as_actor(2, txn_blobs_tests_holding_end, &captures);
+ txn_blobs_tests_chunks_drop(&chunks);
+ host_shared_Held_drop(&deltas);
+ simc_TestIo_drop(&test_io);
+ simc_Process_drop(&process);
+}
+
+/*
+ * test_txn_blobs_tests_a_rolled_back_chunk_takes_nothing_from_its_tops_count:
+ * PLAN's chunk spill row, a chunk's version counts at no status entry, so a
+ * subtransaction's rollback, which prunes its chunk versions, leaves its
+ * top-level transaction's count as its rows left it.
+ */
+void
+test_txn_blobs_tests_a_rolled_back_chunk_takes_nothing_from_its_tops_count(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ HostSharedHeld deltas = txn_blobs_tests_deltas(io, 9326, 357, 73);
+ HostXid top = {txn_blobs_tests_current(io).xid};
+ TxnMemtableVersionWriter sub;
+ TxnStatusRoomed roomed;
+ TxnStatusEntry entry;
+ StdVec chunks;
+ uint64 added;
+ bool has;
+ uint64 got;
+ uint64 want = 1;
+ HostError error;
+
+ txn_blobs_tests_ok(txn_status_roomed(io, top, &roomed, &error), &error, 359, 45);
+ txn_blobs_tests_ok(txn_status_add(io, top, 1, &has, &added, &error), &error, 360, 45);
+ simc_savepoint();
+ sub = txn_blobs_tests_current(io);
+ txn_blobs_tests_ok(txn_written_sub_started(io, (uint32) sub.xid, &error), &error, 363, 57);
+ txn_blobs_tests_written(io, &deltas, 9326, sub, txn_blobs_tests_chunked(3, 5), true, 364, 88);
+ simc_rollback_to();
+ txn_blobs_tests_ok(txn_written_sub_aborted(io, (uint32) sub.xid, &error), &error, 366, 57);
+ chunks = txn_blobs_tests_chunked(3, 4);
+ STD_ASSERT(367, 9, !txn_blobs_tests_spilled(io, &deltas, &STD_VEC_AT(&chunks, txn_TupleAddressVecU8_t, 0).f0, NULL,
+ 367, 77),
+ "spill::chunk(io, deltas.record(), &chunked(3..4)[0].0.0).unwrap() == None");
+ txn_blobs_tests_chunks_drop(&chunks);
+ txn_blobs_tests_ok(txn_status_lookup(io, top, &has, &entry, &error), &error, 368, 56);
+ got = has ? entry.count : 0;
+ STD_ASSERT_EQ(368, 9, has && entry.count == 1, &got, std_u64_Debug_fmt, &want, std_u64_Debug_fmt);
+ txn_blobs_tests_ended(io, true, 370, 35);
+ host_shared_Held_drop(&deltas);
+ simc_TestIo_drop(&test_io);
+ simc_Process_drop(&process);
+}
+
+#endif /* DOLTGRES_TEST_TXN */
diff --git a/src/doltgres/txn/c/change.c b/src/doltgres/txn/c/change.c
index a684d9c2136fa..556263c694961 100644
--- a/src/doltgres/txn/c/change.c
+++ b/src/doltgres/txn/c/change.c
@@ -38,6 +38,16 @@
#define XXH_STATIC_LINKING_ONLY
#include "xxhash.h"
+#ifdef DOLTGRES_TEST_TXN
+#include "doltgres/repo/lib.h"
+#include "doltgres/simc/simc.h"
+#include "doltgres/std/test.h"
+#include "doltgres/txn/insert.h"
+#include "doltgres/txn/state.h"
+#endif
+
+#define RUST_FILE "txn" STD_SEP "src" STD_SEP "change.rs"
+
/* The prepare whose closure answers an end, or none to start over. */
#define TXN_MEMTABLE_TABLE_RS OptionEnded
#define TXN_MEMTABLE_TABLE_RETURNS bool
@@ -484,3 +494,491 @@ txn_change_redo(HostIoIo *io, const HostSharedHeld *held, const TxnRecordRowChan
return txn_memtable_table_prepare_Handle(io, record, &rows, 1, NULL, 0, false, txn_change_redo_closure_0, &inside,
NULL, &handle, error);
}
+
+#ifdef DOLTGRES_TEST_TXN
+
+/*
+ * test_txn_change_tests_a_keys_stripe_is_its_storage_and_bytes_or_its_storage_alone:
+ * a key's stripe of the key lock is the same every time and below the
+ * stripes' count; two storages' stripes of one key differ, as do two keys'
+ * of one storage, for the keys this case picks; and a storage whose equal
+ * keys can have other bytes takes one stripe for every key.
+ */
+void
+test_txn_change_tests_a_keys_stripe_is_its_storage_and_bytes_or_its_storage_alone(void)
+{
+ HostStorage one = {.tablespace = HOST_PG_DEFAULT, .database = 5, .relation = 16384};
+ HostStorage two = {.tablespace = HOST_PG_DEFAULT, .database = 5, .relation = 16385};
+ const uint8 *k = (const uint8 *) "k";
+ const uint8 *l = (const uint8 *) "l";
+
+ STD_ASSERT(__LINE__, 9, txn_change_stripe(&one, k, 1, true) == txn_change_stripe(&one, k, 1, true),
+ "a key's stripe is the same every time");
+ STD_ASSERT(__LINE__, 9, txn_change_stripe(&one, k, 1, true) < HOST_STRIPES, "a key's stripe is below the count");
+ STD_ASSERT(__LINE__, 9, txn_change_stripe(&one, k, 1, true) != txn_change_stripe(&two, k, 1, true),
+ "two storages' stripes of one key differ");
+ STD_ASSERT(__LINE__, 9, txn_change_stripe(&one, k, 1, true) != txn_change_stripe(&one, l, 1, true),
+ "two keys' stripes of one storage differ");
+ STD_ASSERT(__LINE__, 9, txn_change_stripe(&one, k, 1, false) == txn_change_stripe(&one, l, 1, false),
+ "a storage whose equal keys can differ in bytes takes one stripe for every key");
+}
+
+/* TXN_CHANGE_TESTS_UNWRAP_FAILED is a Result's unwrap's panic on a HostError at a line and column. */
+#define TXN_CHANGE_TESTS_UNWRAP_FAILED(line, col, error) \
+ STD_RESULT_EXPECT_FAILED((line), (col), "called `Result::unwrap()` on an `Err` value", (error), \
+ host_Error_Debug_fmt)
+
+/* txn_change_tests_made_t is made's (Storage, Repo, Held) with the reader its store reads through. */
+typedef struct txn_change_tests_made_t
+{
+ HostStorage storage;
+ RepoRepo repo;
+ HostSharedReader reading;
+ HostSharedHeld held;
+} txn_change_tests_made_t;
+
+/* txn_change_tests_made returns a new database's storage in pg_default with its repository, a reader, and its state. */
+static txn_change_tests_made_t
+txn_change_tests_made(HostIoIo *io, uint32 database)
+{
+ txn_change_tests_made_t made = {.storage = txn_insert_tests_stored(database)};
+ ChunksErrorError failed;
+ HostError error;
+
+ if (!repo_Repo_create(io, database, HOST_PG_DEFAULT, &made.repo, &failed))
+ STD_RESULT_EXPECT_FAILED(__LINE__, 0, "called `Result::unwrap()` on an `Err` value", &failed,
+ chunks_error_Error_Debug_fmt);
+ if (!host_shared_read(io, &made.reading, &error) ||
+ !txn_state_state(io, &made.repo, &made.reading, &made.storage, TXN_STATE_PERSISTENCE_PERMANENT, false, NULL,
+ &made.held, &error))
+ TXN_CHANGE_TESTS_UNWRAP_FAILED(__LINE__, 0, &error);
+ return made;
+}
+
+/* txn_change_tests_made_drop frees what made returned. */
+static void
+txn_change_tests_made_drop(txn_change_tests_made_t *made)
+{
+ host_shared_Held_drop(&made->held);
+ host_shared_Reader_drop(&made->reading);
+ repo_Repo_drop(&made->repo);
+}
+
+/* txn_change_tests_logged_t is one of changes' row changes with its record's end. */
+typedef struct txn_change_tests_logged_t
+{
+ TxnRecordRowChange change;
+ uint64 end;
+} txn_change_tests_logged_t;
+
+/* txn_change_tests_changes returns a database's storages' logged row changes, each with its record's end. */
+static StdVec
+txn_change_tests_changes(uint32 database)
+{
+ StdVec logged = simc_logged();
+ StdVec changes;
+
+ std_vec_new(&changes, sizeof(txn_change_tests_logged_t), _Alignof(txn_change_tests_logged_t));
+ for (size_t k = 0; k < logged.len; k++)
+ {
+ simc_TupleU8VecU8Lsn_t *one = &STD_VEC_AT(&logged, simc_TupleU8VecU8Lsn_t, k);
+ txn_change_tests_logged_t row = {.end = one->f2};
+ HostWalKind kind;
+ HostError error;
+
+ if (host_WalKind_of(one->f0, &kind) && kind == HOST_WAL_KIND_ROW_CHANGE)
+ {
+ if (!txn_record_RowChange_decode(one->f1.ptr, one->f1.len, &row.change, &error))
+ host_Error_drop(&error);
+ else if (row.change.storage.database != database)
+ txn_record_RowChange_drop(&row.change);
+ else
+ std_vec_push_Checked(&changes, &row, sizeof(row), _Alignof(txn_change_tests_logged_t));
+ }
+ std_vec_free_Checked(&one->f1, 1, 1);
+ }
+ std_vec_free_Checked(&logged, sizeof(simc_TupleU8VecU8Lsn_t), _Alignof(simc_TupleU8VecU8Lsn_t));
+ return changes;
+}
+
+/* txn_change_tests_changes_drop frees what changes returned. */
+static void
+txn_change_tests_changes_drop(StdVec *changes)
+{
+ for (size_t k = 0; k < changes->len; k++)
+ txn_record_RowChange_drop(&STD_VEC_AT(changes, txn_change_tests_logged_t, k).change);
+ std_vec_free_Checked(changes, sizeof(txn_change_tests_logged_t), _Alignof(txn_change_tests_logged_t));
+}
+
+/* txn_change_tests_shown_t is shown's (String, u32, Option): what a version does, its command, and its LSN. */
+typedef struct txn_change_tests_shown_t
+{
+ const char *does;
+ uint32 raw;
+ bool has_lsn;
+ uint64 lsn;
+} txn_change_tests_shown_t;
+
+/* txn_change_tests_shown returns key k's versions newest first, each as what it does, its command, and its LSN. */
+static StdVec
+txn_change_tests_shown(HostIoIo *io, const HostSharedHeld *held)
+{
+ HostDeltaHold hold;
+ StdVec versions;
+ StdVec shown;
+ HostError error;
+
+ if (!host_delta_hold(io, &hold, &error) ||
+ !txn_memtable_table_versions(io, &hold, host_shared_Held_record(held), (const uint8 *) "k", 1, &versions,
+ &error))
+ TXN_CHANGE_TESTS_UNWRAP_FAILED(__LINE__, 0, &error);
+ std_vec_new(&shown, sizeof(txn_change_tests_shown_t), _Alignof(txn_change_tests_shown_t));
+ for (size_t k = 0; k < versions.len; k++)
+ {
+ TxnMemtableVersionRecord record = txn_memtable_version_Version_record(&STD_VEC_AT(&versions,
+ TxnMemtableVersionVersion,
+ k));
+ TxnMemtableVersionOpRefTag op = txn_memtable_version_Record_op(&record).tag;
+ txn_change_tests_shown_t one = {.does = "other", .raw = txn_memtable_version_Record_raw(&record)};
+
+ if (op == TXN_MEMTABLE_VERSION_OP_REF_PUT)
+ one.does = "put";
+ else if (op == TXN_MEMTABLE_VERSION_OP_REF_DELETE)
+ one.does = "delete";
+ else if (op == TXN_MEMTABLE_VERSION_OP_REF_LOCK)
+ one.does = "lock";
+ one.has_lsn = txn_memtable_version_Record_lsn(&record, &one.lsn);
+ std_vec_push_Checked(&shown, &one, sizeof(one), _Alignof(txn_change_tests_shown_t));
+ }
+ txn_memtable_version_versions_drop(&versions, 0);
+ host_delta_Hold_drop(&hold);
+ return shown;
+}
+
+/* txn_change_tests_shown_eq reports whether two shown versions are the same. */
+static bool
+txn_change_tests_shown_eq(const txn_change_tests_shown_t *a, const txn_change_tests_shown_t *b)
+{
+ return strcmp(a->does, b->does) == 0 && a->raw == b->raw && a->has_lsn == b->has_lsn &&
+ (!a->has_lsn || a->lsn == b->lsn);
+}
+
+/* txn_change_tests_shown_is asserts that key k's versions show as expected, freeing what shown returned. */
+static void
+txn_change_tests_shown_is(uint32 line, StdVec *got, const txn_change_tests_shown_t *want, size_t want_len)
+{
+ bool eq = got->len == want_len;
+
+ for (size_t k = 0; eq && k < want_len; k++)
+ eq = txn_change_tests_shown_eq(&STD_VEC_AT(got, txn_change_tests_shown_t, k), &want[k]);
+ STD_ASSERT(line, 9, eq, "a key's versions show as expected");
+ std_vec_free_Checked(got, sizeof(txn_change_tests_shown_t), _Alignof(txn_change_tests_shown_t));
+}
+
+/* txn_change_tests_row returns an insert of key k's row by a writer, with its writer's room or none. */
+static TxnInsertInsert
+txn_change_tests_row(HostStorage storage, TxnMemtableVersionWriter writer, bool has_roomed, TxnStatusRoomed roomed)
+{
+ TxnInsertInsert row = txn_insert_tests_row(storage, 0);
+
+ std_vec_free_Checked(&row.key, 1, 1);
+ std_slice_to_vec_Checked(&row.key, (const uint8 *) "k", 1, 1, 1);
+ row.writer = writer;
+ row.has_roomed = has_roomed;
+ row.roomed = roomed;
+ return row;
+}
+
+/* txn_change_tests_inserted inserts key k's row and returns its tuple id. */
+static uint64
+txn_change_tests_inserted(HostIoIo *io, const txn_change_tests_made_t *made, TxnInsertInsert row)
+{
+ TreesStoreReader store = repo_Repo_reader(&made->repo, HOST_CACHE_PLACEMENT_SERVER, &made->reading);
+ TxnInsertRoom room = {.bytes = 28, .page = 8168, .most = 291};
+ uint64 tid;
+ HostError error;
+
+ if (!txn_insert_tests_taken(io, TREES_STORE_READER_NODE_STORE(&store), &made->held, &row, &room, NULL, NULL, 0,
+ &tid, &error))
+ TXN_CHANGE_TESTS_UNWRAP_FAILED(__LINE__, 0, &error);
+ return tid;
+}
+
+/* txn_change_tests_ending returns the end of key k's row of a tuple id by a writer at a command, doing an op. */
+static TxnChangeEnding
+txn_change_tests_ending(HostStorage storage, TxnMemtableOp op, TxnMemtableVersionWriter writer, uint32 command,
+ TxnStatusRoomed roomed, uint64 tid)
+{
+ TxnChangeEnding ending = {.storage = storage, .table = 16385, .op = op, .writer = writer, .roomed = roomed,
+ .lock = TXN_MEMTABLE_LOCK_MODE_UPDATE, .logged = true, .tuple = {tid}};
+
+ ending.writer.command = command;
+ std_slice_to_vec_Checked(&ending.key, (const uint8 *) "k", 1, 1, 1);
+ return ending;
+}
+
+/* txn_change_tests_stop is a read that stops with the count of the versions it read, its Stop's usize. */
+static bool
+txn_change_tests_stop(void *captures, HostIoIo *io, TxnMemtableVersionVersion *versions, size_t versions_len,
+ TxnChangeRead *result, void *stopped, HostError *error)
+{
+ result->tag = TXN_CHANGE_READ_STOP;
+ *(size_t *) stopped = versions_len;
+ return true;
+}
+
+/* txn_change_tests_go is a read that goes on to the push, its version showing its writer's command. */
+static bool
+txn_change_tests_go(void *captures, HostIoIo *io, TxnMemtableVersionVersion *versions, size_t versions_len,
+ TxnChangeRead *result, void *stopped, HostError *error)
+{
+ result->tag = TXN_CHANGE_READ_GO;
+ result->u.go.shown.tag = TXN_MEMTABLE_VERSION_SHOWN_COMMAND;
+ result->u.go.cmin = 0;
+ result->u.go.has_row = false;
+ return true;
+}
+
+/* txn_change_tests_ended ends a row with a read and returns whether it pushed, a Stop's count in stopped. */
+static TxnChangeEnded
+txn_change_tests_ended(HostIoIo *io, const HostSharedHeld *held, TxnChangeEnding ending, TxnChangeDecide decide,
+ size_t *stopped)
+{
+ HostIoWaits waits = host_io_Waits_testing();
+ uint32 stripe = txn_change_stripe(&ending.storage, (const uint8 *) "k", 1, true);
+ TxnChangeEnded ended;
+ HostError error;
+
+ if (!txn_change_end(io, held, &ending, &waits, stripe, decide, NULL, NULL, stopped, &ended, &error))
+ TXN_CHANGE_TESTS_UNWRAP_FAILED(__LINE__, 0, &error);
+ return ended;
+}
+
+/* txn_change_tests_op_delete returns a delete's op. */
+static TxnMemtableOp
+txn_change_tests_op_delete(void)
+{
+ TxnMemtableOp op = {.tag = TXN_MEMTABLE_OP_DELETE};
+
+ return op;
+}
+
+/* txn_change_tests_op_share returns a share lock's op. */
+static TxnMemtableOp
+txn_change_tests_op_share(void)
+{
+ TxnMemtableOp op = {.tag = TXN_MEMTABLE_OP_LOCK, .u.lock = TXN_MEMTABLE_LOCK_MODE_SHARE};
+
+ return op;
+}
+
+/*
+ * test_txn_change_tests_a_delete_is_logged_pushed_and_redone_once_and_a_lock_is_never_logged:
+ * a delete whose read stops, seeing the key's one version, pushes nothing and
+ * gives back what it stopped with, and a logged delete of an inserted row
+ * appends its row change and pushes its version with the record's LSN,
+ * showing its own command; redone on another storage, the insert and the
+ * delete each show command 0, as heap's redo writes FirstCommandId, and a
+ * delete redone again, which the delta holds by its key and LSN, pushes
+ * nothing. A lock pushes its version with no record.
+ */
+void
+test_txn_change_tests_a_delete_is_logged_pushed_and_redone_once_and_a_lock_is_never_logged(void)
+{
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ txn_change_tests_made_t made = txn_change_tests_made(io, 9361);
+ TxnMemtableVersionWriter writer = {.xid = 41, .command = 2};
+ bool has_roomed;
+ TxnStatusRoomed roomed;
+ uint64 tid;
+ size_t stopped = 0;
+ StdVec logged;
+ const txn_change_tests_logged_t *inserted;
+ const txn_change_tests_logged_t *deleted;
+ txn_change_tests_made_t replica;
+ TreesStoreReader replica_store;
+ TxnInsertPage page = {.bytes = 8168, .most = 291};
+ HostError error;
+
+ if (!txn_status_redo_roomed(io, &writer, &has_roomed, &roomed, &error))
+ TXN_CHANGE_TESTS_UNWRAP_FAILED(__LINE__, 9, &error);
+ STD_ASSERT(__LINE__, 9, has_roomed, "the writer takes room");
+ tid = txn_change_tests_inserted(io, &made, txn_change_tests_row(made.storage, writer, true, roomed));
+ STD_ASSERT(__LINE__, 9,
+ txn_change_tests_ended(io, &made.held,
+ txn_change_tests_ending(made.storage, txn_change_tests_op_delete(), writer, 3,
+ roomed, tid),
+ txn_change_tests_stop, &stopped) == TXN_CHANGE_ENDED_STOPPED && stopped == 1,
+ "a delete whose read stops pushes nothing and gives back what it stopped with");
+ STD_ASSERT(__LINE__, 9,
+ txn_change_tests_ended(io, &made.held,
+ txn_change_tests_ending(made.storage, txn_change_tests_op_delete(), writer, 3,
+ roomed, tid),
+ txn_change_tests_go, &stopped) == TXN_CHANGE_ENDED_PUSHED,
+ "a delete whose read goes on pushes its version");
+ logged = txn_change_tests_changes(9361);
+ STD_ASSERT(__LINE__, 9, logged.len == 2, "the insert and the delete each logged a row change");
+ inserted = &STD_VEC_AT(&logged, txn_change_tests_logged_t, 0);
+ deleted = &STD_VEC_AT(&logged, txn_change_tests_logged_t, 1);
+ STD_ASSERT(__LINE__, 9, deleted->change.op.tag == TXN_MEMTABLE_OP_DELETE, "the second row change is the delete");
+ {
+ txn_change_tests_shown_t want[] = {{"delete", 3, true, deleted->end}, {"put", 2, true, inserted->end}};
+ StdVec got = txn_change_tests_shown(io, &made.held);
+
+ txn_change_tests_shown_is(__LINE__, &got, want, lengthof(want));
+ }
+ replica = txn_change_tests_made(io, 9362);
+ replica_store = repo_Repo_reader(&replica.repo, HOST_CACHE_PLACEMENT_SERVER, &replica.reading);
+ for (size_t k = 0; k < logged.len; k++)
+ {
+ const txn_change_tests_logged_t *one = &STD_VEC_AT(&logged, txn_change_tests_logged_t, k);
+ const TxnMemtablePut *put;
+ bool ok;
+
+ if (txn_record_RowChange_put(&one->change, &put))
+ ok = txn_insert_redo(io, TREES_STORE_READER_NODE_STORE(&replica_store), &replica.held, &one->change,
+ one->end, &page, NULL, NULL, 0, &error);
+ else
+ ok = txn_change_redo(io, &replica.held, &one->change, one->end, &error);
+ if (!ok)
+ TXN_CHANGE_TESTS_UNWRAP_FAILED(__LINE__, 13, &error);
+ }
+ if (!txn_change_redo(io, &replica.held, &deleted->change, deleted->end, &error))
+ TXN_CHANGE_TESTS_UNWRAP_FAILED(__LINE__, 9, &error);
+ {
+ txn_change_tests_shown_t want[] = {{"delete", 0, true, deleted->end}, {"put", 0, true, inserted->end}};
+ StdVec got = txn_change_tests_shown(io, &replica.held);
+
+ txn_change_tests_shown_is(__LINE__, &got, want, lengthof(want));
+ }
+ STD_ASSERT(__LINE__, 9,
+ txn_change_tests_ended(io, &made.held,
+ txn_change_tests_ending(made.storage, txn_change_tests_op_share(), writer, 4,
+ roomed, tid),
+ txn_change_tests_go, &stopped) == TXN_CHANGE_ENDED_PUSHED,
+ "a lock pushes its version");
+ {
+ StdVec got = txn_change_tests_shown(io, &made.held);
+ txn_change_tests_shown_t lock = {"lock", 4, false, 0};
+
+ STD_ASSERT(__LINE__, 9, got.len > 0 &&
+ txn_change_tests_shown_eq(&STD_VEC_AT(&got, txn_change_tests_shown_t, 0), &lock),
+ "the key's newest version is the lock, with no LSN");
+ std_vec_free_Checked(&got, sizeof(txn_change_tests_shown_t), _Alignof(txn_change_tests_shown_t));
+ }
+ {
+ StdVec again = txn_change_tests_changes(9361);
+
+ STD_ASSERT(__LINE__, 9, again.len == 2, "a lock logs no row change");
+ txn_change_tests_changes_drop(&again);
+ }
+ if (!txn_written_ended(io, &error))
+ TXN_CHANGE_TESTS_UNWRAP_FAILED(__LINE__, 21, &error);
+ txn_change_tests_changes_drop(&logged);
+ txn_change_tests_made_drop(&replica);
+ txn_change_tests_made_drop(&made);
+ simc_TestIo_drop(&test_io);
+}
+
+/*
+ * test_txn_change_tests_an_aborted_delete_and_lock_go_at_the_take_back: an
+ * aborted transaction's delete and lock of a committed row go at its
+ * take-back, since end records the key in its list before its prepare, and
+ * the committed row's put stays the key's one version.
+ */
+void
+test_txn_change_tests_an_aborted_delete_and_lock_go_at_the_take_back(void)
+{
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ txn_change_tests_made_t made = txn_change_tests_made(io, 9363);
+ HostXid committed = simc_begin();
+ HostXid aborted = simc_begin();
+ TxnMemtableVersionWriter by_committed = {.xid = committed.f0};
+ TxnMemtableVersionWriter by_aborted = {.xid = aborted.f0};
+ TxnStatusRoomed unroomed = {0};
+ TxnStatusRoomed roomed;
+ uint64 tid;
+ StdVec got;
+ HostError error;
+
+ tid = txn_change_tests_inserted(io, &made, txn_change_tests_row(made.storage, by_committed, false, unroomed));
+ simc_commit(committed);
+ if (!txn_written_ended(io, &error) || !txn_status_roomed(io, aborted, &roomed, &error))
+ TXN_CHANGE_TESTS_UNWRAP_FAILED(__LINE__, 9, &error);
+ txn_change_tests_ended(io, &made.held,
+ txn_change_tests_ending(made.storage, txn_change_tests_op_share(), by_aborted, 0, roomed,
+ tid),
+ txn_change_tests_go, NULL);
+ txn_change_tests_ended(io, &made.held,
+ txn_change_tests_ending(made.storage, txn_change_tests_op_delete(), by_aborted, 0, roomed,
+ tid),
+ txn_change_tests_go, NULL);
+ got = txn_change_tests_shown(io, &made.held);
+ STD_ASSERT(__LINE__, 9, got.len == 3, "the key holds the put, the lock, and the delete");
+ std_vec_free_Checked(&got, sizeof(txn_change_tests_shown_t), _Alignof(txn_change_tests_shown_t));
+ simc_abort(aborted);
+ if (!txn_written_aborted(io, 0, &error))
+ TXN_CHANGE_TESTS_UNWRAP_FAILED(__LINE__, 9, &error);
+ got = txn_change_tests_shown(io, &made.held);
+ STD_ASSERT(__LINE__, 9, got.len == 1 && strcmp(STD_VEC_AT(&got, txn_change_tests_shown_t, 0).does, "put") == 0,
+ "the take-back leaves the committed row's put the key's one version");
+ std_vec_free_Checked(&got, sizeof(txn_change_tests_shown_t), _Alignof(txn_change_tests_shown_t));
+ txn_change_tests_made_drop(&made);
+ simc_TestIo_drop(&test_io);
+}
+
+/*
+ * test_txn_change_tests_a_change_of_a_spilled_row_reads_its_spilled_version:
+ * a change of a row whose version its transaction spilled reads that version
+ * from the spill, though the row's slice of the memtable's chain holds none of
+ * the key's versions, and pushes its own version over it.
+ */
+void
+test_txn_change_tests_a_change_of_a_spilled_row_reads_its_spilled_version(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ txn_change_tests_made_t made = txn_change_tests_made(io, 9524);
+ HostXid xid = simc_begin();
+ TxnMemtableVersionWriter writer = {.xid = xid.f0, .command = 2};
+ TxnStatusRoomed roomed;
+ uint64 tid;
+ bool spilled;
+ size_t stopped = 0;
+ StdVec got;
+ HostError error;
+
+ if (!txn_status_roomed(io, xid, &roomed, &error))
+ TXN_CHANGE_TESTS_UNWRAP_FAILED(__LINE__, 9, &error);
+ tid = txn_change_tests_inserted(io, &made, txn_change_tests_row(made.storage, writer, true, roomed));
+ if (!txn_spill_spilled(io, &made.storage, TXN_WRITTEN_OF_ROWS, writer.xid, &spilled, &error))
+ TXN_CHANGE_TESTS_UNWRAP_FAILED(__LINE__, 9, &error);
+ STD_ASSERT(__LINE__, 5, spilled, "the insert spilled its row");
+ STD_ASSERT(__LINE__, 9,
+ txn_change_tests_ended(io, &made.held,
+ txn_change_tests_ending(made.storage, txn_change_tests_op_delete(), writer, 3,
+ roomed, tid),
+ txn_change_tests_stop, &stopped) == TXN_CHANGE_ENDED_STOPPED && stopped == 1,
+ "the change reads the row's spilled version");
+ STD_ASSERT(__LINE__, 9,
+ txn_change_tests_ended(io, &made.held,
+ txn_change_tests_ending(made.storage, txn_change_tests_op_delete(), writer, 3,
+ roomed, tid),
+ txn_change_tests_go, &stopped) == TXN_CHANGE_ENDED_PUSHED,
+ "the change pushes its own version over it");
+ got = txn_change_tests_shown(io, &made.held);
+ STD_ASSERT(__LINE__, 9, got.len == 2 && strcmp(STD_VEC_AT(&got, txn_change_tests_shown_t, 0).does, "delete") == 0 &&
+ strcmp(STD_VEC_AT(&got, txn_change_tests_shown_t, 1).does, "put") == 0,
+ "the key's versions are the delete over the put");
+ std_vec_free_Checked(&got, sizeof(txn_change_tests_shown_t), _Alignof(txn_change_tests_shown_t));
+ if (!txn_written_ended(io, &error))
+ TXN_CHANGE_TESTS_UNWRAP_FAILED(__LINE__, 9, &error);
+ txn_change_tests_made_drop(&made);
+ simc_TestIo_drop(&test_io);
+ simc_Process_drop(&process);
+}
+
+#endif /* DOLTGRES_TEST_TXN */
diff --git a/src/doltgres/txn/c/entry.c b/src/doltgres/txn/c/entry.c
index 28c2ae9e2097d..17bb308787092 100644
--- a/src/doltgres/txn/c/entry.c
+++ b/src/doltgres/txn/c/entry.c
@@ -39,6 +39,18 @@
#include "doltgres/txn/visible.h"
#include "doltgres/txn/written.h"
+#ifdef DOLTGRES_TEST_TXN
+#include "doltgres/repo/lib.h"
+#include "doltgres/simc/simc.h"
+#include "doltgres/std/test.h"
+#include "doltgres/txn/change.h"
+#include "doltgres/txn/record.h"
+#include "doltgres/txn/runs.h"
+
+/* RUST_FILE is the file the tests' asserts name, entry.rs. */
+#define RUST_FILE "txn" STD_SEP "src" STD_SEP "entry.rs"
+#endif
+
/* The prepare whose closure pushes an entry's changes. */
#define TXN_MEMTABLE_TABLE_RS Unit
#define TXN_MEMTABLE_TABLE_RETURNS bool
@@ -1237,3 +1249,673 @@ txn_entry_unique(HostIoIo *io, TreesStoreNodeStore store, const HostSharedHeld *
txn_entry_unique_pad(&frame);
return frame.ok;
}
+
+#ifdef DOLTGRES_TEST_TXN
+/* TXN_ENTRY_TESTS_UNWRAP is Result::unwrap's panic text. */
+#define TXN_ENTRY_TESTS_UNWRAP "called `Result::unwrap()` on an `Err` value"
+
+/* txn_entry_tests_ok unwraps a host result at a line. */
+static void
+txn_entry_tests_ok(bool ok, HostError *error, uint32 line)
+{
+ if (!ok)
+ STD_RESULT_EXPECT_FAILED(line, 0, TXN_ENTRY_TESTS_UNWRAP, error, host_Error_Debug_fmt);
+}
+
+/* txn_entry_tests_made_t is made's (Storage, Repo, Held), with the reader the state was made under. */
+typedef struct txn_entry_tests_made_t
+{
+ HostStorage storage;
+ RepoRepo repo;
+ HostSharedReader reading;
+ HostSharedHeld held;
+} txn_entry_tests_made_t;
+
+/* txn_entry_tests_made returns a new database's index storage in pg_default, its state made as a backend's make of an index's storage makes it. */
+static txn_entry_tests_made_t
+txn_entry_tests_made(HostIoIo *io, uint32 database)
+{
+ txn_entry_tests_made_t made = {.storage = {HOST_PG_DEFAULT, database, 16390}};
+ TxnStateMade state = {.key = {.tag = TXN_BASE_KEY_NONE}, .table = 16385, .has_index = true, .index = 16390};
+ ChunksErrorError failed;
+ HostError error;
+
+ if (!repo_Repo_create(io, database, HOST_PG_DEFAULT, &made.repo, &failed))
+ STD_RESULT_EXPECT_FAILED(__LINE__, 0, TXN_ENTRY_TESTS_UNWRAP, &failed, chunks_error_Error_Debug_fmt);
+ txn_entry_tests_ok(host_shared_read(io, &made.reading, &error) &&
+ txn_state_state(io, &made.repo, &made.reading, &made.storage, TXN_STATE_PERSISTENCE_PERMANENT,
+ true, &state, &made.held, &error), &error, __LINE__);
+ return made;
+}
+
+/* txn_entry_tests_made_drop frees what made returned. */
+static void
+txn_entry_tests_made_drop(txn_entry_tests_made_t *made)
+{
+ host_shared_Held_drop(&made->held);
+ host_shared_Reader_drop(&made->reading);
+ repo_Repo_drop(&made->repo);
+}
+
+/* txn_entry_tests_shown_t is shown's (&str, u64, u32, Option): what a version does, its tuple id, its command, and its LSN. */
+typedef struct txn_entry_tests_shown_t
+{
+ const char *does;
+ uint64 tuple;
+ uint32 raw;
+ bool has_lsn;
+ uint64 lsn;
+} txn_entry_tests_shown_t;
+
+/* txn_entry_tests_shown returns a key's versions newest first, each as what it does, its tuple id, its command, and its LSN. */
+static StdVec
+txn_entry_tests_shown(HostIoIo *io, const HostSharedHeld *held, const uint8 *key, size_t key_len)
+{
+ HostDeltaHold hold;
+ StdVec versions;
+ StdVec shown;
+ HostError error;
+
+ txn_entry_tests_ok(host_delta_hold(io, &hold, &error) &&
+ txn_memtable_table_versions(io, &hold, host_shared_Held_record(held), key, key_len, &versions,
+ &error), &error, __LINE__);
+ std_vec_new(&shown, sizeof(txn_entry_tests_shown_t), _Alignof(txn_entry_tests_shown_t));
+ for (size_t k = 0; k < versions.len; k++)
+ {
+ TxnMemtableVersionRecord record = txn_memtable_version_Version_record(&STD_VEC_AT(&versions,
+ TxnMemtableVersionVersion,
+ k));
+ TxnMemtableVersionOpRef op = txn_memtable_version_Record_op(&record);
+ txn_entry_tests_shown_t one = {.does = "other", .raw = txn_memtable_version_Record_raw(&record)};
+
+ if (op.tag == TXN_MEMTABLE_VERSION_OP_REF_PUT)
+ {
+ one.does = "put";
+ one.tuple = op.u.put.tuple.f0;
+ }
+ else if (op.tag == TXN_MEMTABLE_VERSION_OP_REF_DELETE)
+ one.does = "delete";
+ else if (op.tag == TXN_MEMTABLE_VERSION_OP_REF_ADD)
+ {
+ one.does = "add";
+ one.tuple = op.u.add.tuple.f0;
+ }
+ else if (op.tag == TXN_MEMTABLE_VERSION_OP_REF_REMOVE)
+ {
+ one.does = "remove";
+ one.tuple = op.u.remove.f0.f0;
+ }
+ one.has_lsn = txn_memtable_version_Record_lsn(&record, &one.lsn);
+ std_vec_push_Checked(&shown, &one, sizeof(one), _Alignof(txn_entry_tests_shown_t));
+ }
+ for (size_t k = 0; k < versions.len; k++)
+ txn_memtable_version_Version_drop(&STD_VEC_AT(&versions, TxnMemtableVersionVersion, k));
+ std_vec_free_Checked(&versions, sizeof(TxnMemtableVersionVersion), _Alignof(TxnMemtableVersionVersion));
+ host_delta_Hold_drop(&hold);
+ return shown;
+}
+
+/* txn_entry_tests_shown_Debug_fmt is a shown version's Debug. */
+static bool
+txn_entry_tests_shown_Debug_fmt(const void *value, StdFormatter *f)
+{
+ const txn_entry_tests_shown_t *one = value;
+ StdStr does = {one->does, strlen(one->does)};
+ StdOptionRef lsn = {one->has_lsn, STD_ITEM(&one->lsn, std_u64_Debug_fmt)};
+ StdTupleRef tuple = STD_TUPLE(4, STD_ITEM(&does, std_str_Debug_fmt), STD_ITEM(&one->tuple, std_u64_Debug_fmt),
+ STD_ITEM(&one->raw, std_u32_Debug_fmt), STD_ITEM(&lsn, std_option_Debug_fmt));
+
+ return std_tuple_Debug_fmt(&tuple, f);
+}
+
+/* txn_entry_tests_shown_list_Debug_fmt is a Vec of shown versions' Debug. */
+static bool
+txn_entry_tests_shown_list_Debug_fmt(const void *value, StdFormatter *f)
+{
+ const StdVec *shown = value;
+ StdDebugList list = std_fmt_debug_list(f);
+
+ for (size_t k = 0; k < shown->len; k++)
+ std_DebugList_entry(&list, &STD_VEC_AT(shown, txn_entry_tests_shown_t, k), txn_entry_tests_shown_Debug_fmt);
+ return std_DebugList_finish(&list);
+}
+
+/* txn_entry_tests_shown_is asserts a key's shown versions are the wanted ones, freeing them. */
+static void
+txn_entry_tests_shown_is(uint32 line, StdVec *got, const txn_entry_tests_shown_t *want, size_t want_len)
+{
+ StdVec wanted = {(void *) want, want_len, want_len};
+ bool eq = got->len == want_len;
+
+ for (size_t k = 0; k < want_len && eq; k++)
+ {
+ const txn_entry_tests_shown_t *a = &STD_VEC_AT(got, txn_entry_tests_shown_t, k);
+
+ eq = strcmp(a->does, want[k].does) == 0 && a->tuple == want[k].tuple && a->raw == want[k].raw &&
+ a->has_lsn == want[k].has_lsn && (!a->has_lsn || a->lsn == want[k].lsn);
+ }
+ STD_ASSERT_EQ(line, 9, eq, got, txn_entry_tests_shown_list_Debug_fmt, &wanted,
+ txn_entry_tests_shown_list_Debug_fmt);
+ std_vec_free_Checked(got, sizeof(txn_entry_tests_shown_t), _Alignof(txn_entry_tests_shown_t));
+}
+
+/* txn_entry_tests_does_is asserts what a key's versions do, newest first, freeing them. */
+static void
+txn_entry_tests_does_is(uint32 line, StdVec *got, const char *const *want, size_t want_len)
+{
+ bool eq = got->len == want_len;
+
+ for (size_t k = 0; k < want_len && eq; k++)
+ eq = strcmp(STD_VEC_AT(got, txn_entry_tests_shown_t, k).does, want[k]) == 0;
+ STD_ASSERT_MSG(line, 9, eq, "a key's versions do what the test wants");
+ std_vec_free_Checked(got, sizeof(txn_entry_tests_shown_t), _Alignof(txn_entry_tests_shown_t));
+}
+
+/* txn_entry_tests_put returns an op that puts a value under a tuple id. */
+static TxnMemtableOp
+txn_entry_tests_put(const char *value, uint64 tuple)
+{
+ TxnMemtableOp op = {.tag = TXN_MEMTABLE_OP_PUT};
+
+ std_slice_to_vec_Checked(&op.u.put.value, value, strlen(value), 1, 1);
+ std_vec_new(&op.u.put.addresses, sizeof(chunks_TupleU32Locator_t), _Alignof(chunks_TupleU32Locator_t));
+ std_vec_new(&op.u.put.values, sizeof(uint32), _Alignof(uint32));
+ op.u.put.tuple.f0 = tuple;
+ return op;
+}
+
+/* txn_entry_tests_entry returns an entry of a storage's index under a writer, its key's change, logged. */
+static TxnEntryEntry
+txn_entry_tests_entry(HostIoIo *io, const HostStorage *storage, TxnMemtableVersionWriter writer, uint32 index,
+ const char *key, TxnMemtableOp op)
+{
+ TxnEntryEntry entry = {.writer = writer, .storage = *storage, .table = 16385, .index = index, .op = op,
+ .logged = true};
+ bool has;
+ HostError error;
+
+ txn_entry_tests_ok(txn_status_redo_roomed(io, &writer, &has, &entry.roomed, &error), &error, __LINE__);
+ STD_ASSERT_MSG(__LINE__, 0, has, "a roomed entry");
+ std_slice_to_vec_Checked(&entry.key, key, strlen(key), 1, 1);
+ return entry;
+}
+
+/* txn_entry_tests_changed changes an entry, which unwraps at a line. */
+static void
+txn_entry_tests_changed(HostIoIo *io, const HostSharedHeld *held, TxnEntryEntry entry, uint32 line)
+{
+ HostError error;
+
+ txn_entry_tests_ok(txn_entry_change(io, held, &entry, false, NULL, &error), &error, line);
+}
+
+/* txn_entry_tests_writer returns a writer of a transaction the simulation begins. */
+static TxnMemtableVersionWriter
+txn_entry_tests_writer(HostXid *xid)
+{
+ TxnMemtableVersionWriter writer = {0};
+
+ *xid = simc_begin();
+ writer.xid = xid->f0;
+ return writer;
+}
+
+/* txn_entry_tests_logged_t is one of the logged row changes with its record's end. */
+typedef struct txn_entry_tests_logged_t
+{
+ TxnRecordRowChange change;
+ uint64 end;
+} txn_entry_tests_logged_t;
+
+/* txn_entry_tests_changes returns a database's logged row changes, each with its record's end. */
+static StdVec
+txn_entry_tests_changes(uint32 database)
+{
+ StdVec logged = simc_logged();
+ StdVec changes;
+
+ std_vec_new(&changes, sizeof(txn_entry_tests_logged_t), _Alignof(txn_entry_tests_logged_t));
+ for (size_t k = 0; k < logged.len; k++)
+ {
+ simc_TupleU8VecU8Lsn_t *one = &STD_VEC_AT(&logged, simc_TupleU8VecU8Lsn_t, k);
+ txn_entry_tests_logged_t row = {.end = one->f2};
+ HostWalKind kind;
+ HostError error;
+
+ if (host_WalKind_of(one->f0, &kind) && kind == HOST_WAL_KIND_ROW_CHANGE)
+ {
+ if (!txn_record_RowChange_decode(one->f1.ptr, one->f1.len, &row.change, &error))
+ host_Error_drop(&error);
+ else if (row.change.storage.database != database)
+ txn_record_RowChange_drop(&row.change);
+ else
+ std_vec_push_Checked(&changes, &row, sizeof(row), _Alignof(txn_entry_tests_logged_t));
+ }
+ std_vec_free_Checked(&one->f1, 1, 1);
+ }
+ std_vec_free_Checked(&logged, sizeof(simc_TupleU8VecU8Lsn_t), _Alignof(simc_TupleU8VecU8Lsn_t));
+ return changes;
+}
+
+/*
+ * test_txn_entry_tests_an_index_entrys_changes_are_logged_pushed_and_redone_once:
+ * the btree text's section 5 and PLAN's row change row, field 31, an index's
+ * entry put and delete, and a counted entry's added and removed copy, each
+ * append a row change of the index's storage naming its table and its index
+ * and push their versions with its LSN, showing their writer's command; redo
+ * of each on another database's storage, first seen by it, makes its first
+ * base record with that table and index and pushes each version once,
+ * showing command 0, however often it is replayed.
+ */
+void
+test_txn_entry_tests_an_index_entrys_changes_are_logged_pushed_and_redone_once(void)
+{
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ txn_entry_tests_made_t made = txn_entry_tests_made(io, 9371);
+ TxnMemtableVersionWriter writer = {.xid = 51, .command = 2};
+ uint64 copy = (UINT64CONST(1) << 16) | 1;
+ TxnMemtableOp added = txn_entry_tests_put("entry", copy);
+ TxnMemtableOp removed = {.tag = TXN_MEMTABLE_OP_REMOVE};
+ TxnMemtableOp deleted = {.tag = TXN_MEMTABLE_OP_DELETE};
+ StdVec logged;
+ uint64 lsns[4];
+ HostStorage replica_storage = {HOST_PG_DEFAULT, 9372, 16390};
+ RepoRepo replica_repo;
+ HostSharedReader reading;
+ HostSharedHeld replica;
+ TxnBaseBase base;
+ bool some = false;
+ ChunksErrorError failed;
+ HostError error;
+
+ added.tag = TXN_MEMTABLE_OP_ADD;
+ removed.u.remove.f0.f0 = copy;
+ txn_entry_tests_changed(io, &made.held, txn_entry_tests_entry(io, &made.storage, writer, 16390, "p",
+ txn_entry_tests_put("entry", 0)), __LINE__);
+ txn_entry_tests_changed(io, &made.held, txn_entry_tests_entry(io, &made.storage, writer, 16390, "p", deleted),
+ __LINE__);
+ txn_entry_tests_changed(io, &made.held, txn_entry_tests_entry(io, &made.storage, writer, 16390, "c", added),
+ __LINE__);
+ txn_entry_tests_changed(io, &made.held, txn_entry_tests_entry(io, &made.storage, writer, 16390, "c", removed),
+ __LINE__);
+ logged = txn_entry_tests_changes(9371);
+ STD_ASSERT_MSG(__LINE__, 9, logged.len == 4, "logged.len() == 4");
+ for (size_t k = 0; k < logged.len; k++)
+ {
+ const txn_entry_tests_logged_t *one = &STD_VEC_AT(&logged, txn_entry_tests_logged_t, k);
+
+ STD_ASSERT_MSG(__LINE__, 9, one->change.has_index && one->change.index == 16390 && one->change.table == 16385,
+ "every change names the index and its table");
+ lsns[k] = one->end;
+ }
+ {
+ txn_entry_tests_shown_t p[] = {{"delete", 0, 2, true, lsns[1]}, {"put", 0, 2, true, lsns[0]}};
+ txn_entry_tests_shown_t c[] = {{"remove", copy, 2, true, lsns[3]}, {"add", copy, 2, true, lsns[2]}};
+ StdVec got = txn_entry_tests_shown(io, &made.held, (const uint8 *) "p", 1);
+
+ txn_entry_tests_shown_is(__LINE__, &got, p, lengthof(p));
+ got = txn_entry_tests_shown(io, &made.held, (const uint8 *) "c", 1);
+ txn_entry_tests_shown_is(__LINE__, &got, c, lengthof(c));
+ }
+ if (!repo_Repo_create(io, 9372, HOST_PG_DEFAULT, &replica_repo, &failed))
+ STD_RESULT_EXPECT_FAILED(__LINE__, 0, TXN_ENTRY_TESTS_UNWRAP, &failed, chunks_error_Error_Debug_fmt);
+ txn_entry_tests_ok(host_shared_read(io, &reading, &error), &error, __LINE__);
+ for (int pass = 0; pass < 2; pass++)
+ for (size_t k = 0; k < logged.len; k++)
+ {
+ const txn_entry_tests_logged_t *one = &STD_VEC_AT(&logged, txn_entry_tests_logged_t, k);
+ TxnStateMade state = {.key = {.tag = TXN_BASE_KEY_NONE}, .table = one->change.table,
+ .has_index = one->change.has_index, .index = one->change.index};
+
+ txn_entry_tests_ok(txn_state_state(io, &replica_repo, &reading, &replica_storage,
+ TXN_STATE_PERSISTENCE_PERMANENT, true, &state, &replica, &error) &&
+ txn_change_redo(io, &replica, &one->change, one->end, &error), &error, __LINE__);
+ host_shared_Held_drop(&replica);
+ }
+ txn_entry_tests_ok(txn_state_state(io, &replica_repo, &reading, &replica_storage,
+ TXN_STATE_PERSISTENCE_PERMANENT, false, NULL, &replica, &error) &&
+ txn_state_base_record(io, &replica, &some, &base, &error), &error, __LINE__);
+ STD_ASSERT_MSG(__LINE__, 9, some && base.table == 16385 && base.has_index && base.index == 16390 &&
+ base.key.tag == TXN_BASE_KEY_NONE, "(base.table, base.index, base.key) == (16385, Some(16390), None)");
+ txn_base_Base_drop(&base);
+ {
+ txn_entry_tests_shown_t p[] = {{"delete", 0, 0, true, lsns[1]}, {"put", 0, 0, true, lsns[0]}};
+ txn_entry_tests_shown_t c[] = {{"remove", copy, 0, true, lsns[3]}, {"add", copy, 0, true, lsns[2]}};
+ StdVec got = txn_entry_tests_shown(io, &replica, (const uint8 *) "p", 1);
+
+ txn_entry_tests_shown_is(__LINE__, &got, p, lengthof(p));
+ got = txn_entry_tests_shown(io, &replica, (const uint8 *) "c", 1);
+ txn_entry_tests_shown_is(__LINE__, &got, c, lengthof(c));
+ }
+ txn_entry_tests_ok(txn_written_ended(io, &error), &error, __LINE__);
+ for (size_t k = 0; k < logged.len; k++)
+ txn_record_RowChange_drop(&STD_VEC_AT(&logged, txn_entry_tests_logged_t, k).change);
+ std_vec_free_Checked(&logged, sizeof(txn_entry_tests_logged_t), _Alignof(txn_entry_tests_logged_t));
+ host_shared_Held_drop(&replica);
+ host_shared_Reader_drop(&reading);
+ repo_Repo_drop(&replica_repo);
+ txn_entry_tests_made_drop(&made);
+ simc_TestIo_drop(&test_io);
+}
+
+/* txn_entry_tests_refused reports whether a base entry's read is refused with XX001. */
+static bool
+txn_entry_tests_refused(const uint8 *value, size_t value_len, const ChunksTrailerStamp *stamps, size_t stamps_len,
+ bool keyless)
+{
+ StdBytes read;
+ HostError error;
+ bool refused;
+
+ if (txn_entry_based(value, value_len, stamps, stamps_len, keyless, &read, &error))
+ return false;
+ refused = memcmp(host_Error_sqlstate(&error), "XX001", 5) == 0;
+ host_Error_drop(&error);
+ return refused;
+}
+
+/*
+ * test_txn_entry_tests_an_index_base_entry_reads_back_and_refuses_a_keyed_stamp_and_an_uneven_count:
+ * PLAN's format row for an index storage's base leaf entry, a keyed table's
+ * entry reads back its tuple with no stamp and a keyless one its tuple under
+ * a count equal to its stamps, and each other form is data corruption: a
+ * stamp on a keyed table's entry, and a count that differs from a keyless
+ * one's stamps, above or below.
+ */
+void
+test_txn_entry_tests_an_index_base_entry_reads_back_and_refuses_a_keyed_stamp_and_an_uneven_count(void)
+{
+ ChunksTrailerStamp stamps[3] = {{7, 1}, {7, 2}, {7, 3}};
+ StdVec counted;
+ StdBytes read;
+ HostError error;
+
+ txn_entry_tests_ok(txn_entry_based((const uint8 *) "tuple", 5, NULL, 0, false, &read, &error), &error, __LINE__);
+ STD_ASSERT(__LINE__, 9, read.len == 5 && memcmp(read.ptr, "tuple", 5) == 0,
+ "based(b\"tuple\", &[], false).unwrap() == b\"tuple\"");
+ txn_counted_keyless_value(2, (const uint8 *) "tuple", 5, &counted);
+ txn_entry_tests_ok(txn_entry_based(counted.ptr, counted.len, stamps, 2, true, &read, &error), &error, __LINE__);
+ STD_ASSERT(__LINE__, 9, read.len == 5 && memcmp(read.ptr, "tuple", 5) == 0,
+ "based(&counted, &[stamp(1), stamp(2)], true).unwrap() == b\"tuple\"");
+ STD_ASSERT_MSG(__LINE__, 9, txn_entry_tests_refused((const uint8 *) "tuple", 5, stamps, 1, false),
+ "a keyed table's entry with a stamp read back");
+ STD_ASSERT_MSG(__LINE__, 9, txn_entry_tests_refused(counted.ptr, counted.len, stamps, 1, true),
+ "a count above its stamps read back");
+ STD_ASSERT_MSG(__LINE__, 9, txn_entry_tests_refused(counted.ptr, counted.len, stamps, 3, true),
+ "a count below its stamps read back");
+ std_vec_free_Checked(&counted, 1, 1);
+}
+
+/*
+ * test_txn_entry_tests_an_index_storages_large_keys_spill_at_the_threshold:
+ * bugs row 153, an index's 6,000 entries of 1 KB keys, whose versions alone
+ * are a few dozen bytes each, spill by the spill's threshold alone as an
+ * entries spill, since a write's counted bytes take its key's too.
+ */
+void
+test_txn_entry_tests_an_index_storages_large_keys_spill_at_the_threshold(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io;
+ HostIoIo *io;
+ txn_entry_tests_made_t made;
+ HostXid xid;
+ TxnMemtableVersionWriter writer;
+ txn_TupleVecFileU64_t listed;
+ bool spilled = false;
+ HostError error;
+
+ simc_size_delta(UINT64CONST(32) << 20);
+ test_io = simc_io();
+ io = &test_io.io;
+ made = txn_entry_tests_made(io, 9373);
+ writer = txn_entry_tests_writer(&xid);
+ for (uint32 number = 0; number < 6000; number++)
+ {
+ TxnEntryEntry entry = txn_entry_tests_entry(io, &made.storage, writer, 16390, "",
+ txn_entry_tests_put("entry", 0));
+
+ entry.logged = false;
+ std_vec_free_Checked(&entry.key, 1, 1);
+ std_vec_from_elem_zeroed(&entry.key, 1 << 10, 1, 1);
+ for (int b = 0; b < 4; b++)
+ ((uint8 *) entry.key.ptr)[b] = (uint8) (number >> (8 * (3 - b)));
+ txn_entry_tests_changed(io, &made.held, entry, __LINE__);
+ }
+ txn_entry_tests_ok(txn_spill_listed(&TXN_SPILL_STATE_HOME_OPS, io, host_shared_Held_record(&made.held),
+ &made.storage, &listed, &error), &error, __LINE__);
+ for (size_t k = 0; k < listed.f0.len; k++)
+ spilled |= STD_VEC_AT(&listed.f0, TxnSpillFile, k).kind == TXN_RUNS_KIND_SPILLED_ENTRIES;
+ STD_ASSERT_MSG(__LINE__, 9, spilled, "the index's entries spilled as an entries spill");
+ std_vec_free_Checked(&listed.f0, sizeof(TxnSpillFile), _Alignof(TxnSpillFile));
+ txn_entry_tests_ok(txn_written_ended(io, &error), &error, __LINE__);
+ txn_entry_tests_made_drop(&made);
+ simc_TestIo_drop(&test_io);
+ simc_Process_drop(&process);
+}
+
+/* txn_entry_tests_unique_t is a unique test's captures: what its entries' unique checks read. */
+typedef struct txn_entry_tests_unique_t
+{
+ HostIoIo *io;
+ txn_entry_tests_made_t *made;
+ TreesStoreReader store;
+ TxnInsertUniqueness uniqueness;
+ HostIoWaits waits;
+} txn_entry_tests_unique_t;
+
+/* txn_entry_tests_checked runs a unique index's check of an entry in a transaction it commits, answering what it did. */
+static TxnEntryChanged
+txn_entry_tests_checked(txn_entry_tests_unique_t *u, const char *key, TxnMemtableOp op, uint64 tuple, bool partial,
+ bool commit, HostXid xid)
+{
+ TxnMemtableVersionWriter writer = {.xid = xid.f0};
+ TxnEntryEntry entry = txn_entry_tests_entry(u->io, &u->made->storage, writer, u->uniqueness.index, key, op);
+ TxnEntryChanged changed;
+ HostRow row = {tuple};
+ HostError error;
+
+ txn_entry_tests_ok(txn_entry_unique(u->io, TREES_STORE_READER_NODE_STORE(&u->store), &u->made->held, &entry, false,
+ NULL, &u->uniqueness, &u->waits, row, partial, &changed, &error), &error,
+ __LINE__);
+ if (commit)
+ {
+ simc_commit(xid);
+ txn_entry_tests_ok(txn_written_ended(u->io, &error), &error, __LINE__);
+ }
+ return changed;
+}
+
+/* txn_entry_tests_unique begins a unique test's captures over a made index storage, for an index. */
+static txn_entry_tests_unique_t
+txn_entry_tests_unique(HostIoIo *io, txn_entry_tests_made_t *made, const TreesKeyOrder *order, uint32 index)
+{
+ txn_entry_tests_unique_t u = {.io = io, .made = made, .uniqueness = {.order = order, .index = index},
+ .waits = host_io_Waits_testing()};
+
+ u.store = repo_Repo_reader(&made->repo, HOST_CACHE_PLACEMENT_SERVER, &made->reading);
+ return u;
+}
+
+/* txn_entry_tests_exists_is reports whether a unique check answered an existing entry of a value, freeing it. */
+static bool
+txn_entry_tests_exists_is(TxnEntryChanged changed, const char *value)
+{
+ bool is = changed.tag == TXN_ENTRY_CHANGED_EXISTS && changed.u.exists.len == strlen(value) &&
+ memcmp(changed.u.exists.ptr, value, changed.u.exists.len) == 0;
+
+ if (changed.tag == TXN_ENTRY_CHANGED_EXISTS)
+ std_vec_free_Checked(&changed.u.exists, 1, 1);
+ return is;
+}
+
+/*
+ * test_txn_entry_tests_a_unique_entry_takes_a_free_key_and_answers_an_existing_entrys_value:
+ * the btree's part 2, slice 5, a unique index's entry resolves its key as an
+ * immediate key's insert does: a free key takes the put, a key holding a
+ * committed entry answers that entry's value and writes nothing, and once a
+ * committed delete ended the entry, the key takes the put again.
+ */
+void
+test_txn_entry_tests_a_unique_entry_takes_a_free_key_and_answers_an_existing_entrys_value(void)
+{
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ txn_entry_tests_made_t made = txn_entry_tests_made(io, 9373);
+ TreesKeyOrder order = {.tag = TREES_KEY_ORDER_BYTES};
+ txn_entry_tests_unique_t u = txn_entry_tests_unique(io, &made, &order, 16390);
+ static const char *const ops[] = {"put", "delete", "put"};
+ HostXid xid;
+ StdVec shown;
+ HostError error;
+
+ STD_ASSERT_MSG(__LINE__, 9, txn_entry_tests_checked(&u, "k", txn_entry_tests_put("a", 0), 1, false, true,
+ simc_begin()).tag == TXN_ENTRY_CHANGED_DONE,
+ "a free key takes the put");
+ STD_ASSERT_MSG(__LINE__, 9, txn_entry_tests_exists_is(txn_entry_tests_checked(&u, "k", txn_entry_tests_put("b", 0),
+ 1, false, true, simc_begin()), "a"),
+ "a key holding a committed entry answers its value");
+ {
+ TxnMemtableOp deleted = {.tag = TXN_MEMTABLE_OP_DELETE};
+ TxnMemtableVersionWriter writer = txn_entry_tests_writer(&xid);
+
+ txn_entry_tests_changed(io, &made.held, txn_entry_tests_entry(io, &made.storage, writer, 16390, "k", deleted),
+ __LINE__);
+ simc_commit(xid);
+ txn_entry_tests_ok(txn_written_ended(io, &error), &error, __LINE__);
+ }
+ STD_ASSERT_MSG(__LINE__, 9, txn_entry_tests_checked(&u, "k", txn_entry_tests_put("d", 0), 1, false, true,
+ simc_begin()).tag == TXN_ENTRY_CHANGED_DONE,
+ "a key whose entry a committed delete ended takes the put again");
+ shown = txn_entry_tests_shown(io, &made.held, (const uint8 *) "k", 1);
+ txn_entry_tests_does_is(__LINE__, &shown, ops, lengthof(ops));
+ txn_entry_tests_made_drop(&made);
+ simc_TestIo_drop(&test_io);
+}
+
+/*
+ * test_txn_entry_tests_a_counted_unique_entry_writes_nothing_for_its_own_copy_and_answers_another:
+ * the btree's part 2, slice 5 (orev-14's R2 and R4), a keyless table's
+ * unique entry counts copies, so a copy under a tuple id the key holds live
+ * is counted already and writes nothing, and a copy under another tuple id
+ * answers the live copy's value.
+ */
+void
+test_txn_entry_tests_a_counted_unique_entry_writes_nothing_for_its_own_copy_and_answers_another(void)
+{
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ txn_entry_tests_made_t made = txn_entry_tests_made(io, 9374);
+ TreesKeyOrder order = {.tag = TREES_KEY_ORDER_BYTES};
+ txn_entry_tests_unique_t u = txn_entry_tests_unique(io, &made, &order, 16391);
+ static const char *const ops[] = {"add"};
+ StdVec shown;
+
+ for (int k = 0; k < 2; k++)
+ {
+ TxnMemtableOp add = txn_entry_tests_put("v", 1);
+
+ add.tag = TXN_MEMTABLE_OP_ADD;
+ STD_ASSERT_MSG(__LINE__, 9, txn_entry_tests_checked(&u, "k", add, 1, false, true, simc_begin()).tag ==
+ TXN_ENTRY_CHANGED_DONE, "a copy under a tuple id the key holds live writes nothing");
+ }
+ {
+ TxnMemtableOp add = txn_entry_tests_put("v", 2);
+
+ add.tag = TXN_MEMTABLE_OP_ADD;
+ STD_ASSERT_MSG(__LINE__, 9, txn_entry_tests_exists_is(txn_entry_tests_checked(&u, "k", add, 2, false, true,
+ simc_begin()), "v"),
+ "a copy under another tuple id answers the live copy's value");
+ }
+ shown = txn_entry_tests_shown(io, &made.held, (const uint8 *) "k", 1);
+ txn_entry_tests_does_is(__LINE__, &shown, ops, lengthof(ops));
+ txn_entry_tests_made_drop(&made);
+ simc_TestIo_drop(&test_io);
+}
+
+/*
+ * test_txn_entry_tests_a_partial_unique_check_answers_a_running_write_without_waiting:
+ * a speculative insert's partial check (CommPF 10,
+ * insert-conflict-specconflict) answers another transaction's running write
+ * of its key as running, writing nothing and waiting for none, as
+ * _bt_check_unique answers UNIQUE_CHECK_PARTIAL.
+ */
+void
+test_txn_entry_tests_a_partial_unique_check_answers_a_running_write_without_waiting(void)
+{
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ txn_entry_tests_made_t made = txn_entry_tests_made(io, 9375);
+ TreesKeyOrder order = {.tag = TREES_KEY_ORDER_BYTES};
+ txn_entry_tests_unique_t u = txn_entry_tests_unique(io, &made, &order, 16392);
+ static const char *const ops[] = {"put"};
+ HostXid first = simc_begin();
+ HostXid second = simc_begin();
+ StdVec shown;
+ HostError error;
+
+ STD_ASSERT_MSG(__LINE__, 9, txn_entry_tests_checked(&u, "k", txn_entry_tests_put("a", 0), 1, false, false,
+ first).tag == TXN_ENTRY_CHANGED_DONE,
+ "the first write takes the free key");
+ STD_ASSERT_MSG(__LINE__, 9, txn_entry_tests_checked(&u, "k", txn_entry_tests_put("b", 0), 1, true, false,
+ second).tag == TXN_ENTRY_CHANGED_RUNNING,
+ "a partial check answers the running write as running");
+ simc_commit(first);
+ simc_commit(second);
+ txn_entry_tests_ok(txn_written_ended(io, &error), &error, __LINE__);
+ shown = txn_entry_tests_shown(io, &made.held, (const uint8 *) "k", 1);
+ txn_entry_tests_does_is(__LINE__, &shown, ops, lengthof(ops));
+ txn_entry_tests_made_drop(&made);
+ simc_TestIo_drop(&test_io);
+}
+
+/*
+ * test_txn_entry_tests_an_entry_and_its_record_change_together: an index
+ * that keeps records (CommPF 10's no-user-code removal) changes an entry and
+ * its record in one change: both pushed under the entry's writer, after
+ * every entry in the storage's order, and both removed together.
+ */
+void
+test_txn_entry_tests_an_entry_and_its_record_change_together(void)
+{
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ txn_entry_tests_made_t made = txn_entry_tests_made(io, 9376);
+ static const char *const ops[] = {"delete", "put"};
+ StdVec record;
+ StdVec shown;
+ HostError error;
+
+ txn_entry_record((const uint8 *) "row", 3, &record);
+ for (int k = 0; k < 2; k++)
+ {
+ HostXid xid;
+ TxnMemtableVersionWriter writer = txn_entry_tests_writer(&xid);
+ TxnMemtableOp entry_op = {.tag = TXN_MEMTABLE_OP_DELETE};
+ txn_TupleVecU8Op_t recorded = {.f1 = {.tag = TXN_MEMTABLE_OP_DELETE}};
+ TxnEntryEntry entry;
+
+ if (k == 0)
+ {
+ entry_op = txn_entry_tests_put("v", 0);
+ recorded.f1 = txn_entry_tests_put("k", 0);
+ }
+ entry = txn_entry_tests_entry(io, &made.storage, writer, 16393, "k", entry_op);
+ std_slice_to_vec_Checked(&recorded.f0, record.ptr, record.len, 1, 1);
+ txn_entry_tests_ok(txn_entry_change(io, &made.held, &entry, true, &recorded, &error), &error, __LINE__);
+ simc_commit(xid);
+ txn_entry_tests_ok(txn_written_ended(io, &error), &error, __LINE__);
+ }
+ shown = txn_entry_tests_shown(io, &made.held, (const uint8 *) "k", 1);
+ txn_entry_tests_does_is(__LINE__, &shown, ops, lengthof(ops));
+ shown = txn_entry_tests_shown(io, &made.held, record.ptr, record.len);
+ txn_entry_tests_does_is(__LINE__, &shown, ops, lengthof(ops));
+ STD_ASSERT_MSG(__LINE__, 9, record.len > 0 && ((const uint8 *) record.ptr)[0] == TXN_ENTRY_RECORD &&
+ memcmp(record.ptr, "k", 1) > 0, "record.as_slice() > b\"k\" && record[0] == RECORD");
+ std_vec_free_Checked(&record, 1, 1);
+ txn_entry_tests_made_drop(&made);
+ simc_TestIo_drop(&test_io);
+}
+
+#endif /* DOLTGRES_TEST_TXN */
diff --git a/src/doltgres/txn/c/fold.c b/src/doltgres/txn/c/fold.c
index e1e27dec9dc31..6b32fd775b499 100644
--- a/src/doltgres/txn/c/fold.c
+++ b/src/doltgres/txn/c/fold.c
@@ -423,7 +423,7 @@ txn_fold_Every_Rule_ended(const void *self, HostIoIo *io, TxnMemtableVersionVers
}
/* TXN_FOLD_EVERY_RULE_OPS is Every's Rule: every row begun and none ended. */
-static const TxnVisibleRuleOps TXN_FOLD_EVERY_RULE_OPS = {
+const TxnVisibleRuleOps TXN_FOLD_EVERY_RULE_OPS = {
.begun = txn_fold_Every_Rule_begun,
.ended = txn_fold_Every_Rule_ended,
};
@@ -888,6 +888,19 @@ txn_fold_ending(HostIoIo *io, TxnVisibleRule rule, TxnMemtableVersionVersion *ch
return true;
}
+/*
+ * txn_fold_owned returns a pending tuple id's ender word for a row's
+ * ender's xid: marked TXN_BASE_OWN when it is the row's own writer's, 0 for
+ * none.
+ */
+static uint64
+txn_fold_owned(uint64 ender, uint64 writer)
+{
+ return ender != 0 && ender == writer ? ender | TXN_BASE_OWN : ender;
+}
+
+static void txn_fold_PendingRow_drop(void *row_);
+
/* TxnFoldFormedTag is Formed's variants, and none before a key's entry is formed. */
typedef enum TxnFoldFormedTag
{
@@ -948,6 +961,9 @@ typedef struct txn_fold_folded_key_frame_t
bool *has_result;
TxnFoldKeyFold *result;
uint64 *result_ended;
+ /* gone is a Vec of the delta rows the fold ends. */
+ StdVec *gone;
+ bool gone_live;
HostError *error;
bool ok;
} txn_fold_folded_key_frame_t;
@@ -975,7 +991,9 @@ txn_fold_stamp_order(const void *a, const void *b)
/*
* txn_fold_folded_key_ended reads which rows the rule's appliers ended: each
- * delta row it began, for the newest ender, and each base copy, for its
+ * delta row it began, for the newest ender, and, but in an index's storage,
+ * made pending by its tuple id with its ender's xid, its heap bytes, and its
+ * value ids, since it never reaches the base; and each base copy, for its
* ender's xid.
*/
static bool
@@ -987,13 +1005,36 @@ txn_fold_folded_key_ended(txn_fold_folded_key_frame_t *frame)
{
const TxnVisibleSeenRow *seen = &STD_VEC_AT(&frame->every.rows, TxnVisibleSeenRow, i);
TxnVisibleDirty dirty = txn_visible_Dirty_Default_default();
+ uint64 writer = txn_fold_writer_xid(seen->row.version);
bool begun;
+ TxnFoldPendingRow gone;
+ TxnMemtableVersionValues values;
+ uint32 id;
if (!frame->rule.ops->begun(frame->rule.self, frame->io, &seen->row, &dirty, &begun, frame->error))
return false;
- if (begun && !txn_fold_ending(frame->io, frame->rule, seen->changer, txn_fold_writer_xid(seen->row.version),
- &frame->max_ended, &ender, frame->error))
+ if (!begun)
+ continue;
+ if (!txn_fold_ending(frame->io, frame->rule, seen->changer, writer, &frame->max_ended, &ender, frame->error))
return false;
+ if (ender == 0 || frame->index)
+ continue;
+ gone = (TxnFoldPendingRow)
+ {
+ .tuple = seen->row.put.tuple.f0, .ender = txn_fold_owned(ender, writer),
+ .bytes = seen->row.put.heap_bytes
+ };
+ std_vec_new(&gone.ids, TXN_FOLD_U32);
+ values = txn_memtable_version_PutRef_values(&seen->row.put);
+ while (txn_memtable_version_Values_Iterator_next(&values, &id))
+ std_vec_push_Checked(&gone.ids, &id, TXN_FOLD_U32);
+ if (!std_vec_try_reserve(frame->gone, 1, TXN_FOLD_PENDING_ROW))
+ {
+ std_vec_free_Checked(&gone.ids, TXN_FOLD_U32);
+ *frame->error = host_Error_out_of_memory();
+ return false;
+ }
+ std_vec_push_Checked(frame->gone, &gone, TXN_FOLD_PENDING_ROW);
}
std_vec_new(&frame->enders, TXN_FOLD_U64);
frame->enders_live = true;
@@ -1046,7 +1087,7 @@ txn_fold_folded_key_kept(txn_fold_folded_key_frame_t *frame, bool *ended)
}
*ended = true;
pended.f0 = stamp->tuple;
- pended.f1 = STD_VEC_AT(&frame->enders, uint64, i);
+ pended.f1 = txn_fold_owned(STD_VEC_AT(&frame->enders, uint64, i), stamp->txn);
std_vec_reserve_Checked(&frame->pending, 1, TXN_FOLD_PENDED);
std_vec_clone(&pended.f2, &frame->based.ids, TXN_FOLD_U32);
std_vec_push_Checked(&frame->pending, &pended, TXN_FOLD_PENDED);
@@ -1174,6 +1215,8 @@ txn_fold_folded_key_body(void *frame_)
size_t stored_len = 0;
fold->has_formed_by = false;
+ std_vec_new(frame->gone, TXN_FOLD_PENDING_ROW);
+ frame->gone_live = true;
if (!txn_visible_seen(frame->io, frame->rule, frame->versions, frame->versions_len, NULL, &frame->seen,
frame->error))
return;
@@ -1223,6 +1266,7 @@ txn_fold_folded_key_body(void *frame_)
if (!ended && frame->stamps.len == base_copies)
{
*frame->has_result = false;
+ frame->gone_live = false;
frame->ok = true;
return;
}
@@ -1249,6 +1293,7 @@ txn_fold_folded_key_body(void *frame_)
fold->has_formed_by = false;
frame->ctids_live = frame->pending_live = false;
}
+ frame->gone_live = false;
frame->ok = true;
return;
}
@@ -1271,6 +1316,7 @@ txn_fold_folded_key_body(void *frame_)
frame->value_live = frame->addresses_live = frame->stamps_live = frame->formed_ids_live = false;
frame->ctids_live = frame->pending_live = false;
*frame->has_result = true;
+ frame->gone_live = false;
frame->ok = true;
}
@@ -1304,6 +1350,9 @@ txn_fold_folded_key_pad(void *frame_)
txn_visible_Seen_drop(&frame->every);
if (frame->seen_live)
txn_visible_Seen_drop(&frame->seen);
+ if (frame->gone_live)
+ std_vec_drop_Checked(frame->gone, TXN_FOLD_PENDING_ROW, txn_fold_PendingRow_drop);
+ frame->gone_live = false;
frame->addresses_live = frame->value_live = frame->ctids_live = frame->written_live = false;
frame->formed_ids_live = frame->formed_addresses_live = frame->pending_live = frame->stamps_live = false;
frame->enders_live = frame->based_live = frame->every_live = frame->seen_live = false;
@@ -1321,17 +1370,22 @@ txn_fold_folded_key_pad(void *frame_)
* PLAN's form of an index storage's base leaf entry: a keyed table's keeps no
* stamp and is one copy of its row, and a keyless table's counts its copies
* with a stamp each, by writer and then tuple id; it makes no tuple id edit.
+ * Last, it answers each delta row the fold applies and an applied version
+ * ends, which never reaches the base, by its tuple id, with its ender's xid,
+ * its heap bytes, and its value ids, whose tuple id the fold makes pending,
+ * so its take leaves the delta with the rest of its block's run (bugs row
+ * 228's (E)).
*/
bool
txn_fold_folded_key(HostIoIo *io, TreesStoreNodeStore store, bool has_base, TreesStoreTree base,
const TreesKeyOrder *order, bool keyless, bool index, TxnVisibleRule rule, const uint8 *key,
size_t key_len, TxnMemtableVersionVersion *versions, size_t versions_len, bool *has_result,
- TxnFoldKeyFold *result, uint64 *max_ended, HostError *error)
+ TxnFoldKeyFold *result, uint64 *max_ended, StdVec *gone, HostError *error)
{
txn_fold_folded_key_frame_t frame = {.io = io, .store = store, .has_base = has_base, .base = base,
.order = order, .keyless = keyless, .index = index, .rule = rule, .key = key, .key_len = key_len,
.versions = versions, .versions_len = versions_len, .has_result = has_result, .result = result,
- .result_ended = max_ended, .error = error};
+ .result_ended = max_ended, .gone = gone, .error = error};
std_landing(&frame, txn_fold_folded_key_body, txn_fold_folded_key_pad);
txn_fold_folded_key_pad(&frame);
@@ -1447,6 +1501,28 @@ txn_fold_PendingRows_insert(StdVec *rows, TxnFoldPendingRow *row)
std_vec_insert(rows, low, row, TXN_FOLD_PENDING_ROW, RUST_FILE, 480, 25);
}
+/* txn_fold_PendingRows_absent inserts a pending tuple id, taking it, only when the rows hold none of its tuple id, dropping it otherwise. */
+static void
+txn_fold_PendingRows_absent(StdVec *rows, TxnFoldPendingRow *row)
+{
+ size_t low = 0;
+ size_t high = rows->len;
+
+ while (low < high)
+ {
+ size_t mid = low + (high - low) / 2;
+
+ if (STD_VEC_AT(rows, TxnFoldPendingRow, mid).tuple < row->tuple)
+ low = mid + 1;
+ else
+ high = mid;
+ }
+ if (low < rows->len && STD_VEC_AT(rows, TxnFoldPendingRow, low).tuple == row->tuple)
+ txn_fold_PendingRow_drop(row);
+ else
+ std_vec_insert(rows, low, row, TXN_FOLD_PENDING_ROW, RUST_FILE, 488, 46);
+}
+
/* txn_fold_FoldedRows_drop frees what a fold makes of a storage's rows. */
void
txn_fold_FoldedRows_drop(TxnFoldFoldedRows *self)
@@ -1619,6 +1695,7 @@ txn_fold_folded_rows_body(void *frame_)
bool has_next;
bool has_fold;
uint64 ended;
+ StdVec gone;
if (!host_delta_hold(frame->io, &frame->hold, frame->error))
return;
@@ -1652,9 +1729,12 @@ txn_fold_folded_rows_body(void *frame_)
}
if (!txn_fold_folded_key(frame->io, frame->store, frame->has_base, frame->base, frame->order, frame->keyless,
frame->index, rule, frame->next.f0.ptr, frame->next.f0.len, frame->next.f1.ptr,
- frame->next.f1.len, &has_fold, &frame->fold, &ended, frame->error))
+ frame->next.f1.len, &has_fold, &frame->fold, &ended, &gone, frame->error))
return;
rows->max_ended = Max(rows->max_ended, ended);
+ for (size_t i = 0; i < gone.len; i++)
+ txn_fold_PendingRows_absent(&rows->pending, &STD_VEC_AT(&gone, TxnFoldPendingRow, i));
+ std_vec_free_Checked(&gone, TXN_FOLD_PENDING_ROW);
if (has_fold)
{
frame->fold_live = true;
@@ -1731,6 +1811,7 @@ typedef struct txn_fold_committed_moves_frame_t
const TreesKeyOrder *order;
bool keyless;
bool index;
+ HostLsn folded;
uint64 own;
const uint8 *key;
size_t key_len;
@@ -1755,13 +1836,15 @@ txn_fold_committed_moves_closure_0(txn_fold_committed_moves_frame_t *frame, uint
TxnVisibleRule rule = {&TXN_FOLD_COMMITTING_RULE_OPS, &committing};
bool has_fold;
uint64 ended;
+ StdVec gone;
memset(added, 0, sizeof(*added));
memset(removed, 0, sizeof(*removed));
if (!txn_fold_folded_key(frame->io, frame->store, frame->has_base, frame->base, frame->order, frame->keyless,
frame->index, rule, frame->key, frame->key_len, frame->versions.ptr,
- frame->versions.len, &has_fold, &frame->fold, &ended, frame->error))
+ frame->versions.len, &has_fold, &frame->fold, &ended, &gone, frame->error))
return false;
+ std_vec_drop_Checked(&gone, TXN_FOLD_PENDING_ROW, txn_fold_PendingRow_drop);
if (!has_fold)
return true;
frame->fold_live = true;
@@ -1803,8 +1886,8 @@ txn_fold_committed_moves_body(void *frame_)
if (!host_delta_hold(frame->io, &frame->hold, frame->error))
return;
frame->hold_live = true;
- if (!txn_memtable_table_versions(frame->io, &frame->hold, host_shared_Held_record(frame->held), frame->key,
- frame->key_len, &frame->versions, frame->error))
+ if (!txn_visible_versions(frame->io, &frame->hold, host_shared_Held_record(frame->held), frame->key,
+ frame->key_len, frame->folded, &frame->versions, frame->error))
return;
frame->versions_live = true;
if (!txn_fold_committed_moves_closure_0(frame, frame->own, &with_added, &with_removed) ||
@@ -1835,18 +1918,20 @@ txn_fold_committed_moves_pad(void *frame_)
* own moves a storage's totals at a key: what the key's fold over the base
* writes and gives back with the transaction's versions applied, against
* what it does without them, as the sizes it adds and those it takes away,
- * neither below zero.
+ * neither below zero. It folds only the versions a base folded at folded
+ * didn't apply, so a version a restart's redo replayed under a base that
+ * holds it counts once (bugs row 323).
*/
bool
txn_fold_committed_moves(HostIoIo *io, TreesStoreNodeStore store, const HostSharedHeld *held, bool has_base,
TreesStoreTree base, bool has_tuple_ids, TreesStoreTree tuple_ids, const TreesKeyOrder *order,
- bool keyless, bool index, uint64 own, const uint8 *key, size_t key_len,
+ bool keyless, bool index, HostLsn folded, uint64 own, const uint8 *key, size_t key_len,
TxnBaseSizes *result_0, TxnBaseSizes *result_1, HostError *error)
{
txn_fold_committed_moves_frame_t frame = {.io = io, .store = store, .held = held, .has_base = has_base,
.base = base, .has_tuple_ids = has_tuple_ids, .tuple_ids = tuple_ids, .order = order, .keyless = keyless,
- .index = index, .own = own, .key = key, .key_len = key_len, .result_0 = result_0, .result_1 = result_1,
- .error = error};
+ .index = index, .folded = folded, .own = own, .key = key, .key_len = key_len, .result_0 = result_0,
+ .result_1 = result_1, .error = error};
std_landing(&frame, txn_fold_committed_moves_body, txn_fold_committed_moves_pad);
txn_fold_committed_moves_pad(&frame);
@@ -1988,42 +2073,76 @@ txn_fold_committed_value_moves(HostIoIo *io, TreesStoreNodeStore store, const Ho
return frame.ok;
}
-/* txn_TupleHandleWriter_t is ran_key's (Handle, Writer): a version a drop removes, with its writer. */
-typedef struct txn_TupleHandleWriter_t
-{
- TxnMemtableVersionHandle f0;
- TxnMemtableVersionWriter f1;
-} txn_TupleHandleWriter_t;
-
-/* txn_TupleU32VecTupleHandleWriter_t is Spaced's (u32, Vec<(Handle, Writer)>): a key's versions a drop removes. */
-typedef struct txn_TupleU32VecTupleHandleWriter_t
+/* txn_TupleU32VecHandle_t is Spaced's (u32, Vec): a key's versions a drop removes. */
+typedef struct txn_TupleU32VecHandle_t
{
uint32 f0;
StdVec f1;
-} txn_TupleU32VecTupleHandleWriter_t;
+} txn_TupleU32VecHandle_t;
+
+/* txn_fold_pending_holds reports whether pending rows, in tuple id order, hold a tuple id. */
+static bool
+txn_fold_pending_holds(const StdVec *pending, uint64 tid)
+{
+ size_t low = 0;
+ size_t high = pending->len;
+
+ while (low < high)
+ {
+ size_t mid = low + (high - low) / 2;
+ uint64 at = STD_VEC_AT(pending, TxnFoldPendingRow, mid).tuple;
+
+ if (at == tid)
+ return true;
+ if (at < tid)
+ low = mid + 1;
+ else
+ high = mid;
+ }
+ return false;
+}
+
+/* txn_fold_edit_order orders edits by their keys' bytes, for qsort. */
+static int
+txn_fold_edit_order(const void *a, const void *b)
+{
+ StdBytes x = trees_mutate_Edit_key(a);
+ StdBytes y = trees_mutate_Edit_key(b);
+ int c = memcmp(x.ptr, y.ptr, Min(x.len, y.len));
+
+ if (c != 0)
+ return c;
+ return (x.len > y.len) - (x.len < y.len);
+}
/*
* TxnFoldSpaced is Spaced: what the drop through a point takes out of a
- * storage's map, the base's tree's edits, each key's versions it removes with
- * their writers, whether it removes every version the map holds, and
- * whether it met a version whose writer the point leaves for later.
+ * storage's map, the base's tree's edits, each key's versions it removes,
+ * whether it removes every version the map holds, and whether it met a
+ * version whose writer the point leaves for later.
*/
typedef struct TxnFoldSpaced
{
/* edits is a Vec. */
StdVec edits;
- /* removed is a Vec<(u32, Vec<(Handle, Writer)>)>. */
+ /* removed is a Vec<(u32, Vec)>. */
StdVec removed;
bool emptied;
bool unresolved;
+
+ /*
+ * kept is a Vec of the spilled versions the drop keeps, each key's
+ * newest first, which the fold lists in a residual before their files
+ * leave.
+ */
+ StdVec kept;
} TxnFoldSpaced;
-#define TXN_FOLD_REMOVED sizeof(txn_TupleU32VecTupleHandleWriter_t), _Alignof(txn_TupleU32VecTupleHandleWriter_t)
-#define TXN_FOLD_HANDLE_WRITER sizeof(txn_TupleHandleWriter_t), _Alignof(txn_TupleHandleWriter_t)
+#define TXN_FOLD_REMOVED sizeof(txn_TupleU32VecHandle_t), _Alignof(txn_TupleU32VecHandle_t)
-/* TxnFoldFits reports whether a run of a key's versions, newest first, may leave the delta. */
-typedef bool (*TxnFoldFits) (void *captures, HostIoIo *io, TxnMemtableVersionVersion *run, size_t run_len,
- bool *result, HostError *error);
+/* TxnFoldEach reads a run of a key's versions, newest first, that leaves the delta. */
+typedef bool (*TxnFoldEach) (void *captures, HostIoIo *io, TxnMemtableVersionVersion *run, size_t run_len,
+ HostError *error);
/* TxnFoldPaired reports whether the installed base's tuple id tree pairs a tuple id. */
typedef bool (*TxnFoldPaired) (void *captures, HostIoIo *io, uint64 tuple, bool *result, HostError *error);
@@ -2032,9 +2151,19 @@ typedef bool (*TxnFoldPaired) (void *captures, HostIoIo *io, uint64 tuple, bool
static void
txn_fold_Removed_drop(void *removed_)
{
- txn_TupleU32VecTupleHandleWriter_t *removed = removed_;
+ txn_TupleU32VecHandle_t *removed = removed_;
- std_vec_free_Checked(&removed->f1, TXN_FOLD_HANDLE_WRITER);
+ std_vec_free_Checked(&removed->f1, TXN_FOLD_HANDLE);
+}
+
+/* txn_fold_Kept_drop frees a kept version's key and bytes. */
+static void
+txn_fold_Kept_drop(void *kept_)
+{
+ TxnSpillKept *kept = kept_;
+
+ std_vec_free_Checked(&kept->key, TXN_FOLD_BYTES);
+ std_vec_free_Checked(&kept->bytes, TXN_FOLD_BYTES);
}
/* txn_fold_Spaced_drop frees what a drop takes out of a map. */
@@ -2043,6 +2172,7 @@ txn_fold_Spaced_drop(TxnFoldSpaced *self)
{
std_vec_drop_Checked(&self->edits, TXN_FOLD_EDIT, trees_mutate_Edit_drop);
std_vec_drop_Checked(&self->removed, TXN_FOLD_REMOVED, txn_fold_Removed_drop);
+ std_vec_drop_Checked(&self->kept, sizeof(TxnSpillKept), _Alignof(TxnSpillKept), txn_fold_Kept_drop);
}
/* txn_fold_ran_key_frame_t is ran_key's state across its pad: the new entry's key, while live. */
@@ -2058,8 +2188,9 @@ typedef struct txn_fold_ran_key_frame_t
size_t own;
HostLsn point;
bool committed;
- TxnFoldFits fits;
- void *fits_captures;
+ TxnRunsKind spilled;
+ TxnFoldEach each;
+ void *each_captures;
StdVec named;
bool named_live;
HostError *error;
@@ -2077,8 +2208,9 @@ txn_fold_ran_key_named(TxnMemtableVersionVersion *version)
/*
* txn_fold_ran_key_body finds the oldest run of the key's versions the point
- * applies or drops, the longest one that fits, and adds its source's entry
- * and its memtable versions' removal.
+ * applies or drops, hands it to each, keeps each spilled version above it
+ * the point applies or drops, and adds its source's entry and its memtable
+ * versions' removal.
*/
static void
txn_fold_ran_key_body(void *frame_)
@@ -2086,11 +2218,10 @@ txn_fold_ran_key_body(void *frame_)
txn_fold_ran_key_frame_t *frame = frame_;
TxnFoldSpaced *spaced = frame->spaced;
size_t len = frame->versions_len;
- size_t longest = 0;
- size_t run;
+ size_t run = 0;
size_t first;
TxnMemtableVersionVersion *source = NULL;
- txn_TupleU32VecTupleHandleWriter_t removed = {.f0 = frame->id};
+ txn_TupleU32VecHandle_t removed = {.f0 = frame->id};
for (size_t i = len; i-- > 0;)
{
@@ -2103,17 +2234,28 @@ txn_fold_ran_key_body(void *frame_)
spaced->unresolved = true;
break;
}
- longest++;
+ run++;
}
- for (run = longest; run > 0; run--)
+ if (run > 0 && frame->each != NULL &&
+ !frame->each(frame->each_captures, frame->io, &frame->versions[len - run], run, frame->error))
+ return;
+ for (size_t i = Min(frame->own, len - run); i < len - run; i++)
{
- bool fits = true;
+ TxnFoldApplied applied;
+ TxnSpillKept kept = {.kind = frame->spilled};
- if (frame->fits != NULL &&
- !frame->fits(frame->fits_captures, frame->io, &frame->versions[len - run], run, &fits, frame->error))
+ if (!txn_fold_applied(frame->io, &frame->versions[i], frame->point, &applied, frame->error))
return;
- if (fits)
- break;
+ if (applied.tag == TXN_FOLD_APPLIED_STAYS)
+ continue;
+ if (!std_vec_try_reserve(&spaced->kept, 1, sizeof(kept), _Alignof(TxnSpillKept)))
+ {
+ *frame->error = host_Error_out_of_memory();
+ return;
+ }
+ txn_fold_bytes(frame->key, frame->key_len, &kept.key);
+ txn_memtable_version_Version_stored(&frame->versions[i], &kept.bytes);
+ std_vec_push_Checked(&spaced->kept, &kept, sizeof(kept), _Alignof(TxnSpillKept));
}
spaced->emptied &= run == len;
if (run == 0)
@@ -2175,14 +2317,9 @@ txn_fold_ran_key_body(void *frame_)
*frame->error = host_Error_out_of_memory();
return;
}
- std_vec_new(&removed.f1, TXN_FOLD_HANDLE_WRITER);
+ std_vec_new(&removed.f1, TXN_FOLD_HANDLE);
for (size_t i = first; i < Max(frame->own, first); i++)
- {
- TxnMemtableVersionRecord record = txn_memtable_version_Version_record(&frame->versions[i]);
- txn_TupleHandleWriter_t handle = {frame->versions[i].handle, txn_memtable_version_Record_writer(&record)};
-
- std_vec_push_Checked(&removed.f1, &handle, TXN_FOLD_HANDLE_WRITER);
- }
+ std_vec_push_Checked(&removed.f1, &frame->versions[i].handle, TXN_FOLD_HANDLE);
std_vec_push_Checked(&spaced->removed, &removed, TXN_FOLD_REMOVED);
frame->ok = true;
}
@@ -2206,11 +2343,11 @@ txn_fold_ran_key_pad(void *frame_)
static bool
txn_fold_ran_key(HostIoIo *io, TxnFoldSpaced *spaced, const uint8 *key, size_t key_len, uint32 id,
TxnMemtableVersionVersion *versions, size_t versions_len, size_t own, HostLsn point, bool committed,
- TxnFoldFits fits, void *fits_captures, HostError *error)
+ TxnRunsKind spilled, TxnFoldEach each, void *each_captures, HostError *error)
{
txn_fold_ran_key_frame_t frame = {.io = io, .spaced = spaced, .key = key, .key_len = key_len, .id = id,
.versions = versions, .versions_len = versions_len, .own = own, .point = point, .committed = committed,
- .fits = fits, .fits_captures = fits_captures, .error = error};
+ .spilled = spilled, .each = each, .each_captures = each_captures, .error = error};
std_landing(&frame, txn_fold_ran_key_body, txn_fold_ran_key_pad);
txn_fold_ran_key_pad(&frame);
@@ -2230,8 +2367,8 @@ typedef struct txn_fold_ran_frame_t
bool committed;
bool has_spilled;
TxnRunsKind spilled;
- TxnFoldFits fits;
- void *fits_captures;
+ TxnFoldEach each;
+ void *each_captures;
TxnFoldSpaced spaced;
bool spaced_live;
StdVec walked;
@@ -2263,19 +2400,55 @@ typedef struct txn_fold_ran_closure_1_frame_t
bool versions_live;
StdVec spills;
bool spills_live;
+ /* own is how many of the key's versions the memtable gave. */
+ size_t own;
HostError *error;
bool ok;
} txn_fold_ran_closure_1_frame_t;
+/* txn_fold_ran_closure_1_dropped drops the key's versions a read gave. */
+static void
+txn_fold_ran_closure_1_dropped(void *captures)
+{
+ txn_fold_ran_closure_1_frame_t *frame = captures;
+
+ frame->versions_live = false;
+ txn_memtable_version_versions_drop(&frame->versions, 0);
+}
+
+/* txn_fold_ran_closure_1_read reads the key's memtable versions, then its spills' below them. */
+static bool
+txn_fold_ran_closure_1_read(void *captures, uint64 *listed, HostError *error)
+{
+ txn_fold_ran_closure_1_frame_t *frame = captures;
+ txn_fold_ran_frame_t *ran = frame->ran;
+ HostSharedRecord record = host_shared_Held_record(ran->held);
+
+ if (!txn_memtable_map_versions(ran->tree, frame->io, &frame->hold, record, std_u32_from_be_bytes(frame->key),
+ &frame->versions, error))
+ return false;
+ frame->versions_live = true;
+ frame->own = frame->versions.len;
+ if (ran->has_spilled)
+ {
+ if (!txn_spill_versions(&TXN_SPILL_STATE_HOME_OPS, frame->io, &frame->hold, record, ran->spilled, frame->key,
+ frame->key_len, listed, &frame->spills, error))
+ return false;
+ frame->spills_live = true;
+ std_vec_extend_from_slice_Checked(&frame->versions, frame->spills.ptr, frame->spills.len, TXN_FOLD_VERSION);
+ frame->spills_live = false;
+ std_vec_free_Checked(&frame->spills, TXN_FOLD_VERSION);
+ }
+ return true;
+}
+
/* txn_fold_ran_closure_1_body reads the key's memtable versions, then its spills' below them, and adds its run. */
static void
txn_fold_ran_closure_1_body(void *frame_)
{
txn_fold_ran_closure_1_frame_t *frame = frame_;
txn_fold_ran_frame_t *ran = frame->ran;
- HostSharedRecord record = host_shared_Held_record(ran->held);
uint32 id;
- size_t own;
if (frame->key_len != 4)
{
@@ -2286,19 +2459,11 @@ txn_fold_ran_closure_1_body(void *frame_)
if (!host_delta_hold(frame->io, &frame->hold, frame->error))
return;
frame->hold_live = true;
- if (!txn_memtable_map_versions(ran->tree, frame->io, &frame->hold, record, id, &frame->versions, frame->error))
+ if (!txn_spill_taken(&TXN_SPILL_STATE_HOME_OPS, frame->io, host_shared_Held_record(ran->held),
+ txn_fold_ran_closure_1_read, txn_fold_ran_closure_1_dropped, frame, frame->error))
return;
- frame->versions_live = true;
- own = frame->versions.len;
if (ran->has_spilled)
{
- if (!txn_spill_versions(&TXN_SPILL_STATE_HOME_OPS, frame->io, &frame->hold, record, ran->spilled, frame->key,
- frame->key_len, &frame->spills, frame->error))
- return;
- frame->spills_live = true;
- std_vec_extend_from_slice_Checked(&frame->versions, frame->spills.ptr, frame->spills.len, TXN_FOLD_VERSION);
- frame->spills_live = false;
- std_vec_free_Checked(&frame->spills, TXN_FOLD_VERSION);
if (!std_vec_try_reserve(&ran->walked, 1, TXN_FOLD_U32))
{
*frame->error = host_Error_out_of_memory();
@@ -2307,8 +2472,8 @@ txn_fold_ran_closure_1_body(void *frame_)
std_vec_push_Checked(&ran->walked, &id, TXN_FOLD_U32);
}
frame->ok = txn_fold_ran_key(frame->io, &ran->spaced, frame->key, frame->key_len, id, frame->versions.ptr,
- frame->versions.len, own, ran->point, ran->committed, ran->fits, ran->fits_captures,
- frame->error);
+ frame->versions.len, frame->own, ran->point, ran->committed, ran->spilled, ran->each,
+ ran->each_captures, frame->error);
}
/* txn_fold_ran_closure_1_pad frees what the walk holds live: the spills' versions, the versions, and its hold. */
@@ -2362,6 +2527,7 @@ txn_fold_ran_body(void *frame_)
std_vec_new(&frame->spaced.edits, TXN_FOLD_EDIT);
std_vec_new(&frame->spaced.removed, TXN_FOLD_REMOVED);
+ std_vec_new(&frame->spaced.kept, sizeof(TxnSpillKept), _Alignof(TxnSpillKept));
frame->spaced.emptied = true;
frame->spaced.unresolved = false;
frame->spaced_live = true;
@@ -2379,7 +2545,7 @@ txn_fold_ran_body(void *frame_)
frame->hold_live = true;
if (!txn_spill_next(&TXN_SPILL_STATE_HOME_OPS, frame->io, &frame->hold, record, frame->spilled,
frame->has_after, frame->after.ptr, frame->after.len,
- TXN_MEMTABLE_TABLE_DIRECTION_FORWARD, &some, &frame->next, frame->error))
+ TXN_MEMTABLE_TABLE_DIRECTION_FORWARD, NULL, &some, &frame->next, frame->error))
return;
if (!some)
break;
@@ -2393,7 +2559,7 @@ txn_fold_ran_body(void *frame_)
if (bsearch(&id, frame->walked.ptr, frame->walked.len, sizeof(uint32), txn_fold_id_order) == NULL &&
!txn_fold_ran_key(frame->io, &frame->spaced, frame->next.f0.ptr, frame->next.f0.len, id,
frame->next.f1.ptr, frame->next.f1.len, 0, frame->point, frame->committed,
- frame->fits, frame->fits_captures, frame->error))
+ frame->spilled, frame->each, frame->each_captures, frame->error))
return;
if (frame->has_after)
std_vec_free_Checked(&frame->after, TXN_FOLD_BYTES);
@@ -2404,6 +2570,8 @@ txn_fold_ran_body(void *frame_)
frame->hold_live = false;
host_delta_Hold_drop(&frame->hold);
}
+ if (frame->has_spilled && frame->spaced.edits.len > 1)
+ qsort(frame->spaced.edits.ptr, frame->spaced.edits.len, sizeof(TreesMutateEdit), txn_fold_edit_order);
*frame->result = frame->spaced;
frame->spaced_live = false;
frame->ok = true;
@@ -2434,20 +2602,22 @@ txn_fold_ran_pad(void *frame_)
/*
* txn_fold_ran reads what a drop at a fold point takes out of a storage's
* map: for each key, the oldest run of versions whose writers the point
- * applies or that aborted, the longest such run that fits, whose newest, or
- * its newest committed one when committed, none leaving the base's entry, is
- * the key's entry in the new base, a removal its deletion, a values mark
- * passed over. A map whose spills are of a kind reads each key's spilled
- * versions below its memtable's, then the keys its spills alone hold. Only
+ * applies or that aborted, which it hands each, whose newest, or its newest
+ * committed one when committed, none leaving the base's entry, is the key's
+ * entry in the new base, a removal its deletion, a values mark passed over.
+ * A map whose spills are of a kind reads each key's spilled versions below
+ * its memtable's, then the keys its spills alone hold, and puts the edits in
+ * key order, the map's tree's; the spilled versions above a key's run that
+ * the point applies or drops are kept for a residual (bugs row 228). Only
* the memtable's versions are listed for removal.
*/
static bool
txn_fold_ran(HostIoIo *io, const HostSharedHeld *held, const TxnMemtableTableTreeOps *tree, HostLsn point,
- bool committed, bool has_spilled, TxnRunsKind spilled, TxnFoldFits fits, void *fits_captures,
+ bool committed, bool has_spilled, TxnRunsKind spilled, TxnFoldEach each, void *each_captures,
TxnFoldSpaced *result, HostError *error)
{
txn_fold_ran_frame_t frame = {.io = io, .held = held, .tree = tree, .point = point, .committed = committed,
- .has_spilled = has_spilled, .spilled = spilled, .fits = fits, .fits_captures = fits_captures,
+ .has_spilled = has_spilled, .spilled = spilled, .each = each, .each_captures = each_captures,
.result = result, .error = error};
std_landing(&frame, txn_fold_ran_body, txn_fold_ran_pad);
@@ -2455,99 +2625,92 @@ txn_fold_ran(HostIoIo *io, const HostSharedHeld *held, const TxnMemtableTableTre
return frame.ok;
}
+/* TxnFoldTakenOut is spaced's gathering's captures: the store, the base, the page, the pairing, the fold's pending rows, and the takes no layer frees. */
+typedef struct TxnFoldTakenOut
+{
+ TreesStoreNodeStore store;
+ const TxnBaseBase *base;
+ const TxnInsertPage *page;
+ TxnFoldPaired paired;
+ void *paired_captures;
+ const StdVec *pending;
+ /* unfreed is a Vec. */
+ StdVec *unfreed;
+} TxnFoldTakenOut;
+
/*
- * txn_fold_freed reports whether a run of a block's free space versions,
- * newest first, may leave the delta: each take in it, and each take's values
- * mark, names a tuple id its newest entry holds free, which VACUUM freed, or
- * the installed base's tuple id tree pairs.
+ * txn_fold_taken_out adds to the unfreed each take of a run of a block's
+ * free space versions, newest first, that leaves the delta naming a tuple id
+ * no layer frees, as VACUUM's find reads the run's takes (vacuum's leaving):
+ * neither the run's newest entry holds it free, which VACUUM freed, nor the
+ * installed base's tuple id tree pairs it, nor the fold makes it pending,
+ * the fold's ended rows'; so its row aborted, or a prune gave its bytes back
+ * and left it dead, and only VACUUM frees it, from the base's pending tuple
+ * ids (bugs row 228's (E)). Each goes with its stored ender, marked
+ * TXN_BASE_OWN when it is its take's writer's, the bytes its take lowered, 0
+ * for a dead one, and its row's value ids when the writer whose commit
+ * decides them committed.
*/
static bool
-txn_fold_freed(HostIoIo *io, TxnMemtableVersionVersion *run, size_t run_len, HostLsn point, TxnFoldPaired paired,
- void *paired_captures, bool *result, HostError *error)
+txn_fold_taken_out(void *captures, HostIoIo *io, TxnMemtableVersionVersion *run, size_t run_len, HostError *error)
{
- bool has_newest = false;
- StdBytes newest = {0};
-
- for (size_t i = 0; i < run_len; i++)
- {
- TxnMemtableVersionRecord record = txn_memtable_version_Version_record(&run[i]);
- TxnMemtableVersionOpRef op = txn_memtable_version_Record_op(&record);
+ TxnFoldTakenOut *c = captures;
+ StdVec leaving;
+ bool ok = true;
- if (op.tag == TXN_MEMTABLE_VERSION_OP_REF_NAMED)
- continue;
- if (op.tag == TXN_MEMTABLE_VERSION_OP_REF_PUT)
- {
- has_newest = true;
- newest = op.u.put.value;
- }
- break;
- }
- *result = true;
- for (size_t i = 0; i < run_len; i++)
+ if (!txn_vacuum_leaving(io, c->store, c->base, c->page, run, run_len, &leaving, error))
+ return false;
+ for (size_t k = 0; k < leaving.len && ok; k++)
{
- TxnMemtableVersionRecord record = txn_memtable_version_Version_record(&run[i]);
- HostRow tuple = txn_memtable_map_taken(txn_memtable_version_Record_op(&record));
- TxnFoldApplied applied;
- TxnFoldApplied stays = {.tag = TXN_FOLD_APPLIED_STAYS};
- bool frees = false;
+ TxnVacuumLeaving *take = &STD_VEC_AT(&leaving, TxnVacuumLeaving, k);
+ TxnFoldPendingRow row;
bool pairs;
- if (tuple.f0 == 0)
+ if (txn_fold_pending_holds(c->pending, take->tid))
continue;
- if (!txn_fold_applied(io, &run[i], point, &applied, error))
- return false;
- if (txn_fold_Applied_PartialEq_eq(&applied, &stays))
+ ok = c->paired(c->paired_captures, io, take->tid, &pairs, error);
+ if (!ok || pairs)
continue;
- if (has_newest &&
- !txn_free_space_frees(newest.ptr, newest.len, (uint16) (tuple.f0 & 0xffff), &frees, error))
- return false;
- if (frees)
+ if (take->bytes > PG_UINT32_MAX)
+ {
+ *error = txn_fold_fork("XX001", "a take lowered its entry past a page");
+ ok = false;
continue;
- if (!paired(paired_captures, io, tuple.f0, &pairs, error))
- return false;
- if (!pairs)
+ }
+ if (!std_vec_try_reserve(c->unfreed, 1, TXN_FOLD_PENDING_ROW))
{
- *result = false;
- return true;
+ *error = host_Error_out_of_memory();
+ ok = false;
+ continue;
}
+ row = (TxnFoldPendingRow)
+ {
+ .tuple = take->tid, .ender = txn_fold_owned(take->ender, take->writer.xid), .bytes = (uint32) take->bytes,
+ .ids = take->ids
+ };
+ std_vec_new(&take->ids, TXN_FOLD_U32);
+ std_vec_push_Checked(c->unfreed, &row, TXN_FOLD_PENDING_ROW);
}
- return true;
-}
-
-/* txn_fold_spaced_closure_0_captures_t is spaced's fits' captures: the point and the pairing. */
-typedef struct txn_fold_spaced_closure_0_captures_t
-{
- HostLsn point;
- TxnFoldPaired paired;
- void *paired_captures;
-} txn_fold_spaced_closure_0_captures_t;
-
-/* txn_fold_spaced_closure_0 is spaced's fits: whether a run of a block's versions is freed. */
-static bool
-txn_fold_spaced_closure_0(void *captures, HostIoIo *io, TxnMemtableVersionVersion *run, size_t run_len,
- bool *result, HostError *error)
-{
- txn_fold_spaced_closure_0_captures_t *spaced = captures;
-
- return txn_fold_freed(io, run, run_len, spaced->point, spaced->paired, spaced->paired_captures, result, error);
+ txn_vacuum_leaving_drop(&leaving);
+ return ok;
}
/*
* txn_fold_spaced reads what the drop at a fold point takes out of a
* storage's free space map: for each block, the oldest run of versions whose
- * takers the point applies, each take among them naming a tuple id the run's
- * newest entry holds free or the installed base's tuple id tree pairs; the
- * newest of the run is the block's entry in the new base, a removal its
- * deletion.
+ * takers the point applies, whose takes each reads before they leave the
+ * delta, since a take whose tuple id no layer frees goes to the base's
+ * pending tuple ids (bugs row 228's (E)); the newest of the run is the
+ * block's entry in the new base, a removal its deletion, so the base stays
+ * the state before the delta's oldest take of each block. Its spilled
+ * versions are read below its memtable's.
*/
static bool
-txn_fold_spaced(HostIoIo *io, const HostSharedHeld *held, HostLsn point, TxnFoldPaired paired,
- void *paired_captures, TxnFoldSpaced *result, HostError *error)
+txn_fold_spaced(HostIoIo *io, const HostSharedHeld *held, HostLsn point, TxnFoldEach each, void *each_captures,
+ TxnFoldSpaced *result, HostError *error)
{
- txn_fold_spaced_closure_0_captures_t fits = {point, paired, paired_captures};
-
- return txn_fold_ran(io, held, &TXN_MEMTABLE_MAP_FREE_TREE_OPS, point, false, false, 0,
- txn_fold_spaced_closure_0, &fits, result, error);
+ return txn_fold_ran(io, held, &TXN_MEMTABLE_MAP_FREE_TREE_OPS, point, false, true, TXN_RUNS_KIND_SPILLED_FREE,
+ each, each_captures, result, error);
}
/* txn_fold_valued_frame_t is valued's state across its pad: what it takes out of the value map, while live. */
@@ -2738,7 +2901,11 @@ typedef struct txn_fold_recount_frame_t
bool ok;
} txn_fold_recount_frame_t;
-/* txn_fold_recount_body folds every committed version over the newest base and moves its sizes by what that writes. */
+/*
+ * txn_fold_recount_body folds every committed version over the newest base
+ * and moves its sizes by what that writes; it pauses at
+ * doltgres-recount-based after it reads the base.
+ */
static void
txn_fold_recount_body(void *frame_)
{
@@ -2756,6 +2923,8 @@ txn_fold_recount_body(void *frame_)
frame->ok = true;
return;
}
+ if (!host_pause(HOST_SHARED_IO_PAUSES(frame->io), "doltgres-recount-based", frame->error))
+ return;
if (!txn_fold_folded_rows(frame->io, frame->store, frame->held, base->has_root, base->root, base->has_tuple_ids,
base->tuple_ids, frame->order, frame->keyless, base->has_index, PG_UINT64_MAX,
base->folded, &frame->folded, frame->error))
@@ -2854,7 +3023,7 @@ txn_fold_held_pairing(void *captures, HostIoIo *io, uint64 tuple, bool *result,
/*
* txn_fold_unspaced_frame_t is unspaced's state across its pad: the keys it
- * takes, a key's handles and removal, each while live.
+ * takes and a key's removal, while live.
*/
typedef struct txn_fold_unspaced_frame_t
{
@@ -2863,82 +3032,49 @@ typedef struct txn_fold_unspaced_frame_t
const TxnMemtableTableTreeOps *tree;
StdVec removed;
size_t at;
- StdVec handles;
- bool handles_live;
StdVec taken;
bool taken_live;
HostError *error;
bool ok;
} txn_fold_unspaced_frame_t;
-/* txn_fold_unspaced_body takes each key's versions out and gives each taken one's count back at its writer's entry. */
+/* txn_fold_unspaced_body takes each key's versions out, the remover giving back each counted one's count. */
static void
txn_fold_unspaced_body(void *frame_)
{
txn_fold_unspaced_frame_t *frame = frame_;
- TxnMemtableVersionWriter none = TXN_MEMTABLE_VERSION_NO_TRANSACTION;
for (; frame->at < frame->removed.len; frame->at++)
{
- txn_TupleU32VecTupleHandleWriter_t *key = &STD_VEC_AT(&frame->removed, txn_TupleU32VecTupleHandleWriter_t,
- frame->at);
+ txn_TupleU32VecHandle_t *key = &STD_VEC_AT(&frame->removed, txn_TupleU32VecHandle_t, frame->at);
uint8 be[4];
- size_t zipped;
- std_vec_with_capacity_Checked(&frame->handles, key->f1.len, TXN_FOLD_HANDLE);
- frame->handles_live = true;
- for (size_t i = 0; i < key->f1.len; i++)
- std_vec_push_Checked(&frame->handles, &STD_VEC_AT(&key->f1, txn_TupleHandleWriter_t, i).f0,
- TXN_FOLD_HANDLE);
std_u32_to_be_bytes(key->f0, be);
if (!txn_memtable_table_removed(frame->tree, frame->io, host_shared_Held_record(frame->held), be, sizeof(be),
- frame->handles.ptr, frame->handles.len, &frame->taken, frame->error))
+ key->f1.ptr, key->f1.len, &frame->taken, frame->error))
return;
- frame->taken_live = true;
- zipped = Min(key->f1.len, frame->taken.len);
- for (size_t i = 0; i < zipped; i++)
- {
- const TxnMemtableVersionWriter *writer = &STD_VEC_AT(&key->f1, txn_TupleHandleWriter_t, i).f1;
- HostXid top = {writer->has_top ? writer->top : writer->xid};
- bool has_added;
- uint64 added;
-
- if (!STD_VEC_AT(&frame->taken, bool, i) || txn_memtable_version_Writer_PartialEq_eq(writer, &none))
- continue;
- if (!txn_status_add(frame->io, top, PG_UINT64_MAX, &has_added, &added, frame->error))
- return;
- }
- frame->taken_live = false;
std_vec_free_Checked(&frame->taken, sizeof(bool), _Alignof(bool));
- frame->handles_live = false;
- std_vec_free_Checked(&frame->handles, TXN_FOLD_HANDLE);
txn_fold_Removed_drop(key);
}
frame->ok = true;
}
-/* txn_fold_unspaced_pad frees what unspaced holds live: a key's removal and handles, and the keys left. */
+/* txn_fold_unspaced_pad frees what unspaced holds live: the keys left. */
static void
txn_fold_unspaced_pad(void *frame_)
{
txn_fold_unspaced_frame_t *frame = frame_;
- if (frame->taken_live)
- std_vec_free_Checked(&frame->taken, sizeof(bool), _Alignof(bool));
- if (frame->handles_live)
- std_vec_free_Checked(&frame->handles, TXN_FOLD_HANDLE);
for (; frame->at < frame->removed.len; frame->at++)
- txn_fold_Removed_drop(&STD_VEC_AT(&frame->removed, txn_TupleU32VecTupleHandleWriter_t, frame->at));
+ txn_fold_Removed_drop(&STD_VEC_AT(&frame->removed, txn_TupleU32VecHandle_t, frame->at));
std_vec_free_Checked(&frame->removed, TXN_FOLD_REMOVED);
- frame->taken_live = frame->handles_live = false;
}
/*
* txn_fold_unspaced takes out of a storage's map, through the remover, the
* versions an installed base now holds, as spaced or valued read them,
- * taking the list, and takes back the count of each version this removal
- * took out at its writer's entry; a version written by no transaction counts
- * nothing.
+ * taking the list; the remover gives back the count of each counted version
+ * it took out.
*/
static bool
txn_fold_unspaced(HostIoIo *io, const HostSharedHeld *held, const TxnMemtableTableTreeOps *tree, StdVec *removed,
@@ -3066,6 +3202,7 @@ txn_fold_dropped_closure_0_body(void *frame_)
if (seen->changer == NULL || !txn_fold_rows_holds(&frame->unpaired, seen->row.put.tuple))
continue;
if (!txn_memtable_map_ended(frame->io, walk->record, seen->row.put.tuple,
+ txn_fold_writer_xid(seen->row.version),
(uint32) txn_fold_writer_xid(seen->changer), frame->error))
return;
}
@@ -3087,11 +3224,6 @@ txn_fold_dropped_closure_0_body(void *frame_)
frame->error))
return;
frame->taken_live = true;
- for (size_t i = 0; i < Min(frame->done.len, frame->taken.len); i++)
- if (STD_VEC_AT(&frame->taken, bool, i) &&
- !txn_memtable_table_uncounted(frame->io, STD_VEC_AT(&frame->done, TxnMemtableVersionVersion *, i),
- frame->error))
- return;
frame->ok = true;
}
@@ -3225,41 +3357,6 @@ txn_fold_pended_closure_0(void *captures, HostIoIo *io, TreesPinPin *pin, Chunks
return frame.ok;
}
-/* txn_fold_pending_holds reports whether pending rows, in tuple id order, hold a tuple id. */
-static bool
-txn_fold_pending_holds(const StdVec *pending, uint64 tid)
-{
- size_t low = 0;
- size_t high = pending->len;
-
- while (low < high)
- {
- size_t mid = low + (high - low) / 2;
- uint64 at = STD_VEC_AT(pending, TxnFoldPendingRow, mid).tuple;
-
- if (at == tid)
- return true;
- if (at < tid)
- low = mid + 1;
- else
- high = mid;
- }
- return false;
-}
-
-/* txn_fold_edit_order orders edits by their keys' bytes, for qsort. */
-static int
-txn_fold_edit_order(const void *a, const void *b)
-{
- StdBytes x = trees_mutate_Edit_key(a);
- StdBytes y = trees_mutate_Edit_key(b);
- int c = memcmp(x.ptr, y.ptr, Min(x.len, y.len));
-
- if (c != 0)
- return c;
- return (x.len > y.len) - (x.len < y.len);
-}
-
/*
* txn_fold_pended_frame_t is pended's state across its pad: the tuple ids the
* tree holds and the edits, each while live.
@@ -3270,8 +3367,9 @@ typedef struct txn_fold_pended_frame_t
TreesStoreNodeStore store;
const HostSharedHeld *held;
const TxnBaseBase *base;
- const StdVec *pending;
+ StdVec *pending;
const StdVec *paired;
+ StdVec *unfreed;
StdVec tids;
bool tids_live;
StdVec edits;
@@ -3294,7 +3392,7 @@ static void
txn_fold_pended_body(void *frame_)
{
txn_fold_pended_frame_t *frame = frame_;
- size_t left = frame->pending->len;
+ size_t left;
ChunksErrorError failed;
std_vec_new(&frame->tids, TXN_FOLD_U64);
@@ -3309,6 +3407,17 @@ txn_fold_pended_body(void *frame_)
return;
}
}
+ for (size_t i = 0; i < frame->unfreed->len; i++)
+ {
+ TxnFoldPendingRow *row = &STD_VEC_AT(frame->unfreed, TxnFoldPendingRow, i);
+
+ if (bsearch(&row->tuple, frame->tids.ptr, frame->tids.len, sizeof(uint64), txn_fold_tuple_order) != NULL)
+ txn_fold_PendingRow_drop(row);
+ else
+ txn_fold_PendingRows_absent(frame->pending, row);
+ }
+ frame->unfreed->len = 0;
+ left = frame->pending->len;
std_vec_new(&frame->edits, TXN_FOLD_EDIT);
frame->edits_live = true;
for (size_t i = 0; i < frame->tids.len; i++)
@@ -3376,17 +3485,19 @@ txn_fold_pended_pad(void *frame_)
/*
* txn_fold_pended returns the edits of a storage's pending tuple ids' tree a
* fold makes, in key order, and whether the tree holds a tuple id after
- * them: a put of each base row's tuple id the fold unpairs, with its ender's
- * xid and its heap bytes, and a removal of each pending tuple id the fold
- * doesn't unpair that VACUUM has freed or that a row took again, the fold
- * pairing it (paired, ascending).
+ * them: a put of each base row's or delta row's tuple id the fold ends, with
+ * its ender's xid, its heap bytes, and its row's value ids, and of each
+ * tuple id no layer frees whose take leaves the delta (unfreed, which it
+ * takes) that the tree doesn't hold, and a removal of each pending tuple id
+ * the fold doesn't unpair that VACUUM has freed or that a row took again,
+ * the fold pairing it (paired, ascending).
*/
static bool
txn_fold_pended(HostIoIo *io, TreesStoreNodeStore store, const HostSharedHeld *held, const TxnBaseBase *base,
- const StdVec *pending, const StdVec *paired, StdVec *result, bool *left, HostError *error)
+ StdVec *pending, const StdVec *paired, StdVec *unfreed, StdVec *result, bool *left, HostError *error)
{
txn_fold_pended_frame_t frame = {.io = io, .store = store, .held = held, .base = base, .pending = pending,
- .paired = paired, .result = result, .left = left, .error = error};
+ .paired = paired, .unfreed = unfreed, .result = result, .left = left, .error = error};
std_landing(&frame, txn_fold_pended_body, txn_fold_pended_pad);
txn_fold_pended_pad(&frame);
@@ -3424,6 +3535,33 @@ txn_fold_Installed_drop(TxnFoldInstalled *self)
self->some = false;
}
+/* txn_fold_advanced inserts a base's record and gives its end, for an install's logging. */
+static bool
+txn_fold_advanced(HostIoIoDoors io, const TxnRecordBaseAdvanced *advanced, HostLsn *end, HostError *error)
+{
+ StdVec body;
+ bool ok;
+
+ txn_record_BaseAdvanced_encode(advanced, &body);
+ ok = host_wal_insert(io, HOST_WAL_KIND_BASE_ADVANCED, body.ptr, body.len, end, error);
+ std_vec_free_Checked(&body, TXN_FOLD_BYTES);
+ return ok;
+}
+
+/*
+ * txn_fold_replayed is a replayed install's logging: it gives the end of the
+ * record being replayed, which captures names.
+ */
+static bool
+txn_fold_replayed(void *captures, HostIoIoDoors io, HostLsn reached, HostLsn *end, HostError *error)
+{
+ (void) io;
+ (void) reached;
+ (void) error;
+ *end = *(const HostLsn *) captures;
+ return true;
+}
+
/* txn_fold_published_drop frees a writer's published roots. */
static void
txn_fold_published_drop(StdVec *published)
@@ -3437,8 +3575,9 @@ static bool txn_fold_reformed(HostIoIo *io, TreesStoreNodeStore store, const Txn
const TxnVisibleLive *chain, size_t chain_len, StdVec *edits, StdVec *formed,
StdVec *ctids, TxnFoldReform *reform, StdVec *out_edits, StdVec *out_ctids,
TxnBaseSizes *sizes, uint32 *epoch, HostError *error);
-static bool txn_fold_matched(HostIoIoDoors io, const HostSharedHeld *held, const TxnBaseBase *folded,
- TxnFoldReached *reached, const TreesKeyOrder *order, bool *result, HostError *error);
+static bool txn_fold_matched(HostIoIoDoors io, const HostSharedHeld *held, uint64 generation,
+ const TxnBaseBase *folded, TxnFoldReached *reached, const TreesKeyOrder *order,
+ bool *result, HostError *error);
/*
* txn_fold_folded_frame_t is folded's state across its pad: the newest base,
@@ -3455,6 +3594,7 @@ typedef struct txn_fold_folded_frame_t
const TreesKeyOrder *order;
bool keyless;
HostLsn point;
+ const TxnInsertPage *page;
TxnFoldReform *reform;
TxnFoldReached *reached;
uint64 generation;
@@ -3503,6 +3643,19 @@ typedef struct txn_fold_folded_frame_t
bool paired_now_live;
StdVec pends;
bool pends_live;
+
+ /*
+ * unfreed is a Vec of the takes no layer frees that leave the
+ * delta.
+ */
+ StdVec unfreed;
+ bool unfreed_live;
+ /* kept is a Vec of the spilled map versions the drop keeps. */
+ StdVec kept;
+ bool kept_live;
+ /* residuals is a Vec of the residuals the fold listed. */
+ StdVec residuals;
+ bool residuals_live;
bool keeps;
bool has_pended;
size_t pended;
@@ -3571,11 +3724,47 @@ txn_fold_held(TreesStoreBrandedWriter *writer, HostIoTakesShort io, ChunksPacksR
return held;
}
+/* txn_fold_folded_logging_t is a fold's install's logging's captures: the fold, its storage, and its install. */
+typedef struct txn_fold_folded_logging_t
+{
+ const txn_fold_folded_frame_t *folded;
+ HostStorage storage;
+ const TxnFoldInstalled *installed;
+} txn_fold_folded_logging_t;
+
+/*
+ * txn_fold_folded_logged is a fold's install's logging: outside recovery it
+ * inserts the installed base's record and gives its end; in recovery it
+ * gives the later of the point and the end of the record the fold reached.
+ */
+static bool
+txn_fold_folded_logged(void *captures, HostIoIoDoors io, HostLsn reached, HostLsn *end, HostError *error)
+{
+ const txn_fold_folded_logging_t *logging = captures;
+ const txn_fold_folded_frame_t *folded = logging->folded;
+ TxnRecordBaseAdvanced advanced = {.storage = logging->storage, .folded = folded->point,
+ .max_folded = folded->max_folded, .base = logging->installed->next,
+ .packs = logging->installed->packs};
+ bool injected;
+
+ if (host_wal_stand(io).recovering)
+ {
+ *end = Max(reached, folded->point);
+ return true;
+ }
+ if (!host_injected(io, "doltgres-fold-record-root", &injected, error))
+ return false;
+ if (injected && advanced.base.has_root)
+ advanced.base.root.address = chunks_address_Address_of((const uint8 *) "doltgres-fold-record-root", 25);
+ return txn_fold_advanced(io, &advanced, end, error);
+}
+
/*
* txn_fold_folded_closure_11_body holds the fold's roots in the fold's four
* slots, publishes them, and installs the base that names them when the
* generation is still the one read before the fold and a standby's roots
- * match its primary's.
+ * match its primary's; a permanent storage's packs are listed first and its
+ * record logged in the install's step.
*/
static void
txn_fold_folded_closure_11_body(void *frame_)
@@ -3590,6 +3779,11 @@ txn_fold_folded_closure_11_body(void *frame_)
bool held;
bool installed;
HostError host;
+ TxnStatePersistence persistence;
+ txn_fold_folded_logging_t logging = {.folded = folded, .installed = frame->result};
+ bool permanent = txn_state_described(host_shared_Held_record(folded->held), &logging.storage,
+ &persistence) &&
+ persistence == TXN_STATE_PERSISTENCE_PERMANENT;
chunks_packs_roots_RootSlots_fold(frame->slots, &slot);
held = txn_fold_held(&folded->writer, io, slot.s0, true, folded->root, true, frame->error);
@@ -3623,9 +3817,15 @@ txn_fold_folded_closure_11_body(void *frame_)
next->pending = trees_published_Published_base(&STD_VEC_AT(&frame->published, TreesPublishedPublished,
at++));
}
- next->counter = folded->counter;
- next->fresh_end = folded->fresh_end;
+
+ /*
+ * A standby's replay may already hold versions past the point, so its
+ * counter and fresh end are its primary's at the point, from the record.
+ */
+ next->counter = folded->reached != NULL ? folded->reached->base->counter : folded->counter;
+ next->fresh_end = folded->reached != NULL ? folded->reached->base->fresh_end : folded->fresh_end;
next->folded = folded->point;
+ next->read = folded->point;
next->max_folded = folded->max_folded;
next->max_ended = Max(folded->base.max_ended, folded->rows.max_ended);
next->sizes = folded->sizes;
@@ -3635,7 +3835,8 @@ txn_fold_folded_closure_11_body(void *frame_)
{
bool matched;
- if (!txn_fold_matched(doors, folded->held, next, folded->reached, folded->order, &matched, &host))
+ if (!txn_fold_matched(doors, folded->held, folded->generation, next, folded->reached, folded->order,
+ &matched, &host))
{
*frame->error = chunks_error_Error_FromError_from(&host);
return;
@@ -3647,7 +3848,11 @@ txn_fold_folded_closure_11_body(void *frame_)
return;
}
}
- if (!txn_state_installed(io, folded->held, folded->generation, next, &installed, &host))
+ if (permanent &&
+ !chunks_packs_backend_Packs_flush(&folded->repo->packs, io, folded->reading, frame->error))
+ return;
+ if (!txn_state_installed(io, folded->held, folded->generation, next, permanent ? txn_fold_folded_logged : NULL,
+ &logging, &installed, &host))
{
*frame->error = chunks_error_Error_FromError_from(&host);
return;
@@ -3871,9 +4076,16 @@ txn_fold_folded_write(txn_fold_folded_frame_t *frame)
frame->held_pairing.store = store;
frame->held_pairing.has_tuple_ids = frame->base.has_tuple_ids;
frame->held_pairing.tuple_ids = frame->base.tuple_ids;
- if (!txn_fold_spaced(frame->io, frame->held, frame->point, txn_fold_folded_closure_8, frame, &frame->spaces,
- frame->error))
- return false;
+ std_vec_new(&frame->unfreed, TXN_FOLD_PENDING_ROW);
+ frame->unfreed_live = true;
+ {
+ TxnFoldTakenOut gathered = {store, &frame->base, frame->page, txn_fold_folded_closure_8, frame,
+ &frame->rows.pending, &frame->unfreed};
+
+ if (!txn_fold_spaced(frame->io, frame->held, frame->point, txn_fold_taken_out, &gathered, &frame->spaces,
+ frame->error))
+ return false;
+ }
frame->spaces_live = true;
frame->has_spaced = frame->spaces.edits.len > 0;
if (frame->has_spaced)
@@ -3905,7 +4117,7 @@ txn_fold_folded_write(txn_fold_folded_frame_t *frame)
std_vec_push_Checked(&frame->paired_now, &STD_VEC_AT(&frame->pairs, txn_TupleU64Bool_t, i).f0,
TXN_FOLD_U64);
if (!txn_fold_pended(frame->io, store, frame->held, &frame->base, &frame->rows.pending, &frame->paired_now,
- &frame->pends, &left, frame->error))
+ &frame->unfreed, &frame->pends, &left, frame->error))
return false;
frame->pends_live = true;
frame->keeps = frame->pends.len == 0;
@@ -3924,63 +4136,25 @@ txn_fold_folded_write(txn_fold_folded_frame_t *frame)
return true;
}
-/*
- * txn_fold_folded_logged flushes a permanent storage's packs and the WAL
- * through the point, and outside recovery logs the installed base's record,
- * taking the install.
- */
+/* txn_fold_folded_flushed flushes the WAL through the point for a permanent storage. */
static bool
-txn_fold_folded_logged(txn_fold_folded_frame_t *frame)
+txn_fold_folded_flushed(txn_fold_folded_frame_t *frame)
{
- HostIoIoDoors doors = HOST_IO_IO_IO_DOORS(frame->io);
HostStorage storage;
TxnStatePersistence persistence;
- ChunksErrorError failed;
- TxnRecordBaseAdvanced advanced;
- bool injected;
- StdVec body;
- HostLsn lsn;
- bool ok;
if (!txn_state_described(host_shared_Held_record(frame->held), &storage, &persistence) ||
persistence != TXN_STATE_PERSISTENCE_PERMANENT)
return true;
- if (!chunks_packs_backend_Packs_flush(&frame->repo->packs, frame->io, frame->reading, &failed))
- {
- *frame->error = txn_base_storage(&failed);
- return false;
- }
- if (!host_wal_flush(doors, frame->point, frame->error))
- return false;
- if (host_wal_stand(doors).recovering)
- return true;
- advanced.storage = storage;
- advanced.folded = frame->point;
- advanced.max_folded = frame->max_folded;
- advanced.base = frame->installed.next;
- advanced.packs = frame->installed.packs;
- advanced.built = false;
- advanced.horizon = 0;
- advanced.has_chain = false;
- frame->installed.some = false;
- ok = host_injected(doors, "doltgres-fold-record-root", &injected, frame->error);
- if (ok && injected && advanced.base.has_root)
- advanced.base.root.address = chunks_address_Address_of((const uint8 *) "doltgres-fold-record-root", 25);
- if (ok)
- {
- txn_record_BaseAdvanced_encode(&advanced, &body);
- ok = host_wal_insert(doors, HOST_WAL_KIND_BASE_ADVANCED, body.ptr, body.len, &lsn, frame->error);
- std_vec_free_Checked(&body, TXN_FOLD_BYTES);
- }
- txn_record_BaseAdvanced_drop(&advanced);
- return ok;
+ return host_wal_flush(HOST_IO_IO_IO_DOORS(frame->io), frame->point, frame->error);
}
/*
* txn_fold_folded_body folds the storage's rows and maps at the point,
* re-forming its rows through a live chain, writes the new base's trees, and
- * installs the base under the registration that holds its roots, then gives
- * back the chain's counts, logs the base, and drops what the install folded.
+ * installs and logs the base under the registration that holds its roots,
+ * then gives back the chain's counts, flushes the WAL through the point, and
+ * drops what the install folded.
*/
static void
txn_fold_folded_body(void *frame_)
@@ -4004,7 +4178,7 @@ txn_fold_folded_body(void *frame_)
store = TREES_STORE_READER_NODE_STORE(&frame->reader);
if (!txn_fold_folded_rows(frame->io, store, frame->held, frame->base.has_root, frame->base.root,
frame->base.has_tuple_ids, frame->base.tuple_ids, frame->order, frame->keyless,
- frame->base.has_index, frame->point, frame->base.folded, &frame->rows, frame->error))
+ frame->base.has_index, frame->point, frame->base.read, &frame->rows, frame->error))
return;
frame->rows_live = true;
if (frame->rows.overlapping)
@@ -4043,7 +4217,7 @@ txn_fold_folded_body(void *frame_)
{
txn_fold_held_pairing_t pairing = {store, frame->base.has_tuple_ids, frame->base.tuple_ids};
- frame->ok = txn_fold_dropped(frame->io, frame->held, frame->base.folded, txn_fold_held_pairing, &pairing,
+ frame->ok = txn_fold_dropped(frame->io, frame->held, frame->base.read, txn_fold_held_pairing, &pairing,
frame->error) &&
txn_memtable_tuples_emptied(frame->io, record, frame->error);
return;
@@ -4069,6 +4243,26 @@ txn_fold_folded_body(void *frame_)
std_vec_new(&frame->values.edits, TXN_FOLD_EDIT);
if (!txn_fold_folded_sort(frame) || !txn_fold_folded_write(frame))
return;
+ std_vec_new(&frame->kept, sizeof(TxnSpillKept), _Alignof(TxnSpillKept));
+ frame->kept_live = true;
+ if (!std_vec_try_reserve_exact(&frame->kept, frame->spaces.kept.len + frame->values.kept.len,
+ sizeof(TxnSpillKept), _Alignof(TxnSpillKept)))
+ {
+ *frame->error = host_Error_out_of_memory();
+ return;
+ }
+ std_vec_extend_from_slice_Checked(&frame->kept, frame->spaces.kept.ptr, frame->spaces.kept.len,
+ sizeof(TxnSpillKept), _Alignof(TxnSpillKept));
+ frame->spaces.kept.len = 0;
+ std_vec_extend_from_slice_Checked(&frame->kept, frame->values.kept.ptr, frame->values.kept.len,
+ sizeof(TxnSpillKept), _Alignof(TxnSpillKept));
+ frame->values.kept.len = 0;
+ if (!txn_spill_residual(frame->io, record, frame->point, frame->kept.ptr, frame->kept.len, &frame->residuals,
+ frame->error))
+ return;
+ frame->residuals_live = true;
+ if (!host_pause(HOST_SHARED_IO_PAUSES(frame->io), "doltgres-fold-residual-listed", frame->error))
+ return;
if (!chunks_packs_roots_RootSlots_register(frame->io, txn_fold_folded_closure_11, frame, NULL, &frame->installed,
&failed))
{
@@ -4077,7 +4271,7 @@ txn_fold_folded_body(void *frame_)
}
if (!frame->installed.some)
{
- frame->ok = true;
+ frame->ok = txn_spill_unlisted(frame->io, record, &frame->residuals, frame->error);
return;
}
for (size_t i = 0; i < frame->chained.len; i++)
@@ -4089,18 +4283,20 @@ txn_fold_folded_body(void *frame_)
if (!txn_status_add(frame->io, xid, PG_UINT64_MAX, &has_added, &added, frame->error))
return;
}
- if (!txn_fold_folded_logged(frame) ||
- !host_pause(HOST_SHARED_IO_PAUSES(frame->io), "doltgres-fold-installed", frame->error) ||
+ if (!host_pause(HOST_SHARED_IO_PAUSES(frame->io), "doltgres-fold-installed", frame->error) ||
+ !txn_fold_folded_flushed(frame) ||
!txn_fold_dropped(frame->io, frame->held, frame->point, txn_fold_folded_closure_8, frame, frame->error) ||
!host_pause(HOST_SHARED_IO_PAUSES(frame->io), "doltgres-drop-installed", frame->error))
return;
frame->spaces_live = false;
std_vec_free_Checked(&frame->spaces.edits, TXN_FOLD_EDIT);
+ std_vec_free_Checked(&frame->spaces.kept, sizeof(TxnSpillKept), _Alignof(TxnSpillKept));
if (!txn_fold_unspaced(frame->io, frame->held, &TXN_MEMTABLE_MAP_FREE_TREE_OPS, &frame->spaces.removed,
frame->error))
return;
frame->values_live = false;
std_vec_free_Checked(&frame->values.edits, TXN_FOLD_EDIT);
+ std_vec_free_Checked(&frame->values.kept, sizeof(TxnSpillKept), _Alignof(TxnSpillKept));
if (!txn_fold_unspaced(frame->io, frame->held, &TXN_MEMTABLE_MAP_VALUES_TREE_OPS, &frame->values.removed,
frame->error) ||
!txn_memtable_tuples_emptied(frame->io, record, frame->error))
@@ -4116,6 +4312,13 @@ txn_fold_folded_pad(void *frame_)
txn_fold_folded_frame_t *frame = frame_;
txn_fold_Installed_drop(&frame->installed);
+ if (frame->residuals_live)
+ std_vec_free_Checked(&frame->residuals, sizeof(TxnSpillFile), _Alignof(TxnSpillFile));
+ if (frame->kept_live)
+ std_vec_drop_Checked(&frame->kept, sizeof(TxnSpillKept), _Alignof(TxnSpillKept), txn_fold_Kept_drop);
+ if (frame->unfreed_live)
+ std_vec_drop_Checked(&frame->unfreed, TXN_FOLD_PENDING_ROW, txn_fold_PendingRow_drop);
+ frame->residuals_live = frame->kept_live = frame->unfreed_live = false;
if (frame->pends_live)
std_vec_drop_Checked(&frame->pends, TXN_FOLD_EDIT, trees_mutate_Edit_drop);
if (frame->paired_now_live)
@@ -4171,16 +4374,18 @@ txn_fold_folded_pad(void *frame_)
* with more than one row refuses its install. A base whose chain holds a live
* transform installs only with a re-form and every live transform's commit
* at or below the point. A fold in recovery logs no record, and with reached
- * installs only a base whose roots matched accepts. It answers whether it
+ * installs only a base whose roots matched accepts. The spilled map
+ * versions its drop keeps go to residuals listed before it installs, taken
+ * back out when its install is refused (bugs row 228). It answers whether it
* installed.
*/
bool
txn_fold_folded(HostIoIo *io, const RepoRepo *repo, const HostSharedReader *reading, const HostSharedHeld *held,
- const TreesKeyOrder *order, bool keyless, HostLsn point, TxnFoldReform *reform,
- TxnFoldReached *reached, bool *result, HostError *error)
+ const TreesKeyOrder *order, bool keyless, HostLsn point, const TxnInsertPage *page,
+ TxnFoldReform *reform, TxnFoldReached *reached, bool *result, HostError *error)
{
txn_fold_folded_frame_t frame = {.io = io, .repo = repo, .reading = reading, .held = held, .order = order,
- .keyless = keyless, .point = point, .reform = reform, .reached = reached, .result = result,
+ .keyless = keyless, .point = point, .page = page, .reform = reform, .reached = reached, .result = result,
.error = error};
std_landing(&frame, txn_fold_folded_body, txn_fold_folded_pad);
@@ -4234,7 +4439,7 @@ txn_fold_fold_now(HostIoIo *io, const RepoRepo *repo, const HostSharedReader *re
if (!has_point || left)
return true;
if (!txn_fold_bounded(io, held, point, horizon, &bounded, error) ||
- !txn_fold_folded(io, repo, reading, held, order, keyless, bounded, reform, NULL, &folded, error))
+ !txn_fold_folded(io, repo, reading, held, order, keyless, bounded, page, reform, NULL, &folded, error))
return false;
*result = folded || installed;
return true;
@@ -4280,7 +4485,7 @@ txn_fold_in_order_body(void *frame_)
bool folded;
if (!txn_fold_folded(frame->io, frame->repo, frame->reading, frame->held, frame->order, frame->keyless,
- frame->base.folded, NULL, &reached, &folded, frame->error))
+ frame->base.folded, frame->page, NULL, &reached, &folded, frame->error))
return;
frame->base_live = false;
txn_base_Base_drop(&frame->base);
@@ -4574,11 +4779,15 @@ txn_fold_shown(bool has_root, const ChunksAddressAddress *root, StdString *resul
* installs it. A byte-ordered tree whose root differs publishes the
* mismatch, whose FATAL ends recovery, and the fold installs nothing; a rows
* tree in a comparator's order whose root differs is reported and the
- * standby keeps its own.
+ * standby keeps its own. A fold whose state's base generation moved past the
+ * one it read its base at compares nothing and installs nothing, as its
+ * install would fail, since a newer install's drop may have taken what it
+ * read (bugs row 249); with the generation unmoved, nothing it read has
+ * changed since, so a mismatch it finds is its primary's.
*/
static bool
-txn_fold_matched(HostIoIoDoors io, const HostSharedHeld *held, const TxnBaseBase *folded, TxnFoldReached *reached,
- const TreesKeyOrder *order, bool *result, HostError *error)
+txn_fold_matched(HostIoIoDoors io, const HostSharedHeld *held, uint64 generation, const TxnBaseBase *folded,
+ TxnFoldReached *reached, const TreesKeyOrder *order, bool *result, HostError *error)
{
static const char *const names[] = {"rows", "tuple id", "free space", "pending tuple id"};
const TxnBaseBase *theirs = reached->base;
@@ -4591,8 +4800,8 @@ txn_fold_matched(HostIoIoDoors io, const HostSharedHeld *held, const TxnBaseBase
HostStorage storage;
TxnStatePersistence persistence;
- *result = true;
- if (!txn_state_described(host_shared_Held_record(held), &storage, &persistence))
+ *result = txn_state_generation(host_shared_Held_record(held)) == generation;
+ if (!*result || !txn_state_described(host_shared_Held_record(held), &storage, &persistence))
return true;
for (int at = 0; at < lengthof(names); at++)
{
@@ -4800,8 +5009,9 @@ txn_fold_bounded_map(HostIoIo *io, HostSharedRecord record, const TxnMemtableTab
* txn_fold_bounded lowers a fold point so the fold applies no top-level
* transaction at or past a horizon: below the commit end, at or below the
* point, of each committed writer at or past the horizon among the storage's
- * row versions its newest base didn't apply and its free space and value
- * maps' versions, every version a fold applies.
+ * row versions its newest base didn't apply, its free space and value maps'
+ * versions, and its listed spills' transactions, every version a fold
+ * applies.
*/
static bool
txn_fold_bounded(HostIoIo *io, const HostSharedHeld *held, HostLsn point, uint64 horizon, HostLsn *result,
@@ -4811,19 +5021,32 @@ txn_fold_bounded(HostIoIo *io, const HostSharedHeld *held, HostLsn point, uint64
txn_fold_bounded_captures_t walk = {.record = record, .horizon = horizon, .bound = result};
bool some;
TxnBaseBase newest;
+ HostStorage storage;
+ TxnStatePersistence persistence;
+ txn_TupleVecFileU64_t listed;
+ bool ok = true;
*result = point;
if (!txn_state_newest(io, held, &some, &newest, error))
return false;
if (some)
{
- walk.folded = newest.folded;
+ walk.folded = newest.read;
txn_base_Base_drop(&newest);
}
- return txn_memtable_table_walked(&TXN_MEMTABLE_TABLE_ROWS_TREE_OPS, io, record, txn_fold_bounded_closure_1, &walk,
- NULL, error) &&
- txn_fold_bounded_map(io, record, &TXN_MEMTABLE_MAP_FREE_TREE_OPS, horizon, result, error) &&
- txn_fold_bounded_map(io, record, &TXN_MEMTABLE_MAP_VALUES_TREE_OPS, horizon, result, error);
+ if (!txn_memtable_table_walked(&TXN_MEMTABLE_TABLE_ROWS_TREE_OPS, io, record, txn_fold_bounded_closure_1, &walk,
+ NULL, error) ||
+ !txn_fold_bounded_map(io, record, &TXN_MEMTABLE_MAP_FREE_TREE_OPS, horizon, result, error) ||
+ !txn_fold_bounded_map(io, record, &TXN_MEMTABLE_MAP_VALUES_TREE_OPS, horizon, result, error))
+ return false;
+ if (!txn_state_described(record, &storage, &persistence))
+ return true;
+ if (!txn_spill_listed(&TXN_SPILL_STATE_HOME_OPS, io, record, &storage, &listed, error))
+ return false;
+ for (size_t k = 0; k < listed.f0.len && ok; k++)
+ ok = txn_fold_kept_out(io, STD_VEC_AT(&listed.f0, TxnSpillFile, k).xid, horizon, result, error);
+ std_vec_free_Checked(&listed.f0, sizeof(TxnSpillFile), _Alignof(TxnSpillFile));
+ return ok;
}
/*
@@ -4842,6 +5065,8 @@ typedef struct txn_fold_rechained_base_frame_t
bool base_live;
const TxnBasePending *entries;
size_t entries_len;
+ TxnStateLogged logged;
+ void *logged_captures;
TreesStoreWriter plain;
TreesStoreBrandedWriter writer;
bool writer_live;
@@ -4870,7 +5095,11 @@ typedef struct txn_fold_rechained_base_closure_1_frame_t
bool ok;
} txn_fold_rechained_base_closure_1_frame_t;
-/* txn_fold_rechained_base_closure_1_body holds the chain's root in one slot, publishes it, and replaces the base. */
+/*
+ * txn_fold_rechained_base_closure_1_body holds the chain's root in one slot,
+ * publishes it, and replaces the base, listing its pack first when the
+ * replace logs.
+ */
static void
txn_fold_rechained_base_closure_1_body(void *frame_)
{
@@ -4895,7 +5124,11 @@ txn_fold_rechained_base_closure_1_body(void *frame_)
frame->result->next.has_chain = true;
frame->result->next.chain = trees_published_Published_base(&STD_VEC_AT(&frame->published,
TreesPublishedPublished, 0));
- if (!txn_state_rechained(io, rechained->held, rechained->generation, &frame->result->next, &replaced, &host))
+ if (rechained->logged != NULL &&
+ !chunks_packs_backend_Packs_flush(&rechained->repo->packs, io, rechained->reading, frame->error))
+ return;
+ if (!txn_state_rechained(io, rechained->held, rechained->generation, &frame->result->next, rechained->logged,
+ rechained->logged_captures, &replaced, &host))
{
*frame->error = chunks_error_Error_FromError_from(&host);
return;
@@ -5027,16 +5260,17 @@ txn_fold_rechained_base_pad(void *frame_)
* replaces a state's newest base, which it takes, with one naming it when the
* base generation is still the one read with the base, answering the new
* base and the packs it wrote, or nothing when another install moved the
- * generation.
+ * generation. The replace logs through logged, when given.
*/
static bool
txn_fold_rechained_base(HostIoIo *io, const RepoRepo *repo, const HostSharedReader *reading,
const HostSharedHeld *held, uint64 generation, TxnBaseBase *base,
- const TxnBasePending *entries, size_t entries_len, TxnFoldInstalled *result, HostError *error)
+ const TxnBasePending *entries, size_t entries_len, TxnStateLogged logged,
+ void *logged_captures, TxnFoldInstalled *result, HostError *error)
{
txn_fold_rechained_base_frame_t frame = {.io = io, .repo = repo, .reading = reading, .held = held,
.generation = generation, .base = *base, .base_live = true, .entries = entries, .entries_len = entries_len,
- .result = result, .error = error};
+ .logged = logged, .logged_captures = logged_captures, .result = result, .error = error};
result->some = false;
std_landing(&frame, txn_fold_rechained_base_body, txn_fold_rechained_base_pad);
@@ -5061,6 +5295,10 @@ typedef struct txn_fold_chain_frame_t
size_t recorded_entries_len;
TxnBaseBase recorded;
bool recorded_live;
+ /* end is a replayed chain record's end. */
+ HostLsn end;
+ /* storage is a DDL's storage, which its record names. */
+ HostStorage storage;
TxnBaseBase base;
bool base_live;
StdVec current;
@@ -5077,6 +5315,22 @@ typedef struct txn_fold_chain_frame_t
bool ok;
} txn_fold_chain_frame_t;
+/*
+ * txn_fold_chain_logged is a DDL's replace's logging: it inserts the record
+ * of the pass's base and its chain's entries, and gives its end.
+ */
+static bool
+txn_fold_chain_logged(void *captures, HostIoIoDoors io, HostLsn reached, HostLsn *end, HostError *error)
+{
+ const txn_fold_chain_frame_t *frame = captures;
+ TxnRecordBaseAdvanced advanced = {.storage = frame->storage, .folded = frame->installed.next.folded,
+ .max_folded = frame->installed.next.max_folded, .base = frame->installed.next,
+ .packs = frame->installed.packs, .has_chain = true, .chain = frame->entries};
+
+ (void) reached;
+ return txn_fold_advanced(io, &advanced, end, error);
+}
+
/* txn_fold_chain_pass_drop frees what a pass of a chain loop holds: its install, counts, entries, chain, and base. */
static void
txn_fold_chain_pass_drop(txn_fold_chain_frame_t *frame)
@@ -5177,8 +5431,8 @@ txn_fold_chained_body(void *frame_)
HostXid xid = {frame->pending.xid};
bool has_added;
uint64 added;
- HostStorage storage;
TxnStatePersistence persistence;
+ bool permanent;
txn_fold_chain_pass_drop(frame);
if (!txn_fold_chain_read(frame, "a DDL's transform found its storage with no base", &some, &generation))
@@ -5211,34 +5465,17 @@ txn_fold_chained_body(void *frame_)
txn_base_Pending_Clone_clone(&frame->pending, &pending);
std_vec_push_Checked(&frame->entries, &pending, TXN_FOLD_CHAIN);
frame->base_live = false;
+ permanent = txn_state_described(host_shared_Held_record(frame->held), &frame->storage, &persistence) &&
+ persistence == TXN_STATE_PERSISTENCE_PERMANENT;
if (!txn_fold_rechained_base(frame->io, frame->repo, frame->reading, frame->held, generation, &frame->base,
- frame->entries.ptr, frame->entries.len, &frame->installed, frame->error))
+ frame->entries.ptr, frame->entries.len, permanent ? txn_fold_chain_logged : NULL,
+ frame, &frame->installed, frame->error))
return;
if (!frame->installed.some)
continue;
if (!txn_fold_chain_given(frame) ||
!txn_status_add(frame->io, xid, 1, &has_added, &added, frame->error))
return;
- if (txn_state_described(host_shared_Held_record(frame->held), &storage, &persistence) &&
- persistence == TXN_STATE_PERSISTENCE_PERMANENT)
- {
- TxnRecordBaseAdvanced advanced = {.storage = storage, .folded = frame->installed.next.folded,
- .max_folded = frame->installed.next.max_folded, .base = frame->installed.next,
- .packs = frame->installed.packs, .has_chain = true, .chain = frame->entries};
- StdVec body;
- HostLsn lsn;
- bool ok;
-
- frame->installed.some = false;
- frame->entries_live = false;
- txn_record_BaseAdvanced_encode(&advanced, &body);
- ok = host_wal_insert(HOST_IO_IO_IO_DOORS(frame->io), HOST_WAL_KIND_BASE_ADVANCED, body.ptr, body.len,
- &lsn, frame->error);
- std_vec_free_Checked(&body, TXN_FOLD_BYTES);
- txn_record_BaseAdvanced_drop(&advanced);
- if (!ok)
- return;
- }
frame->ok = true;
return;
}
@@ -5314,7 +5551,8 @@ txn_fold_redo_chained_body(void *frame_)
}
frame->base_live = false;
if (!txn_fold_rechained_base(frame->io, frame->repo, frame->reading, frame->held, generation, &frame->base,
- frame->entries.ptr, frame->entries.len, &frame->installed, frame->error))
+ frame->entries.ptr, frame->entries.len, txn_fold_replayed, &frame->end,
+ &frame->installed, frame->error))
return;
if (!frame->installed.some)
continue;
@@ -5360,15 +5598,16 @@ txn_fold_redo_chained_body(void *frame_)
* server's save wrote; each entry without an end the replaced chain lacked
* counts at its top-level entry, and each without one it held that the
* record lacks gives its count back. A storage this server holds no base of
- * yet takes the record's base, which it takes, with no chain.
+ * yet takes the record's base, which it takes, with no chain. The replaced
+ * base keeps the record's end.
*/
bool
txn_fold_redo_chained(HostIoIo *io, const RepoRepo *repo, const HostSharedReader *reading,
const HostSharedHeld *held, const TxnBasePending *entries, size_t entries_len,
- TxnBaseBase *recorded, HostError *error)
+ TxnBaseBase *recorded, HostLsn end, HostError *error)
{
txn_fold_chain_frame_t frame = {.io = io, .repo = repo, .reading = reading, .held = held,
- .recorded_entries = entries, .recorded_entries_len = entries_len, .recorded = *recorded,
+ .recorded_entries = entries, .recorded_entries_len = entries_len, .recorded = *recorded, .end = end,
.recorded_live = true, .error = error};
std_landing(&frame, txn_fold_redo_chained_body, txn_fold_chain_pad);
@@ -5455,7 +5694,8 @@ txn_fold_settled_body(void *frame_)
break;
frame->base_live = false;
if (!txn_fold_rechained_base(frame->io, frame->repo, frame->reading, frame->held, generation, &frame->base,
- frame->entries.ptr, frame->entries.len, &frame->installed, frame->error))
+ frame->entries.ptr, frame->entries.len, NULL, NULL, &frame->installed,
+ frame->error))
return;
if (!frame->installed.some)
continue;
@@ -5513,6 +5753,8 @@ typedef struct txn_fold_whole_frame_t
TxnBaseBase base;
bool base_live;
HostLsn point;
+ /* read is the installed base's read point. */
+ HostLsn read;
uint64 generation;
uint64 horizon;
bool has_nodes;
@@ -5527,6 +5769,30 @@ typedef struct txn_fold_whole_frame_t
bool ok;
} txn_fold_whole_frame_t;
+/* txn_fold_whole_logging_t is a built base's install's logging's captures: the build, its storage, and its install. */
+typedef struct txn_fold_whole_logging_t
+{
+ const txn_fold_whole_frame_t *whole;
+ HostStorage storage;
+ const TxnFoldInstalled *installed;
+} txn_fold_whole_logging_t;
+
+/*
+ * txn_fold_whole_advanced is a built base's install's logging: it inserts
+ * the base's record, after its nodes', and gives its end.
+ */
+static bool
+txn_fold_whole_advanced(void *captures, HostIoIoDoors io, HostLsn reached, HostLsn *end, HostError *error)
+{
+ const txn_fold_whole_logging_t *logging = captures;
+ TxnRecordBaseAdvanced advanced = {.storage = logging->storage, .folded = logging->whole->point,
+ .max_folded = logging->installed->next.max_folded, .base = logging->installed->next,
+ .packs = logging->installed->packs, .built = true, .horizon = logging->whole->horizon};
+
+ (void) reached;
+ return txn_fold_advanced(io, &advanced, end, error);
+}
+
/* txn_fold_whole_closure_0_frame_t is whole's run's state across its pad: the roots it published. */
typedef struct txn_fold_whole_closure_0_frame_t
{
@@ -5559,6 +5825,11 @@ txn_fold_whole_closure_0_body(void *frame_)
HostError host;
bool *has[4];
TreesStoreTree *named[4];
+ TxnStatePersistence persistence;
+ txn_fold_whole_logging_t logging = {.whole = whole, .installed = frame->result};
+ bool permanent = txn_state_described(host_shared_Held_record(whole->held), &logging.storage,
+ &persistence) &&
+ persistence == TXN_STATE_PERSISTENCE_PERMANENT;
chunks_packs_roots_RootSlots_fold(frame->slots, &slot);
held = txn_fold_held(whole->writer, io, slot.s0, whole->has_roots[0], whole->roots[0], true, frame->error);
@@ -5590,7 +5861,11 @@ txn_fold_whole_closure_0_body(void *frame_)
*named[i] = trees_published_Published_base(&STD_VEC_AT(&frame->published, TreesPublishedPublished, at++));
}
next->folded = whole->point;
- if (!txn_state_installed(io, whole->held, whole->generation, next, &installed, &host))
+ next->read = whole->read;
+ if (permanent && !chunks_packs_backend_Packs_flush(&whole->repo->packs, io, whole->reading, frame->error))
+ return;
+ if (!txn_state_installed(io, whole->held, whole->generation, next, permanent ? txn_fold_whole_advanced : NULL,
+ &logging, &installed, &host))
{
*frame->error = chunks_error_Error_FromError_from(&host);
return;
@@ -5661,9 +5936,9 @@ txn_fold_Logged_drop(void *logged_)
}
/*
- * txn_fold_whole_logged logs a permanent storage's built base: its packs and
- * the WAL flushed, each node the writer kept as a Chunk record in its own
- * checkpoint window, children before parents, then the base's record.
+ * txn_fold_whole_logged logs a permanent storage's built base's nodes: the
+ * WAL flushed, each node the writer kept as a Chunk record in its own
+ * checkpoint window, children before parents.
*/
static bool
txn_fold_whole_logged(txn_fold_whole_frame_t *frame)
@@ -5671,8 +5946,6 @@ txn_fold_whole_logged(txn_fold_whole_frame_t *frame)
HostIoIoDoors doors = HOST_IO_IO_IO_DOORS(frame->io);
HostStorage storage;
TxnStatePersistence persistence;
- ChunksErrorError failed;
- TxnRecordBaseAdvanced advanced;
StdVec body;
HostLsn lsn;
bool ok;
@@ -5680,11 +5953,6 @@ txn_fold_whole_logged(txn_fold_whole_frame_t *frame)
if (!txn_state_described(host_shared_Held_record(frame->held), &storage, &persistence) ||
persistence != TXN_STATE_PERSISTENCE_PERMANENT)
return true;
- if (!chunks_packs_backend_Packs_flush(&frame->repo->packs, frame->io, frame->reading, &failed))
- {
- *frame->error = txn_base_storage(&failed);
- return false;
- }
if (!host_wal_flush(doors, frame->point, frame->error))
return false;
if (storage.database == 0)
@@ -5723,25 +5991,13 @@ txn_fold_whole_logged(txn_fold_whole_frame_t *frame)
if (!ok)
return false;
}
- if (!host_pause(HOST_SHARED_IO_PAUSES(frame->io), "doltgres-built-logged", frame->error))
- return false;
- advanced.storage = storage;
- advanced.folded = frame->point;
- advanced.max_folded = frame->installed.next.max_folded;
- advanced.base = frame->installed.next;
- advanced.packs = frame->installed.packs;
- advanced.built = true;
- advanced.horizon = frame->horizon;
- advanced.has_chain = false;
- frame->installed.some = false;
- txn_record_BaseAdvanced_encode(&advanced, &body);
- ok = host_wal_insert(doors, HOST_WAL_KIND_BASE_ADVANCED, body.ptr, body.len, &lsn, frame->error);
- std_vec_free_Checked(&body, TXN_FOLD_BYTES);
- txn_record_BaseAdvanced_drop(&advanced);
- return ok;
+ return host_pause(HOST_SHARED_IO_PAUSES(frame->io), "doltgres-built-logged", frame->error);
}
-/* txn_fold_whole_body takes the writer's logged nodes, installs the base under the registration, and logs it. */
+/*
+ * txn_fold_whole_body takes the writer's logged nodes and logs them, then
+ * installs and logs the base under the registration.
+ */
static void
txn_fold_whole_body(void *frame_)
{
@@ -5750,6 +6006,8 @@ txn_fold_whole_body(void *frame_)
trees_store_Writer_logged(&frame->writer->core, &frame->logged);
frame->logged_live = true;
+ if (!txn_fold_whole_logged(frame))
+ return;
if (!chunks_packs_roots_RootSlots_register(frame->io, txn_fold_whole_closure_0, frame, NULL, &frame->installed,
&failed))
{
@@ -5757,12 +6015,7 @@ txn_fold_whole_body(void *frame_)
return;
}
*frame->result = frame->installed.some;
- if (!frame->installed.some)
- {
- frame->ok = true;
- return;
- }
- frame->ok = txn_fold_whole_logged(frame);
+ frame->ok = true;
}
/* txn_fold_whole_pad frees what whole holds live: the install, the logged nodes, the base, and the writer. */
@@ -5786,8 +6039,8 @@ txn_fold_whole_pad(void *frame_)
* txn_fold_whole installs a base a backend wrote whole, taking the base and
* the writer whose packs hold its trees' roots, held in the registration's
* slots until the base that names them is installed as the state's newest,
- * at a fold point, when its base generation is still the one read before the
- * build, and for a permanent storage logged as built, after every node the
+ * at a fold point and a read point, when its base generation is still the
+ * one read before the build, and for a permanent storage logged as built, after every node the
* writer kept as a Chunk record, each in its own checkpoint window, children
* before parents; with nodes none it logs no chunk. The record carries its
* replay's conflict horizon. It answers whether it installed.
@@ -5795,13 +6048,13 @@ txn_fold_whole_pad(void *frame_)
static bool
txn_fold_whole(HostIoIo *io, const RepoRepo *repo, const HostSharedReader *reading, const HostSharedHeld *held,
TreesStoreBrandedWriter *writer, const bool *has_roots, const size_t *roots, TxnBaseBase *base,
- HostLsn point, uint64 generation, uint64 horizon, bool has_nodes, uint64 nodes, bool *result,
- HostError *error)
+ HostLsn point, HostLsn read, uint64 generation, uint64 horizon, bool has_nodes, uint64 nodes,
+ bool *result, HostError *error)
{
txn_fold_whole_frame_t frame = {.io = io, .repo = repo, .reading = reading, .held = held, .writer = writer,
.writer_live = true, .has_roots = has_roots, .roots = roots, .base = *base, .base_live = true,
- .point = point, .generation = generation, .horizon = horizon, .has_nodes = has_nodes, .nodes = nodes,
- .result = result, .error = error};
+ .point = point, .read = read, .generation = generation, .horizon = horizon, .has_nodes = has_nodes,
+ .nodes = nodes, .result = result, .error = error};
std_landing(&frame, txn_fold_whole_body, txn_fold_whole_pad);
txn_fold_whole_pad(&frame);
@@ -5965,7 +6218,7 @@ txn_fold_built_body(void *frame_)
}
frame->writer_live = frame->base_live = false;
frame->ok = txn_fold_whole(frame->io, frame->repo, frame->reading, frame->held, &frame->writer, frame->has_roots,
- frame->roots, &frame->base, frame->point, generation, frame->horizon,
+ frame->roots, &frame->base, frame->point, frame->point, generation, frame->horizon,
frame->has_nodes, frame->nodes, frame->result, frame->error);
}
@@ -6277,33 +6530,26 @@ txn_fold_reformed_body(void *frame_)
TreesBuildEntry *entry = &row->entry;
ChunksTrailerStamp stamp;
uint32 heap_bytes;
+ uint64 copies_bytes = 0;
txn_TupleU64VecU8U32_t pair;
TreesMutateEdit edit = {.tag = TREES_MUTATE_EDIT_PUT};
- if (entry->stamps.len != 1)
- {
- *frame->error = txn_fold_fork("XX001", "a keyed table's entry holds other than one row");
- return;
- }
- stamp = STD_VEC_AT(&entry->stamps, ChunksTrailerStamp, 0);
- if (row->epoch == end)
+ if (row->epoch != end)
{
- if (!txn_fold_written_heap(&frame->ctids, stamp.tuple, &heap_bytes))
+ size_t from;
+
+ if (entry->stamps.len != 1)
{
- txn_fold_reformed_corrupt(frame);
+ *frame->error = txn_fold_fork("XX001", "a keyed table's entry holds other than one row");
return;
}
- }
- else
- {
- size_t from;
-
if (row->epoch < frame->base->epoch || row->epoch - frame->base->epoch > frame->chain_len)
{
txn_fold_reformed_corrupt(frame);
return;
}
from = row->epoch - frame->base->epoch;
+ stamp = STD_VEC_AT(&entry->stamps, ChunksTrailerStamp, 0);
if (!frame->reform->ops->reformed(frame->reform->self, frame->io, entry->key.ptr, entry->key.len, stamp,
entry->value.ptr, entry->value.len, entry->addresses.ptr,
entry->addresses.len, entry->values.ptr, entry->values.len,
@@ -6318,14 +6564,25 @@ txn_fold_reformed_body(void *frame_)
entry->values = frame->reformed.values;
heap_bytes = frame->reformed.heap_bytes;
}
+ for (size_t c = 0; c < entry->stamps.len; c++)
+ {
+ stamp = STD_VEC_AT(&entry->stamps, ChunksTrailerStamp, c);
+ if (row->epoch == end && !txn_fold_written_heap(&frame->ctids, stamp.tuple, &heap_bytes))
+ {
+ txn_fold_reformed_corrupt(frame);
+ return;
+ }
+ copies_bytes += heap_bytes;
+ pair.f0 = stamp.tuple;
+ pair.f2 = heap_bytes;
+ std_vec_reserve_Checked(&frame->paired, 1, TXN_FOLD_PAIRED);
+ txn_fold_bytes(entry->key.ptr, entry->key.len, &pair.f1);
+ txn_fold_paired_insert(&frame->paired, &pair);
+ }
*frame->sizes = txn_base_Sizes_summed(*frame->sizes, txn_fold_sized(entry->key.ptr, entry->key.len,
- entry->value.ptr, entry->value.len, 1));
- frame->sizes->heap_bytes = STD_ADD(u64, frame->sizes->heap_bytes, heap_bytes, 1844, 9);
- pair.f0 = stamp.tuple;
- pair.f2 = heap_bytes;
- std_vec_reserve_Checked(&frame->paired, 1, TXN_FOLD_PAIRED);
- txn_fold_bytes(entry->key.ptr, entry->key.len, &pair.f1);
- txn_fold_paired_insert(&frame->paired, &pair);
+ entry->value.ptr, entry->value.len,
+ entry->stamps.len));
+ frame->sizes->heap_bytes = STD_ADD(u64, frame->sizes->heap_bytes, copies_bytes, 1844, 9);
edit.u.put = *entry;
std_vec_reserve_Checked(&frame->out, 1, TXN_FOLD_EDIT);
std_vec_push_Checked(&frame->out, &edit, TXN_FOLD_EDIT);
@@ -6408,8 +6665,11 @@ txn_fold_reformed_pad(void *frame_)
* is stored at, through the caller's re-form, its tuple id paired again with
* its new heap bytes, a row written at the chain's end kept as it is, and
* the base's sizes counted afresh over every row it keeps. A keyed table's
- * entry is one row with one stamp. It answers the edits, the tuple id tree's
- * edits, the sizes, and the chain's end as the base's epoch.
+ * entry is one row with one stamp. A keyless table's rows are all at the
+ * chain's end, since its DDL re-keys every row at its commit, so each entry
+ * is kept with every copy's tuple id paired (bugs row 328). It answers the
+ * edits, the tuple id tree's edits, the sizes, and the chain's end as the
+ * base's epoch.
*/
static bool
txn_fold_reformed(HostIoIo *io, TreesStoreNodeStore store, const TxnBaseBase *base, const TxnVisibleLive *chain,
@@ -6610,6 +6870,8 @@ typedef struct txn_fold_copied_frame_t
TxnBaseBase base;
bool base_live;
HostLsn point;
+ /* read is the installed base's read point. */
+ HostLsn read;
bool has_nodes;
uint64 nodes;
TxnFoldCopies *memo;
@@ -6671,8 +6933,8 @@ txn_fold_copied_body(void *frame_)
}
frame->writer_live = frame->base_live = false;
frame->ok = txn_fold_whole(frame->io, frame->repo, frame->reading, frame->held, &frame->writer, has_roots, roots,
- &frame->base, frame->point, generation, frame->base.max_folded, frame->has_nodes,
- frame->nodes, frame->result, frame->error);
+ &frame->base, frame->point, frame->read, generation, frame->base.max_folded,
+ frame->has_nodes, frame->nodes, frame->result, frame->error);
}
/* txn_fold_copied_pad frees what copied holds live: its writer and the base. */
@@ -6692,20 +6954,21 @@ txn_fold_copied_pad(void *frame_)
* txn_fold_copied installs a copy of a base in a new storage, taking the
* base, its four trees written again chunk by chunk into packs of the
* storage's tablespace, every chunk they reach that a pack holds, each
- * keeping its address, hashed bytes, and stamps, at a fold point, and logged
- * as a built base whose replay's conflict horizon is its maxFolded. A chunk
+ * keeping its address, hashed bytes, and stamps, at a fold point and a read
+ * point, and logged as a built base whose replay's conflict horizon is its
+ * maxFolded. A chunk
* the working state's delta holds keeps its unknown locator. A memo of the
* chunks copied writes a chunk the base names more than once once. It
* answers whether it installed.
*/
bool
txn_fold_copied(HostIoIo *io, const RepoRepo *repo, const HostSharedReader *reading, const HostSharedHeld *held,
- TxnBaseBase *base, HostLsn point, bool has_nodes, uint64 nodes, TxnFoldCopies *memo, bool *result,
- HostError *error)
+ TxnBaseBase *base, HostLsn point, HostLsn read, bool has_nodes, uint64 nodes, TxnFoldCopies *memo,
+ bool *result, HostError *error)
{
txn_fold_copied_frame_t frame = {.io = io, .repo = repo, .reading = reading, .held = held, .base = *base,
- .base_live = true, .point = point, .has_nodes = has_nodes, .nodes = nodes, .memo = memo, .result = result,
- .error = error};
+ .base_live = true, .point = point, .read = read, .has_nodes = has_nodes, .nodes = nodes, .memo = memo,
+ .result = result, .error = error};
std_landing(&frame, txn_fold_copied_body, txn_fold_copied_pad);
txn_fold_copied_pad(&frame);
@@ -6725,14 +6988,16 @@ txn_fold_installs_done(HostIoIo *io, const RepoRepo *repo, const HostSharedReade
txn_fold_held_pairing_t paired = {TREES_STORE_READER_NODE_STORE(&reader), base->has_tuple_ids, base->tuple_ids};
TxnFoldSpaced spaced;
- if (!txn_fold_dropped(io, held, base->folded, txn_fold_held_pairing, &paired, error) ||
- !txn_fold_spaced(io, held, base->folded, txn_fold_held_pairing, &paired, &spaced, error))
+ if (!txn_fold_dropped(io, held, base->read, txn_fold_held_pairing, &paired, error) ||
+ !txn_fold_spaced(io, held, base->read, NULL, NULL, &spaced, error))
return false;
std_vec_drop_Checked(&spaced.edits, TXN_FOLD_EDIT, trees_mutate_Edit_drop);
+ std_vec_drop_Checked(&spaced.kept, sizeof(TxnSpillKept), _Alignof(TxnSpillKept), txn_fold_Kept_drop);
if (!txn_fold_unspaced(io, held, &TXN_MEMTABLE_MAP_FREE_TREE_OPS, &spaced.removed, error) ||
- !txn_fold_valued(io, held, base->folded, &spaced, error))
+ !txn_fold_valued(io, held, base->read, &spaced, error))
return false;
std_vec_drop_Checked(&spaced.edits, TXN_FOLD_EDIT, trees_mutate_Edit_drop);
+ std_vec_drop_Checked(&spaced.kept, sizeof(TxnSpillKept), _Alignof(TxnSpillKept), txn_fold_Kept_drop);
return txn_fold_unspaced(io, held, &TXN_MEMTABLE_MAP_VALUES_TREE_OPS, &spaced.removed, error) &&
txn_memtable_tuples_emptied(io, host_shared_Held_record(held), error);
}
@@ -7064,6 +7329,8 @@ typedef struct txn_fold_rebuilt_frame_t
const HostSharedReader *reading;
const HostSharedHeld *held;
const TxnRecordBaseAdvanced *advanced;
+ /* end is the replayed record's end. */
+ HostLsn end;
uint64 generation;
HostSharedHeld deltas;
bool deltas_live;
@@ -7144,7 +7411,10 @@ txn_fold_rebuilt_closure_2_body(void *frame_)
if (rebuilt->has_roots[i])
*named[i] = trees_published_Published_base(&STD_VEC_AT(&frame->published, TreesPublishedPublished, at++));
}
- if (!txn_state_installed(io, rebuilt->held, rebuilt->generation, next, &installed, &host))
+ if (!chunks_packs_backend_Packs_flush(&rebuilt->repo->packs, io, rebuilt->reading, frame->error))
+ return;
+ if (!txn_state_installed(io, rebuilt->held, rebuilt->generation, next, txn_fold_replayed, &rebuilt->end,
+ &installed, &host))
{
*frame->error = chunks_error_Error_FromError_from(&host);
return;
@@ -7289,11 +7559,6 @@ txn_fold_rebuilt_body(void *frame_)
frame->ok = true;
return;
}
- if (!chunks_packs_backend_Packs_flush(&frame->repo->packs, frame->io, frame->reading, &failed))
- {
- *frame->error = txn_base_storage(&failed);
- return;
- }
for (size_t i = 0; i < frame->nodes.len; i++)
{
const txn_TupleAddressBool_t *node = &STD_VEC_AT(&frame->nodes, txn_TupleAddressBool_t, i);
@@ -7334,14 +7599,16 @@ txn_fold_rebuilt_pad(void *frame_)
* packs, children before parents, and installs the base that names them at
* this server's locators, when its base generation is still the one read;
* once the packs are flushed it takes those nodes out of the delta. It
- * clones the record's key, since the installed base outlives the record.
+ * clones the record's key, since the installed base outlives the record. The
+ * installed base keeps the record's end.
*/
static bool
txn_fold_rebuilt(HostIoIo *io, const RepoRepo *repo, const HostSharedReader *reading, const HostSharedHeld *held,
- const TxnRecordBaseAdvanced *advanced, uint64 generation, bool *result, HostError *error)
+ const TxnRecordBaseAdvanced *advanced, HostLsn end, uint64 generation, bool *result,
+ HostError *error)
{
txn_fold_rebuilt_frame_t frame = {.io = io, .repo = repo, .reading = reading, .held = held,
- .advanced = advanced, .generation = generation, .result = result, .error = error};
+ .advanced = advanced, .end = end, .generation = generation, .result = result, .error = error};
std_landing(&frame, txn_fold_rebuilt_body, txn_fold_rebuilt_pad);
txn_fold_rebuilt_pad(&frame);
@@ -7376,6 +7643,45 @@ typedef struct txn_fold_redo_frame_t
bool ok;
} txn_fold_redo_frame_t;
+/*
+ * txn_fold_redo_residuals lists, before a replayed fold's install, the
+ * residuals of the spilled map versions its drop keeps at its point, as its
+ * primary's fold listed them; a built base keeps none.
+ */
+static bool
+txn_fold_redo_residuals(txn_fold_redo_frame_t *frame, StdVec *result)
+{
+ const TxnRecordBaseAdvanced *advanced = frame->advanced;
+ TxnFoldSpaced free;
+ TxnFoldSpaced values;
+ StdVec kept;
+ bool ok;
+
+ if (advanced->built)
+ {
+ std_vec_new(result, sizeof(TxnSpillFile), _Alignof(TxnSpillFile));
+ return true;
+ }
+ if (!txn_fold_spaced(frame->io, frame->held, advanced->folded, NULL, NULL, &free, frame->error))
+ return false;
+ if (!txn_fold_valued(frame->io, frame->held, advanced->folded, &values, frame->error))
+ {
+ txn_fold_Spaced_drop(&free);
+ return false;
+ }
+ kept = free.kept;
+ std_vec_new(&free.kept, sizeof(TxnSpillKept), _Alignof(TxnSpillKept));
+ std_vec_extend_from_slice_Checked(&kept, values.kept.ptr, values.kept.len, sizeof(TxnSpillKept),
+ _Alignof(TxnSpillKept));
+ values.kept.len = 0;
+ txn_fold_Spaced_drop(&free);
+ txn_fold_Spaced_drop(&values);
+ ok = txn_spill_residual(frame->io, host_shared_Held_record(frame->held), advanced->folded, kept.ptr, kept.len,
+ result, frame->error);
+ std_vec_drop_Checked(&kept, sizeof(TxnSpillKept), _Alignof(TxnSpillKept), txn_fold_Kept_drop);
+ return ok;
+}
+
/* txn_fold_redo_body installs the record's base, rebuilds a built one, or records the point, unless a newer base passed it. */
static void
txn_fold_redo_body(void *frame_)
@@ -7403,6 +7709,7 @@ txn_fold_redo_body(void *frame_)
uint64 generation = txn_state_generation(host_shared_Held_record(frame->held));
bool passed = false;
bool installed;
+ StdVec residuals;
if (!txn_state_newest(frame->io, frame->held, &frame->newest_live, &frame->newest, frame->error))
return;
@@ -7423,7 +7730,8 @@ txn_fold_redo_body(void *frame_)
{
bool rebuilt;
- if (!txn_fold_rebuilt(frame->io, frame->repo, frame->reading, frame->held, advanced, generation, &rebuilt,
+ if (!txn_fold_rebuilt(frame->io, frame->repo, frame->reading, frame->held, advanced, frame->end, generation,
+ &rebuilt,
frame->error))
return;
if (rebuilt)
@@ -7433,11 +7741,26 @@ txn_fold_redo_body(void *frame_)
}
continue;
}
+ if (!txn_fold_redo_residuals(frame, &residuals))
+ return;
if (!txn_state_installed(HOST_IO_IO_TAKES_SHORT(frame->io), frame->held, generation, &advanced->base,
+ txn_fold_replayed, &frame->end,
&installed, frame->error))
+ {
+ std_vec_free_Checked(&residuals, sizeof(TxnSpillFile), _Alignof(TxnSpillFile));
return;
+ }
if (!installed)
+ {
+ bool unlisted = txn_spill_unlisted(frame->io, host_shared_Held_record(frame->held), &residuals,
+ frame->error);
+
+ std_vec_free_Checked(&residuals, sizeof(TxnSpillFile), _Alignof(TxnSpillFile));
+ if (!unlisted)
+ return;
continue;
+ }
+ std_vec_free_Checked(&residuals, sizeof(TxnSpillFile), _Alignof(TxnSpillFile));
if (advanced->built)
{
txn_state_grown(host_shared_Held_record(frame->held), &advanced->base);
diff --git a/src/doltgres/txn/c/identity.c b/src/doltgres/txn/c/identity.c
index 8cec866028842..fa7d062e981fb 100644
--- a/src/doltgres/txn/c/identity.c
+++ b/src/doltgres/txn/c/identity.c
@@ -40,9 +40,20 @@
#include "doltgres/txn/memtable/branch.h"
#include "doltgres/txn/memtable/table.h"
#include "doltgres/txn/outcome.h"
+#include "doltgres/txn/spill.h"
#include "doltgres/txn/visible.h"
#include "doltgres/txn/written.h"
+#ifdef DOLTGRES_TEST_TXN
+#include "doltgres/chunks/record.h"
+#include "doltgres/repo/lib.h"
+#include "doltgres/simc/simc.h"
+#include "doltgres/std/test.h"
+
+/* RUST_FILE is the file the tests' asserts name, identity.rs. */
+#define RUST_FILE "txn" STD_SEP "src" STD_SEP "identity.rs"
+#endif
+
/* The prepare whose closure pushes an identity change's versions, or answers false to start over. */
#define TXN_MEMTABLE_BRANCH_RS Bool
#define TXN_MEMTABLE_BRANCH_WS Striping
@@ -94,27 +105,14 @@
#define TXN_IDENTITY_OP sizeof(TxnMemtableOp), _Alignof(TxnMemtableOp)
#define TXN_IDENTITY_NEED sizeof(txn_TupleRefSliceU8Usize_t), _Alignof(txn_TupleRefSliceU8Usize_t)
#define TXN_IDENTITY_HANDLE sizeof(TxnMemtableVersionHandle), _Alignof(TxnMemtableVersionHandle)
-#define TXN_IDENTITY_FOLDED_VERSION sizeof(txn_TupleHandleOptionU64_t), _Alignof(txn_TupleHandleOptionU64_t)
-#define TXN_IDENTITY_FOLDED sizeof(txn_TupleVecU8VecTupleHandleOptionU64_t), \
- _Alignof(txn_TupleVecU8VecTupleHandleOptionU64_t)
+#define TXN_IDENTITY_FOLDED sizeof(txn_TupleVecU8VecHandle_t), _Alignof(txn_TupleVecU8VecHandle_t)
-/* txn_TupleHandleOptionU64_t is dropped's (Handle, Option): a folded version and its top-level xid, or none. */
-typedef struct txn_TupleHandleOptionU64_t
-{
- TxnMemtableVersionHandle f0;
- bool has_f1;
- uint64 f1;
-} txn_TupleHandleOptionU64_t;
-
-/*
- * txn_TupleVecU8VecTupleHandleOptionU64_t is dropped's (Vec, Vec<(Handle,
- * Option)>): a key and its folded versions.
- */
-typedef struct txn_TupleVecU8VecTupleHandleOptionU64_t
+/* txn_TupleVecU8VecHandle_t is dropped's (Vec, Vec): a key and its folded versions. */
+typedef struct txn_TupleVecU8VecHandle_t
{
StdVec f0;
StdVec f1;
-} txn_TupleVecU8VecTupleHandleOptionU64_t;
+} txn_TupleVecU8VecHandle_t;
/* TxnIdentityFoundTag is Found's variants. */
typedef enum TxnIdentityFoundTag
@@ -466,15 +464,6 @@ txn_identity_corrupt(void)
return txn_identity_fork("XX001", "an identity tree's value has the wrong form");
}
-/* txn_identity_storage returns what names a database's identity tree in a transaction's list of touched keys. */
-static HostStorage
-txn_identity_storage(uint32 repository)
-{
- HostStorage storage = {.database = repository};
-
- return storage;
-}
-
/*
* txn_identity_stripe returns the key lock's stripe of a database's identity
* tree, which every change of its pairs takes, so a derivation's probe and
@@ -483,7 +472,7 @@ txn_identity_storage(uint32 repository)
static uint32
txn_identity_stripe(uint32 repository)
{
- HostStorage storage = txn_identity_storage(repository);
+ HostStorage storage = txn_spill_identity_storage(repository);
return txn_change_stripe(&storage, NULL, 0, false);
}
@@ -513,11 +502,42 @@ typedef struct txn_identity_found_frame_t
bool ok;
} txn_identity_found_frame_t;
+/* txn_identity_found_dropped drops the key's versions a read of found gave. */
+static void
+txn_identity_found_dropped(void *captures)
+{
+ txn_identity_found_frame_t *frame = captures;
+
+ if (frame->versions_live)
+ txn_memtable_version_versions_drop(&frame->versions, 0);
+ frame->versions_live = false;
+}
+
+/* txn_identity_found_read is found's read: the key's versions in the memtable, then in its spills (bugs row 291). */
+static bool
+txn_identity_found_read(void *captures, uint64 *listed, HostError *error)
+{
+ txn_identity_found_frame_t *frame = captures;
+ StdVec spilled;
+
+ if (!txn_memtable_branch_identity_versions(frame->io, frame->hold, frame->deltas, frame->key, frame->key_len,
+ &frame->versions, error))
+ return false;
+ frame->versions_live = true;
+ if (!txn_spill_versions(&TXN_SPILL_IDENTITY_HOME_OPS, frame->io, frame->hold, frame->deltas,
+ TXN_RUNS_KIND_SPILLED_IDENTITY, frame->key, frame->key_len, listed, &spilled, error))
+ return false;
+ std_vec_extend_from_slice_Checked(&frame->versions, spilled.ptr, spilled.len, sizeof(TxnMemtableVersionVersion),
+ _Alignof(TxnMemtableVersionVersion));
+ std_vec_free_Checked(&spilled, sizeof(TxnMemtableVersionVersion), _Alignof(TxnMemtableVersionVersion));
+ return true;
+}
+
/*
- * txn_identity_found_body reads the key's versions and then the folded
- * tree's value, resolves the key, and for a key that exists answers the
- * newest put whose writer is the caller's transaction or committed, or the
- * folded tree's value.
+ * txn_identity_found_body reads the key's versions, the memtable's and then
+ * its spills' (bugs row 291), and then the folded tree's value, resolves the
+ * key, and for a key that exists answers the newest put whose writer is the
+ * caller's transaction or committed, or the folded tree's value.
*/
static void
txn_identity_found_body(void *frame_)
@@ -526,10 +546,9 @@ txn_identity_found_body(void *frame_)
ChunksErrorError storage;
StdBytes based;
- if (!txn_memtable_branch_identity_versions(frame->io, frame->hold, frame->deltas, frame->key, frame->key_len,
- &frame->versions, frame->error))
+ if (!txn_spill_taken(&TXN_SPILL_IDENTITY_HOME_OPS, frame->io, frame->deltas, txn_identity_found_read,
+ txn_identity_found_dropped, frame, frame->error))
return;
- frame->versions_live = true;
if (!repo_Repo_identity(frame->repo, frame->io, frame->reading, frame->key, frame->key_len, &frame->has_based,
&frame->based, &storage))
{
@@ -612,8 +631,9 @@ txn_identity_found_pad(void *frame_)
* reads its key, the caller's own changes and every committed one counted,
* with the value of the version that decides it: the newest put whose writer
* is the caller's transaction or committed, or the folded tree's value with
- * no such version. It reads the delta's versions before the folded tree, so
- * a save between the reads only folds versions it has already read.
+ * no such version. It reads the delta's versions, its spills' included,
+ * before the folded tree, so a save between the reads only folds versions it
+ * has already read.
*/
static bool
txn_identity_found(HostIoIo *io, const HostDeltaHold *hold, const RepoRepo *repo, const HostSharedReader *reading,
@@ -839,8 +859,8 @@ typedef struct txn_identity_changed_closure_4_frame_t
/*
* txn_identity_changed_closure_4_body takes the tree's stripe, answers false
* when another writer pushed under it since the caller's count, and else in
- * one checkpoint window appends each change's record and pushes its version
- * with its LSN.
+ * one checkpoint window appends each of the prepare's group's changes' records
+ * and pushes its version with its LSN.
*/
static void
txn_identity_changed_closure_4_body(void *frame_)
@@ -865,8 +885,9 @@ txn_identity_changed_closure_4_body(void *frame_)
if (!host_delay_checkpoint(HOST_IO_TAKES_SHORT_IO_DOORS(io), NULL, 0, &frame->window, frame->error))
return;
frame->window_live = true;
- for (size_t at = 0; at < changed->bodies.len; at++)
+ for (size_t i = 0; i < frame->prepared->aims_len; i++)
{
+ size_t at = frame->prepared->first + i;
const StdVec *body = &STD_VEC_AT(&changed->bodies, StdVec, at);
TxnMemtableVersionChange change = {.writer = changed->writer,
.op = &STD_VEC_AT(&changed->ops, TxnMemtableOp, at), .lock = TXN_MEMTABLE_LOCK_MODE_UPDATE,
@@ -875,7 +896,7 @@ txn_identity_changed_closure_4_body(void *frame_)
if (!host_wal_insert(HOST_IO_TAKES_SHORT_IO_DOORS(io), HOST_WAL_KIND_DATABASE_CHANGE, body->ptr, body->len,
&change.lsn, frame->error) ||
- !txn_memtable_branch_identity_finish(io, frame->prepared, at, &change, true, changed->roomed, &handle,
+ !txn_memtable_branch_identity_finish(io, frame->prepared, i, &change, true, changed->roomed, &handle,
frame->error))
return;
}
@@ -912,15 +933,16 @@ txn_identity_changed_closure_4(void *captures, HostIoIo *io, TxnMemtableTablePre
}
/*
- * txn_identity_changed_body records each pair's key in the transaction's
- * list and makes its record, key, and op, then the versions' needs, and
- * prepares the push.
+ * txn_identity_changed_body makes each pair's record, key, and op, then
+ * records each key in the transaction's list with its version's need,
+ * spilling the transaction's own identity versions past the spill threshold
+ * (bugs row 291), and prepares the push.
*/
static void
txn_identity_changed_body(void *frame_)
{
txn_identity_changed_frame_t *frame = frame_;
- HostStorage storage = txn_identity_storage(frame->repository);
+ HostStorage storage = txn_spill_identity_storage(frame->repository);
uint64 top = frame->writer.has_top ? frame->writer.top : frame->writer.xid;
TxnMemtableVersionShown command = {.tag = TXN_MEMTABLE_VERSION_SHOWN_COMMAND};
size_t len = frame->pairs.len;
@@ -949,13 +971,9 @@ txn_identity_changed_body(void *frame_)
TxnRecordDatabaseChange change = {.tree = TXN_RECORD_TREE_IDENTITY, .writer = frame->writer,
.repository = frame->repository, .key = pair->f0, .has_value = pair->has_f1,
.value = pair->f1};
- uint64 touched;
StdVec body;
TxnMemtableOp op;
- if (!txn_written_touched(frame->io, top, &storage, TXN_WRITTEN_OF_IDENTITY, pair->f0.ptr, pair->f0.len, 0,
- &touched, frame->error))
- return;
txn_record_DatabaseChange_encode(&change, &body);
std_vec_push_Checked(&frame->bodies, &body, TXN_IDENTITY_VEC);
std_vec_push_Checked(&frame->keys, &change.key, TXN_IDENTITY_VEC);
@@ -969,7 +987,18 @@ txn_identity_changed_body(void *frame_)
const StdVec *key = &STD_VEC_AT(&frame->keys, StdVec, i);
txn_TupleRefSliceU8Usize_t need = {{key->ptr, key->len},
txn_memtable_version_encoded(&frame->writer, &STD_VEC_AT(&frame->ops, TxnMemtableOp, i), command, true, 0)};
+ uint64 touched;
+ if (!txn_written_touched(frame->io, top, &storage, TXN_WRITTEN_OF_IDENTITY, key->ptr, key->len, need.f1,
+ &touched, frame->error))
+ return;
+ if (touched >= txn_spill_threshold())
+ {
+ bool spilled;
+
+ if (!txn_spill_spilled(frame->io, &storage, TXN_WRITTEN_OF_IDENTITY, top, &spilled, frame->error))
+ return;
+ }
std_vec_push_Checked(&frame->needs, &need, TXN_IDENTITY_NEED);
}
frame->ok = txn_memtable_branch_identity_prepare_Bool_Striping(frame->io, host_shared_Held_record(frame->deltas),
@@ -1003,11 +1032,14 @@ txn_identity_changed_pad(void *frame_)
* txn_identity_changed gives keys of a database's identity tree their
* values, a removal for none, in the running transaction, taking the pairs:
* it records each key in the transaction's list, which an abort prunes, then
- * in one prepare's window under the tree's stripe appends each change's
- * record and pushes its version with its LSN, every pair of a database being
+ * in the window of each of the prepare's groups under the tree's stripe
+ * appends each change's record and pushes its version with its LSN (bugs row
+ * 338), every pair of a database being
* logged. With the push count the caller read before its probe, it pushes
* nothing and answers false when another writer pushed under the stripe
- * since.
+ * since. A change with a count is at most TXN_MEMTABLE_TABLE_WHOLE pairs, one
+ * group, since a later group's check would meet the earlier groups' own
+ * pushes and start them over.
*/
static bool
txn_identity_changed(HostIoIo *io, HostIoWaits *waits, const HostSharedHeld *deltas, uint32 repository,
@@ -1018,6 +1050,7 @@ txn_identity_changed(HostIoIo *io, HostIoWaits *waits, const HostSharedHeld *del
.writer = writer, .roomed = roomed, .pairs = *pairs, .has_count = has_count, .count = count,
.result = result, .error = error};
+ Assert(!has_count || pairs->len <= TXN_MEMTABLE_TABLE_WHOLE);
std_landing(&frame, txn_identity_changed_body, txn_identity_changed_pad);
txn_identity_changed_pad(&frame);
return frame.ok;
@@ -1314,18 +1347,64 @@ txn_identity_removed(HostIoIo *io, HostIoWaits *waits, uint32 repository, uint32
return frame.ok;
}
-/* txn_identity_folded_closure_0_captures_t is folded's walk's captures: the deltas, the fold point, and the pairs. */
+/* txn_identity_folded_closure_0_captures_t is folded's walk's captures: the deltas, the fold point, the pairs, and the keys the walk read, in order. */
typedef struct txn_identity_folded_closure_0_captures_t
{
HostSharedRecord deltas;
HostLsn point;
StdVec *pairs;
+ StdVec *walked;
} txn_identity_folded_closure_0_captures_t;
+/*
+ * txn_identity_folded_key adds a key's change of its newest version, from
+ * at, that the fold applies, if any, dropping each version it passes over.
+ */
+static bool
+txn_identity_folded_key(HostIoIo *io, HostLsn point, const uint8 *key, size_t key_len, StdVec *versions, size_t *at,
+ StdVec *pairs, HostError *error)
+{
+ for (; *at < versions->len; (*at)++)
+ {
+ TxnMemtableVersionVersion *version = &STD_VEC_AT(versions, TxnMemtableVersionVersion, *at);
+ TxnFoldApplied applied;
+ TxnMemtableVersionRecord record;
+ TxnMemtableVersionOpRef op;
+ TxnIdentityPair pair = {0};
+
+ if (!txn_fold_applied(io, version, point, &applied, error))
+ return false;
+ if (applied.tag != TXN_FOLD_APPLIED_YES)
+ {
+ txn_memtable_version_Version_drop(version);
+ continue;
+ }
+ record = txn_memtable_version_Version_record(version);
+ op = txn_memtable_version_Record_op(&record);
+ if (op.tag != TXN_MEMTABLE_VERSION_OP_REF_PUT && op.tag != TXN_MEMTABLE_VERSION_OP_REF_DELETE)
+ {
+ *error = txn_identity_fork("XX001", "an identity tree's version neither puts nor deletes");
+ return false;
+ }
+ if (!std_vec_try_reserve(pairs, 1, TXN_IDENTITY_PAIR))
+ {
+ *error = host_Error_out_of_memory();
+ return false;
+ }
+ pair.has_f1 = op.tag == TXN_MEMTABLE_VERSION_OP_REF_PUT;
+ if (pair.has_f1)
+ txn_identity_bytes(op.u.put.value.ptr, op.u.put.value.len, &pair.f1);
+ txn_identity_bytes(key, key_len, &pair.f0);
+ std_vec_push_Checked(pairs, &pair, TXN_IDENTITY_PAIR);
+ return true;
+ }
+ return true;
+}
+
/*
* txn_identity_folded_closure_0_frame_t is the walk's state across its pad:
- * a key's hold, its versions from the first it hasn't dropped, and the value
- * it found, each while live.
+ * a key's hold and its versions from the first it hasn't dropped, each while
+ * live.
*/
typedef struct txn_identity_folded_closure_0_frame_t
{
@@ -1338,85 +1417,77 @@ typedef struct txn_identity_folded_closure_0_frame_t
StdVec versions;
size_t at;
bool versions_live;
- bool has_value;
- StdVec value;
- bool value_live;
HostError *error;
bool ok;
} txn_identity_folded_closure_0_frame_t;
-/* txn_identity_folded_closure_0_body adds the key's change of its newest version the fold applies, if any. */
+/* txn_identity_folded_closure_0_dropped drops the key's versions a read gave. */
+static void
+txn_identity_folded_closure_0_dropped(void *captures)
+{
+ txn_identity_folded_closure_0_frame_t *frame = captures;
+
+ if (frame->versions_live)
+ txn_memtable_version_versions_drop(&frame->versions, 0);
+ frame->versions_live = false;
+}
+
+/* txn_identity_folded_closure_0_read reads the key's memtable versions, then its spills' below them. */
+static bool
+txn_identity_folded_closure_0_read(void *captures, uint64 *listed, HostError *error)
+{
+ txn_identity_folded_closure_0_frame_t *frame = captures;
+ StdVec spilled;
+
+ if (!txn_memtable_branch_identity_versions(frame->io, &frame->hold, frame->walk->deltas, frame->key,
+ frame->key_len, &frame->versions, error))
+ return false;
+ frame->versions_live = true;
+ if (!txn_spill_versions(&TXN_SPILL_IDENTITY_HOME_OPS, frame->io, &frame->hold, frame->walk->deltas,
+ TXN_RUNS_KIND_SPILLED_IDENTITY, frame->key, frame->key_len, listed, &spilled, error))
+ return false;
+ std_vec_extend_from_slice_Checked(&frame->versions, spilled.ptr, spilled.len, sizeof(TxnMemtableVersionVersion),
+ _Alignof(TxnMemtableVersionVersion));
+ std_vec_free_Checked(&spilled, sizeof(TxnMemtableVersionVersion), _Alignof(TxnMemtableVersionVersion));
+ return true;
+}
+
+/* txn_identity_folded_closure_0_body reads the key's memtable versions, then its spills' below them, keeps the key as walked, and adds its change. */
static void
txn_identity_folded_closure_0_body(void *frame_)
{
txn_identity_folded_closure_0_frame_t *frame = frame_;
txn_identity_folded_closure_0_captures_t *walk = frame->walk;
+ StdVec key;
if (!host_delta_hold(frame->io, &frame->hold, frame->error))
return;
frame->hold_live = true;
- if (!txn_memtable_branch_identity_versions(frame->io, &frame->hold, walk->deltas, frame->key, frame->key_len,
- &frame->versions, frame->error))
+ if (!txn_spill_taken(&TXN_SPILL_IDENTITY_HOME_OPS, frame->io, walk->deltas, txn_identity_folded_closure_0_read,
+ txn_identity_folded_closure_0_dropped, frame, frame->error))
return;
- frame->versions_live = true;
- for (; frame->at < frame->versions.len; frame->at++)
+ if (!std_vec_try_reserve(walk->walked, 1, TXN_IDENTITY_VEC))
{
- TxnMemtableVersionVersion *version = &STD_VEC_AT(&frame->versions, TxnMemtableVersionVersion, frame->at);
- TxnFoldApplied applied;
- TxnMemtableVersionRecord record;
- TxnMemtableVersionOpRef op;
- TxnIdentityPair pair;
-
- if (!txn_fold_applied(frame->io, version, walk->point, &applied, frame->error))
- return;
- if (applied.tag != TXN_FOLD_APPLIED_YES)
- {
- txn_memtable_version_Version_drop(version);
- continue;
- }
- record = txn_memtable_version_Version_record(version);
- op = txn_memtable_version_Record_op(&record);
- switch (op.tag)
- {
- case TXN_MEMTABLE_VERSION_OP_REF_PUT:
- txn_identity_bytes(op.u.put.value.ptr, op.u.put.value.len, &frame->value);
- frame->has_value = frame->value_live = true;
- break;
- case TXN_MEMTABLE_VERSION_OP_REF_DELETE:
- break;
- default:
- *frame->error = txn_identity_fork("XX001", "an identity tree's version neither puts nor deletes");
- return;
- }
- if (!std_vec_try_reserve(walk->pairs, 1, TXN_IDENTITY_PAIR))
- {
- *frame->error = host_Error_out_of_memory();
- return;
- }
- txn_identity_bytes(frame->key, frame->key_len, &pair.f0);
- pair.has_f1 = frame->has_value;
- if (frame->has_value)
- pair.f1 = frame->value;
- std_vec_push_Checked(walk->pairs, &pair, TXN_IDENTITY_PAIR);
- frame->value_live = false;
- break;
+ *frame->error = host_Error_out_of_memory();
+ return;
}
- frame->ok = true;
+ txn_identity_bytes(frame->key, frame->key_len, &key);
+ std_vec_push_Checked(walk->walked, &key, TXN_IDENTITY_VEC);
+ frame->ok = txn_identity_folded_key(frame->io, walk->point, frame->key, frame->key_len, &frame->versions,
+ &frame->at, walk->pairs, frame->error);
}
-/* txn_identity_folded_closure_0_pad frees what the walk holds live: its value, its versions, and its hold. */
+/* txn_identity_folded_closure_0_pad frees what the walk holds live: its versions and its hold. */
static void
txn_identity_folded_closure_0_pad(void *frame_)
{
txn_identity_folded_closure_0_frame_t *frame = frame_;
- if (frame->value_live)
- std_vec_free_Checked(&frame->value, TXN_IDENTITY_BYTES);
if (frame->versions_live)
txn_memtable_version_versions_drop(&frame->versions, frame->at);
if (frame->hold_live)
host_delta_Hold_drop(&frame->hold);
- frame->value_live = frame->versions_live = frame->hold_live = false;
+ frame->versions_live = frame->hold_live = false;
}
/* txn_identity_folded_closure_0 is folded's walk's closure, of one key of the delta. */
@@ -1432,7 +1503,11 @@ txn_identity_folded_closure_0(void *captures, HostIoIo *io, const uint8 *key, si
return frame.ok;
}
-/* txn_identity_folded_frame_t is folded's state across its pad: its deltas and its pairs, each while live. */
+/*
+ * txn_identity_folded_frame_t is folded's state across its pad: its deltas,
+ * its pairs, the keys its walk read, and the spills' next key, its versions,
+ * and its hold, each while live.
+ */
typedef struct txn_identity_folded_frame_t
{
HostIoIo *io;
@@ -1442,18 +1517,44 @@ typedef struct txn_identity_folded_frame_t
bool deltas_live;
StdVec pairs;
bool pairs_live;
+ StdVec walked;
+ bool walked_live;
+ HostDeltaHold hold;
+ bool hold_live;
+ bool has_after;
+ StdVec after;
+ txn_TupleVecU8VecVersion_t next;
+ size_t at;
+ bool next_live;
StdVec *result;
HostError *error;
bool ok;
} txn_identity_folded_frame_t;
-/* txn_identity_folded_body walks the tree's delta, if the database has one, for each key's folded change. */
+/* txn_identity_folded_walked_cmp orders a key among the walked keys by its bytes. */
+static int
+txn_identity_folded_walked_cmp(const void *a, const void *b)
+{
+ const StdVec *x = a;
+ const StdVec *y = b;
+
+ return std_bytes_Ord_cmp(x->ptr, x->len, y->ptr, y->len);
+}
+
+/*
+ * txn_identity_folded_body walks the tree's delta, if the database has one,
+ * for each key's folded change, and then, when a listed identity spill's
+ * transaction is one the point applies, the keys its identity spills alone
+ * hold (bugs row 291).
+ */
static void
txn_identity_folded_body(void *frame_)
{
txn_identity_folded_frame_t *frame = frame_;
- txn_identity_folded_closure_0_captures_t walk = {.point = frame->point, .pairs = &frame->pairs};
+ txn_identity_folded_closure_0_captures_t walk = {.point = frame->point, .pairs = &frame->pairs,
+ .walked = &frame->walked};
bool some;
+ bool applies;
if (!txn_memtable_branch_find(HOST_SHARED_IO_WITHIN(frame->io), frame->repository, &some, &frame->deltas,
frame->error))
@@ -1467,33 +1568,78 @@ txn_identity_folded_body(void *frame_)
}
frame->deltas_live = true;
frame->pairs_live = true;
+ std_vec_new(&frame->walked, TXN_IDENTITY_VEC);
+ frame->walked_live = true;
walk.deltas = host_shared_Held_record(&frame->deltas);
if (!txn_memtable_table_walked(&TXN_MEMTABLE_BRANCH_IDENTITY_TREE_OPS, frame->io, walk.deltas,
txn_identity_folded_closure_0, &walk, NULL, frame->error))
return;
+ if (!txn_spill_identity_applies(frame->io, walk.deltas, frame->point, &applies, frame->error))
+ return;
+ qsort(frame->walked.ptr, frame->walked.len, sizeof(StdVec), txn_identity_folded_walked_cmp);
+ while (applies)
+ {
+ if (!host_delta_hold(frame->io, &frame->hold, frame->error))
+ return;
+ frame->hold_live = true;
+ if (!txn_spill_next(&TXN_SPILL_IDENTITY_HOME_OPS, frame->io, &frame->hold, walk.deltas,
+ TXN_RUNS_KIND_SPILLED_IDENTITY, frame->has_after, frame->after.ptr, frame->after.len,
+ TXN_MEMTABLE_TABLE_DIRECTION_FORWARD, NULL, &some, &frame->next, frame->error))
+ return;
+ if (!some)
+ break;
+ frame->next_live = true;
+ frame->at = 0;
+ if (bsearch(&frame->next.f0, frame->walked.ptr, frame->walked.len, sizeof(StdVec),
+ txn_identity_folded_walked_cmp) == NULL &&
+ !txn_identity_folded_key(frame->io, frame->point, frame->next.f0.ptr, frame->next.f0.len,
+ &frame->next.f1, &frame->at, &frame->pairs, frame->error))
+ return;
+ if (frame->has_after)
+ std_vec_free_Checked(&frame->after, TXN_IDENTITY_BYTES);
+ frame->after = frame->next.f0;
+ frame->has_after = true;
+ txn_memtable_version_versions_drop(&frame->next.f1, frame->at);
+ frame->next_live = false;
+ frame->hold_live = false;
+ host_delta_Hold_drop(&frame->hold);
+ }
*frame->result = frame->pairs;
frame->pairs_live = false;
frame->ok = true;
}
-/* txn_identity_folded_pad frees what folded holds live: its pairs and its deltas. */
+/* txn_identity_folded_pad frees what folded holds live: the spills' next key and its hold, the walked keys, its pairs, and its deltas. */
static void
txn_identity_folded_pad(void *frame_)
{
txn_identity_folded_frame_t *frame = frame_;
+ if (frame->next_live)
+ {
+ txn_memtable_version_versions_drop(&frame->next.f1, frame->at);
+ std_vec_free_Checked(&frame->next.f0, TXN_IDENTITY_BYTES);
+ }
+ if (frame->has_after)
+ std_vec_free_Checked(&frame->after, TXN_IDENTITY_BYTES);
+ if (frame->hold_live)
+ host_delta_Hold_drop(&frame->hold);
+ if (frame->walked_live)
+ std_vec_drop_Checked(&frame->walked, TXN_IDENTITY_VEC, txn_identity_bytes_drop);
if (frame->pairs_live)
std_vec_drop_Checked(&frame->pairs, TXN_IDENTITY_PAIR, txn_identity_Pair_drop);
if (frame->deltas_live)
host_shared_Held_drop(&frame->deltas);
- frame->pairs_live = frame->deltas_live = false;
+ frame->next_live = frame->has_after = frame->hold_live = frame->walked_live = frame->pairs_live =
+ frame->deltas_live = false;
}
/*
* txn_identity_folded returns what a save at a fold point folds of a
- * database's identity tree: for each key of the tree's delta, the change of
- * its newest version the fold applies, as the key's value or none for its
- * removal, which the save makes only where the folded tree holds another.
+ * database's identity tree: for each key of the tree's delta and its spills,
+ * the change of its newest version the fold applies, as the key's value or
+ * none for its removal, which the save makes only where the folded tree holds
+ * another.
*/
bool
txn_identity_folded(HostIoIo *io, uint32 repository, HostLsn point, StdVec *result, HostError *error)
@@ -1510,10 +1656,10 @@ txn_identity_folded(HostIoIo *io, uint32 repository, HostLsn point, StdVec *resu
static void
txn_identity_folded_drop(void *folded_)
{
- txn_TupleVecU8VecTupleHandleOptionU64_t *folded = folded_;
+ txn_TupleVecU8VecHandle_t *folded = folded_;
std_vec_free_Checked(&folded->f0, TXN_IDENTITY_BYTES);
- std_vec_free_Checked(&folded->f1, TXN_IDENTITY_FOLDED_VERSION);
+ std_vec_free_Checked(&folded->f1, TXN_IDENTITY_HANDLE);
}
/*
@@ -1558,12 +1704,12 @@ txn_identity_dropped_closure_0_body(void *frame_)
{
txn_identity_dropped_closure_0_frame_t *frame = frame_;
txn_identity_dropped_closure_0_captures_t *walk = frame->walk;
- txn_TupleVecU8VecTupleHandleOptionU64_t folded;
+ txn_TupleVecU8VecHandle_t folded;
if (!host_delta_hold(frame->io, &frame->hold, frame->error))
return;
frame->hold_live = true;
- std_vec_new(&frame->applied, TXN_IDENTITY_FOLDED_VERSION);
+ std_vec_new(&frame->applied, TXN_IDENTITY_HANDLE);
frame->applied_live = true;
if (!txn_memtable_branch_identity_versions(frame->io, &frame->hold, walk->deltas, frame->key, frame->key_len,
&frame->versions, frame->error))
@@ -1578,15 +1724,12 @@ txn_identity_dropped_closure_0_body(void *frame_)
return;
if (applied.tag == TXN_FOLD_APPLIED_YES)
{
- txn_TupleHandleOptionU64_t top = {.f0 = version->handle, .has_f1 = applied.u.yes.has_top,
- .f1 = applied.u.yes.top};
-
- if (!std_vec_try_reserve(&frame->applied, 1, TXN_IDENTITY_FOLDED_VERSION))
+ if (!std_vec_try_reserve(&frame->applied, 1, TXN_IDENTITY_HANDLE))
{
*frame->error = host_Error_out_of_memory();
return;
}
- std_vec_push_Checked(&frame->applied, &top, TXN_IDENTITY_FOLDED_VERSION);
+ std_vec_push_Checked(&frame->applied, &version->handle, TXN_IDENTITY_HANDLE);
}
txn_memtable_version_Version_drop(version);
}
@@ -1614,7 +1757,7 @@ txn_identity_dropped_closure_0_pad(void *frame_)
if (frame->versions_live)
txn_memtable_version_versions_drop(&frame->versions, frame->at);
if (frame->applied_live)
- std_vec_free_Checked(&frame->applied, TXN_IDENTITY_FOLDED_VERSION);
+ std_vec_free_Checked(&frame->applied, TXN_IDENTITY_HANDLE);
if (frame->hold_live)
host_delta_Hold_drop(&frame->hold);
frame->versions_live = frame->applied_live = frame->hold_live = false;
@@ -1648,18 +1791,15 @@ typedef struct txn_identity_dropped_frame_t
StdVec folded;
size_t at;
bool folded_live;
- StdVec handles;
- bool handles_live;
- StdVec taken;
- bool taken_live;
+ bool *took;
HostError *error;
bool ok;
} txn_identity_dropped_frame_t;
/*
* txn_identity_dropped_body lists each key's versions the fold applied, then
- * takes each key's out of its chain and gives each taken one's count back at
- * its top-level transaction's entry.
+ * takes each key's out of its chain, the remover giving back each counted
+ * one's count at its top-level transaction's entry.
*/
static void
txn_identity_dropped_body(void *frame_)
@@ -1667,7 +1807,9 @@ txn_identity_dropped_body(void *frame_)
txn_identity_dropped_frame_t *frame = frame_;
txn_identity_dropped_closure_0_captures_t walk = {.point = frame->point, .folded = &frame->folded};
bool some;
+ bool files;
+ *frame->took = false;
if (!txn_memtable_branch_find(HOST_SHARED_IO_WITHIN(frame->io), frame->repository, &some, &frame->deltas,
frame->error))
return;
@@ -1683,68 +1825,37 @@ txn_identity_dropped_body(void *frame_)
if (!txn_memtable_table_walked(&TXN_MEMTABLE_BRANCH_IDENTITY_TREE_OPS, frame->io, walk.deltas,
txn_identity_dropped_closure_0, &walk, NULL, frame->error))
return;
+ *frame->took = frame->folded.len > 0;
for (; frame->at < frame->folded.len; frame->at++)
{
- txn_TupleVecU8VecTupleHandleOptionU64_t *key = &STD_VEC_AT(&frame->folded,
- txn_TupleVecU8VecTupleHandleOptionU64_t,
- frame->at);
- size_t zipped;
-
- std_vec_with_capacity_Checked(&frame->handles, key->f1.len, TXN_IDENTITY_HANDLE);
- frame->handles_live = true;
- for (size_t i = 0; i < key->f1.len; i++)
- std_vec_push_Checked(&frame->handles, &STD_VEC_AT(&key->f1, txn_TupleHandleOptionU64_t, i).f0,
- TXN_IDENTITY_HANDLE);
+ txn_TupleVecU8VecHandle_t *key = &STD_VEC_AT(&frame->folded, txn_TupleVecU8VecHandle_t, frame->at);
+ StdVec taken;
+
if (!txn_memtable_table_removed(&TXN_MEMTABLE_BRANCH_IDENTITY_TREE_OPS, frame->io, walk.deltas, key->f0.ptr,
- key->f0.len, frame->handles.ptr, frame->handles.len, &frame->taken,
- frame->error))
+ key->f0.len, key->f1.ptr, key->f1.len, &taken, frame->error))
return;
- frame->taken_live = true;
- zipped = Min(key->f1.len, frame->taken.len);
- for (size_t i = 0; i < zipped; i++)
- {
- const txn_TupleHandleOptionU64_t *top = &STD_VEC_AT(&key->f1, txn_TupleHandleOptionU64_t, i);
- HostXid xid = {top->f1};
- bool has_added;
- uint64 added;
-
- if (!STD_VEC_AT(&frame->taken, bool, i) || !top->has_f1)
- continue;
- if (!txn_status_add(frame->io, xid, PG_UINT64_MAX, &has_added, &added, frame->error))
- return;
- }
- frame->taken_live = false;
- std_vec_free_Checked(&frame->taken, sizeof(bool), _Alignof(bool));
- frame->handles_live = false;
- std_vec_free_Checked(&frame->handles, TXN_IDENTITY_HANDLE);
+ std_vec_free_Checked(&taken, sizeof(bool), _Alignof(bool));
txn_identity_folded_drop(key);
}
- frame->ok = true;
+ frame->ok = txn_spill_identity_folded(frame->io, walk.deltas, frame->point, &files, frame->error);
+ *frame->took = *frame->took || files;
}
-/*
- * txn_identity_dropped_pad frees what dropped holds live: a key's removal and
- * handles, the folded keys left, and its deltas.
- */
+/* txn_identity_dropped_pad frees what dropped holds live: the folded keys left and its deltas. */
static void
txn_identity_dropped_pad(void *frame_)
{
txn_identity_dropped_frame_t *frame = frame_;
- if (frame->taken_live)
- std_vec_free_Checked(&frame->taken, sizeof(bool), _Alignof(bool));
- if (frame->handles_live)
- std_vec_free_Checked(&frame->handles, TXN_IDENTITY_HANDLE);
if (frame->folded_live)
{
for (; frame->at < frame->folded.len; frame->at++)
- txn_identity_folded_drop(&STD_VEC_AT(&frame->folded, txn_TupleVecU8VecTupleHandleOptionU64_t,
- frame->at));
+ txn_identity_folded_drop(&STD_VEC_AT(&frame->folded, txn_TupleVecU8VecHandle_t, frame->at));
std_vec_free_Checked(&frame->folded, TXN_IDENTITY_FOLDED);
}
if (frame->deltas_live)
host_shared_Held_drop(&frame->deltas);
- frame->taken_live = frame->handles_live = frame->folded_live = frame->deltas_live = false;
+ frame->folded_live = frame->deltas_live = false;
}
/*
@@ -1754,12 +1865,14 @@ txn_identity_dropped_pad(void *frame_)
* one's count back at its top-level transaction's entry, so the folded pairs
* live in the manifest's tree alone. A reader reads the delta and then the
* newest manifest (found), so no reader tells the two apart once the save is
- * durable, and no horizon is waited for.
+ * durable, and no horizon is waited for. It answers whether it took a
+ * version or an identity spill out (bugs row 340).
*/
bool
-txn_identity_dropped(HostIoIo *io, uint32 repository, HostLsn point, HostError *error)
+txn_identity_dropped(HostIoIo *io, uint32 repository, HostLsn point, bool *took, HostError *error)
{
- txn_identity_dropped_frame_t frame = {.io = io, .repository = repository, .point = point, .error = error};
+ txn_identity_dropped_frame_t frame = {.io = io, .repository = repository, .point = point, .took = took,
+ .error = error};
std_landing(&frame, txn_identity_dropped_body, txn_identity_dropped_pad);
txn_identity_dropped_pad(&frame);
@@ -1807,7 +1920,12 @@ typedef struct txn_identity_redo_frame_t
bool ok;
} txn_identity_redo_frame_t;
-/* txn_identity_redo_body pushes the replayed change's version unless the delta holds it already. */
+/*
+ * txn_identity_redo_body pushes the replayed change's version unless the
+ * delta, its spills included, holds it already, counting it toward its
+ * transaction's replayed bytes, which spill past the threshold (bugs row
+ * 291).
+ */
static void
txn_identity_redo_body(void *frame_)
{
@@ -1820,13 +1938,19 @@ txn_identity_redo_body(void *frame_)
TxnMemtableVersionChange pushed;
txn_TupleRefSliceU8Usize_t needs;
TxnMemtableVersionHandle handle;
+ HostStorage storage = txn_spill_identity_storage(change->repository);
+ uint64 top = change->writer.has_top ? change->writer.top : change->writer.xid;
+ uint64 counted;
+ bool spilled;
if (!txn_memtable_branch_deltas(frame->io, change->repository, frame->own, &frame->deltas, frame->error))
return;
frame->deltas_live = true;
deltas = host_shared_Held_record(&frame->deltas);
if (!txn_memtable_branch_identity_holds(frame->io, deltas, change->key.ptr, change->key.len, frame->lsn, &holds,
- frame->error))
+ frame->error) ||
+ (!holds && !txn_spill_holds(&TXN_SPILL_IDENTITY_HOME_OPS, frame->io, deltas, TXN_RUNS_KIND_SPILLED_IDENTITY,
+ change->key.ptr, change->key.len, frame->lsn, &holds, frame->error)))
return;
if (holds)
{
@@ -1841,6 +1965,10 @@ txn_identity_redo_body(void *frame_)
needs.f0.ptr = change->key.ptr;
needs.f0.len = change->key.len;
needs.f1 = txn_memtable_version_encoded(&change->writer, &frame->op, replayed, true, frame->lsn);
+ if (!txn_spill_replayed(frame->io, &storage, TXN_WRITTEN_OF_IDENTITY, top, needs.f1, &counted, frame->error) ||
+ (counted >= txn_spill_threshold() &&
+ !txn_spill_spilled(frame->io, &storage, TXN_WRITTEN_OF_IDENTITY, top, &spilled, frame->error)))
+ return;
pushed.writer = change->writer;
pushed.op = &frame->op;
pushed.lock = TXN_MEMTABLE_LOCK_MODE_UPDATE;
@@ -1873,7 +2001,7 @@ txn_identity_redo_pad(void *frame_)
* txn_identity_redo pushes a replayed identity change into its database's
* working state's delta, taking it, in the startup process or on a standby,
* with no catalog access, unless the checkpoint's runs brought it back,
- * which the delta holds by its key and the record's LSN.
+ * which the delta or its spills hold by its key and the record's LSN.
*/
bool
txn_identity_redo(HostIoIo *io, TxnRecordDatabaseChange *change, uint32 own, uint64 lsn, HostError *error)
@@ -1885,3 +2013,862 @@ txn_identity_redo(HostIoIo *io, TxnRecordDatabaseChange *change, uint32 own, uin
txn_identity_redo_pad(&frame);
return frame.ok;
}
+
+#ifdef DOLTGRES_TEST_TXN
+/* TXN_IDENTITY_TESTS_UNWRAP is Result::unwrap's panic text. */
+#define TXN_IDENTITY_TESTS_UNWRAP "called `Result::unwrap()` on an `Err` value"
+
+/* TXN_IDENTITY_TESTS_SCHEMA is the parent id the tests' tables derive their ids under. */
+#define TXN_IDENTITY_TESTS_SCHEMA (TXN_IDENTITY_OBJECT_ID_BIT | 5)
+
+/* txn_identity_tests_ok unwraps a host result at a line. */
+static void
+txn_identity_tests_ok(bool ok, HostError *error, uint32 line)
+{
+ if (!ok)
+ STD_RESULT_EXPECT_FAILED(line, 0, TXN_IDENTITY_TESTS_UNWRAP, error, host_Error_Debug_fmt);
+}
+
+/* txn_identity_tests_derived returns an object's derived id, which unwraps at a line. */
+static uint64
+txn_identity_tests_derived(TxnIdentityKind kind, uint64 parent, const char *name, size_t name_len, uint32 line)
+{
+ uint64 id;
+ HostError error;
+
+ txn_identity_tests_ok(txn_identity_derive_object_id(kind, parent, (const uint8 *) name, name_len, &id, &error),
+ &error, line);
+ return id;
+}
+
+/* txn_identity_tests_eq reports whether a key is the concatenation of a prefix and a big-endian number's bytes. */
+static bool
+txn_identity_tests_eq(const StdVec *key, const uint8 *prefix, size_t prefix_len, uint64 number, size_t number_len)
+{
+ if (key->len != prefix_len + number_len || memcmp(key->ptr, prefix, prefix_len) != 0)
+ return false;
+ for (size_t k = 0; k < number_len; k++)
+ if (((const uint8 *) key->ptr)[prefix_len + k] != (uint8) (number >> (8 * (number_len - 1 - k))))
+ return false;
+ return true;
+}
+
+/* txn_identity_tests_starts reports whether a key starts with a prefix's bytes. */
+static bool
+txn_identity_tests_starts(const StdVec *key, const StdVec *prefix)
+{
+ return key->len >= prefix->len && memcmp(key->ptr, prefix->ptr, prefix->len) == 0;
+}
+
+/*
+ * test_txn_identity_tests_an_objects_id_derives_from_its_kind_parent_and_name:
+ * §II.12's derivation, the same kind, parent, and name give the same id, with
+ * the user bit set; another name or parent gives another; a taken id moves
+ * to the next free one.
+ */
+void
+test_txn_identity_tests_an_objects_id_derives_from_its_kind_parent_and_name(void)
+{
+ uint64 schema = txn_identity_tests_derived(TXN_IDENTITY_KIND_SCHEMA, 0, "public", 6, __LINE__);
+ uint64 table = txn_identity_tests_derived(TXN_IDENTITY_KIND_RELATION, schema, "t", 1, __LINE__);
+
+ STD_ASSERT_MSG(__LINE__, 9, (schema & TXN_IDENTITY_OBJECT_ID_BIT) == TXN_IDENTITY_OBJECT_ID_BIT,
+ "schema & OBJECT_ID_BIT == OBJECT_ID_BIT");
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_derived(TXN_IDENTITY_KIND_SCHEMA, 0, "public", 6, __LINE__) == schema,
+ "the same derivation gives the same id");
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_derived(TXN_IDENTITY_KIND_RELATION, schema, "u", 1, __LINE__) !=
+ table, "another name gives another id");
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_derived(TXN_IDENTITY_KIND_RELATION, table, "t", 1, __LINE__) !=
+ table, "another parent gives another id");
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_next_id(table) == ((table + 1) | TXN_IDENTITY_OBJECT_ID_BIT),
+ "next_id(table) == (table + 1) | OBJECT_ID_BIT");
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_next_id(PG_UINT64_MAX) == TXN_IDENTITY_OBJECT_ID_BIT,
+ "next_id(u64::MAX) == OBJECT_ID_BIT");
+}
+
+/* txn_identity_tests_column returns a column's derived id under parent 7, by its name and category. */
+static uint64
+txn_identity_tests_column(uint8 category)
+{
+ StdVec name;
+ uint64 id;
+
+ txn_identity_column_name((const uint8 *) "a", 1, category, &name);
+ id = txn_identity_tests_derived(TXN_IDENTITY_KIND_COLUMN, 7, name.ptr, name.len, __LINE__);
+ std_vec_free_Checked(&name, 1, 1);
+ return id;
+}
+
+/*
+ * test_txn_identity_tests_a_columns_id_holds_its_types_category: a column's
+ * name for its id holds its type's category, so the same name with another
+ * category is another column, and the same name and category the same
+ * column.
+ */
+void
+test_txn_identity_tests_a_columns_id_holds_its_types_category(void)
+{
+ uint64 numeric = txn_identity_tests_column('N');
+ uint64 text = txn_identity_tests_column('S');
+
+ STD_ASSERT_MSG(__LINE__, 9, numeric !=text, "int != text");
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_column('N') == numeric, "the same name and category, the same id");
+}
+
+/*
+ * test_txn_identity_tests_a_pairs_keys_find_it_from_either_side: the two key
+ * forms of a pair, and a table's columns' prefix, which every column key of
+ * the table starts with.
+ */
+void
+test_txn_identity_tests_a_pairs_keys_find_it_from_either_side(void)
+{
+ static const uint8 by_id_prefix[] = {1, 2};
+ static const uint8 by_oid_prefix[] = {2, 2};
+ static const uint8 column_prefix[] = {2, 3, 0, 0, 0, 0, 0, 0, 0, 9};
+ StdVec key;
+ StdVec column;
+ StdVec prefix;
+
+ txn_identity_by_id(TXN_IDENTITY_KIND_RELATION, 5, &key);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_eq(&key, by_id_prefix, 2, 5, 8),
+ "by_id(Kind::Relation, 5) == [1, 2] ++ 5u64.to_be_bytes()");
+ std_vec_free_Checked(&key, 1, 1);
+ txn_identity_by_oid(TXN_IDENTITY_KIND_RELATION, 16384, &key);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_eq(&key, by_oid_prefix, 2, 16384, 4),
+ "by_oid(Kind::Relation, 16384) == [2, 2] ++ 16384u32.to_be_bytes()");
+ std_vec_free_Checked(&key, 1, 1);
+ txn_identity_column_by_number(9, 3, &column);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_eq(&column, column_prefix, sizeof(column_prefix), 3, 2),
+ "column_by_number(9, 3) == [2, 3] ++ 9u64.to_be_bytes() ++ 3u16.to_be_bytes()");
+ txn_identity_columns(9, false, &prefix);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_starts(&column, &prefix), "column.starts_with(&columns(9, false))");
+ std_vec_free_Checked(&prefix, 1, 1);
+ std_vec_free_Checked(&column, 1, 1);
+ txn_identity_column_by_id(9, 11, &column);
+ txn_identity_columns(9, true, &prefix);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_starts(&column, &prefix),
+ "column_by_id(9, 11).starts_with(&columns(9, true))");
+ std_vec_free_Checked(&prefix, 1, 1);
+ std_vec_free_Checked(&column, 1, 1);
+}
+
+/* txn_identity_tests_writing_t is writing's (Writer, Roomed): the running transaction's writer and its entry's room. */
+typedef struct txn_identity_tests_writing_t
+{
+ TxnMemtableVersionWriter writer;
+ TxnStatusRoomed roomed;
+} txn_identity_tests_writing_t;
+
+/* txn_identity_tests_writing returns the running transaction's writer at its current command, with its entry's room taken. */
+static txn_identity_tests_writing_t
+txn_identity_tests_writing(HostIoIo *io)
+{
+ txn_identity_tests_writing_t writing = {0};
+ HostXid sub;
+ HostXid top;
+ HostError error;
+
+ txn_identity_tests_ok(host_current_writer(HOST_IO_IO_IO_DOORS(io), &sub, &top, &error) &&
+ txn_status_roomed(io, top, &writing.roomed, &error), &error, __LINE__);
+ writing.writer.xid = sub.f0;
+ writing.writer.has_top = sub.f0 != top.f0;
+ writing.writer.top = top.f0;
+ return writing;
+}
+
+/* txn_identity_tests_committed commits the running transaction as Postgres' commit leaves its entry once the commit's slot is settled: committed at the WAL's end. */
+static void
+txn_identity_tests_committed(HostIoIo *io)
+{
+ HostXid sub;
+ HostXid top;
+ TxnStatusEntry entry;
+ TxnStatusOutcome to = {.tag = TXN_STATUS_OUTCOME_COMMITTED};
+ bool has;
+ bool settled = false;
+ HostError error;
+
+ txn_identity_tests_ok(host_current_writer(HOST_IO_IO_IO_DOORS(io), &sub, &top, &error), &error, __LINE__);
+ simc_end(true);
+ txn_identity_tests_ok(txn_status_lookup(io, top, &has, &entry, &error), &error, __LINE__);
+ STD_ASSERT_MSG(__LINE__, 0, has, "status::lookup(io, top).unwrap().unwrap()");
+ txn_identity_tests_ok(host_fold_point(HOST_IO_IO_IO_DOORS(io), &to.u.committed, &error) &&
+ txn_status_settled(io, top, entry.outcome, to, &settled, &error), &error, __LINE__);
+ STD_ASSERT_MSG(__LINE__, 9, settled, "status::settled(io, top, entry.outcome, Committed(end)).unwrap()");
+}
+
+/* txn_identity_tests_repository makes a test's database's repository and returns its number. */
+static uint32
+txn_identity_tests_repository(HostIoIo *io, uint32 number)
+{
+ RepoRepo repo;
+ ChunksErrorError failed;
+
+ if (!repo_Repo_create(io, number, HOST_PG_DEFAULT, &repo, &failed))
+ STD_RESULT_EXPECT_FAILED(__LINE__, 0, TXN_IDENTITY_TESTS_UNWRAP, &failed, chunks_error_Error_Debug_fmt);
+ repo_Repo_drop(&repo);
+ return number;
+}
+
+/* txn_identity_tests_opened_t is opened's database: the repository opened, a reader, and its number. */
+typedef struct txn_identity_tests_opened_t
+{
+ RepoRepo repo;
+ HostSharedReader reading;
+ uint32 repository;
+} txn_identity_tests_opened_t;
+
+/* txn_identity_tests_opened opens a test's database's repository and a reader, the identity tree as its readers reach it. */
+static txn_identity_tests_opened_t
+txn_identity_tests_opened(HostIoIo *io, uint32 repository)
+{
+ txn_identity_tests_opened_t opened = {.repository = repository};
+ ChunksErrorError failed;
+ HostError error;
+
+ if (!repo_Repo_open(io, repository, HOST_PG_DEFAULT, &opened.repo, &failed))
+ STD_RESULT_EXPECT_FAILED(__LINE__, 0, TXN_IDENTITY_TESTS_UNWRAP, &failed, chunks_error_Error_Debug_fmt);
+ txn_identity_tests_ok(host_shared_read(io, &opened.reading, &error), &error, __LINE__);
+ return opened;
+}
+
+/* txn_identity_tests_opened_database returns an opened repository as the identity tree's database. */
+static TxnIdentityDatabase
+txn_identity_tests_opened_database(const txn_identity_tests_opened_t *opened)
+{
+ TxnIdentityDatabase database = {&opened->repo, &opened->reading, opened->repository};
+
+ return database;
+}
+
+/* txn_identity_tests_opened_drop frees what opened returned. */
+static void
+txn_identity_tests_opened_drop(txn_identity_tests_opened_t *opened)
+{
+ host_shared_Reader_drop(&opened->reading);
+ repo_Repo_drop(&opened->repo);
+}
+
+/* txn_identity_tests_keyed is a table's keyed closure: an id's pair by id, as a relation. */
+static void
+txn_identity_tests_keyed(const void *captures, uint64 id, StdVec *result)
+{
+ (void) captures;
+ txn_identity_by_id(TXN_IDENTITY_KIND_RELATION, id, result);
+}
+
+/* txn_identity_tests_table records a table's pairs under SCHEMA in the running transaction and returns its id. */
+static uint64
+txn_identity_tests_table(HostIoIo *io, uint32 repository, const char *name, uint32 oid)
+{
+ txn_identity_tests_writing_t written = txn_identity_tests_writing(io);
+ txn_identity_tests_opened_t opened = txn_identity_tests_opened(io, repository);
+ HostIoWaits waits = host_io_Waits_testing();
+ StdVec numbered;
+ uint64 id;
+ HostError error;
+
+ txn_identity_by_oid(TXN_IDENTITY_KIND_RELATION, oid, &numbered);
+ txn_identity_tests_ok(txn_identity_derived(io, &waits, txn_identity_tests_opened_database(&opened), written.writer,
+ written.roomed, TXN_IDENTITY_KIND_RELATION, TXN_IDENTITY_TESTS_SCHEMA,
+ (const uint8 *) name, strlen(name), numbered.ptr, numbered.len, oid,
+ txn_identity_tests_keyed, NULL, NULL, &id, &error), &error, __LINE__);
+ std_vec_free_Checked(&numbered, 1, 1);
+ txn_identity_tests_opened_drop(&opened);
+ return id;
+}
+
+/* txn_identity_tests_read reads a key of a database's identity tree for the running transaction, answering whether it holds one, into value when given. */
+static bool
+txn_identity_tests_read(HostIoIo *io, uint32 repository, const StdVec *key, StdVec *value)
+{
+ txn_identity_tests_opened_t opened = txn_identity_tests_opened(io, repository);
+ HostIoWaits waits = host_io_Waits_testing();
+ StdVec read;
+ bool has;
+ HostError error;
+
+ txn_identity_tests_ok(txn_identity_looked_up(io, &waits, txn_identity_tests_opened_database(&opened), key->ptr,
+ key->len, &has, &read, &error), &error, __LINE__);
+ txn_identity_tests_opened_drop(&opened);
+ if (has && value != NULL)
+ *value = read;
+ else if (has)
+ std_vec_free_Checked(&read, 1, 1);
+ return has;
+}
+
+/* txn_identity_tests_read_is reports whether a key of a database's identity tree reads as some bytes. */
+static bool
+txn_identity_tests_read_is(HostIoIo *io, uint32 repository, const StdVec *key, const uint8 *want, size_t want_len)
+{
+ StdVec value;
+ bool is;
+
+ if (!txn_identity_tests_read(io, repository, key, &value))
+ return false;
+ is = value.len == want_len && memcmp(value.ptr, want, want_len) == 0;
+ std_vec_free_Checked(&value, 1, 1);
+ return is;
+}
+
+/* txn_identity_tests_id_bytes returns an id's big-endian bytes. */
+static void
+txn_identity_tests_id_bytes(uint64 id, uint8 *bytes)
+{
+ for (int k = 0; k < 8; k++)
+ bytes[k] = (uint8) (id >> (8 * (7 - k)));
+}
+
+/* TXN_IDENTITY_TESTS_NUMBER is the pair by id's value of OID 16384, its varint. */
+static const uint8 TXN_IDENTITY_TESTS_NUMBER[] = {0x80, 0x80, 0x01};
+
+/* txn_identity_tests_pairs_read reports whether a table's two pairs read as its OID's and its id's. */
+static bool
+txn_identity_tests_pairs_read(HostIoIo *io, uint32 repository, uint32 oid, uint64 id)
+{
+ StdVec by_oid;
+ StdVec by_id;
+ uint8 bytes[8];
+ bool read;
+
+ txn_identity_by_oid(TXN_IDENTITY_KIND_RELATION, oid, &by_oid);
+ txn_identity_by_id(TXN_IDENTITY_KIND_RELATION, id, &by_id);
+ txn_identity_tests_id_bytes(id, bytes);
+ read = txn_identity_tests_read_is(io, repository, &by_oid, bytes, sizeof(bytes)) &&
+ txn_identity_tests_read_is(io, repository, &by_id, TXN_IDENTITY_TESTS_NUMBER,
+ sizeof(TXN_IDENTITY_TESTS_NUMBER));
+ std_vec_free_Checked(&by_id, 1, 1);
+ std_vec_free_Checked(&by_oid, 1, 1);
+ return read;
+}
+
+/* txn_identity_tests_pairs_gone reports whether neither of a table's two pairs reads. */
+static bool
+txn_identity_tests_pairs_gone(HostIoIo *io, uint32 repository, uint32 oid, uint64 id)
+{
+ StdVec by_oid;
+ StdVec by_id;
+ bool gone;
+
+ txn_identity_by_oid(TXN_IDENTITY_KIND_RELATION, oid, &by_oid);
+ txn_identity_by_id(TXN_IDENTITY_KIND_RELATION, id, &by_id);
+ gone = !txn_identity_tests_read(io, repository, &by_oid, NULL) &&
+ !txn_identity_tests_read(io, repository, &by_id, NULL);
+ std_vec_free_Checked(&by_id, 1, 1);
+ std_vec_free_Checked(&by_oid, 1, 1);
+ return gone;
+}
+
+/* txn_identity_tests_logged_count returns how many database change records the simulation logged. */
+static size_t
+txn_identity_tests_logged_count(void)
+{
+ StdVec logged = simc_logged();
+ size_t count = 0;
+
+ for (size_t k = 0; k < logged.len; k++)
+ {
+ simc_TupleU8VecU8Lsn_t *one = &STD_VEC_AT(&logged, simc_TupleU8VecU8Lsn_t, k);
+ HostWalKind kind;
+
+ count += host_WalKind_of(one->f0, &kind) && kind == HOST_WAL_KIND_DATABASE_CHANGE;
+ std_vec_free_Checked(&one->f1, 1, 1);
+ }
+ std_vec_free_Checked(&logged, sizeof(simc_TupleU8VecU8Lsn_t), _Alignof(simc_TupleU8VecU8Lsn_t));
+ return count;
+}
+
+/* txn_identity_tests_versions returns how many identity versions the delta holds at a key. */
+static size_t
+txn_identity_tests_versions(HostIoIo *io, uint32 repository, const StdVec *key)
+{
+ HostSharedHeld deltas;
+ HostDeltaHold hold;
+ StdVec versions;
+ size_t count;
+ HostError error;
+
+ txn_identity_tests_ok(txn_memtable_branch_deltas(io, repository, HOST_PG_DEFAULT, &deltas, &error) &&
+ host_delta_hold(io, &hold, &error) &&
+ txn_memtable_branch_identity_versions(io, &hold, host_shared_Held_record(&deltas), key->ptr,
+ key->len, &versions, &error), &error, __LINE__);
+ count = versions.len;
+ for (size_t k = 0; k < versions.len; k++)
+ txn_memtable_version_Version_drop(&STD_VEC_AT(&versions, TxnMemtableVersionVersion, k));
+ std_vec_free_Checked(&versions, sizeof(TxnMemtableVersionVersion), _Alignof(TxnMemtableVersionVersion));
+ host_delta_Hold_drop(&hold);
+ host_shared_Held_drop(&deltas);
+ return count;
+}
+
+/* txn_identity_tests_dropped drops a repository's branch state, which unwraps at a line. */
+static void
+txn_identity_tests_dropped(HostIoIo *io, uint32 repository, uint32 line)
+{
+ HostError error;
+
+ txn_identity_tests_ok(txn_memtable_branch_dropped(io, repository, &error), &error, line);
+}
+
+/*
+ * test_txn_identity_tests_a_tables_pairs_read_back_by_its_oid_and_its_id:
+ * PLAN item 10's pairs, a table's derivation records its pair by OID and its
+ * pair by id, which read back in its transaction and, once it commits, in
+ * the next; the id is the derivation's, and recording the table again finds
+ * its id and pushes nothing.
+ */
+void
+test_txn_identity_tests_a_tables_pairs_read_back_by_its_oid_and_its_id(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ uint32 repository;
+ uint64 id;
+ size_t logged;
+ StdVec by_id;
+ HostError error;
+
+ simc_end(false);
+ repository = txn_identity_tests_repository(io, 9470);
+ id = txn_identity_tests_table(io, repository, "t", 16384);
+ STD_ASSERT_MSG(__LINE__, 9, id == txn_identity_tests_derived(TXN_IDENTITY_KIND_RELATION, TXN_IDENTITY_TESTS_SCHEMA,
+ "t", 1, __LINE__),
+ "id == derive_object_id(Kind::Relation, SCHEMA, b\"t\").unwrap()");
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_pairs_read(io, repository, 16384, id),
+ "the table's pairs read back in its transaction");
+ logged = txn_identity_tests_logged_count();
+ simc_end(true);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_table(io, repository, "t", 16384) == id,
+ "recording the table again finds its id");
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_logged_count() == logged, "recording it again pushed nothing");
+ txn_identity_by_id(TXN_IDENTITY_KIND_RELATION, id, &by_id);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_read_is(io, repository, &by_id, TXN_IDENTITY_TESTS_NUMBER,
+ sizeof(TXN_IDENTITY_TESTS_NUMBER)),
+ "read(io, repository, &by_id(Kind::Relation, id)) == Some(vec![0x80, 0x80, 0x01])");
+ std_vec_free_Checked(&by_id, 1, 1);
+ simc_end(true);
+ txn_identity_tests_dropped(io, repository, __LINE__);
+ txn_identity_tests_ok(txn_written_ended(io, &error), &error, __LINE__);
+ simc_TestIo_drop(&test_io);
+ simc_Process_drop(&process);
+}
+
+/*
+ * test_txn_identity_tests_an_aborted_transactions_pairs_go_at_the_take_back:
+ * 2g's take-back for the identity tree, an aborted transaction's pairs go at
+ * its abort, so its table has none.
+ */
+void
+test_txn_identity_tests_an_aborted_transactions_pairs_go_at_the_take_back(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ uint32 repository;
+ uint64 id;
+ StdVec by_id;
+ HostError error;
+
+ simc_end(false);
+ repository = txn_identity_tests_repository(io, 9471);
+ id = txn_identity_tests_table(io, repository, "t", 16384);
+ simc_end(false);
+ txn_identity_tests_ok(txn_written_aborted(io, 0, &error), &error, __LINE__);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_pairs_gone(io, repository, 16384, id),
+ "an aborted transaction's pairs went at its abort");
+ txn_identity_by_id(TXN_IDENTITY_KIND_RELATION, id, &by_id);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_versions(io, repository, &by_id) == 0,
+ "branch::identity_versions(...).unwrap().is_empty()");
+ std_vec_free_Checked(&by_id, 1, 1);
+ txn_identity_tests_dropped(io, repository, __LINE__);
+ simc_TestIo_drop(&test_io);
+ simc_Process_drop(&process);
+}
+
+/*
+ * test_txn_identity_tests_a_taken_id_moves_on_and_a_removed_pair_frees_it:
+ * I14 in one process, a derivation whose id another object's live pair holds
+ * takes the next free id, and once that object's pairs are removed, an
+ * object made again under its name derives its id again.
+ */
+void
+test_txn_identity_tests_a_taken_id_moves_on_and_a_removed_pair_frees_it(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ uint32 repository;
+ uint64 first;
+ txn_identity_tests_writing_t written;
+ HostIoWaits waits = host_io_Waits_testing();
+ StdVec keys;
+ StdVec key;
+ HostError error;
+
+ simc_end(false);
+ repository = txn_identity_tests_repository(io, 9472);
+ first = txn_identity_tests_table(io, repository, "t", 16384);
+ simc_end(true);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_table(io, repository, "t", 16390) == txn_identity_next_id(first),
+ "a taken id moves on to the next free one");
+ simc_end(true);
+ written = txn_identity_tests_writing(io);
+ std_vec_new(&keys, sizeof(StdVec), _Alignof(StdVec));
+ txn_identity_by_oid(TXN_IDENTITY_KIND_RELATION, 16384, &key);
+ std_vec_push_Checked(&keys, &key, sizeof(key), _Alignof(StdVec));
+ txn_identity_by_id(TXN_IDENTITY_KIND_RELATION, first, &key);
+ std_vec_push_Checked(&keys, &key, sizeof(key), _Alignof(StdVec));
+ txn_identity_tests_ok(txn_identity_removed(io, &waits, repository, HOST_PG_DEFAULT, written.writer, written.roomed,
+ &keys, &error), &error, __LINE__);
+ txn_identity_by_oid(TXN_IDENTITY_KIND_RELATION, 16384, &key);
+ STD_ASSERT_MSG(__LINE__, 9, !txn_identity_tests_read(io, repository, &key, NULL),
+ "read(io, repository, &by_oid(Kind::Relation, 16384)) == None");
+ std_vec_free_Checked(&key, 1, 1);
+ simc_end(true);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_table(io, repository, "t", 16400) == first,
+ "a removed pair frees its id");
+ simc_end(true);
+ txn_identity_tests_dropped(io, repository, __LINE__);
+ txn_identity_tests_ok(txn_written_ended(io, &error), &error, __LINE__);
+ simc_TestIo_drop(&test_io);
+ simc_Process_drop(&process);
+}
+
+/*
+ * test_txn_identity_tests_redone_identity_changes_give_the_same_pairs_once:
+ * I12's case for the identity tree, a database's logged identity changes,
+ * redone into another database's delta with no catalog, give it the same
+ * pairs, and a change redone again is held by its LSN and pushes nothing.
+ */
+void
+test_txn_identity_tests_redone_identity_changes_give_the_same_pairs_once(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ uint32 primary;
+ uint32 replica;
+ uint64 id;
+ StdVec logged;
+ StdVec records;
+ StdVec by_id;
+ HostError error;
+
+ simc_end(false);
+ primary = txn_identity_tests_repository(io, 9473);
+ replica = txn_identity_tests_repository(io, 9474);
+ id = txn_identity_tests_table(io, primary, "t", 16384);
+ simc_end(true);
+ logged = simc_logged();
+ std_vec_new(&records, sizeof(simc_TupleU8VecU8Lsn_t), _Alignof(simc_TupleU8VecU8Lsn_t));
+ for (size_t k = 0; k < logged.len; k++)
+ {
+ simc_TupleU8VecU8Lsn_t *one = &STD_VEC_AT(&logged, simc_TupleU8VecU8Lsn_t, k);
+ TxnRecordDatabaseChange change;
+ HostWalKind kind;
+ bool kept = false;
+
+ if (host_WalKind_of(one->f0, &kind) && kind == HOST_WAL_KIND_DATABASE_CHANGE)
+ {
+ if (!txn_record_DatabaseChange_decode(one->f1.ptr, one->f1.len, &change, &error))
+ host_Error_drop(&error);
+ else
+ {
+ kept = change.repository == primary;
+ txn_record_DatabaseChange_drop(&change);
+ }
+ }
+ if (kept)
+ std_vec_push_Checked(&records, one, sizeof(*one), _Alignof(simc_TupleU8VecU8Lsn_t));
+ else
+ std_vec_free_Checked(&one->f1, 1, 1);
+ }
+ std_vec_free_Checked(&logged, sizeof(simc_TupleU8VecU8Lsn_t), _Alignof(simc_TupleU8VecU8Lsn_t));
+ STD_ASSERT_MSG(__LINE__, 9, records.len == 2, "records.len() == 2");
+ for (int pass = 0; pass < 2; pass++)
+ for (size_t k = 0; k < records.len; k++)
+ {
+ simc_TupleU8VecU8Lsn_t *one = &STD_VEC_AT(&records, simc_TupleU8VecU8Lsn_t, k);
+ TxnRecordDatabaseChange change;
+
+ txn_identity_tests_ok(txn_record_DatabaseChange_decode(one->f1.ptr, one->f1.len, &change, &error), &error,
+ __LINE__);
+ change.repository = replica;
+ txn_identity_tests_ok(txn_identity_redo(io, &change, HOST_PG_DEFAULT, one->f2, &error), &error,
+ __LINE__);
+ }
+ for (size_t k = 0; k < records.len; k++)
+ std_vec_free_Checked(&STD_VEC_AT(&records, simc_TupleU8VecU8Lsn_t, k).f1, 1, 1);
+ std_vec_free_Checked(&records, sizeof(simc_TupleU8VecU8Lsn_t), _Alignof(simc_TupleU8VecU8Lsn_t));
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_pairs_read(io, replica, 16384, id),
+ "the replica's pairs read as the primary's");
+ txn_identity_by_id(TXN_IDENTITY_KIND_RELATION, id, &by_id);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_versions(io, replica, &by_id) == 1,
+ "a change redone again pushes nothing");
+ std_vec_free_Checked(&by_id, 1, 1);
+ txn_identity_tests_dropped(io, primary, __LINE__);
+ txn_identity_tests_dropped(io, replica, __LINE__);
+ txn_identity_tests_ok(txn_written_ended(io, &error), &error, __LINE__);
+ simc_TestIo_drop(&test_io);
+ simc_Process_drop(&process);
+}
+
+/* txn_identity_tests_refused reports whether a database change record's body is refused. */
+static bool
+txn_identity_tests_refused(ChunksRecordRecordWriter *w)
+{
+ StdVec body = chunks_record_RecordWriter_finish(w);
+ TxnRecordDatabaseChange change;
+ HostError error;
+ bool refused = !txn_record_DatabaseChange_decode(body.ptr, body.len, &change, &error);
+
+ if (refused)
+ host_Error_drop(&error);
+ else
+ txn_record_DatabaseChange_drop(&change);
+ std_vec_free_Checked(&body, 1, 1);
+ return refused;
+}
+
+/*
+ * test_txn_identity_tests_an_identity_change_reads_back_as_written: the
+ * database change record's form, a subtransaction's writer, a put's value,
+ * and a removal's lack of one read back as written, and an unknown field
+ * and a tree none is are refused.
+ */
+void
+test_txn_identity_tests_an_identity_change_reads_back_as_written(void)
+{
+ TxnMemtableVersionWriter writer = {.xid = 41, .has_top = true, .top = 40, .command = 3};
+ ChunksRecordRecordWriter w;
+
+ for (int put = 0; put < 2; put++)
+ {
+ TxnRecordDatabaseChange change = {.tree = TXN_RECORD_TREE_IDENTITY, .writer = writer, .repository = 9475,
+ .has_value = put == 0};
+ TxnRecordDatabaseChange read;
+ StdVec body;
+ HostError error;
+
+ txn_identity_by_oid(TXN_IDENTITY_KIND_RELATION, 16384, &change.key);
+ if (put == 0)
+ std_slice_to_vec_Checked(&change.value, "v", 1, 1, 1);
+ txn_record_DatabaseChange_encode(&change, &body);
+ txn_identity_tests_ok(txn_record_DatabaseChange_decode(body.ptr, body.len, &read, &error), &error, __LINE__);
+ STD_ASSERT_MSG(__LINE__, 13, txn_record_DatabaseChange_PartialEq_eq(&read, &change),
+ "DatabaseChange::decode(&change.encode()).unwrap() == change");
+ txn_record_DatabaseChange_drop(&read);
+ txn_record_DatabaseChange_drop(&change);
+ std_vec_free_Checked(&body, 1, 1);
+ }
+ w = chunks_record_RecordWriter_with_capacity(32);
+ chunks_record_RecordWriter_uint(chunks_record_RecordWriter_uint(chunks_record_RecordWriter_uint(
+ chunks_record_RecordWriter_uint(&w, 1, 1), 2, 9475), 3, 40), 5, 0);
+ chunks_record_RecordWriter_uint(chunks_record_RecordWriter_field(&w, 6, (const uint8 *) "k", 1), 8, 1);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_refused(&w), "an unknown field read back");
+ w = chunks_record_RecordWriter_with_capacity(32);
+ chunks_record_RecordWriter_uint(chunks_record_RecordWriter_uint(chunks_record_RecordWriter_uint(
+ chunks_record_RecordWriter_uint(&w, 1, 2), 2, 9475), 3, 40), 5, 0);
+ chunks_record_RecordWriter_field(&w, 6, (const uint8 *) "k", 1);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_refused(&w), "a tree none is read back");
+}
+
+/* txn_identity_tests_gather is a save's gather of the folded pairs, each as the identity tree's pair. */
+static bool
+txn_identity_tests_gather(void *captures, HostIoIo *io, const RepoRead *read, StdVec *result, ChunksErrorError *error)
+{
+ const StdVec *pairs = captures;
+
+ (void) io;
+ (void) read;
+ (void) error;
+ std_vec_new(result, sizeof(RepoGathered), _Alignof(RepoGathered));
+ for (size_t k = 0; k < pairs->len; k++)
+ {
+ const TxnIdentityPair *pair = &STD_VEC_AT(pairs, TxnIdentityPair, k);
+ RepoGathered gathered = {.tag = REPO_GATHERED_PAIR, .u.pair = {.has_value = pair->has_f1}};
+
+ std_slice_to_vec_Checked(&gathered.u.pair.key, pair->f0.ptr, pair->f0.len, 1, 1);
+ if (pair->has_f1)
+ std_slice_to_vec_Checked(&gathered.u.pair.value, pair->f1.ptr, pair->f1.len, 1, 1);
+ std_vec_push_Checked(result, &gathered, sizeof(gathered), _Alignof(RepoGathered));
+ }
+ return true;
+}
+
+/* txn_identity_tests_saved is a save's after-write call, which does nothing. */
+static bool
+txn_identity_tests_saved(void *captures, HostIoIo *io, ChunksErrorError *error)
+{
+ (void) captures;
+ (void) io;
+ (void) error;
+ return true;
+}
+
+/* txn_identity_tests_folded folds a repository's committed pairs at a point and saves them into the tree the manifest names, returning the pairs. */
+static StdVec
+txn_identity_tests_folded(HostIoIo *io, uint32 repository, HostLsn point)
+{
+ StdVec pairs;
+ txn_identity_tests_opened_t opened;
+ ChunksErrorError failed;
+ HostError error;
+
+ txn_identity_tests_ok(txn_identity_folded(io, repository, point, &pairs, &error), &error, __LINE__);
+ opened = txn_identity_tests_opened(io, repository);
+ if (!repo_Repo_save(&opened.repo, io, &opened.reading, false, NULL, txn_identity_tests_gather, &pairs,
+ txn_identity_tests_saved, NULL, &failed))
+ STD_RESULT_EXPECT_FAILED(__LINE__, 0, TXN_IDENTITY_TESTS_UNWRAP, &failed, chunks_error_Error_Debug_fmt);
+ txn_identity_tests_opened_drop(&opened);
+ return pairs;
+}
+
+/* txn_identity_tests_pairs_drop drops folded's pairs. */
+static void
+txn_identity_tests_pairs_drop(StdVec *pairs)
+{
+ for (size_t k = 0; k < pairs->len; k++)
+ {
+ TxnIdentityPair *pair = &STD_VEC_AT(pairs, TxnIdentityPair, k);
+
+ std_vec_free_Checked(&pair->f0, 1, 1);
+ if (pair->has_f1)
+ std_vec_free_Checked(&pair->f1, 1, 1);
+ }
+ std_vec_free_Checked(pairs, sizeof(TxnIdentityPair), _Alignof(TxnIdentityPair));
+}
+
+/* txn_identity_tests_point returns the fold point the running fold position lowers to. */
+static HostLsn
+txn_identity_tests_point(HostIoIo *io)
+{
+ HostLsn position;
+ HostLsn point = 0;
+ HostError error;
+
+ txn_identity_tests_ok(host_fold_point(HOST_IO_IO_IO_DOORS(io), &position, &error) &&
+ txn_status_lowered(io, position, &point, &error), &error, __LINE__);
+ return point;
+}
+
+/*
+ * test_txn_identity_tests_a_saves_fold_keeps_its_committed_pairs_and_leaves_running_ones:
+ * §III.6's fold of the identity tree at a save, a save folds the pairs whose
+ * writers committed at or below its point into the tree the manifest names,
+ * a running writer's pair staying in the delta; with the delta's versions
+ * gone, the folded pairs read from the tree alone, and the running writer's
+ * pair is gone with them.
+ */
+void
+test_txn_identity_tests_a_saves_fold_keeps_its_committed_pairs_and_leaves_running_ones(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ uint32 repository;
+ uint64 id;
+ uint64 running;
+ HostLsn point;
+ StdVec pairs;
+ HostError error;
+
+ simc_end(false);
+ repository = txn_identity_tests_repository(io, 9477);
+ id = txn_identity_tests_table(io, repository, "t", 16384);
+ txn_identity_tests_committed(io);
+ point = txn_identity_tests_point(io);
+ running = txn_identity_tests_table(io, repository, "u", 16385);
+ pairs = txn_identity_tests_folded(io, repository, point);
+ STD_ASSERT_MSG(__LINE__, 9, pairs.len == 2, "pairs.len() == 2");
+ txn_identity_tests_pairs_drop(&pairs);
+ simc_end(false);
+ txn_identity_tests_ok(txn_written_aborted(io, 0, &error), &error, __LINE__);
+ txn_identity_tests_dropped(io, repository, __LINE__);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_pairs_read(io, repository, 16384, id),
+ "the folded pairs read from the tree alone");
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_pairs_gone(io, repository, 16385, running),
+ "the running writer's pair is gone");
+ txn_identity_tests_dropped(io, repository, __LINE__);
+ txn_identity_tests_ok(txn_written_ended(io, &error), &error, __LINE__);
+ simc_TestIo_drop(&test_io);
+ simc_Process_drop(&process);
+}
+
+/* txn_identity_tests_kept returns how many identity versions the delta holds at a table's two pairs. */
+static size_t
+txn_identity_tests_kept(HostIoIo *io, uint32 repository, uint32 oid, uint64 id)
+{
+ StdVec key;
+ size_t kept;
+
+ txn_identity_by_oid(TXN_IDENTITY_KIND_RELATION, oid, &key);
+ kept = txn_identity_tests_versions(io, repository, &key);
+ std_vec_free_Checked(&key, 1, 1);
+ txn_identity_by_id(TXN_IDENTITY_KIND_RELATION, id, &key);
+ kept += txn_identity_tests_versions(io, repository, &key);
+ std_vec_free_Checked(&key, 1, 1);
+ return kept;
+}
+
+/* txn_identity_tests_counted returns an xid's entry's count, 0 for none. */
+static uint64
+txn_identity_tests_counted(HostIoIo *io, HostXid top)
+{
+ TxnStatusEntry entry;
+ bool has;
+ HostError error;
+
+ txn_identity_tests_ok(txn_status_lookup(io, top, &has, &entry, &error), &error, __LINE__);
+ return has ? entry.count : 0;
+}
+
+/*
+ * test_txn_identity_tests_a_saves_drop_takes_its_folded_pairs_out: bugs row
+ * 164, a save's drop takes the identity versions it folded out of the delta
+ * and gives their counts back at their writer's entry, the pairs then
+ * reading from the manifest's tree alone.
+ */
+void
+test_txn_identity_tests_a_saves_drop_takes_its_folded_pairs_out(void)
+{
+ SimcProcess process = simc_process();
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ uint32 repository;
+ uint64 id;
+ HostXid sub;
+ HostXid top;
+ HostLsn point;
+ StdVec pairs;
+ HostError error;
+
+ simc_end(false);
+ repository = txn_identity_tests_repository(io, 9479);
+ id = txn_identity_tests_table(io, repository, "t", 16384);
+ txn_identity_tests_ok(host_current_writer(HOST_IO_IO_IO_DOORS(io), &sub, &top, &error), &error, __LINE__);
+ txn_identity_tests_committed(io);
+ point = txn_identity_tests_point(io);
+ pairs = txn_identity_tests_folded(io, repository, point);
+ txn_identity_tests_pairs_drop(&pairs);
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_kept(io, repository, 16384, id) == 2 &&
+ txn_identity_tests_counted(io, top) == 2, "(kept(io), counted(io)) == (2, 2)");
+ {
+ bool took;
+
+ txn_identity_tests_ok(txn_identity_dropped(io, repository, point, &took, &error), &error, __LINE__);
+ }
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_kept(io, repository, 16384, id) == 0 &&
+ txn_identity_tests_counted(io, top) == 0, "(kept(io), counted(io)) == (0, 0)");
+ STD_ASSERT_MSG(__LINE__, 9, txn_identity_tests_pairs_read(io, repository, 16384, id),
+ "the pairs read from the manifest's tree alone");
+ txn_identity_tests_dropped(io, repository, __LINE__);
+ txn_identity_tests_ok(txn_written_ended(io, &error), &error, __LINE__);
+ simc_TestIo_drop(&test_io);
+ simc_Process_drop(&process);
+}
+
+#endif /* DOLTGRES_TEST_TXN */
diff --git a/src/doltgres/txn/c/insert.c b/src/doltgres/txn/c/insert.c
index 91e7516530b78..3b7d74625821b 100644
--- a/src/doltgres/txn/c/insert.c
+++ b/src/doltgres/txn/c/insert.c
@@ -788,6 +788,17 @@ txn_insert_Seen_drop(TxnInsertSeen *self)
self->has_current = self->has_below = false;
}
+/* txn_insert_Seen_read is a take's read of a block whose entry it saw outside the window, with the base generation read before it. */
+static TxnMemtableMapRead
+txn_insert_Seen_read(const TxnInsertSeen *self, const TxnInsertBase *base)
+{
+ return (TxnMemtableMapRead)
+ {
+ .has_entry = self->has_current, .entry = self->has_current ? self->current.ptr : NULL,
+ .entry_len = self->has_current ? self->current.len : 0, .generation = base->generation
+ };
+}
+
/* txn_insert_seen_frame_t is seen's state across its pad: what it read, while live. */
typedef struct txn_insert_seen_frame_t
{
@@ -802,7 +813,7 @@ typedef struct txn_insert_seen_frame_t
bool ok;
} txn_insert_seen_frame_t;
-/* txn_insert_seen_body reads the block's newest entry, the delta's else the base's, and decodes it. */
+/* txn_insert_seen_body reads the block's newest entry, the memtable's, else its spills', else the base's, and decodes it. */
static void
txn_insert_seen_body(void *frame_)
{
@@ -813,15 +824,28 @@ txn_insert_seen_body(void *frame_)
ChunksErrorError failed;
uint8 key[4];
bool some;
+ bool spilled;
- if (!txn_memtable_map_newest(frame->io, frame->record, TXN_MEMTABLE_MAP_MAP_FREE, frame->block, &some, &newest,
- frame->error))
+ if (!txn_memtable_map_in_memtable(frame->io, frame->record, TXN_MEMTABLE_MAP_MAP_FREE, frame->block, &some,
+ &newest, frame->error))
return;
if (some)
{
result->has_current = newest.has_value;
result->current = newest.value;
}
+ else if (!txn_memtable_map_spilled_newest(frame->io, frame->record, frame->block, &spilled, &newest, frame->error))
+ return;
+ else if (spilled)
+ {
+ result->has_below = newest.has_value;
+ result->below = newest.value;
+ if (result->has_below)
+ {
+ std_slice_to_vec_Checked(&result->current, result->below.ptr, result->below.len, 1, 1);
+ result->has_current = true;
+ }
+ }
else if (frame->base->has_tree)
{
TreesStoreTree tree = frame->base->tree;
@@ -860,7 +884,8 @@ txn_insert_seen_pad(void *frame_)
* txn_insert_seen reads a block's entry outside any window, the delta's
* newest else the base's, and decodes it there, so a corrupt entry raises
* XX001 outside the critical section; it returns the bytes a take compares
- * under its lock, and the base's entry the take falls back to.
+ * under its lock, and the entry below the memtable the take falls back to:
+ * the spills' newest, else the base's.
*/
static bool
txn_insert_seen(HostIoIo *io, TreesStoreNodeStore store, HostSharedRecord record, const TxnInsertBase *base,
@@ -1473,11 +1498,9 @@ txn_insert_ended(const TxnInsertInsert *row, const TxnInsertReplaces *replaces,
return true;
}
-/* txn_insert_TakeStep_t is a take's step's captures: what it compares and the entry it reads. */
+/* txn_insert_TakeStep_t is a take's step's captures: the entry it reads. */
typedef struct txn_insert_TakeStep_t
{
- bool has_expected;
- const StdVec *expected;
HostSharedRecord record;
const TxnInsertBase *base;
const TxnInsertRoom *room;
@@ -1486,18 +1509,6 @@ typedef struct txn_insert_TakeStep_t
uint64 tid;
} txn_insert_TakeStep_t;
-/* txn_insert_unmoved reports whether a block's entry and the base are as they were read outside the window. */
-static bool
-txn_insert_unmoved(const txn_insert_TakeStep_t *step, bool has_current, const uint8 *current, size_t current_len)
-{
- if (has_current != step->has_expected)
- return false;
- if (has_current && (current_len != step->expected->len ||
- (current_len != 0 && memcmp(current, step->expected->ptr, current_len) != 0)))
- return false;
- return txn_state_generation(step->record) == step->base->generation;
-}
-
/*
* txn_insert_filled is the step of an update whose new row found no room on
* its replaced row's block: the block's full flag set, as heap_update sets
@@ -1525,10 +1536,9 @@ txn_insert_filled(void *captures, bool has_current, const uint8 *current, size_t
/*
* txn_insert_take_block is a take's step on a block other than the fresh
- * page, pure over its input: Retry when the entry or the base moved since
- * they were read outside the window, Refuse when the entry has no room for
- * the row, and otherwise the entry with its lowest free offset and the row's
- * bytes taken, written into out, or its removal when it was the last.
+ * page, pure over its input: Refuse when the entry has no room for the row,
+ * and otherwise the entry with its lowest free offset and the row's bytes
+ * taken, written into out, or its removal when it was the last.
*/
static TxnMemtableMapStep
txn_insert_take_block(void *captures, bool has_current, const uint8 *current, size_t current_len, StdVec *out,
@@ -1538,8 +1548,6 @@ txn_insert_take_block(void *captures, bool has_current, const uint8 *current, si
TxnFreeSpaceFree entry;
uint16 offset;
- if (!txn_insert_unmoved(step, has_current, current, current_len))
- return TXN_MEMTABLE_MAP_STEP_RETRY;
if (!has_current)
return TXN_MEMTABLE_MAP_STEP_REFUSE;
if (!txn_insert_parsed(current, current_len, step->block, step->base, txn_state_counter(step->record),
@@ -1560,9 +1568,9 @@ txn_insert_take_block(void *captures, bool has_current, const uint8 *current, si
* txn_insert_take_fresh is a take's step on the fresh page, the counter's
* block, pure over its input: its lowest freed offset, else the counter's
* while the page has room and offsets, else the page left for a new one with
- * its counter offsets handed to the map; Retry when the entry, the counter,
- * or the base moved since they were read. An untouched page has a page's
- * bytes free; a touched one with no entry is full.
+ * its counter offsets handed to the map; Retry when the counter moved since
+ * it was read. An untouched page has a page's bytes free; a touched one with
+ * no entry is full.
*/
static TxnMemtableMapStep
txn_insert_take_fresh(void *captures, bool has_current, const uint8 *current, size_t current_len, StdVec *out,
@@ -1575,8 +1583,7 @@ txn_insert_take_fresh(void *captures, bool has_current, const uint8 *current, si
uint16 freed;
uint16 next;
- if (!txn_insert_unmoved(step, has_current, current, current_len) ||
- txn_state_counter(step->record) != step->counter)
+ if (txn_state_counter(step->record) != step->counter)
return TXN_MEMTABLE_MAP_STEP_RETRY;
if (has_current)
{
@@ -1627,7 +1634,11 @@ txn_insert_fresh_moved(void *captures, const TxnInsertGot *got)
txn_state_moved(step->record, step->counter, STD_ADD(u64, step->counter >> 16, 1, 953, 65) << 16 | 1);
}
-/* txn_insert_stored is a stored value's step: its entry written where the map holds none, else Retry. */
+/*
+ * txn_insert_stored is a stored value's step: its entry written where the map
+ * holds none, else Retry. Its take has no read to compare: the value id is
+ * new, so no entry falls back from under it (bugs row 342).
+ */
static TxnMemtableMapStep
txn_insert_stored(void *captures, bool has_current, const uint8 *current, size_t current_len, StdVec *out)
{
@@ -1637,7 +1648,12 @@ txn_insert_stored(void *captures, bool has_current, const uint8 *current, size_t
return TXN_MEMTABLE_MAP_STEP_PUT;
}
-/* txn_insert_added is a kept value's step, values' count add. */
+/*
+ * txn_insert_added is a kept value's step, values' count add. Its take falls
+ * back to the newest entry its caller read whole, which a spill moves
+ * unchanged, and the values mark its caller rechecks before the window
+ * covers a write between, so it has no read to compare (bugs row 342).
+ */
static TxnMemtableMapStep
txn_insert_added(void *captures, bool has_current, const uint8 *current, size_t current_len, StdVec *out)
{
@@ -1806,6 +1822,27 @@ txn_insert_turn_closure_9_logged(txn_insert_turn_closure_9_frame_t *frame, HostI
return true;
}
+/*
+ * txn_insert_turn_closure_9_spent logs, for a logged turn that took its
+ * target, the take of a tuple id the turn keeps without its row.
+ */
+static bool
+txn_insert_turn_closure_9_spent(txn_insert_turn_closure_9_frame_t *frame, HostIoIoDoors doors, uint64 tid,
+ const TxnInsertRoom *room)
+{
+ txn_insert_turn_frame_t *turn = frame->turn;
+ TxnInsertWriting *w = turn->w;
+ TxnRecordSpent spent = {.writer = w->change.writer, .storage = w->change.storage, .tuple = {tid},
+ .heap_bytes = room->bytes};
+ HostLsn spent_lsn;
+
+ if (!w->logged || turn->target.tag == TXN_INSERT_TARGET_TAKEN)
+ return true;
+ turn->spending_live = false;
+ txn_record_Spent_encode_with(&spent, &turn->spending, &frame->body);
+ return txn_insert_turn_closure_9_logged(frame, doors, HOST_WAL_KIND_ROW_CHANGE, &spent_lsn);
+}
+
/* txn_insert_turn_closure_9_body reads under the prepare's hold, rechecks under the stripes, and runs the window. */
static void
txn_insert_turn_closure_9_body(void *frame_)
@@ -1814,7 +1851,7 @@ txn_insert_turn_closure_9_body(void *frame_)
txn_insert_turn_frame_t *turn = frame->turn;
TxnInsertWriting *w = turn->w;
const HostDeltaHold *hold = txn_memtable_table_PreparedState_hold(frame->prepared);
- static const char *const pauses[] = {"doltgres-update-ended", "doltgres-insert-logged"};
+ static const char *const pauses[] = {"doltgres-update-ended", "doltgres-insert-logged", "doltgres-insert-flagged-moved"};
bool has_recheck[2] = {false, false};
bool has_newest[2] = {false, false};
TxnMemtableVersionHandle newest[2];
@@ -2009,24 +2046,21 @@ txn_insert_turn_closure_9_body(void *frame_)
}
else
{
- txn_insert_TakeStep_t step = {.has_expected = turn->seen.has_current, .expected = &turn->seen.current,
- .record = turn->record, .base = turn->base, .room = room, .block = turn->block,
+ txn_insert_TakeStep_t step = {.record = turn->record, .base = turn->base, .room = room, .block = turn->block,
.counter = turn->target.tag == TXN_INSERT_TARGET_FRESH ? turn->target.u.fresh : 0};
+ TxnMemtableMapRead read = txn_insert_Seen_read(&turn->seen, turn->base);
bool fresh = turn->target.tag == TXN_INSERT_TARGET_FRESH;
HostRow unclaimed = {0};
- if (!txn_memtable_map_took_Got(io, frame->prepared, 0, writer, false, 0, false, unclaimed, 0,
- turn->seen.has_below, turn->seen.below.ptr, turn->seen.below.len,
- fresh ? txn_insert_take_fresh : txn_insert_take_block, &step, NULL,
- txn_insert_Got_tuple, NULL, NULL,
+ if (!txn_memtable_map_took_Got(io, frame->prepared, 0, writer, w->has_roomed, w->roomed, false, 0, false,
+ unclaimed, 0, turn->seen.has_below, turn->seen.below.ptr,
+ turn->seen.below.len, &read, fresh ? txn_insert_take_fresh : txn_insert_take_block,
+ &step, NULL, txn_insert_Got_tuple, NULL, NULL,
fresh ? txn_insert_fresh_moved : txn_insert_nothing_got, &step, NULL,
&taken, &got, frame->error))
return;
- if (taken == TXN_MEMTABLE_MAP_TAKEN_TOOK &&
- !txn_memtable_table_counted(io, frame->prepared, w->has_roomed, w->roomed, frame->error))
- return;
}
- if (taken == TXN_MEMTABLE_MAP_TAKEN_RETRY ||
+ if (taken == TXN_MEMTABLE_MAP_TAKEN_RETRY || taken == TXN_MEMTABLE_MAP_TAKEN_MOVED ||
(taken == TXN_MEMTABLE_MAP_TAKEN_TOOK && got.tag == TXN_INSERT_GOT_LEFT))
{
if (taken == TXN_MEMTABLE_MAP_TAKEN_TOOK && w->logged && turn->target.tag == TXN_INSERT_TARGET_FRESH)
@@ -2049,17 +2083,8 @@ txn_insert_turn_closure_9_body(void *frame_)
return;
}
tid = got.tid;
- if ((has_waiting || has_refused) && w->logged && turn->target.tag != TXN_INSERT_TARGET_TAKEN)
- {
- TxnRecordSpent spent = {.writer = writer, .storage = w->change.storage, .tuple = {tid},
- .heap_bytes = room->bytes};
- HostLsn spent_lsn;
-
- turn->spending_live = false;
- txn_record_Spent_encode_with(&spent, &turn->spending, &frame->body);
- if (!txn_insert_turn_closure_9_logged(frame, doors, HOST_WAL_KIND_ROW_CHANGE, &spent_lsn))
- return;
- }
+ if ((has_waiting || has_refused) && !txn_insert_turn_closure_9_spent(frame, doors, tid, room))
+ return;
if (has_waiting)
{
txn_insert_turn_closure_9_finish(frame, TXN_INSERT_TURN_WAIT);
@@ -2082,17 +2107,15 @@ txn_insert_turn_closure_9_body(void *frame_)
}
for (size_t at = 0; at < turn->stored; at++)
{
- if (!txn_memtable_map_finish_Unit(io, frame->prepared, at + turn->first, writer, false, 0, false, NULL, 0,
- txn_insert_stored, (void *) &w->entries[at], NULL, txn_insert_nothing, NULL,
- NULL, &taken, frame->error))
+ if (!txn_memtable_map_finish_Unit(io, frame->prepared, at + turn->first, writer, w->has_roomed, w->roomed,
+ false, 0, false, NULL, 0, NULL, txn_insert_stored, (void *) &w->entries[at], NULL,
+ txn_insert_nothing, NULL, NULL, &taken, frame->error))
return;
if (taken != TXN_MEMTABLE_MAP_TAKEN_TOOK)
{
txn_insert_turn_closure_9_finish(frame, TXN_INSERT_TURN_RETRY);
return;
}
- if (!txn_memtable_table_counted(io, frame->prepared, w->has_roomed, w->roomed, frame->error))
- return;
}
if (w->toast != NULL)
{
@@ -2109,6 +2132,39 @@ txn_insert_turn_closure_9_body(void *frame_)
return;
}
}
+ if (turn->has_flagged)
+ {
+ txn_insert_TakeStep_t step = {.record = turn->record, .base = turn->base, .room = room,
+ .block = turn->flagged};
+ TxnMemtableMapRead read = txn_insert_Seen_read(&turn->flagged_seen, turn->base);
+
+ if (!txn_memtable_map_finish_Unit(io, frame->prepared, turn->flagging, writer, w->has_roomed, w->roomed,
+ false, 0, turn->flagged_seen.has_below, turn->flagged_seen.below.ptr,
+ turn->flagged_seen.below.len, &read, txn_insert_filled, &step, NULL,
+ txn_insert_nothing, NULL, NULL, &taken, frame->error))
+ return;
+ if (taken == TXN_MEMTABLE_MAP_TAKEN_MOVED)
+ {
+ /*
+ * The block is read again outside the window, where an error is
+ * an ordinary one, by a turn that keeps this tuple id, so the
+ * flag it pushes is the one its record names (bugs row 342).
+ */
+ if (!host_pause(HOST_SHARED_KEY_IO_PAUSES(&key_io), "doltgres-insert-flagged-moved", frame->error) ||
+ !txn_insert_turn_closure_9_spent(frame, doors, tid, room))
+ return;
+ txn_insert_turn_closure_9_finish(frame, TXN_INSERT_TURN_RETRY);
+ w->has_taken = true;
+ w->taken = tid;
+ return;
+ }
+ if (taken == TXN_MEMTABLE_MAP_TAKEN_RETRY)
+ {
+ std_string_from_Checked(&message, "a free space map's entry is corrupt", 35);
+ *frame->error = host_Error_fork((const uint8 *) "XX001", &message);
+ return;
+ }
+ }
if (w->logged && w->decoded && w->has_replacing &&
!host_wal_new_cid(doors, &w->change.storage, w->replaces.tuple,
txn_change_changed_cid(shown, cmin, writer.command), &cid_lsn, frame->error))
@@ -2177,9 +2233,10 @@ txn_insert_turn_closure_9_body(void *frame_)
{
const TxnInsertKept *read = &STD_VEC_AT(turn->kept, TxnInsertKept, at);
- if (!txn_memtable_map_finish_Unit(io, frame->prepared, turn->first + turn->stored + at, writer, has_lsn, lsn,
- read->has_f0, read->f0.ptr, read->f0.len, txn_insert_added, NULL, NULL,
- txn_insert_nothing, NULL, NULL, &taken, frame->error))
+ if (!txn_memtable_map_finish_Unit(io, frame->prepared, turn->first + turn->stored + at, writer,
+ w->has_roomed, w->roomed, has_lsn, lsn, read->has_f0, read->f0.ptr,
+ read->f0.len, NULL, txn_insert_added, NULL, NULL, txn_insert_nothing, NULL, NULL,
+ &taken, frame->error))
return;
if (taken != TXN_MEMTABLE_MAP_TAKEN_TOOK)
{
@@ -2187,8 +2244,6 @@ txn_insert_turn_closure_9_body(void *frame_)
*frame->error = host_Error_fork((const uint8 *) "XX001", &message);
return;
}
- if (!txn_memtable_table_counted(io, frame->prepared, w->has_roomed, w->roomed, frame->error))
- return;
}
if (turn->marks_live && turn->marks.len > 0)
{
@@ -2196,9 +2251,8 @@ txn_insert_turn_closure_9_body(void *frame_)
StdVec marks = turn->marks;
std_vec_new(&turn->marks, TXN_INSERT_ID);
- if (!txn_memtable_map_named(io, frame->prepared, turn->marking, writer, has_lsn, lsn, row, &marks,
- frame->error) ||
- !txn_memtable_table_counted(io, frame->prepared, w->has_roomed, w->roomed, frame->error))
+ if (!txn_memtable_map_named(io, frame->prepared, turn->marking, writer, w->has_roomed, w->roomed, has_lsn,
+ lsn, row, &marks, frame->error))
return;
}
if (!host_pause(HOST_SHARED_KEY_IO_PAUSES(&key_io), "doltgres-insert-logged", frame->error))
@@ -2241,23 +2295,6 @@ txn_insert_turn_closure_9_body(void *frame_)
if (!done || !txn_memtable_tuples_finish(io, frame->prepared, row, frame->error))
return;
}
- if (turn->has_flagged)
- {
- txn_insert_TakeStep_t step = {.record = turn->record, .base = turn->base, .room = room,
- .block = turn->flagged};
-
- if (!txn_memtable_map_finish_Unit(io, frame->prepared, turn->flagging, writer, false, 0,
- turn->flagged_seen.has_below, turn->flagged_seen.below.ptr,
- turn->flagged_seen.below.len, txn_insert_filled, &step, NULL,
- txn_insert_nothing, NULL, NULL, &taken, frame->error))
- return;
- if (taken == TXN_MEMTABLE_MAP_TAKEN_RETRY)
- {
- std_string_from_Checked(&message, "a free space map's entry is corrupt", 35);
- *frame->error = host_Error_fork((const uint8 *) "XX001", &message);
- return;
- }
- }
txn_insert_turn_closure_9_finish(frame, TXN_INSERT_TURN_TOOK);
frame->result->u.took = tid;
}
@@ -2313,7 +2350,7 @@ txn_insert_turn_take(HostSharedRecord state, TxnMemtableMapMap map, uint32 key,
*/
static bool
txn_insert_turn_touched(HostIoIo *io, uint64 top, const HostStorage *storage, TxnWrittenOf of, const uint8 *key,
- size_t key_len, size_t need, bool unique, HostStorage over[3], TxnWrittenOf overs[3],
+ size_t key_len, size_t need, bool unique, HostStorage over[4], TxnWrittenOf overs[4],
size_t *over_len, HostError *error)
{
uint64 touched;
@@ -2341,8 +2378,8 @@ txn_insert_turn_body(void *frame_)
const TxnMemtablePut *put;
size_t free_most = txn_free_space_most_len(w->room->most);
uint64 top = change->writer.has_top ? change->writer.top : change->writer.xid;
- HostStorage over[3];
- TxnWrittenOf overs[3];
+ HostStorage over[4];
+ TxnWrittenOf overs[4];
size_t over_len = 0;
TxnMemtableMapTake take;
txn_TupleRefSliceU8Usize_t row;
@@ -2409,9 +2446,8 @@ txn_insert_turn_body(void *frame_)
&frame->flagged_seen, frame->error))
return;
frame->has_flagged = true;
- if (frame->flagged_seen.has_current)
- std_vec_free_Checked(&frame->flagged_seen.current, 1, 1);
- frame->flagged_seen.has_current = false;
+ if (!host_pause(HOST_SHARED_IO_PAUSES(frame->io), "doltgres-insert-flagged-read", frame->error))
+ return;
}
if (txn_record_RowChange_put(change, &put))
std_slice_to_vec_Checked(&frame->marks, put->values.ptr, put->values.len, TXN_INSERT_ID);
@@ -2484,6 +2520,18 @@ txn_insert_turn_body(void *frame_)
return;
}
}
+ for (size_t i = 0; i < frame->takes.len; i++)
+ {
+ const TxnMemtableMapTake *take = &STD_VEC_AT(&frame->takes, TxnMemtableMapTake, i);
+ uint8 block[4];
+
+ if (take->map != TXN_MEMTABLE_MAP_MAP_FREE)
+ continue;
+ std_u32_to_be_bytes(take->key, block);
+ if (!txn_insert_turn_touched(frame->io, top, &change->storage, TXN_WRITTEN_OF_FREE, block, sizeof(block),
+ take->most, true, over, overs, &over_len, frame->error))
+ return;
+ }
for (size_t i = 0; i < over_len; i++)
{
bool spilled;
@@ -3097,6 +3145,7 @@ typedef struct txn_insert_jump_closure_0_captures_t
{
TxnMemtableVersionWriter writer;
const TxnInsertSeen *seen;
+ const TxnInsertBase *base;
TxnMemtableMapStep (*step) (void *captures, bool has_current, const uint8 *current, size_t current_len,
StdVec *out);
txn_insert_JumpStep_t *captures;
@@ -3104,20 +3153,18 @@ typedef struct txn_insert_jump_closure_0_captures_t
TxnStatusRoomed roomed;
} txn_insert_jump_closure_0_captures_t;
-/* txn_insert_jump_closure_0 is a jump's prepare's closure: the counter block's take, counted when it took. */
+/* txn_insert_jump_closure_0 is a jump's prepare's closure: the counter block's take. */
static bool
txn_insert_jump_closure_0(void *captures, HostIoIo *io, TxnMemtableTablePreparedState *prepared,
TxnMemtableMapTaken *result, HostError *error)
{
txn_insert_jump_closure_0_captures_t *inside = captures;
+ TxnMemtableMapRead read = txn_insert_Seen_read(inside->seen, inside->base);
- if (!txn_memtable_map_finish_Unit(HOST_IO_IO_TAKES_SHORT(io), prepared, 0, inside->writer, false, 0,
- inside->seen->has_below, inside->seen->below.ptr, inside->seen->below.len,
- inside->step, inside->captures, NULL, txn_insert_jump_moved, inside->captures,
- NULL, result, error))
- return false;
- return *result != TXN_MEMTABLE_MAP_TAKEN_TOOK ||
- txn_memtable_table_counted(HOST_IO_IO_TAKES_SHORT(io), prepared, inside->has_roomed, inside->roomed, error);
+ return txn_memtable_map_finish_Unit(HOST_IO_IO_TAKES_SHORT(io), prepared, 0, inside->writer, inside->has_roomed,
+ inside->roomed, false, 0, inside->seen->has_below, inside->seen->below.ptr,
+ inside->seen->below.len, &read, inside->step, inside->captures, NULL,
+ txn_insert_jump_moved, inside->captures, NULL, result, error);
}
/* txn_insert_jump_frame_t is jump's state across its pad: the counter block's entries it read. */
@@ -3146,20 +3193,42 @@ txn_insert_jump_body(void *frame_)
uint32 block = (uint32) (frame->step_captures.counter >> 16);
TxnMemtableVersionWriter none = TXN_MEMTABLE_VERSION_NO_TRANSACTION;
txn_insert_jump_closure_0_captures_t inside = {.writer = frame->writer, .seen = &frame->seen,
- .step = frame->step, .captures = &frame->step_captures};
- TxnMemtableMapTake take;
+ .base = &frame->base, .step = frame->step, .captures = &frame->step_captures};
+ TxnMemtableMapTake take = txn_insert_turn_take(record, TXN_MEMTABLE_MAP_MAP_FREE, block,
+ txn_free_space_most_len(frame->step_captures.room->most));
+ HostStorage storage;
+ TxnStatePersistence persistence;
- if (!txn_insert_read_base(frame->io, frame->held, &frame->base, frame->error) ||
- !txn_insert_seen(frame->io, frame->store, record, &frame->base, frame->step_captures.room, block,
- &frame->seen, frame->error))
- return;
- take = txn_insert_turn_take(record, TXN_MEMTABLE_MAP_MAP_FREE, block,
- txn_free_space_most_len(frame->step_captures.room->most));
+ if (!txn_memtable_version_Writer_PartialEq_eq(&frame->writer, &none) &&
+ txn_state_described(record, &storage, &persistence))
+ {
+ uint64 top = frame->writer.has_top ? frame->writer.top : frame->writer.xid;
+ uint64 bytes;
+ bool spilled;
+
+ if (!txn_spill_replayed(frame->io, &storage, TXN_WRITTEN_OF_FREE, top, take.most + 4, &bytes, frame->error))
+ return;
+ if (bytes >= txn_spill_threshold() &&
+ !txn_spill_spilled(frame->io, &storage, TXN_WRITTEN_OF_FREE, top, &spilled, frame->error))
+ return;
+ }
if (!txn_memtable_version_Writer_PartialEq_eq(&frame->writer, &none) &&
!txn_status_redo_roomed(frame->io, &frame->writer, &inside.has_roomed, &inside.roomed, frame->error))
return;
- frame->ok = txn_memtable_map_prepare_Taken(frame->io, &take, 1, txn_insert_jump_closure_0, &inside, NULL,
- frame->result, frame->error);
+ for (;;)
+ {
+ if (!txn_insert_read_base(frame->io, frame->held, &frame->base, frame->error) ||
+ !txn_insert_seen(frame->io, frame->store, record, &frame->base, frame->step_captures.room, block,
+ &frame->seen, frame->error) ||
+ !host_pause(HOST_SHARED_IO_PAUSES(frame->io), "doltgres-insert-jump-seen", frame->error) ||
+ !txn_memtable_map_prepare_Taken(frame->io, &take, 1, txn_insert_jump_closure_0, &inside, NULL,
+ frame->result, frame->error))
+ return;
+ if (*frame->result != TXN_MEMTABLE_MAP_TAKEN_MOVED)
+ break;
+ txn_insert_Seen_drop(&frame->seen);
+ }
+ frame->ok = true;
}
/* txn_insert_jump_pad frees the entries jump read. */
@@ -3174,7 +3243,13 @@ txn_insert_jump_pad(void *frame_)
/*
* txn_insert_jump moves the counter from its load to a later tuple id by one
* map-only prepare and a take of the counter block's entry, whose step says
- * what the entry becomes, and answers what the take did.
+ * what the entry becomes, and answers what the take did. A replayed writer's
+ * take counts toward its transaction's free space map spill, as redo counts
+ * its rows, before the entry is read, so a spill it makes moves no version
+ * from under the entry the take falls back to; while the take answers that
+ * the entry or the base moved since the read, as a spill at its prepare's
+ * wait or a fold makes them, it reads the entry and prepares again (bugs row
+ * 342).
*/
static bool
txn_insert_jump(HostIoIo *io, TreesStoreNodeStore store, const HostSharedHeld *held, TxnMemtableVersionWriter writer,
@@ -3267,8 +3342,8 @@ txn_insert_Placed_read(HostIoIo *io, TreesStoreNodeStore store, const HostShared
if (!txn_insert_seen(io, store, host_shared_Held_record(held), &result->base, &result->room,
(uint32) (tid.f0 >> 16), &seen, error))
return false;
- if (seen.has_current)
- std_vec_free_Checked(&seen.current, 1, 1);
+ result->has_current = seen.has_current;
+ result->current = seen.current;
result->has_below = seen.has_below;
result->below = seen.below;
return true;
@@ -3282,49 +3357,243 @@ txn_insert_Placed_take(const TxnInsertPlaced *self, HostSharedRecord record)
txn_free_space_most_len(self->room.most));
}
-/* txn_insert_Placed_drop frees a place's base entry of its block. */
+/* txn_insert_Placed_drop frees a place's entries of its block. */
void
txn_insert_Placed_drop(TxnInsertPlaced *self)
{
+ if (self->has_current)
+ std_vec_free_Checked(&self->current, 1, 1);
if (self->has_below)
std_vec_free_Checked(&self->below, 1, 1);
- self->has_below = false;
+ self->has_current = self->has_below = false;
+}
+
+/* txn_insert_unfree returns the internal error of a carried version's place its storage's base doesn't hold free. */
+static HostError
+txn_insert_unfree(uint64 tid)
+{
+ StdFmtPiece pieces[] = {STD_FMT_LIT("a carried version's place "), STD_FMT_ARG(0),
+ STD_FMT_LIT(" isn't free in its storage's base")};
+ StdFmtArg args[] = {{&tid, std_u64_Display_fmt}};
+ StdFmtArguments arguments = {pieces, lengthof(pieces), args};
+ StdString message;
+
+ std_fmt_format_Checked(&message, &arguments);
+ return host_Error_fork((const uint8 *) "XX000", &message);
}
/*
* txn_insert_place_taken takes a placed tuple id's place out of its block's
* entry inside the window of the prepare whose take Placed_take readied it,
* as the writer's, with redo's step, so redo of the row's record finds it
- * taken; a place the entry doesn't hold free is an internal error.
+ * taken; a place the entry doesn't hold free is an internal error. It
+ * answers moved, taking nothing, when the block's entry or the base moved
+ * since Placed_read read them, so its caller reads the place again (bugs row
+ * 342).
*/
bool
txn_insert_place_taken(HostIoTakesShort io, TxnMemtableTablePreparedState *prepared, size_t take,
- HostSharedRecord record, TxnMemtableVersionWriter writer, const TxnInsertPlaced *placed,
- HostError *error)
+ HostSharedRecord record, TxnMemtableVersionWriter writer, bool has_roomed,
+ TxnStatusRoomed roomed, const TxnInsertPlaced *placed, bool *moved, HostError *error)
{
txn_insert_ReplayStep_t step = {.record = record, .base = &placed->base, .room = &placed->room,
.counter = placed->counter, .tid = placed->tid};
+ TxnMemtableMapRead read = {.has_entry = placed->has_current, .entry = placed->has_current ? placed->current.ptr : NULL,
+ .entry_len = placed->has_current ? placed->current.len : 0, .generation = placed->base.generation};
HostRow unclaimed = {0};
TxnMemtableMapTaken taken;
- StdFmtPiece pieces[] = {STD_FMT_LIT("a carried version's place "), STD_FMT_ARG(0),
- STD_FMT_LIT(" isn't free in its storage's base")};
- StdFmtArg args[] = {{&placed->tid, std_u64_Display_fmt}};
- StdFmtArguments arguments = {pieces, lengthof(pieces), args};
- StdString message;
- if (!txn_memtable_map_took_Unit(io, prepared, take, writer, false, 0, false, unclaimed, 0, placed->has_below,
- placed->below.ptr, placed->below.len, txn_insert_replayed, &step, NULL,
+ if (!txn_memtable_map_took_Unit(io, prepared, take, writer, has_roomed, roomed, false, 0, false, unclaimed, 0,
+ placed->has_below, placed->below.ptr, placed->below.len, &read, txn_insert_replayed,
+ &step, NULL,
txn_insert_replayed_tuple, &step, NULL, txn_insert_nothing, NULL, NULL, &taken,
error))
return false;
- if (taken == TXN_MEMTABLE_MAP_TAKEN_TOOK)
+ *moved = taken == TXN_MEMTABLE_MAP_TAKEN_MOVED;
+ if (taken == TXN_MEMTABLE_MAP_TAKEN_TOOK || *moved)
return true;
- std_fmt_format_Checked(&message, &arguments);
- *error = host_Error_fork((const uint8 *) "XX000", &message);
+ *error = txn_insert_unfree(placed->tid);
return false;
}
-/* txn_insert_redo_spent_closure_0_captures_t is redo_spent's prepare's closure's captures. */
+/* txn_insert_carried_entry_t is carried_takes' block entry: the block and its entry after the takes so far, none for a removal. */
+typedef struct txn_insert_carried_entry_t
+{
+ uint32 block;
+ bool has_current;
+ StdVec current;
+} txn_insert_carried_entry_t;
+
+/* txn_insert_carried_frame_t is carried_takes' state across its pad: the blocks' entries, a block's seen, and the takes, while live. */
+typedef struct txn_insert_carried_frame_t
+{
+ HostIoIo *io;
+ TreesStoreNodeStore store;
+ const HostSharedHeld *held;
+ const TxnInsertCarriedPut *puts;
+ size_t puts_len;
+ const TxnInsertPage *page;
+ StdVec *result;
+ HostError *error;
+ bool ok;
+ TxnInsertBase base;
+ StdVec entries;
+ bool entries_live;
+ TxnInsertSeen seen;
+ bool result_live;
+} txn_insert_carried_frame_t;
+
+/* txn_insert_carried_pad frees what carried_takes holds live. */
+static void
+txn_insert_carried_pad(void *frame_)
+{
+ txn_insert_carried_frame_t *frame = frame_;
+
+ txn_insert_Seen_drop(&frame->seen);
+ if (frame->entries_live)
+ {
+ for (size_t k = 0; k < frame->entries.len; k++)
+ {
+ txn_insert_carried_entry_t *entry = &STD_VEC_AT(&frame->entries, txn_insert_carried_entry_t, k);
+
+ if (entry->has_current)
+ std_vec_free_Checked(&entry->current, 1, 1);
+ }
+ std_vec_free_Checked(&frame->entries, sizeof(txn_insert_carried_entry_t),
+ _Alignof(txn_insert_carried_entry_t));
+ }
+ if (frame->result_live)
+ txn_insert_carried_takes_drop(frame->result);
+ frame->entries_live = frame->result_live = false;
+}
+
+/* txn_insert_carried_body takes each put's place after the take before it of its block, the first from the block's entry below the memtable. */
+static void
+txn_insert_carried_body(void *frame_)
+{
+ txn_insert_carried_frame_t *frame = frame_;
+ HostSharedRecord record = host_shared_Held_record(frame->held);
+ uint64 counter = txn_state_counter(record);
+
+ std_vec_new(frame->result, sizeof(TxnInsertCarriedTake), _Alignof(TxnInsertCarriedTake));
+ frame->result_live = true;
+ std_vec_new(&frame->entries, sizeof(txn_insert_carried_entry_t), _Alignof(txn_insert_carried_entry_t));
+ frame->entries_live = true;
+ if (!txn_insert_read_base(frame->io, frame->held, &frame->base, frame->error))
+ return;
+ if (!std_vec_try_reserve_exact(frame->result, frame->puts_len, sizeof(TxnInsertCarriedTake),
+ _Alignof(TxnInsertCarriedTake)))
+ {
+ *frame->error = host_Error_out_of_memory();
+ return;
+ }
+ for (size_t k = 0; k < frame->puts_len; k++)
+ {
+ const TxnInsertCarriedPut *put = &frame->puts[k];
+ TxnInsertRoom room = {.bytes = put->heap_bytes, .page = frame->page->bytes, .most = frame->page->most};
+ uint32 block = (uint32) (put->tid.f0 >> 16);
+ txn_insert_carried_entry_t *entry = NULL;
+ txn_insert_ReplayStep_t step = {.record = record, .base = &frame->base, .room = &room, .counter = counter,
+ .tid = put->tid.f0};
+ TxnMemtableVersionShown command = {.tag = TXN_MEMTABLE_VERSION_SHOWN_COMMAND};
+ TxnMemtableOp op = {0};
+ TxnInsertCarriedTake take = {.block = block};
+ StdVec value;
+
+ for (size_t at = 0; at < frame->entries.len && entry == NULL; at++)
+ if (STD_VEC_AT(&frame->entries, txn_insert_carried_entry_t, at).block == block)
+ entry = &STD_VEC_AT(&frame->entries, txn_insert_carried_entry_t, at);
+ if (entry == NULL)
+ {
+ txn_insert_carried_entry_t seen = {.block = block};
+
+ if (!txn_insert_seen(frame->io, frame->store, record, &frame->base, &room, block, &frame->seen,
+ frame->error))
+ return;
+ seen.has_current = frame->seen.has_current;
+ seen.current = frame->seen.current;
+ frame->seen.has_current = false;
+ txn_insert_Seen_drop(&frame->seen);
+ if (!std_vec_try_reserve(&frame->entries, 1, sizeof(seen), _Alignof(txn_insert_carried_entry_t)))
+ {
+ if (seen.has_current)
+ std_vec_free_Checked(&seen.current, 1, 1);
+ *frame->error = host_Error_out_of_memory();
+ return;
+ }
+ std_vec_push_Checked(&frame->entries, &seen, sizeof(seen), _Alignof(txn_insert_carried_entry_t));
+ entry = &STD_VEC_AT(&frame->entries, txn_insert_carried_entry_t, frame->entries.len - 1);
+ }
+ std_vec_new(&value, 1, 1);
+ switch (txn_insert_replayed(&step, entry->has_current, entry->current.ptr, entry->current.len, &value))
+ {
+ case TXN_MEMTABLE_MAP_STEP_PUT:
+ op.tag = TXN_MEMTABLE_OP_PUT;
+ op.u.put.value = value;
+ std_vec_new(&op.u.put.addresses, sizeof(chunks_TupleU32Locator_t), _Alignof(chunks_TupleU32Locator_t));
+ std_vec_new(&op.u.put.values, sizeof(uint32), _Alignof(uint32));
+ op.u.put.tuple = put->tid;
+ break;
+ case TXN_MEMTABLE_MAP_STEP_REMOVE:
+ std_vec_free_Checked(&value, 1, 1);
+ op.tag = TXN_MEMTABLE_OP_EMPTIED;
+ op.u.emptied = put->tid;
+ break;
+ case TXN_MEMTABLE_MAP_STEP_RETRY:
+ case TXN_MEMTABLE_MAP_STEP_REFUSE:
+ std_vec_free_Checked(&value, 1, 1);
+ *frame->error = txn_insert_unfree(put->tid.f0);
+ return;
+ }
+ std_vec_new(&take.record, 1, 1);
+ txn_memtable_version_encode(&put->writer, &op, TXN_MEMTABLE_LOCK_MODE_UPDATE, command, false, 0, &take.record);
+ if (entry->has_current)
+ std_vec_free_Checked(&entry->current, 1, 1);
+ entry->has_current = op.tag == TXN_MEMTABLE_OP_PUT;
+ if (entry->has_current)
+ {
+ entry->current = op.u.put.value;
+ std_vec_free_Checked(&op.u.put.addresses, sizeof(chunks_TupleU32Locator_t),
+ _Alignof(chunks_TupleU32Locator_t));
+ std_vec_free_Checked(&op.u.put.values, sizeof(uint32), _Alignof(uint32));
+ }
+ std_vec_push_Checked(frame->result, &take, sizeof(take), _Alignof(TxnInsertCarriedTake));
+ }
+ frame->result_live = false;
+ frame->ok = true;
+}
+
+/* txn_insert_carried_takes_drop frees a carry's takes. */
+void
+txn_insert_carried_takes_drop(StdVec *takes)
+{
+ for (size_t k = 0; k < takes->len; k++)
+ std_vec_free_Checked(&STD_VEC_AT(takes, TxnInsertCarriedTake, k).record, 1, 1);
+ std_vec_free_Checked(takes, sizeof(TxnInsertCarriedTake), _Alignof(TxnInsertCarriedTake));
+}
+
+/*
+ * txn_insert_carried_takes returns the free space map versions by which a
+ * carry's puts of other writers take their places in a new storage whose
+ * spill files they go to (P2), each put's block and its take's record, in
+ * the puts' order: each as place_taken takes it after the take before it of
+ * its block, the first from the block's entry below the memtable, so their
+ * chains read as the memtable's would have.
+ */
+bool
+txn_insert_carried_takes(HostIoIo *io, TreesStoreNodeStore store, const HostSharedHeld *held,
+ const TxnInsertCarriedPut *puts, size_t puts_len, const TxnInsertPage *page, StdVec *result,
+ HostError *error)
+{
+ txn_insert_carried_frame_t frame = {.io = io, .store = store, .held = held, .puts = puts, .puts_len = puts_len,
+ .page = page, .result = result, .error = error};
+
+ std_landing(&frame, txn_insert_carried_body, txn_insert_carried_pad);
+ txn_insert_carried_pad(&frame);
+ return frame.ok;
+}
+
+/* txn_insert_redo_spent_closure_0_captures_t is redo_spent's prepare's closure's captures, and what its take did. */
typedef struct txn_insert_redo_spent_closure_0_captures_t
{
TxnMemtableVersionWriter writer;
@@ -3332,6 +3601,7 @@ typedef struct txn_insert_redo_spent_closure_0_captures_t
txn_insert_ReplayStep_t *step;
bool has_roomed;
TxnStatusRoomed roomed;
+ TxnMemtableMapTaken taken;
} txn_insert_redo_spent_closure_0_captures_t;
/* txn_insert_redo_spent_moved is redo_spent's take's then: the counter moved past a tuple id at or past it. */
@@ -3344,23 +3614,20 @@ txn_insert_redo_spent_moved(void *captures)
txn_state_moved(step->record, step->counter, STD_ADD(u64, step->tid, 1, 1479, 55));
}
-/* txn_insert_redo_spent_closure_0 is redo_spent's prepare's closure: the place's take, counted when it took. */
+/* txn_insert_redo_spent_closure_0 is redo_spent's prepare's closure: the place's take. */
static bool
txn_insert_redo_spent_closure_0(void *captures, HostIoIo *io, TxnMemtableTablePreparedState *prepared,
HostError *error)
{
txn_insert_redo_spent_closure_0_captures_t *inside = captures;
HostRow unclaimed = {0};
- TxnMemtableMapTaken taken;
+ TxnMemtableMapRead read = txn_insert_Seen_read(inside->seen, inside->step->base);
- if (!txn_memtable_map_took_Unit(HOST_IO_IO_TAKES_SHORT(io), prepared, 0, inside->writer, false, 0, false,
- unclaimed, 0, inside->seen->has_below, inside->seen->below.ptr,
- inside->seen->below.len, txn_insert_replayed, inside->step, NULL,
- txn_insert_replayed_tuple, inside->step, NULL, txn_insert_redo_spent_moved,
- inside->step, NULL, &taken, error))
- return false;
- return taken != TXN_MEMTABLE_MAP_TAKEN_TOOK ||
- txn_memtable_table_counted(HOST_IO_IO_TAKES_SHORT(io), prepared, inside->has_roomed, inside->roomed, error);
+ return txn_memtable_map_took_Unit(HOST_IO_IO_TAKES_SHORT(io), prepared, 0, inside->writer, inside->has_roomed,
+ inside->roomed, false, 0, false, unclaimed, 0, inside->seen->has_below,
+ inside->seen->below.ptr, inside->seen->below.len, &read, txn_insert_replayed,
+ inside->step, NULL, txn_insert_replayed_tuple, inside->step, NULL,
+ txn_insert_redo_spent_moved, inside->step, NULL, &inside->taken, error);
}
/* txn_insert_redo_spent_frame_t is redo_spent's state across its pad: the block's entries it read. */
@@ -3391,7 +3658,7 @@ txn_insert_redo_spent_body(void *frame_)
uint32 block;
txn_insert_ReplayStep_t step;
txn_insert_redo_spent_closure_0_captures_t inside = {.writer = frame->spent->writer, .seen = &frame->seen,
- .step = &step};
+ .step = &step, .taken = TXN_MEMTABLE_MAP_TAKEN_TOOK};
TxnMemtableMapTake take;
if (!txn_insert_read_base(frame->io, frame->held, &frame->base, frame->error))
@@ -3418,9 +3685,15 @@ txn_insert_redo_spent_body(void *frame_)
step.room = &frame->room;
step.counter = counter;
step.tid = tid;
- frame->ok = txn_memtable_map_prepare_Unit(frame->io, &take, 1, txn_insert_redo_spent_closure_0, &inside,
- NULL, frame->error);
- return;
+ if (!txn_memtable_map_prepare_Unit(frame->io, &take, 1, txn_insert_redo_spent_closure_0, &inside, NULL,
+ frame->error))
+ return;
+ if (inside.taken != TXN_MEMTABLE_MAP_TAKEN_MOVED)
+ {
+ frame->ok = true;
+ return;
+ }
+ txn_insert_Seen_drop(&frame->seen);
}
}
@@ -3436,7 +3709,9 @@ txn_insert_redo_spent_pad(void *frame_)
/*
* txn_insert_redo_spent applies a replayed spent place, with no catalog
* access: it takes the place out of the free space map as the primary's
- * take did, as redo takes a row's, and pushes no version.
+ * take did, as redo takes a row's, and pushes no version, reading the
+ * block's entry and preparing again while its take answers that the entry or
+ * the base moved since the read (bugs row 342).
*/
bool
txn_insert_redo_spent(HostIoIo *io, TreesStoreNodeStore store, const HostSharedHeld *held,
@@ -3580,6 +3855,13 @@ typedef struct txn_insert_redo_frame_t
uint64 tid;
size_t need;
bool pushed;
+
+ /*
+ * mark is the state's mark the entries were read at, and done whether the
+ * window found it unmoved.
+ */
+ std_TupleU64U64_t mark;
+ bool done;
StdVec stored;
bool stored_live;
StdVec kept;
@@ -3599,7 +3881,11 @@ typedef struct txn_insert_redo_frame_t
bool ok;
} txn_insert_redo_frame_t;
-/* txn_insert_redo_refuse is a replayed stored value's step: its entry where the map holds none, else nothing. */
+/*
+ * txn_insert_redo_refuse is a replayed stored value's step: its entry where
+ * the map holds none, else nothing; as txn_insert_stored's, its take has no
+ * read to compare (bugs row 342).
+ */
static TxnMemtableMapStep
txn_insert_redo_refuse(void *captures, bool has_current, const uint8 *current, size_t current_len, StdVec *out)
{
@@ -3623,23 +3909,29 @@ txn_insert_redo_closure_9(void *captures, HostIoIo *io_, TxnMemtableTablePrepare
TxnMemtableMapTaken taken;
TxnMemtableVersionHandle handle;
StdString message;
+ std_TupleU64U64_t mark = txn_values_mark(step.record);
+ TxnMemtableMapRead read = txn_insert_Seen_read(&frame->seen, &frame->base);
- if (!txn_memtable_map_took_Unit(io, prepared, 0, change->writer, false, 0, false, unclaimed, 0,
- frame->seen.has_below, frame->seen.below.ptr, frame->seen.below.len,
- txn_insert_replayed, &step, NULL, txn_insert_replayed_tuple, &step, NULL,
- txn_insert_replayed_moved, &step, NULL, &taken, error))
- return false;
- if (taken == TXN_MEMTABLE_MAP_TAKEN_TOOK &&
- !txn_memtable_table_counted(io, prepared, frame->has_roomed, frame->roomed, error))
+ frame->done = mark.f0 == frame->mark.f0 && mark.f1 == frame->mark.f1;
+ if (!frame->done)
+ return true;
+ if (!txn_memtable_map_took_Unit(io, prepared, 0, change->writer, frame->has_roomed, frame->roomed, false, 0, false,
+ unclaimed, 0, frame->seen.has_below, frame->seen.below.ptr,
+ frame->seen.below.len, &read, txn_insert_replayed, &step, NULL,
+ txn_insert_replayed_tuple, &step, NULL, txn_insert_replayed_moved, &step, NULL,
+ &taken, error))
return false;
+ if (taken == TXN_MEMTABLE_MAP_TAKEN_MOVED)
+ {
+ frame->done = false;
+ return true;
+ }
for (size_t at = 0; frame->toast != NULL && at < frame->entries_len; at++)
{
- if (!txn_memtable_map_finish_Unit(io, prepared, at + 1, change->writer, false, 0, false, NULL, 0,
- txn_insert_redo_refuse, (void *) &frame->entries[at], NULL,
- txn_insert_nothing, NULL, NULL, &taken, error))
- return false;
- if (taken == TXN_MEMTABLE_MAP_TAKEN_TOOK &&
- !txn_memtable_table_counted(io, prepared, frame->has_roomed, frame->roomed, error))
+ if (!txn_memtable_map_finish_Unit(io, prepared, at + 1, change->writer, frame->has_roomed, frame->roomed,
+ false, 0, false, NULL, 0, NULL, txn_insert_redo_refuse,
+ (void *) &frame->entries[at], NULL, txn_insert_nothing, NULL, NULL, &taken,
+ error))
return false;
}
for (size_t at = 0; at < frame->kept.len; at++)
@@ -3647,8 +3939,9 @@ txn_insert_redo_closure_9(void *captures, HostIoIo *io_, TxnMemtableTablePrepare
const txn_insert_TupleU32OptionVecU8_t *kept = &STD_VEC_AT(&frame->kept, txn_insert_TupleU32OptionVecU8_t,
at);
- if (!txn_memtable_map_finish_Unit(io, prepared, 1 + frame->stored.len + at, change->writer, true, frame->lsn,
- kept->has_f1, kept->f1.ptr, kept->f1.len, txn_insert_added, NULL, NULL,
+ if (!txn_memtable_map_finish_Unit(io, prepared, 1 + frame->stored.len + at, change->writer,
+ frame->has_roomed, frame->roomed, true, frame->lsn, kept->has_f1,
+ kept->f1.ptr, kept->f1.len, NULL, txn_insert_added, NULL, NULL,
txn_insert_nothing, NULL, NULL, &taken, error))
return false;
if (taken != TXN_MEMTABLE_MAP_TAKEN_TOOK)
@@ -3657,17 +3950,14 @@ txn_insert_redo_closure_9(void *captures, HostIoIo *io_, TxnMemtableTablePrepare
*error = host_Error_fork((const uint8 *) "XX001", &message);
return false;
}
- if (!txn_memtable_table_counted(io, prepared, frame->has_roomed, frame->roomed, error))
- return false;
}
if (frame->marks.len > 0)
{
StdVec marks = frame->marks;
std_vec_new(&frame->marks, TXN_INSERT_ID);
- if (!txn_memtable_map_named(io, prepared, frame->marking, change->writer, true, frame->lsn, row, &marks,
- error) ||
- !txn_memtable_table_counted(io, prepared, frame->has_roomed, frame->roomed, error))
+ if (!txn_memtable_map_named(io, prepared, frame->marking, change->writer, frame->has_roomed, frame->roomed,
+ true, frame->lsn, row, &marks, error))
return false;
}
if (!frame->pushed)
@@ -3683,14 +3973,28 @@ txn_insert_redo_closure_9(void *captures, HostIoIo *io_, TxnMemtableTablePrepare
return false;
if (frame->has_flagged)
{
- txn_insert_TakeStep_t filled = {.record = step.record, .base = &frame->base, .room = &frame->room,
+ TxnInsertBase flagged_base = frame->base;
+ txn_insert_TakeStep_t filled = {.record = step.record, .base = &flagged_base, .room = &frame->room,
.block = frame->flagged};
- if (!txn_memtable_map_finish_Unit(io, prepared, frame->takes.len - 1, change->writer, false, 0,
- frame->flagged_seen.has_below, frame->flagged_seen.below.ptr,
- frame->flagged_seen.below.len, txn_insert_filled, &filled, NULL,
- txn_insert_nothing, NULL, NULL, &taken, error))
- return false;
+ for (;;)
+ {
+ TxnMemtableMapRead flagged = txn_insert_Seen_read(&frame->flagged_seen, &flagged_base);
+
+ if (!txn_memtable_map_finish_Unit(io, prepared, frame->takes.len - 1, change->writer, frame->has_roomed,
+ frame->roomed, false, 0, frame->flagged_seen.has_below,
+ frame->flagged_seen.below.ptr, frame->flagged_seen.below.len, &flagged,
+ txn_insert_filled, &filled, NULL, txn_insert_nothing, NULL, NULL, &taken,
+ error))
+ return false;
+ if (taken != TXN_MEMTABLE_MAP_TAKEN_MOVED)
+ break;
+ txn_insert_Seen_drop(&frame->flagged_seen);
+ if (!txn_insert_read_base(frame->io, frame->held, &flagged_base, error) ||
+ !txn_insert_seen(frame->io, frame->store, step.record, &flagged_base, &frame->room, frame->flagged,
+ &frame->flagged_seen, error))
+ return false;
+ }
if (taken == TXN_MEMTABLE_MAP_TAKEN_RETRY)
{
std_string_from_Checked(&message, "a free space map's entry is corrupt", 35);
@@ -3771,6 +4075,20 @@ txn_insert_redo_body(void *frame_)
!txn_spill_spilled(frame->io, &change->storage, TXN_WRITTEN_OF_ROWS, top, &spilled, frame->error))
return;
}
+ if (!frame->pushed)
+ {
+ size_t frees = 1 + (change->has_replaced && change->replaced.f0 >> 16 != frame->tid >> 16);
+ size_t freeing = frees * (txn_free_space_most_len(frame->room.most) + 4);
+ uint64 bytes;
+ bool spilled;
+
+ if (!txn_spill_replayed(frame->io, &change->storage, TXN_WRITTEN_OF_FREE, top, freeing, &bytes,
+ frame->error))
+ return;
+ if (bytes >= txn_spill_threshold() &&
+ !txn_spill_spilled(frame->io, &change->storage, TXN_WRITTEN_OF_FREE, top, &spilled, frame->error))
+ return;
+ }
for (;;)
{
uint32 block;
@@ -3787,8 +4105,10 @@ txn_insert_redo_body(void *frame_)
continue;
}
block = (uint32) (frame->tid >> 16);
+ frame->mark = txn_values_mark(record);
if (!txn_insert_seen(frame->io, frame->store, record, &frame->base, &frame->room, block, &frame->seen,
- frame->error))
+ frame->error) ||
+ !host_pause(HOST_SHARED_IO_PAUSES(frame->io), "doltgres-redo-take-seen", frame->error))
return;
std_vec_new(&frame->takes, TXN_INSERT_TAKE);
frame->takes_live = true;
@@ -3836,9 +4156,22 @@ txn_insert_redo_body(void *frame_)
row.f0.ptr = change->key.ptr;
row.f0.len = change->key.len;
row.f1 = frame->need;
- frame->ok = txn_memtable_table_prepare_Unit(frame->io, record, &row, 1, frame->takes.ptr, frame->takes.len,
- true, txn_insert_redo_closure_9, frame, NULL, frame->error);
- return;
+ if (!txn_memtable_table_prepare_Unit(frame->io, record, &row, 1, frame->takes.ptr, frame->takes.len, true,
+ txn_insert_redo_closure_9, frame, NULL, frame->error))
+ return;
+ if (frame->done)
+ {
+ frame->ok = true;
+ return;
+ }
+ if (frame->has_flagged)
+ txn_insert_Seen_drop(&frame->flagged_seen);
+ frame->has_flagged = false;
+ std_vec_free_Checked(&frame->marks, TXN_INSERT_ID);
+ frame->marks_live = false;
+ std_vec_free_Checked(&frame->takes, TXN_INSERT_TAKE);
+ frame->takes_live = false;
+ txn_insert_Seen_drop(&frame->seen);
}
}
@@ -3884,7 +4217,10 @@ txn_insert_redo_pad(void *frame_)
* values has each value's map entry written unless the map holds the id
* already. A row version the checkpoint's runs brought back isn't pushed
* again, while the record's map changes and its tuple id's slot apply either
- * way.
+ * way. The entries a take falls back to are read before its prepare, which
+ * may spill at the delta's budget or meet a fold's install; a window that
+ * finds the state's mark moved since writes nothing and reads them again, so
+ * no take compares against an entry a spill moved out of the memtable.
*/
bool
txn_insert_redo(HostIoIo *io, TreesStoreNodeStore store, const HostSharedHeld *held, const TxnRecordRowChange *change,
@@ -3899,3 +4235,4167 @@ txn_insert_redo(HostIoIo *io, TreesStoreNodeStore store, const HostSharedHeld *h
txn_insert_redo_pad(&frame);
return frame.ok;
}
+
+#ifdef DOLTGRES_TEST_TXN
+#include "doltgres/chunks/trailer.h"
+#include "doltgres/repo/lib.h"
+#include "doltgres/simc/simc.h"
+#include "doltgres/std/test.h"
+#include "doltgres/txn/blobs.h"
+#include "doltgres/txn/change.h"
+#include "doltgres/txn/memtable/branch.h"
+#include "doltgres/txn/record.h"
+#include "doltgres/txn/runs.h"
+#include "doltgres/txn/values.h"
+#include "doltgres/txn/visible.h"
+
+/*
+ * txn_insert_tests_taken inserts a row as a test does, taking its key's
+ * stripe with a test's Waits and no resolve, and returns its tuple id.
+ */
+bool
+txn_insert_tests_taken(HostIoIo *io, TreesStoreNodeStore store, const HostSharedHeld *held, TxnInsertInsert *row,
+ const TxnInsertRoom *room, const HostSharedHeld *toast, const TxnValuesEntry *toast_entries,
+ size_t toast_entries_len, uint64 *result, HostError *error)
+{
+ static const StdFmtPiece pieces[] = {STD_FMT_LIT("a test's insert gave "), STD_FMT_ARG(0)};
+ HostIoWaits waits = host_io_Waits_testing();
+ TxnInsertKeyed keyed = {.stripe = txn_change_stripe(&row->storage, row->key.ptr, row->key.len, true)};
+ TxnInsertInserted inserted;
+ TxnInsertInsertedRef other = {&inserted, {NULL, std_unit_Debug_fmt}};
+ StdFmtArg args[] = {{&other, txn_insert_Inserted_Debug_fmt}};
+ StdFmtArguments arguments = {pieces, lengthof(pieces), args};
+ StdString message;
+
+ if (!txn_insert_insert(io, store, held, row, room, NULL, toast, toast_entries, toast_entries_len, keyed, &waits,
+ &inserted, error))
+ return false;
+ if (inserted.tag == TXN_INSERT_INSERTED_TOOK)
+ {
+ *result = inserted.u.took.tuple;
+ return true;
+ }
+ std_fmt_format_Checked(&message, &arguments);
+ *error = host_Error_fork((const uint8 *) "XX000", &message);
+ return false;
+}
+
+/* txn_insert_tests_stored returns a test's storage in pg_default, by its database. */
+HostStorage
+txn_insert_tests_stored(uint32 database)
+{
+ return (HostStorage)
+ {
+ .tablespace = HOST_PG_DEFAULT, .database = database, .relation = 16384
+ };
+}
+
+/* txn_insert_tests_row returns a keyed row's insert by its key's number, written by a transaction's first command. */
+TxnInsertInsert
+txn_insert_tests_row(HostStorage storage, uint64 n)
+{
+ TxnInsertInsert row = {.storage = storage, .table = 16385, .put = {.heap_bytes = 32}, .writer = {.xid = 41},
+ .logged = true};
+ uint8 key[8];
+
+ std_u64_to_be_bytes(n, key);
+ std_slice_to_vec_Checked(&row.key, key, sizeof(key), 1, 1);
+ std_slice_to_vec_Checked(&row.put.value, (const uint8 *) "row", 3, 1, 1);
+ std_vec_new(&row.put.addresses, sizeof(chunks_TupleU32Locator_t), _Alignof(chunks_TupleU32Locator_t));
+ std_vec_new(&row.put.values, sizeof(uint32), _Alignof(uint32));
+ std_vec_new(&row.kept, sizeof(size_t), _Alignof(size_t));
+ return row;
+}
+
+/* TXN_INSERT_TESTS_UNWRAP_FAILED is a Result's unwrap's panic on a HostError at a line and column. */
+#define TXN_INSERT_TESTS_UNWRAP_FAILED(line, col, error) \
+ STD_RESULT_EXPECT_FAILED((line), (col), "called `Result::unwrap()` on an `Err` value", (error), \
+ host_Error_Debug_fmt)
+
+/* TXN_INSERT_TESTS_PAGE is an 8 kB heap page as redo knows it: its bytes past its header and its most tuples. */
+static const TxnInsertPage TXN_INSERT_TESTS_PAGE = {.bytes = 8168, .most = 291};
+
+/* txn_insert_tests_room returns what a row of some heap bytes needs of an 8 kB heap page, with no fill factor's reserve. */
+static TxnInsertRoom
+txn_insert_tests_room(uint64 bytes)
+{
+ TxnInsertRoom room = {.bytes = bytes, .page = 8168, .most = 291};
+
+ return room;
+}
+
+/* txn_insert_tests_repository makes a test's repository of a database and a reader of the shared state, unwrapped at the lines a case gives. */
+static void
+txn_insert_tests_repository(HostIoIo *io, uint32 database, uint32 create_line, uint32 create_col, uint32 read_line,
+ uint32 read_col, RepoRepo *repo, HostSharedReader *reading)
+{
+ ChunksErrorError failed;
+ HostError error;
+
+ if (!repo_Repo_create(io, database, HOST_PG_DEFAULT, repo, &failed))
+ STD_RESULT_EXPECT_FAILED(create_line, create_col, "called `Result::unwrap()` on an `Err` value", &failed,
+ chunks_error_Error_Debug_fmt);
+ if (!host_shared_read(io, reading, &error))
+ TXN_INSERT_TESTS_UNWRAP_FAILED(read_line, read_col, &error);
+}
+
+/* txn_insert_tests_stated returns the state of a storage, unwrapped at a line and column. */
+static HostSharedHeld
+txn_insert_tests_stated(HostIoIo *io, const RepoRepo *repo, const HostSharedReader *reading, HostStorage storage,
+ uint32 line, uint32 col)
+{
+ HostSharedHeld held;
+ HostError error;
+
+ if (!txn_state_state(io, repo, reading, &storage, TXN_STATE_PERSISTENCE_PERMANENT, false, NULL, &held, &error))
+ TXN_INSERT_TESTS_UNWRAP_FAILED(line, col, &error);
+ return held;
+}
+
+/* txn_insert_tests_took inserts a row as taken does, unwrapped at a line and column, and returns its tuple id. */
+static uint64
+txn_insert_tests_took(HostIoIo *io, TreesStoreNodeStore store, const HostSharedHeld *held, TxnInsertInsert row,
+ uint64 bytes, const HostSharedHeld *toast, const TxnValuesEntry *entries, size_t entries_len,
+ uint32 line, uint32 col)
+{
+ TxnInsertRoom room = txn_insert_tests_room(bytes);
+ HostError error;
+ uint64 tid;
+
+ if (!txn_insert_tests_taken(io, store, held, &row, &room, toast, entries, entries_len, &tid, &error))
+ TXN_INSERT_TESTS_UNWRAP_FAILED(line, col, &error);
+ return tid;
+}
+
+/* txn_insert_tests_ended ends the test's transaction as its commit's callbacks do, unwrapped at a line and column. */
+static void
+txn_insert_tests_ended(HostIoIo *io, uint32 line, uint32 col)
+{
+ HostError error;
+
+ if (!txn_written_ended(io, &error))
+ TXN_INSERT_TESTS_UNWRAP_FAILED(line, col, &error);
+}
+
+/* txn_insert_tests_inserted returns the tuple ids a new storage's inserts of a row's heap bytes take, one row a key. */
+static StdVec
+txn_insert_tests_inserted(HostIoIo *io, uint32 database, uint64 bytes, uint64 rows)
+{
+ RepoRepo repo;
+ HostSharedReader reading;
+ HostSharedHeld held;
+ TreesStoreReader store;
+ StdVec tids;
+
+ txn_insert_tests_repository(io, database, 1740, 91, 1741, 46, &repo, &reading);
+ held = txn_insert_tests_stated(io, &repo, &reading, txn_insert_tests_stored(database), 1742, 110);
+ store = repo_Repo_reader(&repo, HOST_CACHE_PLACEMENT_SERVER, &reading);
+ std_vec_with_capacity_Checked(&tids, rows, sizeof(uint64), _Alignof(uint64));
+ for (uint64 n = 0; n < rows; n++)
+ {
+ uint64 tid = txn_insert_tests_took(io, TREES_STORE_READER_NODE_STORE(&store), &held,
+ txn_insert_tests_row(txn_insert_tests_stored(database), n), bytes,
+ NULL, NULL, 0, 1745, 91);
+
+ std_vec_push_Checked(&tids, &tid, sizeof(tid), _Alignof(uint64));
+ }
+ host_shared_Held_drop(&held);
+ host_shared_Reader_drop(&reading);
+ repo_Repo_drop(&repo);
+ return tids;
+}
+
+/* txn_insert_tests_u64s_is asserts at a line and column that some u64s are those wanted, as slices compare. */
+static void
+txn_insert_tests_u64s_is(uint32 line, uint32 col, const uint64 *got, size_t got_len, const uint64 *want,
+ size_t want_len)
+{
+ StdVec left = {(void *) got, got_len, got_len};
+ StdVec right = {(void *) want, want_len, want_len};
+
+ STD_ASSERT_EQ(line, col, got_len == want_len && memcmp(got, want, got_len * sizeof(uint64)) == 0, &left,
+ chunks_trailer_VecU64_Debug_fmt, &right, chunks_trailer_VecU64_Debug_fmt);
+}
+
+/*
+ * PLAN item 2's allocation on a new table, heap's packing: rows of 28 heap
+ * bytes take (0,1) up to (0,291), a page's most tuples, then (1,1); rows of
+ * 240 heap bytes fill a page by its bytes at its 34th row, and the 35th goes
+ * to (1,1).
+ */
+void
+test_txn_insert_tests_inserts_take_heaps_tuple_ids_by_offsets_and_by_bytes(void)
+{
+ static const uint64 first[] = {1, 2};
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ StdVec narrow = txn_insert_tests_inserted(io, 9301, 28, 292);
+ StdVec wide;
+
+ txn_insert_tests_u64s_is(1756, 9, narrow.ptr, 2, first, lengthof(first));
+ STD_ASSERT_EQ_U64(1757, 9, STD_VEC_AT(&narrow, uint64, 290), 291);
+ STD_ASSERT_EQ_U64(1758, 9, STD_VEC_AT(&narrow, uint64, 291), 1 << 16 | 1);
+ wide = txn_insert_tests_inserted(io, 9302, 240, 35);
+ STD_ASSERT_EQ_U64(1760, 9, STD_VEC_AT(&wide, uint64, 33), 34);
+ STD_ASSERT_EQ_U64(1761, 9, STD_VEC_AT(&wide, uint64, 34), 1 << 16 | 1);
+ txn_insert_tests_ended(io, 1762, 35);
+ std_vec_free_Checked(&wide, sizeof(uint64), _Alignof(uint64));
+ std_vec_free_Checked(&narrow, sizeof(uint64), _Alignof(uint64));
+ simc_TestIo_drop(&test_io);
+}
+
+/* txn_insert_tests_last_page_t is the last page case's captures: its storage, its state and store, the rows put, and the block the last took. */
+typedef struct txn_insert_tests_last_page_t
+{
+ HostStorage storage;
+ TreesStoreNodeStore store;
+ const HostSharedHeld *held;
+ uint64 n;
+ bool has_target;
+ uint32 target;
+} txn_insert_tests_last_page_t;
+
+/* txn_insert_tests_last_page_put is the last page case's put: the next key's row of some heap bytes, at the block the last row took. */
+static uint64
+txn_insert_tests_last_page_put(txn_insert_tests_last_page_t *c, HostIoIo *io, uint64 bytes)
+{
+ TxnInsertInsert row;
+ uint64 tid;
+
+ c->n++;
+ row = txn_insert_tests_row(c->storage, c->n);
+ row.has_target = c->has_target;
+ row.target = c->target;
+ tid = txn_insert_tests_took(io, c->store, c->held, row, bytes, NULL, NULL, 0, 1781, 93);
+ c->has_target = true;
+ c->target = (uint32) (tid >> 16);
+ return tid;
+}
+
+/*
+ * PLAN item 2's allocation in RelationGetBufferForTuple's order, with a fresh
+ * page left by its bytes handing its counter offsets to the map: rows of 3000
+ * heap bytes take (0,1), (0,2), then (1,1), (1,2), (2,1); rows that fit the
+ * last page go there, (2,2) and (2,3); and a row the last page can't hold
+ * goes to the lowest page whose room the map shows, the page left first, at
+ * its first counter offset, (0,3).
+ */
+void
+test_txn_insert_tests_a_row_the_last_page_cant_hold_goes_to_the_lowest_page_left_with_room(void)
+{
+ static const uint64 sizes[] = {3000, 3000, 3000, 3000, 3000, 2000, 3000, 2000};
+ static const uint64 want[] = {1, 2, 1 << 16 | 1, 1 << 16 | 2, 2 << 16 | 1, 2 << 16 | 2, 2 << 16 | 3, 3};
+ SimcTestIo test_io = simc_io();
+ HostIoIo *io = &test_io.io;
+ RepoRepo repo;
+ HostSharedReader reading;
+ HostSharedHeld held;
+ TreesStoreReader store;
+ txn_insert_tests_last_page_t c = {.storage = txn_insert_tests_stored(9303)};
+ uint64 taken[lengthof(sizes)];
+
+ txn_insert_tests_repository(io, 9303, 1773, 87, 1774, 46, &repo, &reading);
+ held = txn_insert_tests_stated(io, &repo, &reading, c.storage, 1775, 95);
+ store = repo_Repo_reader(&repo, HOST_CACHE_PLACEMENT_SERVER, &reading);
+ c.store = TREES_STORE_READER_NODE_STORE(&store);
+ c.held = &held;
+ for (size_t k = 0; k < lengthof(sizes); k++)
+ taken[k] = txn_insert_tests_last_page_put(&c, io, sizes[k]);
+ txn_insert_tests_u64s_is(1787, 9, taken, lengthof(taken), want, lengthof(want));
+ txn_insert_tests_ended(io, 1788, 35);
+ host_shared_Held_drop(&held);
+ host_shared_Reader_drop(&reading);
+ repo_Repo_drop(&repo);
+ simc_TestIo_drop(&test_io);
+}
+
+/* txn_insert_tests_logged_tag_t is Logged's variants. */
+typedef enum txn_insert_tests_logged_tag_t
+{
+ /* A row change with its record's end. */
+ TXN_INSERT_TESTS_LOGGED_ROW,
+ /* A fresh page's leave. */
+ TXN_INSERT_TESTS_LOGGED_LEFT,
+ /* A chunk with its record's end. */
+ TXN_INSERT_TESTS_LOGGED_CHUNK,
+} txn_insert_tests_logged_tag_t;
+
+/*
+ * txn_insert_tests_logged_t is Logged: one of a database's logged writes, as
+ * redo reads it back, a row change with its record's end, a fresh page's
+ * leave, or a chunk with its record's end.
+ */
+typedef struct txn_insert_tests_logged_t
+{
+ txn_insert_tests_logged_tag_t tag;
+ union
+ {
+ TxnRecordRowChange row;
+ TxnRecordPageLeft left;
+ TxnRecordChunkWrite chunk;
+ } u;
+ HostLsn end;
+} txn_insert_tests_logged_t;
+
+/* txn_insert_tests_logged_drop drops a logged write. */
+static void
+txn_insert_tests_logged_drop(txn_insert_tests_logged_t *write)
+{
+ if (write->tag == TXN_INSERT_TESTS_LOGGED_ROW)
+ txn_record_RowChange_drop(&write->u.row);
+ else if (write->tag == TXN_INSERT_TESTS_LOGGED_CHUNK)
+ txn_record_ChunkWrite_drop(&write->u.chunk);
+}
+
+/* txn_insert_tests_logged_free frees a Vec and its writes. */
+static void
+txn_insert_tests_logged_free(StdVec *writes)
+{
+ for (size_t k = 0; k < writes->len; k++)
+ txn_insert_tests_logged_drop(&STD_VEC_AT(writes, txn_insert_tests_logged_t, k));
+ std_vec_free_Checked(writes, sizeof(txn_insert_tests_logged_t), _Alignof(txn_insert_tests_logged_t));
+}
+
+/* txn_insert_tests_logged returns the writes the simulation logged for a database's storages and its repository, in their order. */
+static StdVec
+txn_insert_tests_logged(uint32 database)
+{
+ StdVec logged = simc_logged();
+ StdVec writes;
+
+ std_vec_new(&writes, sizeof(txn_insert_tests_logged_t), _Alignof(txn_insert_tests_logged_t));
+ for (size_t k = 0; k < logged.len; k++)
+ {
+ simc_TupleU8VecU8Lsn_t *one = &STD_VEC_AT(&logged, simc_TupleU8VecU8Lsn_t, k);
+ txn_insert_tests_logged_t write = {.end = one->f2};
+ HostWalKind kind;
+ HostError error;
+ bool kept = false;
+
+ if (!host_WalKind_of(one->f0, &kind))
+ kind = HOST_WAL_KIND_REPOSITORY_CREATED;
+ if (kind == HOST_WAL_KIND_ROW_CHANGE)
+ {
+ write.tag = TXN_INSERT_TESTS_LOGGED_ROW;
+ if (!txn_record_RowChange_decode(one->f1.ptr, one->f1.len, &write.u.row, &error))
+ host_Error_drop(&error);
+ else if (!(kept = write.u.row.storage.database == database))
+ txn_record_RowChange_drop(&write.u.row);
+ }
+ else if (kind == HOST_WAL_KIND_PAGE_LEFT)
+ {
+ write.tag = TXN_INSERT_TESTS_LOGGED_LEFT;
+ if (!txn_record_PageLeft_decode(one->f1.ptr, one->f1.len, &write.u.left, &error))
+ host_Error_drop(&error);
+ else
+ kept = write.u.left.storage.database == database;
+ }
+ else if (kind == HOST_WAL_KIND_CHUNK)
+ {
+ write.tag = TXN_INSERT_TESTS_LOGGED_CHUNK;
+ if (!txn_record_ChunkWrite_decode(one->f1.ptr, one->f1.len, &write.u.chunk, &error))
+ host_Error_drop(&error);
+ else if (!(kept = write.u.chunk.repository == database))
+ txn_record_ChunkWrite_drop(&write.u.chunk);
+ }
+ if (kept)
+ std_vec_push_Checked(&writes, &write, sizeof(write), _Alignof(txn_insert_tests_logged_t));
+ std_vec_free_Checked(&one->f1, 1, 1);
+ }
+ std_vec_free_Checked(&logged, sizeof(simc_TupleU8VecU8Lsn_t), _Alignof(simc_TupleU8VecU8Lsn_t));
+ return writes;
+}
+
+/* txn_insert_tests_count returns how many of some logged writes are of a variant. */
+static size_t
+txn_insert_tests_count(const StdVec *writes, txn_insert_tests_logged_tag_t tag)
+{
+ size_t count = 0;
+
+ for (size_t k = 0; k < writes->len; k++)
+ count += STD_VEC_AT(writes, txn_insert_tests_logged_t, k).tag == tag;
+ return count;
+}
+
+/* txn_insert_tests_mapped_t is an Option