Best way to keep only last NNN rows of a table

I followed your suggestion adding the in_transaction decorator (but keeping the rest of my original code structure, for lazyness I guess :slight_smile:):

@tables.in_transaction()        
def make_db_housekeeping():
    # cut tables to max dimensions
    max_documenti = 1000
    # insert order guaranteed? TODO: add a date field or a counter
    documenti = app_tables.esaw_files_buffer.search()
    num_docs = len(documenti)
    if num_docs > max_documenti:
        to_delete = num_docs - max_documenti
        deleted = 0
        for doc_row in documenti:
            doc_row.delete()			#	--> this is row 1140
            deleted += 1
            if deleted == to_delete:
                break

It worked seamlessly since then until 2 days ago.
Since 2 days ago I am consistently getting the error

anvil.tables.TransactionConflict: Another transaction has changed this data; aborting
<running on the server>
called from /downlink/anvil/_server.py, line 42
called from server_module, line 1140

This code is executed by a background task, nightly, at 03.00 am, without any concurrency.
No other task is running at that time and no front-end user too.
esaw_files_buffer is a 1000 rows table (now 1006, since last 3 runs failed).

Main question: I can’t understand this error.

Side questions:

  1. I see I am having a consistent number of DB errors since 1 month ago (approx)… code that never gave such problems… I guess something has changed in DB on Anvil’s side, can someone confirm that?

  2. Am I the only one here having this brand new set of DB errors on consolidated, stable production code?

Thanks and BR