Commit 607d7f7f by luoqi

fix(persona): recompute-persona --clinics 大诊所分块查患者(避开 PG 32767 绑定参数上限)

建国门补摄(~5万患者)时 prisma.patient.findMany({ id: { in: 50383 } }) 触发
"too many bind variables, max 32767"。改为按 2万/块分批查再合并(排序保持分片稳定序)。
小诊所行为不变;不传 --clinics 仍走原全量路径。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
parent 3fb81481
Pipeline #3365 failed in 0 seconds
......@@ -75,16 +75,34 @@ async function bootstrap() {
clinicPatientIds = ids;
}
let patients = await prisma.patient.findMany({
where: {
hostId: host.id,
active: true,
...(clinicPatientIds ? { id: { in: clinicPatientIds } } : {}),
...(args.pids?.length ? { externalId: { in: args.pids } } : args.pid ? { externalId: args.pid } : {}),
},
select: { id: true, externalId: true, name: true },
orderBy: { id: 'asc' }, // 稳定序,供分片按下标取模(各进程不重不漏)
});
const extraWhere = args.pids?.length
? { externalId: { in: args.pids } }
: args.pid
? { externalId: args.pid }
: {};
let patients: { id: string; externalId: string; name: string | null }[];
if (clinicPatientIds) {
// Postgres 单条 prepared statement 绑定参数上限 32767 —— 大诊所(如建国门 ~5 万患者)
// 一次 id IN (...) 会超限报错,分块查再合并。块大小留足余量给其他 where 条件。
const CHUNK = 20000;
const acc: { id: string; externalId: string; name: string | null }[] = [];
for (let i = 0; i < clinicPatientIds.length; i += CHUNK) {
const slice = clinicPatientIds.slice(i, i + CHUNK);
const part = await prisma.patient.findMany({
where: { hostId: host.id, active: true, id: { in: slice }, ...extraWhere },
select: { id: true, externalId: true, name: true },
});
acc.push(...part);
}
acc.sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)); // 稳定序供分片
patients = acc;
} else {
patients = await prisma.patient.findMany({
where: { hostId: host.id, active: true, ...extraWhere },
select: { id: true, externalId: true, name: true },
orderBy: { id: 'asc' }, // 稳定序,供分片按下标取模(各进程不重不漏)
});
}
if (args.shard) {
const { i, k } = args.shard;
patients = patients.filter((_, idx) => idx % k === i);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment