Skip to content

fix(databases): add the database identity to the query cache key - #735

Draft
omkargaikwad23 wants to merge 1 commit into
mainfrom
fix/query-cache-key-database
Draft

omkargaikwad23 wants to merge 1 commit into
mainfrom
fix/query-cache-key-database

Conversation

@omkargaikwad23

Copy link
Copy Markdown
Collaborator

What this fixes

The key of the query cache was a hash of the query and the engine URL. For some databases, that value does not identify the database:

  • Postgres and MySQL with the Cloud SQL connector: the URL is postgresql+pg8000:// or mysql+pymysql://.
  • SQL Server: the URL is mssql+pytds://.
  • SQLite: the key used the database name without the directory.

Thus the same query on two different databases used one cache entry. The second database got the rows of the first.

How

  • New method DB._cache_scope(). It returns the database type, the path (host or instance), the database name, and the user name.
  • Postgres, MySQL, SQL Server, and SQLite use this value for the cache key. AlloyDB uses the Postgres code.

Behavior change

The cache key changes for these databases. The cache entries that exist now are not read again.

Testing

  • New tests in query_cache_key_test.py. On main, 10 of the 12 test cases fail.
  • A before/after comparison is in a comment on this PR.

@omkargaikwad23

omkargaikwad23 commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator Author

Verification

I ran the same script on main (6ca77dc) and on this branch. The cache is a dict, and no database server is used. For each row, the script runs SELECT id FROM t with use_cache=True on database A and then on database B.

# Databases A and B Rows that B gets on main Rows that B gets with this PR
1 Postgres with the Cloud SQL connector, database names tenant_a and tenant_b Rows of tenant_a Rows of tenant_b
2 MySQL with the Cloud SQL connector, database names tenant_a and tenant_b Rows of tenant_a Rows of tenant_b
3 SQL Server, database names tenant_a and tenant_b Rows of tenant_a Rows of tenant_b
4 SQLite, the name shop in the directories a and b Rows of directory a Rows of directory b

Local tests:

  • query_cache_key_test.py, rows_to_dicts_test.py, test_db_cleanups.py, and spi_custom_class_loader_test.py: 31 passed.
  • pytest evalbench/test with SKIP_CLOUD_TESTS=true: 1298 passed, 11 skipped.

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.

1 participant