denodrivers / postgres

PostgreSQL driver for Deno

Home Page:https://denodrivers.github.io/postgres

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prepared statement error in multiple queries

malj opened this issue · comments

  • deno-postgres 0.12.0 (also after #321)
  • deno 1.14.0
  • psql 12.8
  • ubuntu 20.04.1 (WSL2)

Problem

Executing multiple invalid prepared statements using the same connection throws an unexpected error Error: Unexpected frame: Z after the first attempt, and breaks the connection.

Expectation

Throw PostgresError every time, and recover the connection for other queries.

Example

import { Client } from "https://deno.land/x/postgres@v0.12.0/mod.ts"
import { PostgresError } from "https://deno.land/x/postgres@v0.12.0/connection/warning.ts"

const client = new Client({ ... })

await client.queryArray`create table if not exists example (n int)`

try {
    await client.queryArray("insert into example (n) values ($1)", "text")
} catch (error) {
    console.log(error) // PostgresError: invalid input syntax for type integer: "text"
    console.log(error instanceof PostgresError) // true
}

try {
    await client.queryArray("insert into example (n) values ($1)", "text")
} catch (error) {
    console.log(error) // Error: Unexpected frame: Z (stack trace below)
    console.log(error instanceof PostgresError) // false
}

try {
    await client.queryArray`select * from example`
} catch (error) {
    console.log(error) // Error: Unexpected frame: 1 (stack trace below)
}

await client.end()
Error: Unexpected frame: Z
    at assertQueryResponse (https://deno.land/x/postgres@v0.12.0/connection/connection.ts:106:13)
    at Connection.#preparedQuery (https://deno.land/x/postgres@v0.12.0/connection/connection.ts:834:11)
    at async Connection.query (https://deno.land/x/postgres@v0.12.0/connection/connection.ts:922:16)
Error: Unexpected frame: 1
    at Connection.#simpleQuery (https://deno.land/x/postgres@v0.12.0/connection/connection.ts:681:15)
    at async Connection.query (https://deno.land/x/postgres@v0.12.0/connection/connection.ts:920:16)

@malj You are not gonna get the latest changes by using a tagged version, you need to pull from master
This is the latest passing commit https://raw.githubusercontent.com/denodrivers/postgres/235d57568bd3a49c353a988918d06077197e9044/mod.ts

@Soremwar I know, I pulled the changes directly from the main branch after you've merged the PR, but the results are the same. I used the tagged version in the example for clarity, since this is a new issue.

After digging a lot into the Postgres spec I finally found the reason of the failure, and why the previous patch only worked on simple queries and not prepared ones. Thanks for the report @malj !

@Soremwar I've just tested the changes, everything seems to work as expected now. Thanks for your time and very quick responses!