kysely-postgrejs 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +72 -20
- package/README.md +326 -113
- package/package.json +7 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,35 +1,87 @@
|
|
|
1
|
-
|
|
1
|
+
# Changelog
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<!-- rman:documented-up-to a13fa6fec34f8c99f10b1c50ff257b79fbccad0b -->
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## v1.2.0 (2026-10-05)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
### ✨ Features
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
- measure this dialect against pg, and rewrite the README around it (219ef0c)
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
### 🐛 Bug Fixes
|
|
12
12
|
|
|
13
|
-
-
|
|
13
|
+
- **benchmark:** settle the machine before the first scenario is timed (0ee166e)
|
|
14
|
+
- **ci:** restore the type check as a script, and point CI at it (27c05a1)
|
|
15
|
+
- **test:** stop pinning the time value to a time zone (deb47dc)
|
|
14
16
|
|
|
15
|
-
###
|
|
17
|
+
### 📦 Build System
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
- adopt rman 2 and the shared preset (96d2858)
|
|
20
|
+
- move to rman 2.9.0 and preset 1.8.1 (f7b0857)
|
|
21
|
+
- move to rman 2.11.0 and preset 1.8.2 (8b5f40a)
|
|
18
22
|
|
|
19
|
-
|
|
20
|
-
- feat: run Kysely's own dialect suite, and fix what it found @Eray Hanoğlu
|
|
21
|
-
- feat: cancelQuery and killSession, the in-flight abort strategies @Eray Hanoğlu
|
|
22
|
-
- feat: pass fetchAsString through, and take the suite down to three @Eray Hanoğlu
|
|
23
|
+
### 🤖 Continuous Integration
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
- move to the shared v3 workflows (0f93919)
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
- docs: record where the suite's last failure goes @Eray Hanoğlu
|
|
27
|
+
---
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
## v1.1.1 (2026-09-22)
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
### 📚 Documentation
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
- say what each default gives you, not what it guards against (7873de7)
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## v1.1.0 (2026-09-22)
|
|
38
|
+
|
|
39
|
+
### 📚 Documentation
|
|
40
|
+
|
|
41
|
+
- keep the unspecified parameter types, and say what they cost (a160295)
|
|
42
|
+
|
|
43
|
+
### 🧹 Chores
|
|
44
|
+
|
|
45
|
+
- update PostgreJS peer and dev dependency to 3.10.0 (50d201e)
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## v1.0.1 (2026-09-20)
|
|
50
|
+
|
|
51
|
+
### 📚 Documentation
|
|
52
|
+
|
|
53
|
+
- the suite passes outright, and the peer floor follows PostgreJS (6b1a2c7)
|
|
54
|
+
|
|
55
|
+
### 🧹 Chores
|
|
56
|
+
|
|
57
|
+
- require PostgreJS 3.7 (983fd2b)
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## v1.0.0 (2026-09-20)
|
|
62
|
+
|
|
63
|
+
### ✨ Features
|
|
64
|
+
|
|
65
|
+
- the Kysely dialect itself - dialect, driver, connection (8fee481)
|
|
66
|
+
- cancelQuery and killSession, the in-flight abort strategies (f7f31d7)
|
|
67
|
+
- run Kysely's own dialect suite, and fix what it found (eced70f)
|
|
68
|
+
- pass fetchAsString through, and take the suite down to three (946728a)
|
|
69
|
+
|
|
70
|
+
### 📚 Documentation
|
|
71
|
+
|
|
72
|
+
- bring CLAUDE.md in line with the dialect that got built (21d5159)
|
|
73
|
+
- record where the suite's last failure goes (5ac2d0a)
|
|
74
|
+
|
|
75
|
+
### 🧪 Tests
|
|
76
|
+
|
|
77
|
+
- point the suite's two pg-specific tests at this dialect (7773191)
|
|
78
|
+
|
|
79
|
+
### 🧹 Chores
|
|
80
|
+
|
|
81
|
+
- packaging, changelog and a CI job for the dialect suite (89c06d7)
|
|
82
|
+
- require PostgreJS 3.6, and stop documenting 3.5 (12d405c)
|
|
83
|
+
- update CI to ubuntu-24.04, bump PostgreJS to 3.6.1 (f512831)
|
|
84
|
+
|
|
85
|
+
### 💬 General Changes
|
|
86
|
+
|
|
87
|
+
- Initial commit (b53971d)
|
package/README.md
CHANGED
|
@@ -1,10 +1,26 @@
|
|
|
1
1
|
# kysely-postgrejs
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
[![NPM Version][npm-image]][npm-url]
|
|
4
|
+
[![NPM Downloads][downloads-image]][downloads-url]
|
|
5
|
+
[![CI Tests][ci-test-image]][ci-test-url]
|
|
6
|
+
[![Test Coverage][coveralls-image]][coveralls-url]
|
|
5
7
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
A [Kysely](https://kysely.dev) dialect for [PostgreJS](https://github.com/panates/postgrejs). Put it
|
|
9
|
+
where Kysely's own `PostgresDialect` goes and everything above it stays the same - your schema
|
|
10
|
+
types, your queries, your migrations.
|
|
11
|
+
|
|
12
|
+
<!-- bench:intro -->
|
|
13
|
+
|
|
14
|
+
It is faster where it counts, and it allocates far less doing it. A 4 MB `bytea` comes back in 15.6
|
|
15
|
+
ms against 34.2 ms, on 4.1 MB a call against 51.7 MB - `pg` reads that column as hex text, twice the
|
|
16
|
+
size, off the JS heap where a heap figure alone cannot see it. A 100k-element `int4[]` runs 3.3x, on
|
|
17
|
+
2.2 MB against 22.7 MB. Ordinary queries gain less and gain it repeatably: a point read is the
|
|
18
|
+
faster of the two in 92 of 101 alternated pairs. All of it measured through Kysely against Kysely's
|
|
19
|
+
own `PostgresDialect` over `pg`, on the same server: [`doc/BENCHMARKS.md`](doc/BENCHMARKS.md).
|
|
20
|
+
|
|
21
|
+
<!-- /bench:intro -->
|
|
22
|
+
|
|
23
|
+
And the client underneath can do things Kysely has no way to ask for.
|
|
8
24
|
|
|
9
25
|
## Install
|
|
10
26
|
|
|
@@ -12,9 +28,13 @@ and query compiler are Kysely's own `Postgres*` implementations.
|
|
|
12
28
|
npm install kysely-postgrejs kysely postgrejs
|
|
13
29
|
```
|
|
14
30
|
|
|
15
|
-
`kysely` (>=0.29 <0.31) and `postgrejs` (>=3.
|
|
31
|
+
`kysely` (>=0.29 <0.31) and `postgrejs` (>=3.11 <4) are peer dependencies. Node >=22, PostgreSQL 14
|
|
32
|
+
or later - 16 and 18 are what CI runs.
|
|
16
33
|
|
|
17
|
-
|
|
34
|
+
Only the driver is PostgreJS-specific. The adapter, the introspector and the query compiler are
|
|
35
|
+
Kysely's own `Postgres*` implementations, so the SQL is the same either way.
|
|
36
|
+
|
|
37
|
+
## Quick start
|
|
18
38
|
|
|
19
39
|
```ts
|
|
20
40
|
import { Kysely } from 'kysely';
|
|
@@ -28,8 +48,40 @@ const db = new Kysely<Database>({
|
|
|
28
48
|
});
|
|
29
49
|
```
|
|
30
50
|
|
|
31
|
-
|
|
32
|
-
|
|
51
|
+
That is the whole change.
|
|
52
|
+
|
|
53
|
+
## Usage
|
|
54
|
+
|
|
55
|
+
### Connecting
|
|
56
|
+
|
|
57
|
+
`pool` takes a PostgreJS `Pool`, or an async function returning one - it is called once, when the
|
|
58
|
+
driver initialises, which is where a factory belongs if the connection details have to be fetched
|
|
59
|
+
first:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
new PostgrejsDialect({ pool: new Pool('postgres://…') });
|
|
63
|
+
new PostgrejsDialect({ pool: async () => new Pool(await secrets()) });
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`db.destroy()` closes the pool. The pool you passed in is still yours to `acquire()` from directly.
|
|
67
|
+
|
|
68
|
+
### Queries
|
|
69
|
+
|
|
70
|
+
Everything the query builder does works as it does on any PostgreSQL dialect. A transaction holds
|
|
71
|
+
one connection for the whole block and gives it back however it ends, and `trx.savepoint()` is a
|
|
72
|
+
savepoint:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
await db.transaction().execute(async trx => {
|
|
76
|
+
await trx.insertInto('person').values({ name: 'ada' }).execute();
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Every one of those statements goes out through `executeQuery`, which is the seam Kysely wraps its
|
|
81
|
+
logging around - so `log` and `db.on('query')` see the `BEGIN`, the `SAVEPOINT` and the `COMMIT`,
|
|
82
|
+
not only the queries between them.
|
|
83
|
+
|
|
84
|
+
### Streaming
|
|
33
85
|
|
|
34
86
|
Streaming goes through a server-side cursor, with Kysely's chunk size as the cursor's batch size:
|
|
35
87
|
|
|
@@ -40,72 +92,61 @@ for await (const person of db.selectFrom('person').selectAll().stream(100)) {
|
|
|
40
92
|
}
|
|
41
93
|
```
|
|
42
94
|
|
|
43
|
-
|
|
44
|
-
|
|
95
|
+
### Aborting a query
|
|
96
|
+
|
|
97
|
+
Pass a signal, and pick what should happen to the statement already running on the server:
|
|
45
98
|
|
|
46
99
|
```ts
|
|
47
|
-
new
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
},
|
|
100
|
+
const controller = new AbortController();
|
|
101
|
+
|
|
102
|
+
await db.selectFrom('person').selectAll().execute({
|
|
103
|
+
signal: controller.signal,
|
|
104
|
+
inflightQueryAbortStrategy: 'cancel query', // or 'kill session'
|
|
53
105
|
});
|
|
54
106
|
```
|
|
55
107
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
## Config
|
|
59
|
-
|
|
60
|
-
| Option | Default | What it does |
|
|
61
|
-
| --------------------- | ----------------- | ----------------------------------------------------------------------------------------- |
|
|
62
|
-
| `pool` | (required) | A PostgreJS `Pool`, or a function returning one. |
|
|
63
|
-
| `fetchAsString` | - | OIDs to hand back as the server's own text. `[DataTypeOIDs.int8]` is how to get `pg`'s bigints. |
|
|
64
|
-
| `fetchCount` | `4294967295` | How many rows a statement may return before the portal suspends. See below. |
|
|
65
|
-
| `inferParameterTypes` | `true` | Whether a `null` parameter is left for PostgreSQL to type from context. |
|
|
66
|
-
| `prepare` | connection's own | Whether statements are cached as server-side prepared statements. `false` for PgBouncer. |
|
|
67
|
-
| `rollbackOnError` | `false` | Whether a failed statement leaves the rest of the transaction usable. See below. |
|
|
68
|
-
| `typeMap` | `GlobalTypeMap` | A custom `DataTypeMap`, to override how individual PostgreSQL types are decoded. |
|
|
69
|
-
| `onCreateConnection` | - | Called once per physical connection, before it is first handed to Kysely. |
|
|
70
|
-
| `onReserveConnection` | - | Called every time a connection is acquired from the pool. |
|
|
71
|
-
|
|
72
|
-
### Why `fetchCount` defaults to "everything"
|
|
108
|
+
Both strategies are supported, and neither queues behind the pool:
|
|
73
109
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
110
|
+
- **`'cancel query'`** sends a CancelRequest, which the protocol carries on a connection of its own.
|
|
111
|
+
The statement rejects with PostgreSQL's `57014` and the connection stays usable. Kysely's `pg`
|
|
112
|
+
dialect has to run `pg_cancel_backend()` from a second connection instead - either a dedicated
|
|
113
|
+
client or, failing that, one it waits for the pool to free.
|
|
114
|
+
- **`'kill session'`** runs `pg_terminate_backend()` from a session opened for the occasion. The
|
|
115
|
+
query, its transaction and its locks go with the backend; the pool notices the closed connection
|
|
116
|
+
and replaces it.
|
|
79
117
|
|
|
80
|
-
|
|
118
|
+
The default, `'ignore query'`, stops waiting and leaves the statement running - no dialect support
|
|
119
|
+
is involved.
|
|
81
120
|
|
|
82
|
-
|
|
83
|
-
`varchar` and it cannot go into a `json` column; declare a number `int4` and it cannot be coalesced
|
|
84
|
-
with a `varchar` column, compared against `jsonb`, or assigned into one:
|
|
121
|
+
### Options
|
|
85
122
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
123
|
+
| Option | Default | What it does |
|
|
124
|
+
| --------------------- | ---------------- | --------------------------------------------------------------------------------------------- |
|
|
125
|
+
| `pool` | (required) | A PostgreJS `Pool`, or a function returning one. |
|
|
126
|
+
| `fetchAsString` | - | OIDs to hand back as the server's own text. `[DataTypeOIDs.int8]` is how to get `pg`'s bigints. |
|
|
127
|
+
| `fetchCount` | `4294967295` | How many rows a statement may return before the portal suspends. See below. |
|
|
128
|
+
| `inferParameterTypes` | `true` | Whether parameters go to the server untyped, for PostgreSQL to resolve from context. |
|
|
129
|
+
| `prepare` | connection's own | Whether statements are cached as server-side prepared statements. `false` for PgBouncer. |
|
|
130
|
+
| `rollbackOnError` | `false` | Whether a failed statement leaves the rest of the transaction usable. See below. |
|
|
131
|
+
| `typeMap` | `GlobalTypeMap` | A custom `DataTypeMap`, to override how individual PostgreSQL types are decoded. |
|
|
132
|
+
| `onCreateConnection` | - | Called once per physical connection, before it is first handed to Kysely. |
|
|
133
|
+
| `onReserveConnection` | - | Called every time a connection is acquired from the pool. |
|
|
91
134
|
|
|
92
|
-
|
|
93
|
-
where it appears, so none of that surfaces there. The dialect does the same for strings, numbers,
|
|
94
|
-
booleans, bigints and nulls. Dates, buffers, arrays and objects keep PostgreJS's typed binary
|
|
95
|
-
encoders, since their text form is not something the server could parse out of context.
|
|
135
|
+
#### Complete results by default
|
|
96
136
|
|
|
97
|
-
|
|
98
|
-
|
|
137
|
+
`fetchCount` is set to the protocol maximum, so every row a statement produces comes back in one
|
|
138
|
+
result and a row count is a row count. Lower it when you want a statement to stop early and you know
|
|
139
|
+
what a short result means for your queries. Streaming is unaffected: `streamQuery` works from
|
|
140
|
+
Kysely's `chunkSize`, which becomes the cursor's batch size.
|
|
99
141
|
|
|
100
|
-
|
|
101
|
-
await sql`select ${5} as v`.execute(db) // '5'
|
|
102
|
-
await sql`select ${5} + 1 as v`.execute(db) // 6 - the context decides
|
|
103
|
-
```
|
|
142
|
+
#### PostgreSQL's own transaction semantics
|
|
104
143
|
|
|
105
|
-
|
|
106
|
-
|
|
144
|
+
Inside a transaction a failed statement aborts the transaction, the way PostgreSQL and `pg` behave,
|
|
145
|
+
so code written against either keeps working unchanged. PostgreJS can instead wrap each statement in
|
|
146
|
+
a savepoint of its own and leave the transaction usable afterwards: `rollbackOnError: true` opts
|
|
147
|
+
into that.
|
|
107
148
|
|
|
108
|
-
|
|
149
|
+
#### Getting `pg`'s bigints
|
|
109
150
|
|
|
110
151
|
PostgreJS decodes `int8` as a number inside the safe integer range and a `BigInt` beyond it, where
|
|
111
152
|
`pg` hands back a string - which is what Kysely's generated types and most code ported from `pg`
|
|
@@ -120,76 +161,233 @@ new PostgrejsDialect({ pool, fetchAsString: [DataTypeOIDs.int8] });
|
|
|
120
161
|
The server renders those columns as text and the dialect hands them over untouched, so a value past
|
|
121
162
|
2^53 keeps every digit. Any OID works - `numeric`, `date`, `json` - and nothing else is affected.
|
|
122
163
|
|
|
123
|
-
###
|
|
124
|
-
|
|
125
|
-
PostgreJS wraps every statement inside a transaction in a savepoint of its own, so a failed
|
|
126
|
-
statement leaves the transaction usable. That is not what PostgreSQL does, nor what `pg` - and
|
|
127
|
-
therefore every existing Kysely user - expects. The dialect turns it off: a failed statement aborts
|
|
128
|
-
the transaction. Set it back to `true` to opt into PostgreJS's behaviour.
|
|
164
|
+
### Reaching the client underneath
|
|
129
165
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
Pass a signal, and pick what should happen to the statement already running on the server:
|
|
166
|
+
COPY, LISTEN/NOTIFY, large objects, logical replication and pipelining are all a method call away:
|
|
167
|
+
the two hooks hand you the PostgreJS connection, with its full API intact.
|
|
133
168
|
|
|
134
169
|
```ts
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
170
|
+
new PostgrejsDialect({
|
|
171
|
+
pool,
|
|
172
|
+
onCreateConnection: async connection => {
|
|
173
|
+
const postgrejs = (connection as PostgrejsConnection).connection;
|
|
174
|
+
await postgrejs.query(`set application_name = 'reports'`);
|
|
175
|
+
},
|
|
140
176
|
});
|
|
141
177
|
```
|
|
142
178
|
|
|
143
|
-
|
|
179
|
+
## Why
|
|
180
|
+
|
|
181
|
+
It is a drop-in swap for Kysely's own `PostgresDialect`: the same `new Kysely({ dialect })` call,
|
|
182
|
+
the same schema types, the same queries, the same migrations. What you get for it:
|
|
183
|
+
|
|
184
|
+
<!-- bench:payload -->
|
|
185
|
+
|
|
186
|
+
- **Faster where the payload is large** - 2.2x on a 4 MB `bytea` and 3.3x on a 100k-element
|
|
187
|
+
`int4[]`, on a fraction of the memory, because the values arrive in PostgreSQL's binary format
|
|
188
|
+
rather than as text to be parsed.
|
|
189
|
+
|
|
190
|
+
<!-- /bench:payload -->
|
|
191
|
+
|
|
192
|
+
- **Slightly faster on ordinary round trips**, repeatably - statements are prepared and reused
|
|
193
|
+
without anyone asking for it.
|
|
194
|
+
- **A client that can do what Kysely has no way to ask for** - cursors, `COPY`, `LISTEN`/`NOTIFY`,
|
|
195
|
+
large objects, logical replication and pipelining, on the same pool your queries use.
|
|
196
|
+
- **Checked against Kysely's own dialect suite** - every test, no skips, with Kysely's `pg` dialect
|
|
197
|
+
run over the same checkout as the control.
|
|
198
|
+
|
|
199
|
+
<!-- bench:headline -->
|
|
200
|
+
|
|
201
|
+
| Scenario | node-postgres<br>allocated per call | postgrejs<br>allocated per call | |
|
|
202
|
+
| ------------------------------------------------------------------------------------------------------ | ----------------------------------- | -------------------------------- | --------------------- |
|
|
203
|
+
| point read - 1 row of 9 columns | 0.313 ms<br>**27 KB**/call | **0.268 ms**<br>30 KB/call | **1.17x**<br>+13% |
|
|
204
|
+
| point read through the builder - the same row, selected with selectFrom().where() instead of a sql tag | 0.311 ms<br>**29 KB**/call | **0.267 ms**<br>33 KB/call | **1.16x**<br>+14% |
|
|
205
|
+
| page of 200 - 200 rows of 9 columns, mixed types | 1.452 ms<br>479 KB/call | **1.267 ms**<br>**348 KB**/call | **1.15x**<br>**-27%** |
|
|
206
|
+
| concurrent reads - 20 reads at once of 1 row each, pool of 10 | 1.673 ms<br>**433 KB**/call | **1.494 ms**<br>511 KB/call | **1.12x**<br>+18% |
|
|
207
|
+
| int4[] of 100k, full width - 1 row holding 1 array of 100 000 values that use the whole type | 26.082 ms<br>22.7 MB/call | **7.879 ms**<br>**2.2 MB**/call | **3.31x**<br>**-90%** |
|
|
208
|
+
| float8 of 5k rows, full width - 5000 rows of 1 value, eight bytes against seventeen significant digits | 1.241 ms<br>1.4 MB/call | **0.788 ms**<br>**1.2 MB**/call | **1.57x**<br>**-13%** |
|
|
209
|
+
| float8[] of 5k in one row - 1 row holding 1 array of the same 5000 values | 2.286 ms<br>2.9 MB/call | **0.664 ms**<br>**228 KB**/call | **3.44x**<br>**-92%** |
|
|
210
|
+
| uuid of 5k rows - 5000 rows of 1 value, sixteen bytes against thirty-six characters | 1.447 ms<br>1.5 MB/call | **1.067 ms**<br>1.5 MB/call | **1.36x**<br>level |
|
|
211
|
+
| box of 5k rows - 5000 rows of 1 value, four float8s against coordinates that use them | 2.543 ms<br>1.9 MB/call | **1.358 ms**<br>1.9 MB/call | **1.87x**<br>level |
|
|
212
|
+
| bytea of 4MB - 1 row holding 1 value of 4 MB | 34.175 ms<br>51.7 MB/call | **15.566 ms**<br>**4.1 MB**/call | **2.20x**<br>**-92%** |
|
|
213
|
+
| insert one row - 1 row, six parameters | 0.336 ms<br>**22 KB**/call | **0.291 ms**<br>30 KB/call | **1.16x**<br>+33% |
|
|
214
|
+
| insert 500 rows - 500 rows in 1 statement, 2500 parameters that fill their types | 4.149 ms<br>**1.4 MB**/call | **3.383 ms**<br>1.5 MB/call | **1.23x**<br>+9% |
|
|
215
|
+
| insert a 4MB bytea - 1 row holding 1 value of 4 MB, binary on both sides | 19.470 ms<br>**3.3 MB**/call | **17.370 ms**<br>4.1 MB/call | **1.12x**<br>+24% |
|
|
216
|
+
| insert a 100k int4[] - 1 row holding 1 array of 100 000 values, text on both sides | 18.573 ms<br>27.3 MB/call | **13.414 ms**<br>**1.7 MB**/call | **1.38x**<br>**-94%** |
|
|
217
|
+
| twenty inserts in a transaction - 20 rows, one statement each, inside one transaction | 6.633 ms<br>**289 KB**/call | **5.884 ms**<br>406 KB/call | **1.13x**<br>+40% |
|
|
144
218
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
client or, failing that, one it waits for the pool to free.
|
|
149
|
-
- **`'kill session'`** runs `pg_terminate_backend()` from a session opened for the occasion. The
|
|
150
|
-
query, its transaction and its locks go with the backend; the pool notices the closed connection
|
|
151
|
-
and replaces it.
|
|
219
|
+
`kysely` 0.29.6, `postgrejs` 3.12.1, `pg` 8.23.0, PostgreSQL on loopback, Node 24.15.0. Medians; how
|
|
220
|
+
that was measured and how much each row can bear are in [How the numbers were
|
|
221
|
+
measured](#how-the-numbers-were-measured).
|
|
152
222
|
|
|
153
|
-
|
|
154
|
-
|
|
223
|
+
<!-- /bench:headline -->
|
|
224
|
+
|
|
225
|
+
**The gain follows the payload, not the query.** An ordinary read or write gains a little and gains
|
|
226
|
+
it consistently; a column that carries bulk - an array, a `bytea`, anything large in a single row -
|
|
227
|
+
gains twice over, in time and in memory. A schema of text, integers and timestamps will see the top
|
|
228
|
+
of that table and not the bottom.
|
|
229
|
+
|
|
230
|
+
## How the numbers were measured
|
|
231
|
+
|
|
232
|
+
Both drivers run in one process and alternate inside every pair, with the order swapped each time,
|
|
233
|
+
so neither gets a warmer machine than the other. Each figure above is a median of 101 pairs, or 61
|
|
234
|
+
and 41 for the heavier workloads.
|
|
235
|
+
|
|
236
|
+
The medians alone would not be worth much: this is a shared machine, and the absolute figures drift.
|
|
237
|
+
What does not drift is _which_ of the two won each pair, so that is counted separately:
|
|
155
238
|
|
|
156
|
-
|
|
239
|
+
<!-- bench:signtest -->
|
|
157
240
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
241
|
+
| Scenario | pairs | postgrejs faster in | odds of that by luck |
|
|
242
|
+
| ------------------------------- | ----- | ------------------- | -------------------- |
|
|
243
|
+
| point read | 101 | 92 | < 1 in 10^17 |
|
|
244
|
+
| point read through the builder | 101 | 95 | < 1 in 10^20 |
|
|
245
|
+
| page of 200 | 101 | 78 | < 1 in 10^7 |
|
|
246
|
+
| concurrent reads | 61 | 46 | < 1 in 10^4 |
|
|
247
|
+
| int4[] of 100k, full width | 41 | 41 | < 1 in 10^12 |
|
|
248
|
+
| float8 of 5k rows, full width | 61 | 60 | < 1 in 10^16 |
|
|
249
|
+
| float8[] of 5k in one row | 61 | 61 | < 1 in 10^18 |
|
|
250
|
+
| uuid of 5k rows | 61 | 54 | < 1 in 10^9 |
|
|
251
|
+
| box of 5k rows | 61 | 61 | < 1 in 10^18 |
|
|
252
|
+
| bytea of 4MB | 41 | 41 | < 1 in 10^12 |
|
|
253
|
+
| insert one row | 101 | 90 | < 1 in 10^15 |
|
|
254
|
+
| insert 500 rows | 61 | 55 | < 1 in 10^10 |
|
|
255
|
+
| insert a 4MB bytea | 41 | 36 | < 1 in 10^6 |
|
|
256
|
+
| insert a 100k int4[] | 41 | 40 | < 1 in 10^10 |
|
|
257
|
+
| twenty inserts in a transaction | 61 | 58 | < 1 in 10^13 |
|
|
162
258
|
|
|
163
|
-
|
|
259
|
+
<!-- /bench:signtest -->
|
|
164
260
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
261
|
+
That is a sign test - only which driver won counts, and by how much is thrown away, which is exactly
|
|
262
|
+
what makes it survive a noisy machine. Two drivers of equal speed would split the pairs evenly, so
|
|
263
|
+
the last column is the probability of seeing a split that lopsided from a fair coin. It says which
|
|
264
|
+
differences are real; it says nothing about their size, which is what the ratio column is for. A
|
|
265
|
+
split a coin would produce is printed as level rather than rounded into a win.
|
|
266
|
+
|
|
267
|
+
<!-- bench:binary -->
|
|
268
|
+
|
|
269
|
+
Result columns arrive in PostgreSQL's binary format and are decoded per type, where `pg` asks for
|
|
270
|
+
text and parses it. On bulk that is the whole difference: a 100k-element `int4[]` costs 7.9 ms and
|
|
271
|
+
2.2 MB here against 26.1 ms and 22.7 MB, because the text path has to materialise the array literal
|
|
272
|
+
as one string before it can parse it.
|
|
273
|
+
|
|
274
|
+
<!-- /bench:binary -->
|
|
275
|
+
|
|
276
|
+
<!-- bench:prepared -->
|
|
277
|
+
|
|
278
|
+
PostgreJS names and caches a statement per connection - 64 by default, least-recently-used closed -
|
|
279
|
+
so each distinct SQL string is parsed and planned once rather than on every call. `pg` prepares only
|
|
280
|
+
a query it was given a name for, and Kysely does not give it one, so the same statement is parsed
|
|
281
|
+
again on every call there. That is what the point read's 92 pairs of 101 is made of.
|
|
282
|
+
|
|
283
|
+
<!-- /bench:prepared -->
|
|
284
|
+
|
|
285
|
+
<!-- bench:builder -->
|
|
286
|
+
|
|
287
|
+
The query builder itself costs 2.5 KB a call over a `sql` tag here and 1.9 KB on `pg`, and nothing
|
|
288
|
+
that separates from drift on the clock - Kysely's own compiler runs on both sides, so it is the same
|
|
289
|
+
work paid once per driver rather than a difference between them. The same read is 1.17x through the
|
|
290
|
+
tag and 1.16x through the builder.
|
|
291
|
+
|
|
292
|
+
<!-- /bench:builder -->
|
|
293
|
+
|
|
294
|
+
Memory is measured in a child process per driver, because it cannot be measured in a shared one: the
|
|
295
|
+
baseline would be taken with both clients already up, so what a client allocates once and keeps
|
|
296
|
+
would sit under the window rather than in it. The figure is `heapUsed + external`, since a `bytea`
|
|
297
|
+
arrives as a `Buffer` and lives outside the JS heap entirely.
|
|
298
|
+
|
|
299
|
+
Run it yourself with `npm run bench`; [`doc/BENCHMARKS.md`](doc/BENCHMARKS.md) has the held-memory
|
|
300
|
+
and high-water figures, the wire counts, and the rest of the method.
|
|
301
|
+
|
|
302
|
+
## Tested against Kysely's own suite
|
|
303
|
+
|
|
304
|
+
Kysely holds its dialects to a suite of several hundred tests. `scripts/run-kysely-suite.sh` checks
|
|
305
|
+
Kysely out at a known version, points its `postgres` variant at this dialect instead of the built-in
|
|
306
|
+
`pg` one, and runs all of it:
|
|
168
307
|
|
|
169
308
|
```sh
|
|
170
309
|
scripts/run-kysely-suite.sh
|
|
171
310
|
```
|
|
172
311
|
|
|
173
|
-
Against Kysely v0.29.6: **684 passing, nothing failing** - the same
|
|
174
|
-
|
|
175
|
-
range: **728 passing, nothing failing
|
|
312
|
+
Against Kysely v0.29.6: **684 passing, nothing failing** - the same score Kysely's own `pg` dialect
|
|
313
|
+
gets on that checkout, measured the same way on the same machine, with no test skipped. Against
|
|
314
|
+
v0.30.0-beta.2, the other end of the peer range: **728 passing, nothing failing**, and again the
|
|
315
|
+
same for `pg`.
|
|
176
316
|
|
|
177
|
-
Two of those tests name the `pg` driver
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
317
|
+
Two of those tests name the `pg` driver itself: one asserts the error is an instance of `pg`'s
|
|
318
|
+
`DatabaseError`, and one stubs `PostgresDriver.prototype` and expects the stub to be called. The
|
|
319
|
+
patch points both at this dialect's equivalents, so they exercise the same behaviour here that they
|
|
320
|
+
exercise for `pg`.
|
|
181
321
|
|
|
182
322
|
The suite also runs with `fetchAsString: [DataTypeOIDs.int8]`, since every expectation in it is
|
|
183
323
|
written against `pg`'s string bigints.
|
|
184
324
|
|
|
185
|
-
A weekly CI job re-runs both
|
|
186
|
-
|
|
325
|
+
A weekly CI job re-runs both and fails if either count moves in either direction.
|
|
326
|
+
|
|
327
|
+
The suite is also what settled two design decisions. Transaction and savepoint commands go through
|
|
328
|
+
`connection.executeQuery`, the seam Kysely wraps its logging around, so `log` and `db.on('query')`
|
|
329
|
+
see every `BEGIN`, `SAVEPOINT` and `COMMIT` - two dozen tests assert the exact statements a
|
|
330
|
+
transaction runs. And parameter types are left to the server, which is what keeps a parameter usable
|
|
331
|
+
in every context PostgreSQL can infer a type from.
|
|
332
|
+
|
|
333
|
+
## What changes when you switch
|
|
334
|
+
|
|
335
|
+
Everything above the driver is Kysely's own code, so the SQL, the types and the query builder are
|
|
336
|
+
unchanged. What differs is what the client underneath makes of a value on its way back, and
|
|
337
|
+
[`test/B-live/value-shapes.spec.ts`](test/B-live/value-shapes.spec.ts) runs every row of this
|
|
338
|
+
section through both dialects so the table is a test rather than a note.
|
|
339
|
+
|
|
340
|
+
### Values
|
|
341
|
+
|
|
342
|
+
`pg` hands most of the less common types back as the server's own text. PostgreJS decodes them:
|
|
343
|
+
|
|
344
|
+
| case | Kysely + `pg` | kysely-postgrejs |
|
|
345
|
+
| -------------------------------------- | ----------------------- | ------------------------------- |
|
|
346
|
+
| `int8`, so `count` and `sum` | `"2"` | `2`, and a `BigInt` past 2^53 |
|
|
347
|
+
| `numeric` | `"12.34"` | `12.34` |
|
|
348
|
+
| `money` | `"$12.34"` | `12.34` |
|
|
349
|
+
| `time` | `"10:20:30"` | a `Date` |
|
|
350
|
+
| `int4range` and the rest of the family | `"[1,5)"` | a `Range` |
|
|
351
|
+
| `point`, `circle` | plain objects | `Point`, `Circle` - same fields |
|
|
352
|
+
| `box`, `line`, `lseg`, `path`, `polygon` | the text literal | their own classes |
|
|
353
|
+
|
|
354
|
+
Everywhere else the two agree, which is the stronger half of the table: `text`, `boolean`,
|
|
355
|
+
`float8`, `date`, `timestamptz`, `json`, `jsonb`, `uuid`, `bytea`, arrays, `inet` and `bit` all
|
|
356
|
+
arrive identically.
|
|
357
|
+
|
|
358
|
+
`int8` is the one worth deciding about rather than discovering, since `count(*)` is in everyone's
|
|
359
|
+
code: `fetchAsString: [DataTypeOIDs.int8]` gives you `pg`'s strings back, and the test asserts that
|
|
360
|
+
the two then match exactly.
|
|
187
361
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
362
|
+
### Parameters take their type from where they appear
|
|
363
|
+
|
|
364
|
+
Strings, numbers, booleans, bigints and nulls go to the server untyped, exactly as `pg` sends them,
|
|
365
|
+
so PostgreSQL resolves each one from the position it appears in. The same value is a `varchar` next
|
|
366
|
+
to a `varchar` column, a `jsonb` next to a `jsonb` one, and an `int4` in arithmetic - which is what
|
|
367
|
+
keeps `coalesce`, `json` and `jsonb` operators, string concatenation, overloaded functions and
|
|
368
|
+
subscripted assignment working with a plain JavaScript argument:
|
|
369
|
+
|
|
370
|
+
```ts
|
|
371
|
+
await sql`select coalesce(nickname, ${'anonymous'}) from person`.execute(db);
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
Dates, buffers, arrays and objects keep PostgreJS's typed binary encoders, which is what makes them
|
|
375
|
+
compact on the wire.
|
|
376
|
+
|
|
377
|
+
A parameter standing on its own has nothing to resolve against, and PostgreSQL settles on `text`
|
|
378
|
+
there:
|
|
379
|
+
|
|
380
|
+
```ts
|
|
381
|
+
await sql`select ${5} as v`.execute(db); // '5'
|
|
382
|
+
await sql`select ${5} + 1 as v`.execute(db); // 6 - the context decides
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
`inferParameterTypes: false` declares each type from the JavaScript value instead.
|
|
386
|
+
|
|
387
|
+
### Errors
|
|
388
|
+
|
|
389
|
+
Errors are PostgreJS's `DatabaseError`, not `pg`'s. The PostgreSQL `code` (`23505`, `42P01`) is the
|
|
390
|
+
same and is what to match on; `instanceof` against `pg`'s class is not.
|
|
193
391
|
|
|
194
392
|
## Use from a MikroORM driver
|
|
195
393
|
|
|
@@ -218,18 +416,33 @@ The unit tests need nothing; the live ones need a PostgreSQL at `127.0.0.1:5432`
|
|
|
218
416
|
```sh
|
|
219
417
|
npm test # unit + live tests
|
|
220
418
|
npm run citest # the same, with coverage
|
|
221
|
-
npm run
|
|
222
|
-
|
|
419
|
+
npm run typecheck # type check without emitting
|
|
420
|
+
rman lint # eslint, with the organization's flags
|
|
421
|
+
rman check # circular dependencies
|
|
422
|
+
rman build # compile into build/, ready to publish
|
|
223
423
|
|
|
224
424
|
scripts/run-kysely-suite.sh # Kysely's own suite, on its own database
|
|
425
|
+
npm run bench # the benchmark, against pg through Kysely
|
|
426
|
+
npm run bench:report # render the last run into the docs
|
|
225
427
|
```
|
|
226
428
|
|
|
227
|
-
|
|
429
|
+
The tests come in two kinds, and the split is deliberate:
|
|
228
430
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
431
|
+
- `test/A-common` - against the fakes in `test/_support/fakes.ts`, no server. What SQL the dialect
|
|
432
|
+
sends, in what order, with which options: a fake connection records every call and can hold a
|
|
433
|
+
query open, which is how the abort handlers are tested without a server.
|
|
434
|
+
- `test/B-live` - against a real server, including the truncation regression and both abort
|
|
435
|
+
strategies, asserted against `pg_stat_activity` rather than just the promise.
|
|
232
436
|
|
|
233
437
|
## License
|
|
234
438
|
|
|
235
439
|
BSD-3-Clause
|
|
440
|
+
|
|
441
|
+
[npm-image]: https://img.shields.io/npm/v/kysely-postgrejs
|
|
442
|
+
[npm-url]: https://npmjs.org/package/kysely-postgrejs
|
|
443
|
+
[downloads-image]: https://img.shields.io/npm/dm/kysely-postgrejs.svg
|
|
444
|
+
[downloads-url]: https://npmjs.org/package/kysely-postgrejs
|
|
445
|
+
[ci-test-image]: https://github.com/panates/postgrejs-kysely/actions/workflows/test.yml/badge.svg
|
|
446
|
+
[ci-test-url]: https://github.com/panates/postgrejs-kysely/actions/workflows/test.yml
|
|
447
|
+
[coveralls-image]: https://img.shields.io/coveralls/panates/postgrejs-kysely/dev.svg
|
|
448
|
+
[coveralls-url]: https://coveralls.io/r/panates/postgrejs-kysely
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kysely-postgrejs",
|
|
3
3
|
"description": "Kysely dialect for PostgreJS - run a Kysely query builder on PostgreJS's wire-protocol client",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.2.0",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "BSD-3-Clause",
|
|
7
7
|
"peerDependencies": {
|
|
8
8
|
"kysely": ">=0.29.0 <0.31.0",
|
|
9
|
-
"postgrejs": ">=3.
|
|
9
|
+
"postgrejs": ">=3.11.0 <4"
|
|
10
10
|
},
|
|
11
11
|
"type": "module",
|
|
12
12
|
"module": "./index.js",
|
|
@@ -23,7 +23,10 @@
|
|
|
23
23
|
},
|
|
24
24
|
"homepage": "https://www.postgrejs.com/",
|
|
25
25
|
"contributors": [
|
|
26
|
-
"Eray Hanoglu <e.hanoglu@panates.com>"
|
|
26
|
+
"Eray Hanoglu <e.hanoglu@panates.com>",
|
|
27
|
+
"Ilker Gurelli <i.gurelli@panates.com>",
|
|
28
|
+
"Onur Tokel <o.tokel@panates.com>",
|
|
29
|
+
"Bircan Yuruk <b.yuruk@panates.com>"
|
|
27
30
|
],
|
|
28
31
|
"repository": {
|
|
29
32
|
"type": "git",
|
|
@@ -45,4 +48,4 @@
|
|
|
45
48
|
"publishConfig": {
|
|
46
49
|
"access": "public"
|
|
47
50
|
}
|
|
48
|
-
}
|
|
51
|
+
}
|