mirror of
https://github.com/domainaware/parsedmarc.git
synced 2026-10-24 11:38:58 +00:00
Fix "Aggregate Report Details" Grafana panel timing out on PostgreSQL (#923)
PostgreSQL does not create an index on the referencing side of a foreign key (docs, "Foreign Keys"), and create_tables() indexed only two of the schema's seven foreign-key columns. The panel's per-record DKIM/SPF subqueries on record_id therefore ran two sequential scans per row and timed out on a 413k-record database. Index the five remaining foreign-key columns, and add a test that every REFERENCES column in the schema has an index. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4d3afac0c9
commit
65a7bfb7f9
3 files changed
+67
No files matched your search
@@ -337,6 +337,45 @@ class TestPostgreSQLConstruction(unittest.TestCase):
|
||||
self.assertIn(f"CREATE TABLE IF NOT EXISTS {table}", executed)
|
||||
self.assertIn("CREATE INDEX IF NOT EXISTS", executed)
|
||||
|
||||
def test_create_tables_indexes_every_foreign_key(self):
|
||||
"""Every REFERENCES column gets an index.
|
||||
|
||||
PostgreSQL indexes the referenced primary key but not the referencing
|
||||
column ("the declaration of a foreign key constraint does not
|
||||
automatically create an index on the referencing columns", PostgreSQL
|
||||
docs, "Foreign Keys"), so without one a per-record lookup such as the
|
||||
PostgreSQL Grafana dashboard's DKIM/SPF result subqueries scans the
|
||||
whole child table.
|
||||
"""
|
||||
import re
|
||||
|
||||
client, mock_conn = _make_client()
|
||||
cur = _mock_cursor(mock_conn, [])
|
||||
client.create_tables()
|
||||
statements = _executed_sql(cur)
|
||||
|
||||
foreign_keys = set()
|
||||
for stmt in statements:
|
||||
table = re.search(r"CREATE TABLE IF NOT EXISTS (\w+)", stmt)
|
||||
if table:
|
||||
for column in re.findall(
|
||||
r"^\s*(\w+)[^\n]*\n?\s*REFERENCES", stmt, re.M | re.I
|
||||
):
|
||||
foreign_keys.add((table.group(1), column))
|
||||
indexed = {
|
||||
(m.group(1), m.group(2))
|
||||
for stmt in statements
|
||||
if (m := re.search(r"ON (\w+) \((\w+)\)", stmt))
|
||||
}
|
||||
|
||||
# Every REFERENCES in the DDL must have been attributed to a column,
|
||||
# so a layout the column regex misses fails here instead of passing.
|
||||
references = sum(
|
||||
len(re.findall(r"\bREFERENCES\b", s, re.I)) for s in statements
|
||||
)
|
||||
self.assertEqual(len(foreign_keys), references)
|
||||
self.assertEqual(foreign_keys - indexed, set())
|
||||
|
||||
def test_create_tables_wraps_db_error(self):
|
||||
"""A driver error during DDL is wrapped in PostgreSQLError."""
|
||||
|
||||
|
||||
Reference in new issue
Block a user