Bug in postgres-js driver causes queries to return corrupt results after timeout
A recurring crash in a backend service was traced to a bug in the postgres-js database driver, where a query result cursor was not reset after a connection error. When a query is cancelled mid-execution — typically due to a statement timeout — the row counter retains its previous value, causing the next query on that pooled connection to write results into a sparse array with empty leading slots. This produces arrays whose length exceeds the actual row count, explaining why a LIMIT 500 query appeared to return over 800 rows. The failure mode is particularly dangerous because sparse arrays do not throw errors, silently producing nulls in logs, incorrect API responses, or false 'not found' outcomes on valid records. The bug has been present in the upstream postgres-js repository for approximately a year and is difficult to reproduce locally since it requires a slow query to trigger a mid-scan timeout.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in