Summary: Encountering an error invalid DSA memory alloc request size 1811939328 when executing a prepared statement after multiple runs. The error occurs due to a different execution plan being chosen on the 6th attempt. [...] Version on which the error is not reproduced: "PostgreSQL 16.2 (Debian 16.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit"
If this is indeed a regression from 16.2 odds are good you will need to work around the bug. Namely by ensuring that a generic plan for that query is never chosen. See: plan_cache_mode