fix: 限定捞瓶查询与重试成本

This commit is contained in:
root
2026-09-15 21:13:28 +08:00
parent 0c5cbd75d9
commit c94d7b1af0
7 changed files with 425 additions and 49 deletions
+66
View File
@@ -93,6 +93,72 @@ describe("database authority constraints", () => {
).toBe(true);
});
it("uses the database-only bottle pick candidate partial index", async () => {
const author = await createAccount("candidate-index-author");
await prisma.bottle.createMany({
data: [
...Array.from({ length: 2_000 }, (_, index) => ({
authorId: author.id,
clientRequestId: `ineligible-${index}`,
contentText: `ineligible-${index}`,
reviewStatus: "REVIEWING" as const,
poolStatus: "CLOSED" as const,
})),
...Array.from({ length: 3 }, (_, index) => ({
authorId: author.id,
clientRequestId: `eligible-${index}`,
contentText: `eligible-${index}`,
reviewStatus: "APPROVED" as const,
poolStatus: "IN_POOL" as const,
approvedAt: new Date(),
})),
],
});
const [index] = await prisma.$queryRaw<Array<{ indexdef: string }>>`
SELECT indexdef FROM pg_indexes
WHERE schemaname = 'public' AND tablename = 'bottles'
AND indexname = 'bottles_pick_candidates_id_idx'`;
expect(index?.indexdef).toMatch(
/ON public\.bottles USING btree \(id\) WHERE .*review_status = 'APPROVED'.*pool_status = 'IN_POOL'.*active_lease_id IS NULL/,
);
const { plan, rows } = await prisma.$transaction(async (tx) => {
await tx.$executeRawUnsafe("SET LOCAL enable_seqscan = off");
const explained = await tx.$queryRawUnsafe<
Array<{ "QUERY PLAN": Array<{ Plan: { "Index Name"?: string } }> }>
>(`EXPLAIN (FORMAT JSON)
SELECT id FROM bottles
WHERE review_status = 'APPROVED'
AND pool_status = 'IN_POOL'
AND active_lease_id IS NULL
ORDER BY id LIMIT 32`);
const selected = await tx.$queryRaw<Array<{ content_text: string }>>`
SELECT content_text FROM bottles
WHERE review_status = 'APPROVED'
AND pool_status = 'IN_POOL'
AND active_lease_id IS NULL
ORDER BY id LIMIT 32`;
return { plan: explained[0]!["QUERY PLAN"][0]!.Plan, rows: selected };
});
const indexNames = (node: unknown): string[] => {
if (!node || typeof node !== "object") return [];
const record = node as Record<string, unknown>;
return [
...(typeof record["Index Name"] === "string"
? [record["Index Name"]]
: []),
...Object.values(record).flatMap(indexNames),
];
};
expect(indexNames(plan)).toContain("bottles_pick_candidates_id_idx");
expect(rows.map(({ content_text }) => content_text).sort()).toEqual([
"eligible-0",
"eligible-1",
"eligible-2",
]);
});
it("backfills and requires the session token version snapshot", async () => {
const columns = await prisma.$queryRaw<
Array<{ is_nullable: string; column_default: string | null }>