Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected NULL timestamp during some queries #3274

Open
mdhornet90 opened this issue Jul 2, 2024 · 3 comments
Open

Unexpected NULL timestamp during some queries #3274

mdhornet90 opened this issue Jul 2, 2024 · 3 comments

Comments

@mdhornet90
Copy link

mdhornet90 commented Jul 2, 2024

I'm using pg v8.12.0, and I'm noticing that in some cases where I'm executing queries a timestamp (with time zone) that's expected to have a value always returns NULL.

The error manifests when I run a large SELECT * FROM table WHERE id in ('about', 'onehundred', 'ids'); query. If I pick one id out of the array and use pg to run that same query again with only one element in the array, I get the timestamp as expected. I also ran this same one-hundred-elem-array query directly against Postgres and could not reproduce this issue, so it seems to be solely the fault of pg. Any ideas what might be happening?

Here's how I'm using pg for reference:

declare namespace DB {
  class User {
    email: string;
    email_verified: boolean;
    password: string;
    attempt_count: number;
    batch_id?: number;
    uploaded_at?: Date; // This timestamptz always returns a value
    job_id?: string;
    linked_at?: Date; // This one never does
  }
}
...
export async function executeQuery<O, I = DB.User>(
  pool: pg.Pool,
  queryString: string,
  transformFn?: (e: I) => O
): Promise<O[]> {
  const client = await pool.connect();
  try {
    const { rows } = await client.query(queryString);
    if (transformFn) {
      return rows.map(transformFn);
    }
    return rows;
  } catch (err: any) {
    logError(err);
    throw err;
  } finally {
    client.release();
  }
}

// USAGE:
await executeInexpensiveQuery(
    pool,
    `SELECT * FROM upload_data WHERE id IN (${userIds.map(id => `'${id}'`).join(',')});`,
)

I know the docs say to not inline those parameters but none of this is injectable. Still, I can try the prescribed way and report back.

@mdhornet90
Copy link
Author

I have some updates based on further testing:

  1. This problem actually presents itself with just one element in that dynamic array. I have no idea why a formatted string would cause problem as opposed to a hard-coded string.
  2. Using values instead of the inline mapping makes no difference.

@brianc
Copy link
Owner

brianc commented Jul 2, 2024 via email

@mdhornet90
Copy link
Author

I appreciate the fast response. I have some client deadlines I need to meet but as soon as I have a free moment I'll send over a POC.

Also just some more context in case this makes a difference or jogs your memory: I'm using the pg-query-stream package for an initial large request against the Pool, and send the results of that query through an awaited pipeline. In every case I interact with the database, I follow the pattern above of connecting a client to the pool and releasing it when I'm done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants