SurrealDB DEFINE FIELD IF NOT EXISTS: Potential Write Blocking in SCHEMAFULL Mode
0 reputation · 02 Jan 2022, 02:28 UTC
0 reputation · 02 Jan 2022, 02:28 UTC
When migrating a small application to a newer SurrealDB instance, a common strategy is to add a new column with DEFINE FIELD IF NOT EXISTS so that existing rows keep returning NULL until updated. This approach works smoothly in schemaless mode, but the behavior in SCHEMAFULL mode is unclear.
In SCHEMAFULL mode, the command may trigger a validation pass that could momentarily lock the table or abort concurrent transactions. Additionally, live queries (LIVE SELECT) and changefeeds may not emit events for the new field until a value is explicitly written, potentially causing a brief period of inconsistency for real‑time consumers.
To ensure zero downtime, we need to understand whether the operation blocks concurrent writes, whether it can abort in-flight transactions, and how live queries react during the transition.
Questions:
DEFINE FIELD IF NOT EXISTS acquire an exclusive lock that blocks concurrent inserts?33775 reputation · 02 Jan 2022, 12:28 UTC
In SCHEMAFULL mode, DEFINE FIELD IF NOT EXISTS behaves as a schema‑altering transaction that:
INSERT, UPDATE, and DELETE statements.LIVE SELECT) and changefeeds continue to emit events based on the existing schema; they do not generate a synthetic event for the newly added field. The field appears as NULL in subsequent document updates, and a changefeed will only emit an event when a document is actually updated to set a non‑NULL value for that field.Confirmed (based on SurrealDB’s transaction model):
Likely (requires verification in your deployment):
BEGIN; INSERT INTO your_table VALUES (...); COMMIT;(repeat).
DEFINE FIELD IF NOT EXISTS your_table.new_field TYPE string;
CHANGEFEED FOR your_table;then run the same
DEFINE FIELD statement. Observe that no event is emitted for new_field until you subsequently update a row to set a non‑NULL value.If you observe that the lock duration exceeds acceptable limits, please provide the average write throughput (inserts/updates/deletes per second) on the table during the test. This information determines whether reducing concurrent load or adjusting lock timeout would be necessary to meet your zero‑downtime goal.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.