From 18186998b89a8ec60b82fd7140783f8833810e2d Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Wed, 18 Sep 2024 10:15:01 +1000 Subject: [PATCH 01/17] Update README.md to fix broken link to Node.js stream backpressure documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 421d19a0..7c7b83f0 100644 --- a/README.md +++ b/README.md @@ -537,7 +537,7 @@ for await (const chunk of readableStream) { } ``` -> **NOTE** This is a low-level API which does not provide any type safety. To make this work, you must match your [`copy query` parameters](https://www.postgresql.org/docs/14/sql-copy.html) correctly to your [Node.js stream read or write](https://nodejs.org/api/stream.html) code. Ensure [Node.js stream backpressure](https://nodejs.org/en/docs/guides/backpressuring-in-streams/) is handled correctly to avoid memory exhaustion. +> **NOTE** This is a low-level API which does not provide any type safety. To make this work, you must match your [`copy query` parameters](https://www.postgresql.org/docs/14/sql-copy.html) correctly to your [Node.js stream read or write](https://nodejs.org/api/stream.html) code. Ensure [Node.js stream backpressure](https://nodejs.org/en/learn/modules/backpressuring-in-streams) is handled correctly to avoid memory exhaustion. ### Canceling Queries in Progress From 75dab3771074cec8595c0a403d1e19218017415c Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Fri, 27 Sep 2024 12:30:54 +0200 Subject: [PATCH 02/17] Try postgres 17 (might be too soon) --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index aec631bf..48948290 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,7 @@ jobs: fail-fast: false matrix: node: ['12', '14', '16', '18', '20', '21'] - postgres: ['12', '13', '14', '15', '16'] + postgres: ['12', '13', '14', '15', '16', '17'] runs-on: ubuntu-latest services: postgres: From f84f21a282b7a15ccb5cba6bb772f815bf0467f5 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Fri, 27 Sep 2024 12:42:12 +0200 Subject: [PATCH 03/17] also node 22 --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 48948290..bf65797a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: false matrix: - node: ['12', '14', '16', '18', '20', '21'] + node: ['12', '14', '16', '18', '20', '21', '22'] postgres: ['12', '13', '14', '15', '16', '17'] runs-on: ubuntu-latest services: From 5fb70c14c08c7f378562571ea66ee7a69f19bd17 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Fri, 25 Oct 2024 10:36:31 +0200 Subject: [PATCH 04/17] Fix for deno 2 --- transpile.deno.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transpile.deno.js b/transpile.deno.js index 923ac9af..f077677b 100644 --- a/transpile.deno.js +++ b/transpile.deno.js @@ -55,7 +55,7 @@ function transpile(x, name, folder) { .replace('{ spawnSync }', '{ spawn }') } if (name === 'index.js') - x += '\n;window.addEventListener("unload", () => Deno.exit(process.exitCode))' + x += '\n;globalThis.addEventListener("unload", () => Deno.exit(process.exitCode))' } const buffer = x.includes('Buffer') From 5974e7fcc171e456737e9eb34a90f0ba2ea6ef56 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Fri, 25 Oct 2024 10:36:45 +0200 Subject: [PATCH 05/17] build --- cf/src/connection.js | 2 +- cjs/src/connection.js | 2 +- deno/README.md | 2 +- deno/src/connection.js | 2 +- deno/tests/index.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cf/src/connection.js b/cf/src/connection.js index c9231dc6..ee8b1e69 100644 --- a/cf/src/connection.js +++ b/cf/src/connection.js @@ -387,7 +387,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose } function queryError(query, err) { - Object.defineProperties(err, { + 'query' in err || 'parameters' in err || Object.defineProperties(err, { stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug }, query: { value: query.string, enumerable: options.debug }, parameters: { value: query.parameters, enumerable: options.debug }, diff --git a/cjs/src/connection.js b/cjs/src/connection.js index 3b913a47..f7f58d14 100644 --- a/cjs/src/connection.js +++ b/cjs/src/connection.js @@ -385,7 +385,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose } function queryError(query, err) { - 'parameters' in err || Object.defineProperties(err, { + 'query' in err || 'parameters' in err || Object.defineProperties(err, { stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug }, query: { value: query.string, enumerable: options.debug }, parameters: { value: query.parameters, enumerable: options.debug }, diff --git a/deno/README.md b/deno/README.md index 31ea4aea..6f8085cf 100644 --- a/deno/README.md +++ b/deno/README.md @@ -533,7 +533,7 @@ for await (const chunk of readableStream) { } ``` -> **NOTE** This is a low-level API which does not provide any type safety. To make this work, you must match your [`copy query` parameters](https://www.postgresql.org/docs/14/sql-copy.html) correctly to your [Node.js stream read or write](https://nodejs.org/api/stream.html) code. Ensure [Node.js stream backpressure](https://nodejs.org/en/docs/guides/backpressuring-in-streams/) is handled correctly to avoid memory exhaustion. +> **NOTE** This is a low-level API which does not provide any type safety. To make this work, you must match your [`copy query` parameters](https://www.postgresql.org/docs/14/sql-copy.html) correctly to your [Node.js stream read or write](https://nodejs.org/api/stream.html) code. Ensure [Node.js stream backpressure](https://nodejs.org/en/learn/modules/backpressuring-in-streams) is handled correctly to avoid memory exhaustion. ### Canceling Queries in Progress diff --git a/deno/src/connection.js b/deno/src/connection.js index 81f26c08..1726a9aa 100644 --- a/deno/src/connection.js +++ b/deno/src/connection.js @@ -388,7 +388,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose } function queryError(query, err) { - Object.defineProperties(err, { + 'query' in err || 'parameters' in err || Object.defineProperties(err, { stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug }, query: { value: query.string, enumerable: options.debug }, parameters: { value: query.parameters, enumerable: options.debug }, diff --git a/deno/tests/index.js b/deno/tests/index.js index 754eabd3..5b5d6e57 100644 --- a/deno/tests/index.js +++ b/deno/tests/index.js @@ -2583,4 +2583,4 @@ t('arrays in reserved connection', async() => { ] }) -;window.addEventListener("unload", () => Deno.exit(process.exitCode)) \ No newline at end of file +;globalThis.addEventListener("unload", () => Deno.exit(process.exitCode)) \ No newline at end of file From b231b688489212e40ab54e9870f84f55f2be5dd0 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Fri, 25 Oct 2024 10:42:52 +0200 Subject: [PATCH 06/17] 3.4.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 47f3add2..d53fe2ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postgres", - "version": "3.4.4", + "version": "3.4.5", "description": "Fastest full featured PostgreSQL client for Node.js", "type": "module", "module": "src/index.js", From 9f38ea1c2e2ab88c4b1c207c32c68ee47c327e2a Mon Sep 17 00:00:00 2001 From: gimse <23360355+gimse@users.noreply.github.com> Date: Sun, 7 Jul 2024 10:07:18 +0200 Subject: [PATCH 07/17] adding env.PGUSERNAME || --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 0573e2bc..b6a9a9f7 100644 --- a/src/index.js +++ b/src/index.js @@ -480,7 +480,7 @@ function parseOptions(a, b) { {} ), connection : { - application_name: 'postgres.js', + application_name: env.PGUSERNAME || 'postgres.js', ...o.connection, ...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {}) }, From be716e220066470436012d76eec850a37de2f077 Mon Sep 17 00:00:00 2001 From: gimse <23360355+gimse@users.noreply.github.com> Date: Sun, 7 Jul 2024 10:14:15 +0200 Subject: [PATCH 08/17] README --- README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7c7b83f0..8f59cef4 100644 --- a/README.md +++ b/README.md @@ -1125,15 +1125,16 @@ It is also possible to connect to the database without a connection string or an const sql = postgres() ``` -| Option | Environment Variables | -| ----------------- | ------------------------ | -| `host` | `PGHOST` | -| `port` | `PGPORT` | -| `database` | `PGDATABASE` | -| `username` | `PGUSERNAME` or `PGUSER` | -| `password` | `PGPASSWORD` | -| `idle_timeout` | `PGIDLE_TIMEOUT` | -| `connect_timeout` | `PGCONNECT_TIMEOUT` | +| Option | Environment Variables | +| ------------------ | ------------------------ | +| `host` | `PGHOST` | +| `port` | `PGPORT` | +| `database` | `PGDATABASE` | +| `username` | `PGUSERNAME` or `PGUSER` | +| `password` | `PGPASSWORD` | +| `application_name` | `PGAPPNAME` | +| `idle_timeout` | `PGIDLE_TIMEOUT` | +| `connect_timeout` | `PGCONNECT_TIMEOUT` | ### Prepared statements From ef7afdb817d00cc7208bd1cefa88f861bfc2cbde Mon Sep 17 00:00:00 2001 From: gimse <23360355+gimse@users.noreply.github.com> Date: Sun, 7 Jul 2024 10:14:47 +0200 Subject: [PATCH 09/17] env.PGAPPNAME || --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index b6a9a9f7..2dfd24e8 100644 --- a/src/index.js +++ b/src/index.js @@ -480,7 +480,7 @@ function parseOptions(a, b) { {} ), connection : { - application_name: env.PGUSERNAME || 'postgres.js', + application_name: env.PGAPPNAME || 'postgres.js', ...o.connection, ...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {}) }, From 4099f3412bb1d9f58ef223e7e4444bc5e4a74a2d Mon Sep 17 00:00:00 2001 From: gimse <23360355+gimse@users.noreply.github.com> Date: Sun, 7 Jul 2024 10:35:18 +0200 Subject: [PATCH 10/17] changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8939f7c8..ed7ec4f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## v3.3.0 - 9 July 2024 +- Adding support for the PGAPPNAME environment variable + ## v3.2.4 - 25 May 2022 - Allow setting keep_alive: false bee62f3 - Fix support for null in arrays - fixes #371 b04c853 From a2c7de12b3bfc6809051d94ba6115150f80678e3 Mon Sep 17 00:00:00 2001 From: gimse <23360355+gimse@users.noreply.github.com> Date: Sun, 7 Jul 2024 15:01:06 +0200 Subject: [PATCH 11/17] removing change log --- CHANGELOG.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed7ec4f8..8939f7c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,5 @@ # Changelog -## v3.3.0 - 9 July 2024 -- Adding support for the PGAPPNAME environment variable - ## v3.2.4 - 25 May 2022 - Allow setting keep_alive: false bee62f3 - Fix support for null in arrays - fixes #371 b04c853 From 6ec85a432b17661ccacbdf7f765c651e88969d36 Mon Sep 17 00:00:00 2001 From: mrl5 <31549762+mrl5@users.noreply.github.com> Date: Fri, 24 May 2024 19:34:59 +0200 Subject: [PATCH 12/17] docs(readme): mention pgbouncer supports protocol-level named prepared statements --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 8f59cef4..1dcdd668 100644 --- a/README.md +++ b/README.md @@ -1140,6 +1140,10 @@ const sql = postgres() Prepared statements will automatically be created for any queries where it can be inferred that the query is static. This can be disabled by using the `prepare: false` option. For instance — this is useful when [using PGBouncer in `transaction mode`](https://github.com/porsager/postgres/issues/93#issuecomment-656290493). +**update**: [since 1.21.0](https://www.pgbouncer.org/2023/10/pgbouncer-1-21-0) +PGBouncer supports protocol-level named prepared statements when [configured +properly](https://www.pgbouncer.org/config.html#max_prepared_statements) + ## Custom Types You can add ergonomic support for custom types, or simply use `sql.typed(value, type)` inline, where type is the PostgreSQL `oid` for the type and the correctly serialized string. _(`oid` values for types can be found in the `pg_catalog.pg_type` table.)_ From 93f5686ff9cd86ab0590e79b4d94f984a40183ad Mon Sep 17 00:00:00 2001 From: Valentinas Janeiko Date: Tue, 21 Jan 2025 19:51:25 +0000 Subject: [PATCH 13/17] chore: fix CI --- .github/workflows/test.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bf65797a..8ae323dd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,7 +28,6 @@ jobs: - uses: actions/checkout@v4 - run: | date - sudo apt purge postgresql-14 sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo apt-get update From 3374a8aeb681b9d573459f1f5897c854a367cc55 Mon Sep 17 00:00:00 2001 From: Valentinas Janeiko <2305836+valeneiko@users.noreply.github.com> Date: Tue, 21 Jan 2025 20:29:12 +0000 Subject: [PATCH 14/17] try purging PG16 instead? --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8ae323dd..af00f7e0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,6 +28,7 @@ jobs: - uses: actions/checkout@v4 - run: | date + sudo apt purge postgresql-16 sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo apt-get update From 089214e85c23c90cf142d47fb30bd03f42874984 Mon Sep 17 00:00:00 2001 From: Louis Orleans Date: Tue, 21 Jan 2025 17:16:15 -0800 Subject: [PATCH 15/17] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20fix=20CONNECT=5FTIME?= =?UTF-8?q?OUT=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `CONNECT_TIMEOUT` error's name is `CONNECT_TIMEOUT`, not `CONNECTION_CONNECT_TIMEOUT`. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1dcdd668..b79c207f 100644 --- a/README.md +++ b/README.md @@ -1303,8 +1303,8 @@ This error is thrown if the user has called [`sql.end()`](#teardown--cleanup) an This error is thrown for any queries that were pending when the timeout to [`sql.end({ timeout: X })`](#teardown--cleanup) was reached. -##### CONNECTION_CONNECT_TIMEOUT -> write CONNECTION_CONNECT_TIMEOUT host:port +##### CONNECT_TIMEOUT +> write CONNECT_TIMEOUT host:port This error is thrown if the startup phase of the connection (tcp, protocol negotiation, and auth) took more than the default 30 seconds or what was specified using `connect_timeout` or `PGCONNECT_TIMEOUT`. From ad0ed4476e09f41f147859cb5a42971d2b99e9c7 Mon Sep 17 00:00:00 2001 From: adrtivv Date: Fri, 11 Apr 2025 00:37:07 +0530 Subject: [PATCH 16/17] fixed typings for generic error code variants --- types/index.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 8989ff47..eb604918 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -456,7 +456,8 @@ declare namespace postgres { | 'NOT_TAGGED_CALL' | 'UNDEFINED_VALUE' | 'MAX_PARAMETERS_EXCEEDED' - | 'SASL_SIGNATURE_MISMATCH'; + | 'SASL_SIGNATURE_MISMATCH' + | 'UNSAFE_TRANSACTION'; message: string; } From b0d8c8f363e006a74472d76f859da60c52a80368 Mon Sep 17 00:00:00 2001 From: Stephen Haberman Date: Sat, 3 May 2025 21:23:22 -0500 Subject: [PATCH 17/17] docs: Add prepare: true to sql.unsafe docs. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b79c207f..c135cd17 100644 --- a/README.md +++ b/README.md @@ -568,6 +568,8 @@ If you know what you're doing, you can use `unsafe` to pass any string you'd lik sql.unsafe('select ' + danger + ' from users where id = ' + dragons) ``` +By default, `sql.unsafe` assumes the `query` string is sufficiently dynamic that prepared statements do not make sense, and so defaults them to off. If you'd like to re-enable prepared statements, you can pass `{ prepare: true }`. + You can also nest `sql.unsafe` within a safe `sql` expression. This is useful if only part of your fraction has unsafe elements. ```js