kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.44k stars 266 forks source link

Postgres: is prepared statement feature currently used? #329

Closed ilijaNL closed 1 year ago

ilijaNL commented 1 year ago

Hello, inspecting the implementation of postgres dialect and driver at https://github.com/koskimas/kysely/blob/fdc7b36c4823898ede69fe11959cb5bd695eeb94/src/dialect/postgres/postgres-driver.ts#L103 I do not see that the queryId is used (should be 2nd argument looking at the usage references). Does this mean that the prepared statement feature is not used (https://node-postgres.com/features/queries#prepared-statements)?

koskimas commented 1 year ago

~No it doesn't mean that. Prepared statements are used. You can trust that we have some tiny bit of competence here.~

Oh shit, misunderstood your question there.

We don't use the prepared statement cache feature. Prepared statement cache is separate for each connection AFAIK. You'd only get performance benefits if you execute the same statement multiple times inside a transaction. Otherwise kysely always takes a random connection from the pool and you'd get a large amount of cache misses.

ilijaNL commented 1 year ago

You are right, but sometimes it can be an useful optimization especially when the pool is small and query is complex.

I guess current workaround is to .compile() and calling the pg.query directly.