stac-utils / stac-utils/pgstac

Queued query errors leak into later successful history rows

Open Beginner friendly
#487 0 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

This was generated by AI during triage.

Bug

run_queued_queries() and run_queued_queries_intransaction() retain the local error variable across loop iterations. After one queued statement fails, later successful statements processed by the same invocation are written to query_queue_history with the previous statement's error.

The SQL itself succeeds; the history metadata is incorrect.

Reproduction

Using pgSTAC 0.9.12:

TRUNCATE pgstac.query_queue, pgstac.query_queue_history;

-- The queue processes newest first, so the failing statement runs first.
INSERT INTO pgstac.query_queue(query, added) VALUES
    ('SELECT 1 /* queue error reset success */', '2000-01-01 00:00:00+00'),
    ('SELECT 1 / 0 /* queue error reset failure */', '2000-01-02 00:00:00+00');

SELECT pgstac.run_queued_queries_intransaction();

SELECT query, error
FROM pgstac.query_queue_history
ORDER BY finished;

Actual result:

SELECT 1 / 0 ... | division by zero | 22012
SELECT 1 ...     | division by zero | 22012

The same behavior reproduces through CALL pgstac.run_queued_queries().

Expected behavior

Only the failed statement should have a non-NULL error. A successful statement must be recorded with error = NULL.

Cause

Both queue runners declare error text outside the loop and assign it only in the exception handler. Successful iterations therefore retain an error assigned by an earlier iteration.

Proposed fix

Set error := NULL after claiming each queue item and before executing it in both queue runners. Add a PostgreSQL/PGTap regression covering a failed statement followed by a successful statement.

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 by running the supplied SQL reproduction against pgSTAC, then inspect run_queued_queries() and run_queued_queries_intransaction(), focusing on how error is handled between loop iterations. Add the requested PostgreSQL/PGTap regression for a failed statement followed by a successful one. Done means the failed history row has an error while the successful row records error as NULL.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql
Domain
database, testing
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.