stac-utils / stac-utils/pgstac

High Latency and DB Locks on Bulk Deletion Endpoint

Open
#381 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PLpgSQL
Stars
223
Forks
57
PR merge metrics
No merged PRs in 30d

Description

Hi,

Description:
We are using a custom deletion endpoint based on this code
to perform bulk deletion of items.
The code performs a simple query as shown below.

class CustomBulkTransactionsClient(BulkTransactionsClient):
    """BulkTransactionsClient with bulk delete capabilities."""

    async def bulk_item_delete(self, items: ItemsPayload, request: Request) -> str:
        """Bulk delete items."""
        if not items.ids:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="No IDs provided for bulk deletion.",
            )

        query, params = render(
            """
            DELETE FROM items
            WHERE collection = :collection_id
            AND id = ANY(:ids)
            """,
            collection_id=request.path_params["collection_id"],
            ids=items.ids,
        )

        async with request.app.state.get_connection(request, "w") as conn:
            result = await conn.execute(query, *params)
            _, deleted_count_str = result.split()
            deleted_count = int(deleted_count_str)

        return f"Successfully deleted {deleted_count} items."

Issue:

What we’ve noticed is that the latency is quite high, and we are running into database locks when executing the bulk deletion. We believe that the trigger function after deletion might be contributing to this. Do you agree with this assessment?

Additionally, would you have any recommendations for optimizations or changes? Our partitions are relatively constant, and we could potentially accept delayed re-calculations for efficiency.

Screenshot (for reference):

Image Image Image

Looking forward to your input.
Erik

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with stac_fastapi/pgstac/transactions.py around line 348 and review the DELETE query alongside the post-delete trigger mentioned in the report. Measure query latency and lock behavior, then determine whether deferred recalculation is feasible; done requires an agreed optimization backed by improved measurements.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python, sql
Domain
databases, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.