perf(db): compute route ambiguity once per reconfirm run

The four-way GROUP BY over node_short_ids ran once per 1000-route batch, up to 750 times a run, and could eat the whole 5s batch budget. The task now fetches the set once and passes it to each batch.
This commit is contained in:
MrAlders0n authored and MrAlders0n committed 2026-09-30 10:31:08 -04:00
1 parent ab27334f15
commit 1c2841a3c9
8 files changed
+152 -36

No files matched your search

+52 -10
View File
@@ -12,6 +12,43 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const ambiguousPrefixes = `-- name: AmbiguousPrefixes :many
SELECT iata::text AS iata, 1::int AS len, prefix_1 AS prefix FROM node_short_ids GROUP BY iata, prefix_1 HAVING COUNT(*) > 1
UNION ALL
SELECT iata::text, 2::int, prefix_2 FROM node_short_ids GROUP BY iata, prefix_2 HAVING COUNT(*) > 1
UNION ALL
SELECT iata::text, 3::int, prefix_3 FROM node_short_ids GROUP BY iata, prefix_3 HAVING COUNT(*) > 1
UNION ALL
SELECT iata::text, 4::int, prefix_4 FROM node_short_ids GROUP BY iata, prefix_4 HAVING COUNT(*) > 1
`
type AmbiguousPrefixesRow struct {
Iata string `json:"iata"`
Len int32 `json:"len"`
Prefix []byte `json:"prefix"`
}
// Hop prefixes that match >1 node in an IATA, per width. Computed once per reconfirm run.
func (q *Queries) AmbiguousPrefixes(ctx context.Context) ([]AmbiguousPrefixesRow, error) {
rows, err := q.db.Query(ctx, ambiguousPrefixes)
if err != nil {
return nil, err
}
defer rows.Close()
items := []AmbiguousPrefixesRow{}
for rows.Next() {
var i AmbiguousPrefixesRow
if err := rows.Scan(&i.Iata, &i.Len, &i.Prefix); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const createAccount = `-- name: CreateAccount :one
INSERT INTO accounts (name) VALUES ($1)
ON CONFLICT (name) WHERE deactivated_at IS NULL DO NOTHING
@@ -3972,14 +4009,9 @@ WITH batch AS MATERIALIZED (
LIMIT $2
FOR UPDATE OF r SKIP LOCKED
),
amb AS MATERIALIZED (
SELECT iata, 1 AS len, prefix_1 AS p FROM node_short_ids GROUP BY iata, prefix_1 HAVING COUNT(*) > 1
UNION ALL
SELECT iata, 2, prefix_2 FROM node_short_ids GROUP BY iata, prefix_2 HAVING COUNT(*) > 1
UNION ALL
SELECT iata, 3, prefix_3 FROM node_short_ids GROUP BY iata, prefix_3 HAVING COUNT(*) > 1
UNION ALL
SELECT iata, 4, prefix_4 FROM node_short_ids GROUP BY iata, prefix_4 HAVING COUNT(*) > 1
amb AS (
SELECT a.iata::char(3) AS iata, a.len, a.p
FROM ROWS FROM (unnest($3::text[]), unnest($4::int[]), unnest($5::bytea[])) AS a(iata, len, p)
),
dead AS (
SELECT b.iata, b.path_key
@@ -4020,13 +4052,23 @@ SELECT count(*) FROM batch
type ReconfirmRoutesParams struct {
Before pgtype.Timestamptz `json:"before"`
BatchSize int32 `json:"batch_size"`
AmbIata []string `json:"amb_iata"`
AmbLen []int32 `json:"amb_len"`
AmbPrefix [][]byte `json:"amb_prefix"`
}
// Checks one batch of least-recently-reconfirmed routes: deletes those with a departed
// hop node or a hop prefix now matching >1 node in that IATA (length-aware:
// 1/2/3/4-byte hop prefixes check prefix_1/2/3/4), and stamps the survivors.
// 1/2/3/4-byte hop prefixes check prefix_1/2/3/4; ambiguity set supplied by AmbiguousPrefixes),
// and stamps the survivors.
func (q *Queries) ReconfirmRoutes(ctx context.Context, arg ReconfirmRoutesParams) (int64, error) {
row := q.db.QueryRow(ctx, reconfirmRoutes, arg.Before, arg.BatchSize)
row := q.db.QueryRow(ctx, reconfirmRoutes,
arg.Before,
arg.BatchSize,
arg.AmbIata,
arg.AmbLen,
arg.AmbPrefix,
)
var count int64
err := row.Scan(&count)
return count, err