Skip to content

fix(rust): change optimize steps ordering - #3122

Closed
sprytnyk wants to merge 2 commits into
lancedb:mainfrom
sprytnyk:bugfix/VK/2963-fix-broken-optimize-process
Closed

sprytnyk wants to merge 2 commits into
lancedb:mainfrom
sprytnyk:bugfix/VK/2963-fix-broken-optimize-process

Conversation

@sprytnyk

@sprytnyk sprytnyk commented Mar 10, 2026 •

Copy link
Copy Markdown

Indexing should happen before purning in order to avoid lance error: LanceError(IO): External error: Not found data.

It causes dodgy race conditions intermittently with parameters arguments like:

  • cleanup_older_than=timedelta(seconds=0)
  • delete_unverified=True

I ran tests with cargo test --features remote --tests and all them passing.

Refs: #2963

Indexing should happen before purning in order to avoid lance error:
LanceError(IO): External error: Not found data.

It causes dodgy race conditions intermittently with parameters arguments like:
- cleanup_older_than=timedelta(seconds=0)
- delete_unverified=True

Refs: lancedb#2963
@github-actions github-actions Bot added bug Something isn't working Python Python SDK Rust Rust related issues labels Mar 10, 2026
@westonpace

Copy link
Copy Markdown
Contributor

I would want some more details into what exact failure we are fixing. Why did the old order not work correctly? I'm not against changing the order (as long as compaction comes before indexing) but I don't expect it to fix any bugs.

It causes dodgy race conditions intermittently with parameters arguments like:

cleanup_older_than=timedelta(seconds=0)
delete_unverified=True

This is a very dangerous combination, with or without this fix. If this optimization is run concurrent with any other operation it can lead to the error described. That is because, with this configuration, the cleanup job will delete files from operations that are in-progress.

@sprytnyk

Copy link
Copy Markdown
Author

I would want some more details into what exact failure we are fixing.

Basically, I have such scenario:

  1. Adding continuously new vectors into tables
  2. Run optimize script every hour

I have such indices for my tables:

>>> from lancedb import connect_async
... db = await connect_async('/lancedb')
>>> table = await db.open_table('table')
>>> await table.list_indices()
[Index(BTree, columns=["pk"], name="pk_idx"),
 Index(Bitmap, columns=["visibility"], name="visibility_idx"),
 Index(IvfPq, columns=["embedding"], name="embedding_idx")]

During the day run such script to index newly added data:

    for table_name in table_names:
        table = await db.open_table(table_name)

        # Clean up files in dirs _versions, _transactions, merge data in data
        # dir, add new vectors into scalar and vector indices.
        logger.warning(f"Optimizing {table_name} table...")
        stats = await table.optimize(cleanup_older_than=timedelta(seconds=0), delete_unverified=True)
        logger.info(stats)
        logger.info(f"Finished optimizing {table_name} table.")

I don't have any kind of concurrency when run the above script, execution of the script is not overlapping, the same table isn't used twice as well.
This works well for weeks but at some point the next time I try to run the script it throws lance error: LanceError(IO): External error: Not found, so somehow await table.optimize(cleanup_older_than=timedelta(seconds=0), delete_unverified=True) corrupts data, index directories.

This is a very dangerous combination, with or without this fix.

If so, then why it's not explicitly stated in docs?

It's up to you whether accept this PR or not. I don't know all ins and outs of this project, you have this context, but to me is more logical to create index first and then delete any kind of data that potentially may be used for indexing process.

@westonpace

Copy link
Copy Markdown
Contributor

I don't have any kind of concurrency when run the above script, execution of the script is not overlapping, the same table isn't used twice as well.

Are you continuously adding new vectors while you are executing the optimize script? Even if the add is happening in a different script?

If so, then why it's not explicitly stated in docs?

That's a good catch. I thought it was stated. I've opened #3125 to fix that.

It's up to you whether accept this PR or not. I don't know all ins and outs of this project, you have this context, but to me is more logical to create index first and then delete any kind of data that potentially may be used for indexing process.

Makes a lot of sense to me. I just remember we debated this order for a while when it was first introduced and want to refresh my memory on why we chose this path.

@sprytnyk

sprytnyk commented Mar 11, 2026 •

Copy link
Copy Markdown
Author

Are you continuously adding new vectors while you are executing the optimize script? Even if the add is happening in a different script?

Yes, I don't stop adding new data when run optimize from a different script.
Want to amend it with such parameters arguments, run every 2 hours, and see how it will go:

        await table.optimize(
            cleanup_older_than=timedelta(hours=3),
            delete_unverified=False,
        )

I think it's more of problem with delete_unverified=True because adding new data may mark some datasets as in-progress, but with such flag, it schedules for deletion by optimize.

Here's a full script:
https://sprytnyk.dev/posts/running-lancedb-in-production/#cleanup-script

@Xuanwo

Xuanwo commented Nov 24, 2026

Copy link
Copy Markdown
Member

Thank you @sprytnyk for investigating and opening this PR! Reordering the optimize and cleanup steps cannot protect a concurrent writer's uncommitted files when running with cleanup_older_than=timedelta(seconds=0) and delete_unverified=True (see the discussion in #2963 and the documentation follow-up in #3125). Closing this PR since it does not fix the underlying race.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Python Python SDK Rust Rust related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants