Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Keep cached counts in sync when insert(ignore=True) adds a row - #1002

Open
anlonely wants to merge 4 commits into
simonw:mainfrom
anlonely:fix/cached-counts-insert-ignore
Open

anlonely wants to merge 4 commits into
simonw:mainfrom
anlonely:fix/cached-counts-insert-ignore

Conversation

@anlonely

@anlonely anlonely commented Dec 5, 2026 •

Copy link
Copy Markdown

Fixes #1001.

SQLite applies the conflict policy of an outer INSERT OR IGNORE to the insert inside the count trigger. Once a row exists in _counts, this can silently skip the counter update even though the source row was inserted.

The triggers now update the existing count directly. If someone deletes the cache row, table.count falls back to a live count until reset_counts() or enable_counts() restores it. A trigger cannot safely rebuild the cache from count(*) during nested writes because other trigger updates may still be pending. The regression tests exercise nested inserts and deletes with the count triggers installed in either order. The trigger SQL also works on SQLite 3.23.1.

For databases with existing count triggers, running enable-counts again refreshes the cached value and upgrades the definitions written by earlier sqlite-utils releases. Only recognized legacy definitions are replaced. The tests cover quote styles used by 3.x and 4.x, including the mixed quoting SQLite writes after renaming a 3.x table and renaming it back. They also check that unrelated user triggers, including same-name TEMP triggers, are preserved.

With standard transactions and with autocommit, each full-suite run passed 1,639 tests with 17 optional-dependency skips. The tests also exercise table names with quotes and with an indented line break. I reproduced the stale count on a database created by the unmodified code, then verified that re-enabling counts and continuing the import kept the persisted and actual counts in sync.

Prepared and tested by OpenAI Codex on behalf of @anlonely.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-05T14:41:10.787928Z 4693b33 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e1aa034584

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread sqlite_utils/db.py Outdated
Comment on lines +3772 to +3773
SELECT {table_name}, (SELECT count(*) FROM {table})
WHERE changes() = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Make missing-row recovery safe for nested inserts

If an empty table's _counts row is removed and an AFTER INSERT trigger created after enable_counts() inserts a second row into the same table, this recount runs inside the nested insertion while both rows are already visible. It seeds 2, then the still-pending outer count trigger adds another 1, leaving table.count == 3 for two rows. The previous triggers returned 2 for this same sequence, so the recovery path introduces persistent cache corruption for nested writes. Account for pending outer trigger updates rather than combining a full recount with their deltas.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Confirmed. With the cache row removed, I reproduced a cached count of 3 for two rows. In d61cfc2 the triggers only update existing cache entries. When an entry is missing, table.count uses its live query until reset_counts() or enable_counts() restores the entry, so pending trigger frames cannot apply a delta to a partial recount. The regression test covers nested inserts and deletes with the count triggers created in either order and checks that subsequent updates stay in sync after a reset.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d61cfc2a76

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread sqlite_utils/db.py Outdated
Comment on lines +3766 to +3767
statements.append(textwrap.dedent(f"""
CREATE TRIGGER IF NOT EXISTS {trigger} AFTER {event} ON {table}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Dedent the SQL template before interpolating table names

For a table name containing a newline followed by spaces, such as items\n archive, expanding the f-string before calling dedent() removes whitespace inside the quoted identifiers and string literals, changing the name to items\narchive. I reproduced enable_counts() raising OperationalError: no such table for this input; if the dedented name belongs to another table, the triggers are instead installed there and the original table's cached count remains stale. The previous implementation dedented the template before formatting it, preserving the supplied names. Keep interpolation after dedenting.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Confirmed on the previous head: enable_counts() failed for a table named items\n archive. In 537f74d the trigger statement is built without dedenting its substituted identifiers or literals. The regression test exercises inserts, duplicate ignores and deletes using that name. I also checked pairs of coexisting table names that differed only in whitespace; each maintained its own count. Both full-suite runs passed with standard transactions and autocommit.

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.

Successful insert(ignore=True) leaves the cached row count unchanged

1 participant