nathan-watkins-unityai
08/29/2025, 7:50 PMSELECT "Patient"."id", "Patient"."lastUpdated", "Patient"."content" FROM "Patient" WHERE ("Patient"."deleted" = 'false' AND "Patient"."projectId" IN ('0123456-0123-0123-0123-01234567','0123456-0123-0123-0123-01234567' AND "Patient"."__identifier" @> ARRAY['0123456-0123-0123-0123-01234567']::UUID[])) LIMIT 3
Explain output
Limit (cost=0.00..169.32 rows=3 width=1085)
-> Seq Scan on "Patient" (cost=0.00..329770.80 rows=5843 width=1085)
Filter: ((NOT deleted) AND (__identifier @> '{0123456-0123-0123-0123-01234567}'::uuid[]) AND ("projectId" = ANY ('{0123456-0123-0123-0123-01234567,0123456-0123-0123-0123-01234567}'::uuid[])))nathan-watkins-unityai
08/29/2025, 7:50 PMenable_seqscan off
Saw it referenced [here](https://github.com/medplum/medplum/issues/3860) (recognizing this isn't documented or intended for production), it did change the queries from sequential scan to index scan but minimal impact
Limit (cost=0.43..178.13 rows=3 width=1083)
-> Index Scan using "Patient_projectId_idx" on "Patient" (cost=0.43..365719.35 rows=6174 width=1083)
Index Cond: ("projectId" = ANY ('{0123456-0123-0123-0123-01234567,0123456-0123-0123-0123-01234567}'::uuid[]))
Filter: ((NOT deleted) AND (__identifier @> '{0123456-0123-0123-0123-01234567}'::uuid[]))
enable_indexscan off
This was just to see the impact, seems to solve this specific problem but also seems highly likely to cause issues elsewhere...
Bitmap Heap Scan on "Patient" (cost=2470.04..53353.51 rows=5843 width=1085)
Recheck Cond: (__identifier @> '{0123456-0123-0123-0123-01234567}'::uuid[])
Filter: ((NOT deleted) AND ("projectId" = ANY ('{0123456-0123-0123-0123-01234567,0123456-0123-0123-0123-01234567}'::uuid[])))
-> Bitmap Index Scan on "Patient___idnt_idx" (cost=0.00..2468.58 rows=5843 width=0)
Index Cond: (__identifier @> '{0123456-0123-0123-0123-01234567}'::uuid[])
We also tried changing from batch bundles to individual upserts with no significant improvement.
Other details-
- PG 16.8, 16vCPU, 128GB
- All migrations for each upgrade completed successfully
- No findings when running $db-schema-diff operation
- $db-invalid-indexes operations returns {"resourceType":"Parameters"}
- It seems the LIMIT 3 from the generated query is what's causing it to use the sequential scan. Removing it leverages bitmap heat scanKarl
09/03/2025, 9:16 PMEXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON)
more info this waynathan-watkins-unityai
09/03/2025, 11:12 PMrandom_page_cost, effective_cache_size, and autovacuum_analyze_threshold/autovacuum_vacuum_threshold.
I'll work on getting a new env stood up to repro and provide the more detailed EXPLAIN, since that will definitely be helpful in determining root cause