mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 15:55:19 +00:00
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).
204 lines
8.7 KiB
Plaintext
204 lines
8.7 KiB
Plaintext
---
|
|
title: "Attach Database"
|
|
sidebarTitle: "Attach Database"
|
|
description: "Point Enterprise at an existing database in object storage with ATTACH DATABASE, without copying data."
|
|
icon: plug
|
|
keywords: ["attach database", "migrate", "embedded", "object storage", "s3", "gcs", "azure"]
|
|
availability:
|
|
embedded: unavailable
|
|
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,
|
|
rather than copying any data. A database on local disk has no such path:
|
|
`ATTACH DATABASE` only accepts an object-storage URI or a path relative
|
|
to Enterprise's own storage root, never a local filesystem path. Move it
|
|
to object storage first if it is not there already.
|
|
|
|
## Prerequisites {#prerequisites}
|
|
|
|
A [storage integration](/sql/ddl/storage-integration) covering the
|
|
database's location must already exist. `ATTACH DATABASE` never names an
|
|
integration explicitly -- Enterprise automatically binds the most
|
|
specific integration whose `STORAGE_PREFIX` covers the location you
|
|
attach.
|
|
|
|
<CodeGroup>
|
|
```sql SQL icon="database"
|
|
CREATE STORAGE INTEGRATION raw
|
|
STORAGE_PROVIDER = 'S3'
|
|
STORAGE_PREFIX = 's3://customer-data/embedded-db/'
|
|
STORAGE_AWS_ROLE_ARN = 'arn:aws:iam::123456789012:role/lancedb-access'
|
|
```
|
|
</CodeGroup>
|
|
|
|
## Attach the database {#attach}
|
|
|
|
<CodeGroup>
|
|
```sql SQL icon="database"
|
|
ATTACH DATABASE analytics WITH (location = 's3://customer-data/embedded-db/')
|
|
```
|
|
</CodeGroup>
|
|
|
|
See [Database](/sql/ddl/database#attach) for the full `ATTACH DATABASE`
|
|
syntax, including GCS and Azure locations (Azure requires an
|
|
account-qualified URI), `IF NOT EXISTS`, and quoted names.
|
|
|
|
## What changes, and what doesn't {#what-changes}
|
|
|
|
Attaching makes the location a database LanceDB Enterprise manages the
|
|
same way as one it created itself: Enterprise writes its own tracking
|
|
metadata into that location to bring it under management, enabling
|
|
capabilities Embedded never had there -- every table becomes reachable
|
|
through Enterprise's SQL and SDK surface immediately, with Enterprise's
|
|
authorization, Secrets, Functions, and
|
|
[background maintenance](/basics/jobs) now available to
|
|
it. Attaching does not rewrite, move, or re-encode any table's own data.
|
|
|
|
[`DROP DATABASE`](/sql/ddl/database#drop) on an attached database
|
|
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`).
|
|
|
|
<CodeBlock filename="Python" language="Python" icon="python">
|
|
{PyAttachReadZeroInterval}
|
|
</CodeBlock>
|
|
|
|
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.
|
|
|
|
<CodeBlock filename="Python" language="Python" icon="python">
|
|
{PyAttachRefreshReader}
|
|
</CodeBlock>
|
|
|
|
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()`.
|
|
|
|
<CodeBlock filename="Python" language="Python" icon="python">
|
|
{PyAttachTagCachedVersion}
|
|
</CodeBlock>
|
|
|
|
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.
|
|
|
|
<CodeBlock filename="Python" language="Python" icon="python">
|
|
{PyAttachReadCachedSnapshot}
|
|
</CodeBlock>
|
|
|
|
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.
|
|
|
|
<CodeBlock filename="Python" language="Python" icon="python">
|
|
{PyAttachBuildStableIdCache}
|
|
</CodeBlock>
|
|
|
|
`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).
|
|
|
|
<CodeBlock filename="Python" language="Python" icon="python">
|
|
{PyAttachReadStableIdCache}
|
|
</CodeBlock>
|
|
|
|
### 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}
|
|
|
|
<CardGroup cols={2}>
|
|
<Card title="Storage Integration" icon="cloud" href="/sql/ddl/storage-integration">
|
|
Configure the credential an attach depends on, for S3, GCS, or Azure.
|
|
</Card>
|
|
<Card title="Database" icon="database" href="/sql/ddl/database">
|
|
Full ATTACH, CREATE, DESCRIBE and DROP DATABASE syntax.
|
|
</Card>
|
|
</CardGroup>
|