How to Write Continuously Running Queries That Allow Data Deletion
This page provides an overview of how to delete data without causing query failures in Core+. For this example, our query only uses the current day's data, so, to save disk space, we can delete data that is more than two days old. This page demonstrates how to write a query so that it can still run when old data is deleted.
Note
A simple db.liveTable call that filters to all days of the week would crash if we deleted data out from under it. This is because liveTables are "add-only", which allows the engine to optimize operations. When you remove data, the add-only behavior is broken and causes a failure. If you try to read data that was deleted (for recomputing operations using previous values), then those operations would fail.
Querying the data
The first step is to retrieve the data, using a SourcePartitionedTable. The SourcePartitionedTable creates one constituent table per intraday partition. When a partition is deleted, the constituent is removed from the SourcePartitionedTable. Each constituent table is still add-only, and rows are never removed from the constituent tables.
A SourcePartitionedTable lets us filter out the column partitions (in this case, the date) that will eventually be deleted. We want our query to look only at today's data, with a rollover at midnight. We set up a WindowCheck to filter out data that is more than 24 hours old:
You can query the result and the constituent tables of the SourcePartitionedTable as normal as long as it is done after filtering out the old partitions. For example, the
ErrorMonitor query in the example continues to run after old dates are deleted:
This query, on the other hand, fails once old dates are deleted:
Delete old data
Once data has been filtered from the query, it is safe to delete. This example uses the data control tool:
Warning
Ensure that the query has already stopped using the table data before you delete it. Attempting to delete data while it is simultaneously being filtered may cause the query to crash.