From 36814ac87b055e5f88c065cb803c968b74e02f6b Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Wed, 2 Dec 2026 08:46:47 +0000 Subject: [PATCH] docs: clarify Enterprise migration writes and row caches (#5348) The Attach Database guide now shows how to switch writers to the Enterprise SDK or SQL and how to request a fresh read. It also shows how to retain a tagged snapshot for cached row addresses or rebuild a cache with business keys or stable row IDs that are already enabled on the target table. The saved Enterprise 1.12.0, 1.13.0, and 1.14.0 editions omit this migration guidance. The six Python examples ran with Enterprise 1.16.0 and Python SDK 0.42.1 on both directory and manifest catalogs. With a configured 60-second weak read interval, a primed reader still saw v1 after a bare URI commit. The zero interval and `checkout_latest()` each read v2 in independent cases. Related issues are [OSS-3150](https://linear.app/lancedb/issue/OSS-3150) and [OSS-3151](https://linear.app/lancedb/issue/OSS-3151). --- docs/web-tests/py/test_attach_database.py | 130 ++++++++++++++++++++++ docs/web/ingestion/attach-database.mdx | 129 +++++++++++++++++++++ docs/web/snippets/attach_database.mdx | 14 +++ 3 files changed, 273 insertions(+) create mode 100644 docs/web-tests/py/test_attach_database.py create mode 100644 docs/web/snippets/attach_database.mdx diff --git a/docs/web-tests/py/test_attach_database.py b/docs/web-tests/py/test_attach_database.py new file mode 100644 index 000000000..085c3d5bf --- /dev/null +++ b/docs/web-tests/py/test_attach_database.py @@ -0,0 +1,130 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Copyright The LanceDB Authors + +from datetime import timedelta + +import lancedb + + +def test_attached_snapshot_cache(tmp_db): + db = tmp_db + table = db.create_table("samples", [{"sample_id": 101}]) + table.add([{"sample_id": 102}]) + table_name = table.name + namespace_path = table.namespace + branch = table.current_branch() + cached_version = table.version + cached_address = ( + table.search() + .where("sample_id = 102") + .select(["_rowaddr"]) + .limit(1) + .to_list()[0]["_rowaddr"] + ) + table.add([{"sample_id": 103}]) + + # --8<-- [start:attach_tag_cached_version] + tag = "training-run-42" + table = db.open_table(table_name, namespace_path=namespace_path, branch=branch) + table.tags.create(tag, cached_version) + # --8<-- [end:attach_tag_cached_version] + + table.optimize() + assert table.count_rows() == 3 + assert table.search().where(f"_rowaddr = {cached_address}").limit(1).to_list() == [] + dataset = table.to_lance() + dataset.cleanup_old_versions( + older_than=timedelta(0), error_if_tagged_old_versions=False + ) + assert {version["version"] for version in dataset.versions()} == { + cached_version, + table.version, + } + + # --8<-- [start:attach_read_cached_snapshot] + snapshot = db.open_table(table_name, namespace_path=namespace_path, branch=branch) + snapshot.checkout(tag) + if snapshot.version != cached_version: + raise RuntimeError(f"Tag {tag} no longer points to version {cached_version}") + + rows = snapshot.search().where(f"_rowaddr = {cached_address}").limit(1).to_list() + # --8<-- [end:attach_read_cached_snapshot] + + assert rows == [{"sample_id": 102}] + assert snapshot.count_rows() == 2 + + +def test_attached_stable_id_cache(tmp_db): + db = tmp_db + writer = db.create_table( + "samples", + [{"sample_id": 101}, {"sample_id": 102}], + storage_options={"new_table_enable_stable_row_ids": "true"}, + ) + writer.add([{"sample_id": 103}]) + table_name = writer.name + namespace_path = writer.namespace + branch = writer.current_branch() + + # --8<-- [start:attach_build_stable_id_cache] + table = db.open_table(table_name, namespace_path=namespace_path, branch=branch) + table.checkout_latest() + cached_rows = ( + table.search() + .where("sample_id IN (101, 103)") + .select(["sample_id"]) + .with_row_id(True) + .limit(2) + .to_list() + ) + ids_by_key = {row["sample_id"]: row["_rowid"] for row in cached_rows} + # --8<-- [end:attach_build_stable_id_cache] + + writer.delete("sample_id = 101") + writer.optimize() + assert writer.stats()["fragment_stats"]["num_fragments"] == 1 + + # --8<-- [start:attach_read_stable_id_cache] + table.checkout_latest() + row_ids = [ids_by_key[101], ids_by_key[103]] + rows = table.take_row_ids(row_ids, with_row_id=True).to_list() + rows_by_id = {row["_rowid"]: row for row in rows} + batch = [rows_by_id.get(row_id) for row_id in row_ids] + # --8<-- [end:attach_read_stable_id_cache] + + assert batch == [None, {"sample_id": 103, "_rowid": ids_by_key[103]}] + + +def test_attached_reader_freshness(tmp_path): + database_uri = str(tmp_path) + connection_options = {} + writer = lancedb.connect(database_uri).create_table("samples", [{"sample_id": 101}]) + table_name = writer.name + namespace_path = writer.namespace + branch = writer.current_branch() + reader = lancedb.connect(database_uri).open_table(table_name) + assert reader.count_rows() == 1 + writer.add([{"sample_id": 102}]) + assert reader.count_rows() == 1 + + # --8<-- [start:attach_read_zero_interval] + from datetime import timedelta + + fresh_db = lancedb.connect( + database_uri, + read_consistency_interval=timedelta(0), + **connection_options, + ) + fresh_reader = fresh_db.open_table( + table_name, namespace_path=namespace_path, branch=branch + ) + latest_count = fresh_reader.count_rows() + # --8<-- [end:attach_read_zero_interval] + + # --8<-- [start:attach_refresh_reader] + reader.checkout_latest() + refreshed_count = reader.count_rows() + # --8<-- [end:attach_refresh_reader] + + assert latest_count == 2 + assert refreshed_count == 2 diff --git a/docs/web/ingestion/attach-database.mdx b/docs/web/ingestion/attach-database.mdx index e55c29275..9fe286dda 100644 --- a/docs/web/ingestion/attach-database.mdx +++ b/docs/web/ingestion/attach-database.mdx @@ -9,6 +9,15 @@ availability: enterprise: available --- +import { + PyAttachBuildStableIdCache, + PyAttachReadCachedSnapshot, + PyAttachReadStableIdCache, + PyAttachReadZeroInterval, + PyAttachRefreshReader, + PyAttachTagCachedVersion, +} from '/snippets/attach_database.mdx'; + If your LanceDB Embedded database already lives in object storage -- Embedded reads and writes tables on S3, GCS or Azure as well as local disk -- you can move to Enterprise by attaching that same location, @@ -62,6 +71,126 @@ unregisters it and releases the storage-integration reference, without deleting the underlying data -- the location can be reattached at any time. +## Switch writes to Enterprise {#write-cutover} + +Before attaching, stop writers that commit directly to the storage location and +wait for their in-flight writes to finish. After attaching, write through the +attached database using [the LanceDB SDK](/ingestion/insert) or +[SQL](/sql/dml/insert). Use the same cutover if you migrate by copying the data +into Enterprise storage. + +A PyLance writer that commits to a bare storage URI bypasses the Enterprise +catalog's write path, including its cache updates and maintenance notifications. +The commit can succeed in storage while Enterprise readers still see an older +version under the deployment's read freshness policy. + +To keep a dataset under an independent writer, leave it outside the managed +catalog and configure a storage integration. +A [SQL query against the external URI](/sql/ddl/storage-integration#read-import) +reads a snapshot of the source for that statement. `CREATE TABLE ... AS SELECT` +imports the query result into a separate managed table. Future source changes +stay in the source. + +If your deployment supports writes through a [Lance namespace](/basics/sdk#lance-namespace-sdks), +pass the namespace client and logical table ID to PyLance. PyLance uses +`declare_table` for new tables and `describe_table` for existing ones. The +writer needs authorized storage access and follows the namespace's commit +protocol, including the namespace version API when managed versioning is +advertised. + +### Request a fresh read {#read-freshness} + +After a write completes, use these controls with a compatible Enterprise client +and server. To request the latest committed version on every read, connect with +a zero `read_consistency_interval`. Reuse the URI and options from your +[Enterprise connection](/management/database#deployment). Here `database_uri` +is the `db://` URI and `connection_options` contains the endpoint, region and +authentication settings for that connection. Use your reader's `table_name`, +`namespace_path` (`[]` for the root namespace), and `branch` (`None` for `main`). + + + {PyAttachReadZeroInterval} + + +For a reader you already opened, call `checkout_latest()` before the next read. +This returns a reader checked out to a historical version to the latest version +of its branch. + + + {PyAttachRefreshReader} + + +These controls apply to reads on that connection or handle. Use the Enterprise +write path for maintenance notifications. Read visibility across replicas follows +the deployment's consistency policy. See [Consistency](/tables/consistency). + +## Migrate row caches {#row-caches} + +Enterprise can compact fragments and clean up older versions in the background. +Compaction rewrites fragments, so a cached physical row address (`_rowaddr`) +can become invalid on a later version even when the row's stable `_rowid` is +unchanged. On a table without stable row IDs, PyLance's `with_row_id=True` +returns address-based IDs. Choose how to rebuild or retain the cache based on +which data the reader needs. + +### Keep an exact historical snapshot {#retain-snapshot} + +Use a [version tag](/management/table#versioning-tag-based-versioning) for a +fixed training dataset or export. Save the database, namespace, table, branch, +exact version and tag with the cache. Reopen the original table instance. If a +migration recreates or imports the table, rebuild the cache against that table. + +Tag the exact cached version before cleanup prunes it. Open the table using +its recorded `table_name`, `namespace_path` and `branch` on a connection (`db`) +to the recorded database. Pass `cached_version` from the cache to `tags.create()`. + + + {PyAttachTagCachedVersion} + + +On a later run, open a handle on the saved branch and check out the saved tag. +Verify the version before reusing any cached addresses. This example uses one +integer `cached_address` from the cache. + + + {PyAttachReadCachedSnapshot} + + +Cleanup protects the snapshot while the tag points to it. Once a version has +been pruned, rebuild the cache from a retained version. + +### Follow the latest logical rows {#follow-latest-rows} + +For a reader that follows new versions, rebuild the cache with a unique, +immutable business key such as `sample_id`. Query the current version with a +filter such as `sample_id = 101`. A key may be absent after a delete. Choose +whether to skip the missing row or reject the batch. + +For a table with stable row IDs enabled, this example builds a new cache from +the `_rowid` values of two samples on the selected branch. + + + {PyAttachBuildStableIdCache} + + +`take_row_ids()` omits missing or deleted IDs. Request `_rowid` in the result +and match by ID so a missing row cannot shift the rest of the batch. The example +uses `None` for missing rows. For more lookup options, see +[Selective Sampling](/curation/selective-sampling). + + + {PyAttachReadStableIdCache} + + +### Review maintenance settings {#maintenance-policy} + +[Compaction](/maintenance/compaction) and [cleanup](/maintenance/cleanup) follow +your deployment's policy and any supported table overrides. Cleanup retention +sets the age at which older, untagged versions become eligible for removal. +On compatible deployments, [table overrides](/maintenance/compaction#table-compaction-defaults) +can tune or disable automatic scheduling. Running jobs continue under their +submitted settings, and authorized operators can still run manual maintenance. + ## What to read next {#next} diff --git a/docs/web/snippets/attach_database.mdx b/docs/web/snippets/attach_database.mdx new file mode 100644 index 000000000..f0ddc6930 --- /dev/null +++ b/docs/web/snippets/attach_database.mdx @@ -0,0 +1,14 @@ +{/* Auto-generated by scripts/mdx_snippets_gen.py. Do not edit manually. */} + +export const PyAttachBuildStableIdCache = "table = db.open_table(table_name, namespace_path=namespace_path, branch=branch)\ntable.checkout_latest()\ncached_rows = (\n table.search()\n .where(\"sample_id IN (101, 103)\")\n .select([\"sample_id\"])\n .with_row_id(True)\n .limit(2)\n .to_list()\n)\nids_by_key = {row[\"sample_id\"]: row[\"_rowid\"] for row in cached_rows}\n"; + +export const PyAttachReadCachedSnapshot = "snapshot = db.open_table(table_name, namespace_path=namespace_path, branch=branch)\nsnapshot.checkout(tag)\nif snapshot.version != cached_version:\n raise RuntimeError(f\"Tag {tag} no longer points to version {cached_version}\")\n\nrows = snapshot.search().where(f\"_rowaddr = {cached_address}\").limit(1).to_list()\n"; + +export const PyAttachReadStableIdCache = "table.checkout_latest()\nrow_ids = [ids_by_key[101], ids_by_key[103]]\nrows = table.take_row_ids(row_ids, with_row_id=True).to_list()\nrows_by_id = {row[\"_rowid\"]: row for row in rows}\nbatch = [rows_by_id.get(row_id) for row_id in row_ids]\n"; + +export const PyAttachReadZeroInterval = "from datetime import timedelta\n\nfresh_db = lancedb.connect(\n database_uri,\n read_consistency_interval=timedelta(0),\n **connection_options,\n)\nfresh_reader = fresh_db.open_table(\n table_name, namespace_path=namespace_path, branch=branch\n)\nlatest_count = fresh_reader.count_rows()\n"; + +export const PyAttachRefreshReader = "reader.checkout_latest()\nrefreshed_count = reader.count_rows()\n"; + +export const PyAttachTagCachedVersion = "tag = \"training-run-42\"\ntable = db.open_table(table_name, namespace_path=namespace_path, branch=branch)\ntable.tags.create(tag, cached_version)\n"; +