porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.04k stars 257 forks source link

Intermittent undefined values in query results #795

Open KenLR opened 5 months ago

KenLR commented 5 months ago

Hi everyone! I've encountered an intermittent and unexpected behaviour with postgres.js.

Summary

The result from postgres.js queries occasionally contains undefined values. The result in such cases would look like:

It's not clear to me if this is an expected behaviour and, if so, under what circumstances it occurs.

This unexpected behaviour leads to errors like: Cannot read properties of undefined (reading 'id') as until recently, our results were always predictable: we'd receive either an empty or non-sparse array as a result.

Details

The issue appears sporadically and doesn't seem to correlate directly with any specific type of query. However, we've observed increased Postgres latency, with some queries timing out around the same period. It's unclear if these timeouts are somehow related to this behaviour; the frequency of timeouts is much higher compared to the undefined values.

I am unable to reproduce the issue locally; we only have production logs showing this occurring a few times a day.

Here is a portion from our codebase:

this.sqlClient = postgres({ ... });

...

const exec = this.sqlClient.unsafe(query.text, query.values, { prepare: true });

const timeoutMs = opts.timeoutMs ?? this.defaultQueryTimeoutMs;

if (timeoutMs) {
    const timer = setTimeout(() => exec.cancel(), timeoutMs + 1);
    return exec.finally(() => clearTimeout(timer));
} else {
    return exec;
}

Workaround

If I apply a filter to the results returned by exec, as shown in the example below, it effectively prevents the errors we've been experiencing. This leads me to believe that the undefined values are indeed originating from postgres.js, rather than from other parts of our stack.

exec.then((results) => results.filter((result) => result !== undefined))

Environment

Component Version
postgres.js 3.4.3
PostgreSQL 15.3
Node.js 20.5.1

Happy to provide more details. Any insights or guidance would be greatly appreciated.

Thank you!

porsager commented 4 months ago

Hi @KenLR - Thanks for the informative issue. I'm sorry I haven't had time to respond, if this is correct, that is quite serious! I'm unfortunately very pressed for time, so I don't know when I can dig into it, but if you have any new information, do post it here!