Changeset: c9e5babc8412 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/c9e5babc8412
Added Files:
sql/test/BugTracker-2026/Tests/8083-savepoint-parent-update-bug.test
sql/test/BugTracker-2026/Tests/8083-storage-catalog-delta-chain.test
sql/test/BugTracker-2026/Tests/8083-storage-dict-append-retarget.test
Modified Files:
sql/storage/bat/bat_storage.c
sql/test/BugTracker-2026/Tests/All
Branch: default
Log Message:
fixes for issue #8083, and remving conflicts on ddl statements
diffs (truncated from 1224 to 300 lines):
diff --git a/sql/storage/bat/bat_storage.c b/sql/storage/bat/bat_storage.c
--- a/sql/storage/bat/bat_storage.c
+++ b/sql/storage/bat/bat_storage.c
@@ -42,8 +42,6 @@ static lng merge_delta( sql_delta *obat)
* deleted && TS > tr->ts && OLDTS < tr->ts deleted after current
transaction
*/
-#define JUST_CHANGED(tr, seg) (seg->ts == tr->tid)
-
#define VALID_4_READ(TS,tr) \
((TS == tr->tid) || (tr->parent && tr_version_of_parent(tr, TS)) || TS
< tr->ts)
@@ -171,6 +169,49 @@ trans_add_table(sql_trans *tr, sql_base
trans_add(tr, b, data, cleanup, commit, log, false);
}
+/* Return the change this transaction already registered for data, if any.
+ * Callers use this both to avoid duplicate registrations and to retarget a
+ * change when an append installs a fresh delta. */
+static sql_change *
+trans_find_change(sql_trans *tr, void *data)
+{
+ sql_change *found = NULL;
+
+ if (!tr) /* an uninitialized transaction has no registered changes */
+ return NULL;
+ MT_lock_set(&tr->lock);
+ if (tr->changes) {
+ for (node *n = tr->changes->h; n && !found; n = n->next) {
+ sql_change *c = n->data;
+ if (c->data == data)
+ found = c;
+ }
+ }
+ MT_lock_unset(&tr->lock);
+ return found;
+}
+
+/* An append can make a fresh delta via tr_dup_delta(); make the already
+ * registered change point at it so commit/log/rollback all use the live
+ * object instead of the stale head. */
+static void
+trans_retarget_change(sql_trans *tr, void *data, void *ndata)
+{
+ if (!tr || data == ndata)
+ return;
+ MT_lock_set(&tr->lock);
+ if (tr->changes) {
+ for (node *n = tr->changes->h; n; n = n->next) {
+ sql_change *c = n->data;
+ if (c->data == data) {
+ c->data = ndata;
+ break;
+ }
+ }
+ }
+ MT_lock_unset(&tr->lock);
+}
+
static int
tc_gc_seg( sql_store Store, sql_change *change, ulng oldest)
{
@@ -1907,14 +1948,192 @@ for_append_val(column_storage *cs, void
return i;
}
+/* Catalogue deltas form a chain protected by the column/idx lock. Every
+ * traversal that can observe or mutate the chain must take that lock, even
+ * though the head itself is an atomic pointer (appends use a CAS). */
+static void
+lock_delta_chain(sql_trans *tr, sql_column *c, sql_idx *i)
+{
+ assert(c || i);
+ if (c)
+ lock_column(tr->store, c);
+ else
+ lock_idx(tr->store, i);
+}
+
+static void
+unlock_delta_chain(sql_trans *tr, sql_column *c, sql_idx *i)
+{
+ assert(c || i);
+ if (c)
+ unlock_column(tr->store, c);
+ else
+ unlock_idx(tr->store, i);
+}
+
+static bool
+bat_contains_oid(BAT *b, oid o)
+{
+ struct canditer ci;
+ oid r;
+
+ if (!b || !BATcount(b))
+ return false;
+ canditer_init(&ci, NULL, b);
+ while (!is_oid_nil(r = canditer_next(&ci))) {
+ if (r == o)
+ return true;
+ }
+ return false;
+}
+
+/* Return true if the delta has an update for any rid in tids. */
+static bool
+delta_has_update_in_tids(sql_delta *d, BAT *tids)
+{
+ BAT *ui;
+ BATiter uii;
+ bool found = false;
+
+ assert(d != NULL);
+ if (!BATcount(tids) || !d->cs.ucnt)
+ return false;
+ if (!d->cs.uibid) {
+ /* ucnt without an id BAT means we cannot prove the rows are
+ * disjoint; treat it as a conflict. */
+ TRC_ERROR(SQL_STORE, "delta_has_update_in_tids: missing
update-id BAT\n");
+ return true;
+ }
+ if (!(ui = temp_descriptor(d->cs.uibid))) {
+ TRC_ERROR(SQL_STORE, "delta_has_update_in_tids: cannot open
update-id BAT\n");
+ return true; /* be conservative on allocation failure */
+ }
+ uii = bat_iterator(ui);
+ for (BUN i = 0; i < BATcount(ui) && !found; i++)
+ found = bat_contains_oid(tids, BUNtoid(&uii, i));
+ bat_iterator_end(&uii);
+ bat_destroy(ui);
+ return found;
+}
+
+/* Return true if the delta has an update for any rid in [start, start+cnt). */
+static bool
+delta_has_update_in_range(sql_delta *d, oid start, size_t cnt)
+{
+ BAT *ui;
+ BATiter uii;
+ bool found = false;
+
+ assert(d != NULL);
+ if (!cnt || !d->cs.ucnt)
+ return false;
+ if (!d->cs.uibid) {
+ /* ucnt without an id BAT means we cannot prove the rows are
+ * disjoint; treat it as a conflict. */
+ TRC_ERROR(SQL_STORE, "delta_has_update_in_range: missing
update-id BAT\n");
+ return true;
+ }
+ if (!(ui = temp_descriptor(d->cs.uibid))) {
+ TRC_ERROR(SQL_STORE, "delta_has_update_in_range: cannot open
update-id BAT\n");
+ return true; /* be conservative on allocation failure */
+ }
+ uii = bat_iterator(ui);
+ for (BUN i = 0; i < BATcount(ui) && !found; i++) {
+ oid r = BUNtoid(&uii, i);
+ if (r >= start && (r - start) < cnt)
+ found = true;
+ }
+ bat_iterator_end(&uii);
+ bat_destroy(ui);
+ return found;
+}
+
+/* Return true if another transaction's delta in the chain has an update for
rid. */
+static bool
+update_conflicts_with_other(sql_trans *tr, ATOMIC_PTR_TYPE *data,
+ sql_column *c, sql_idx *i, oid rid)
+{
+ sql_delta *d;
+ bool conflict = false;
+
+ if (!tr) /* no transaction, so no conflict to report */
+ return false;
+ assert(data != NULL);
+ lock_delta_chain(tr, c, i);
+ for (d = ATOMIC_PTR_GET(data); d; d = d->next) {
+ if (d->cs.ts == tr->tid || (tr->parent &&
tr_version_of_parent(tr, d->cs.ts)))
+ continue;
+ if (VALID_4_READ(d->cs.ts, tr))
+ break;
+ if (delta_has_update_in_range(d, rid, 1)) {
+ conflict = true;
+ break;
+ }
+ }
+ unlock_delta_chain(tr, c, i);
+ return conflict;
+}
+
+/* BAT variant: one locked chain walk for all target rows. */
+static bool
+update_conflicts_with_tids(sql_trans *tr, ATOMIC_PTR_TYPE *data,
+ sql_column *c, sql_idx *i, BAT *tids)
+{
+ sql_delta *d;
+ bool conflict = false;
+
+ if (!tr || !BATcount(tids))
+ return false;
+ assert(data != NULL);
+ lock_delta_chain(tr, c, i);
+ for (d = ATOMIC_PTR_GET(data); d; d = d->next) {
+ if (d->cs.ts == tr->tid || (tr->parent &&
tr_version_of_parent(tr, d->cs.ts)))
+ continue;
+ if (VALID_4_READ(d->cs.ts, tr))
+ break;
+ if (delta_has_update_in_tids(d, tids)) {
+ conflict = true;
+ break;
+ }
+ }
+ unlock_delta_chain(tr, c, i);
+ return conflict;
+}
+
+/* Return this transaction's delta in the chain, if any. Our delta may sit
+ * below another transaction's uncommitted delta, so the head is not enough. */
+static sql_delta *
+find_own_delta(sql_delta *d, sql_trans *tr)
+{
+ if (!tr) /* no transaction, so no own delta */
+ return NULL;
+ for (; d; d = d->next) {
+ if (d->cs.ts == tr->tid)
+ return d;
+ if (VALID_4_READ(d->cs.ts, tr))
+ break;
+ }
+ return NULL;
+}
+
static int
-cs_update_val( sql_trans *tr, sql_delta **batp, sql_table *t, oid rid, void
*upd, int is_new)
+cs_update_val( sql_trans *tr, sql_delta **batp, sql_table *t,
+ /* delta chain, for concurrent catalogue conflict checks */
+ ATOMIC_PTR_TYPE *data, sql_column *c, sql_idx *i,
+ oid rid, void *upd, int is_new)
{
void *oupd = upd;
sql_delta *bat = *batp;
column_storage *cs = &bat->cs;
storage *s = ATOMIC_PTR_GET(&t->data);
assert(!is_oid_nil(rid));
+ /* validate the delta chain passed in for conflict checks */
+ assert(data != NULL);
+ assert(bat->cs.ts == tr->tid);
+ if (!data) {
+ TRC_ERROR(SQL_STORE, "cs_update_val: null delta chain\n");
+ return LOG_ERR;
+ }
int inplace = is_new || cs->cleared || segments_is_append (s->segs->h,
tr, rid);
if (cs->st == ST_DICT) {
@@ -1934,10 +2153,15 @@ cs_update_val( sql_trans *tr, sql_delta
GDKfree(upd);
return LOG_CONFLICT;
}
+ /* System tables allow concurrent deltas, so a write-write
+ * conflict on the same row must be detected explicitly. */
+ if (isSystemTable(t) && update_conflicts_with_other(tr, data,
c, i, rid)) {
+ if (oupd != upd)
+ GDKfree(upd);
+ return LOG_CONFLICT;
+ }
BAT *ui, *uv;
- /* When we go to smaller grained update structures we should
check for concurrent updates on this column ! */
- /* currently only one update delta is possible */
if (cs_real_update_bats(cs, &ui, &uv) != LOG_OK) {
if (oupd != upd)
GDKfree(upd);
@@ -1980,11 +2204,15 @@ cs_update_val( sql_trans *tr, sql_delta
}
static int
-delta_update_val( sql_trans *tr, sql_delta **bat, sql_table *t, oid rid, void
*upd, int is_new)
+delta_update_val( sql_trans *tr, sql_delta **bat, sql_table *t,
+ /* delta chain and its lock, forwarded to cs_update_val */
+ ATOMIC_PTR_TYPE *data, sql_column *c, sql_idx *i,
+ oid rid, void *upd, int is_new)
{
int res = LOG_OK;
lock_table(tr->store, t);
- res = cs_update_val(tr, bat, t, rid, upd, is_new);
+ /* forward the delta chain for conflict checks */
+ res = cs_update_val(tr, bat, t, data, c, i, rid, upd, is_new);
unlock_table(tr->store, t);
return res;
}
@@ -2038,6 +2266,62 @@ destroy_delta(sql_delta *b, bool recursi
_DELETE(b);
}
+/* Create a new delta on top of the current head, retrying if another
+ * transaction installs a delta concurrently. Catalogue tables allow
+ * concurrent writers; the same-row check in cs_update_val prevents two
+ * transactions from updating the same row.
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]