Skip to content

Drain JDBC realm time-series data via a batched purge command#5089

Open
ayushtkn wants to merge 1 commit into
apache:mainfrom
ayushtkn:deleteAll
Open

Drain JDBC realm time-series data via a batched purge command#5089
ayushtkn wants to merge 1 commit into
apache:mainfrom
ayushtkn:deleteAll

Conversation

@ayushtkn

@ayushtkn ayushtkn commented Jul 18, 2026

Copy link
Copy Markdown
Member

deleteAll (a BasePersistence operation) previously left a realm's time-series data — events, scan_metrics_report, commit_metrics_report — behind. Rather than fix that with a broad inline delete, this makes the time-series purge asynchronous, queued, and batched, matching somewhat the behavior of the NoSQL maintenance command.

deleteAll no longer touches time-series tables. It atomically enqueues the realm into a new realm_purge marker table, as the last statement in its own transaction — so a purged realm is never left with orphaned time-series data, and the marker never commits without the metastore delete.

  • A new operator-run admin command, purge-drain, processes the queue: it claims each realm under a lease (one active drainer per realm across nodes, via a conditional UPDATE), deletes its rows in bounded batches, then clears the marker.

A realm whose drain fails or is interrupted keeps its marker and is retried once its lease expires. It's safe to run repeatedly/concurrently and is meant to be scheduled periodically (e.g. once a day), like nosql maintenance-run.

Per-table purgers are discovered via CDI (Instance<RealmDataPurger>), so adding a table means adding a producer, not editing the drainer.

Checklist

  • 🛡️ Don't disclose security issues! (contact security@apache.org)
  • 🔗 Clearly explained why the changes are needed, or linked related issues: Fixes #
  • 🧪 Added/updated tests with good coverage, or manually tested (and explained how)
  • 💡 Added comments for complex logic
  • 🧾 Updated CHANGELOG.md (if needed)
  • 📚 Updated documentation in site/content/in-dev/unreleased (if needed)

Copilot AI review requested due to automatic review settings July 18, 2026 08:02
@github-project-automation github-project-automation Bot moved this to PRs In Progress in Basic Kanban Board Jul 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Extends JDBC metastore cleanup so JdbcBasePersistenceImpl.deleteAll() purges additional realm-scoped tables introduced in newer schema versions (events in v3+, scan/commit metrics report tables in v4+), preventing stale data from remaining after a realm-scoped delete.

Changes:

  • Update JdbcBasePersistenceImpl.deleteAll() to delete from events, scan_metrics_report, and commit_metrics_report when the schema version supports them.
  • Add a new integration-style test that populates all realm-scoped tables in schema v5 and verifies deleteAll() purges only the target realm.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcBasePersistenceImpl.java Extends deleteAll() to delete newer realm-scoped tables, gated by schema version.
persistence/relational-jdbc/src/test/java/org/apache/polaris/persistence/relational/jdbc/JdbcBasePersistenceImplTest.java Adds coverage verifying realm-scoped purge behavior across all relevant tables for schema v5.

Comment on lines +413 to +432
if (schemaVersion >= 3) {
datasourceOperations.execute(
connection,
QueryGenerator.generateDeleteQuery(
ModelEvent.ALL_COLUMNS, ModelEvent.TABLE_NAME, params));
}
if (schemaVersion >= 4) {
datasourceOperations.execute(
connection,
QueryGenerator.generateDeleteQuery(
ModelScanMetricsReport.ALL_COLUMNS,
ModelScanMetricsReport.TABLE_NAME,
params));
datasourceOperations.execute(
connection,
QueryGenerator.generateDeleteQuery(
ModelCommitMetricsReport.ALL_COLUMNS,
ModelCommitMetricsReport.TABLE_NAME,
params));
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense. added tests for all versions

@dimas-b dimas-b left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for bringing this up, @ayushtkn ! I think it's a good area to improve, but I'm not sure about using a simple DELETE SQL for that 🤔

connection,
QueryGenerator.generateDeleteQuery(
ModelScanMetricsReport.ALL_COLUMNS,
ModelScanMetricsReport.TABLE_NAME,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The javadoc on deleteAll mentions only "entity and grant record metadata".

deleteAll() is specified by BasePersistence, so I do not think it applies to events and metrics, which are under MetricsPersistence. It's currently detached from BasePersistence in terms of interfaces and its implementation is moving to extensions in #5068 🤔

I believe purging metric should be done under a separate method call and probably after merging #5068.

IMHO, conceptually, events should also not be bound to BasePersistence either. I'd like to isolate them link metrics and then add dedicated maintenance tasks for purging (see my other comment).

WDYT?

datasourceOperations.execute(
connection,
QueryGenerator.generateDeleteQuery(
ModelEvent.ALL_COLUMNS, ModelEvent.TABLE_NAME, params));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Events a time-series type of data. There may be a lot more of them in the database than Meta Store data. I'm not sure issuing a broad DELETE for them in one Tx is a robust approach.

I tend to think it would be preferable to handle their purge asynchronously and in a retriable / staged manner similar to NoSQL maintenance: https://github.com/apache/polaris/blob/apache-polaris-1.6.0/persistence/nosql/persistence/maintenance/api/src/main/java/org/apache/polaris/persistence/nosql/maintenance/api/package-info.java

@ayushtkn

ayushtkn commented Jul 19, 2026

Copy link
Copy Markdown
Member Author

Thanks @dimas-b — agreed. We shouldn’t extend deleteAll() with sync DELETEs for events and metrics; that doesn’t match where this is heading with #5068 and the NoSQL maintenance model for time-series data.

I’ll revisit after #5068 and the linked PRs land, once metrics are decoupled from JdbcBasePersistenceImpl and we have a clearer place to hook purge in. From what I’ve looked at so far, doing JDBC realm purge async/staged like NoSQL would be a much larger effort (preserve realm state, maintenance command, batched deletes, etc.). I only did a quick pass on the NoSQL side — there may be a shortcut, refactor, or constraint I overlooked :-)

Thanks — I’ll park this for now and circle back.

@ayushtkn ayushtkn changed the title Fix JDBC deleteAll not purging events and metrics report tables Drain JDBC realm time-series data via a batched purge command Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants