Skip to content

Intermittent undefined values in query results #795

Closed

Description

@KenLR

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:

  • [{ id: 1 }, { id: 2 }, undefined]

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!

Activity

  1. added
    bug
    on Feb 17, 2024
  2. porsager commented on Feb 17, 2024

    @porsager
    Owner

    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!

  3. henryzhang03 commented on Feb 5, 2026

    @henryzhang03

    @porsager hey! we are running into these issues as well, could we get an update on this?

  4. henryzhang03 commented on Feb 5, 2026

    @henryzhang03

    @KenLR also if you guys solved the issue, would love to hear if you implemented something different than just filtering out undefined values

  5. porsager commented on Feb 5, 2026

    @porsager
    Owner

    @henryzhang03 I haven't had any other report than OPs back then, and haven't been able to reproduce.. Can you supply more info?

  6. henryzhang03 commented on Feb 6, 2026

    @henryzhang03

    @porsager , to give you some context:

    We use DigitalOcean's managed database with connection pooling enabled (they use pgbouncer under the hood). While using postgres.js, sometimes SELECT queries will return an array of values like this: [validValue1, validValue2, undefined, undefined, undefined, undefined, undefined] when the correct result is actually just the non-undefined values: [validValue1, validValue2]. We believe this has something to do with how the library handles connection pooling/high throughput queries on the application layer.

  7. porsager commented on Feb 6, 2026

    @porsager
    Owner

    What version are you using?

  8. henryzhang03 commented on Feb 6, 2026

    @henryzhang03

    What version are you using?

    3.4.5

  9. henryzhang03 commented on Feb 6, 2026

    @henryzhang03
  10. porsager commented on Feb 6, 2026

    @porsager
    Owner

    Can you give the latest a try?

  11. henryzhang03 commented on Feb 6, 2026

    @henryzhang03

    @porsager we upgraded to 3.4.7 as I think 3.4.8 people mentioned bugs with transactions. We also are encountering another bug:

    {
      "err": {
        "type": "TypeError",
        "message": "Cannot read properties of undefined (reading 'length')",
        "stack": [
          "TypeError: Cannot read properties of undefined (reading 'length')",
          "    at DataRow (/root/<REDACTED>/node_modules/postgres/cjs/src/connection.js:490:49)",
          "    at handle (/root/<REDACTED>/node_modules/postgres/cjs/src/connection.js:480:6)",
          "    at TLSSocket.data (/root/<REDACTED>/node_modules/postgres/cjs/src/connection.js:315:9)",
          "    at TLSSocket.emit (node:events:508:28)",
          "    at addChunk (node:internal/streams/readable:559:12)",
          "    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)",
          "    at Readable.push (node:internal/streams/readable:390:5)",
          "    at TLSWrap.onStreamRead (node:internal/stream_base_commons:189:23)",
          "    at cachedError (/root/<REDACTED>/node_modules/postgres/cjs/src/query.js:170:23)",
          "    at new Query (/root/<REDACTED>/node_modules/postgres/cjs/src/query.js:36:24)",
          "    at sql (/root/<REDACTED>/node_modules/postgres/cjs/src/index.js:112:11)"
        ]
      }
    }
    
  12. henryzhang03 commented on Feb 6, 2026

    @henryzhang03

    on 3.4.7^

  13. added a commit that references this issue on Oct 29, 2026
    06cf284
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions