Performance
postgrelize builds SQL, maps rows to instances and runs hooks and validation itself, so its overhead is what you pay on top of PostgreSQL. This page shows what that overhead is compared with Sequelize v6, how it is measured, and what to tune in your application.
How it compares with Sequelize v6
The benchmark (bench/) runs the same scenarios on postgrelize and on sequelize@6 in one process, alternating which library goes first in every round, and records CPU time per operation (process.cpuUsage()). The ratio is stable on a busy machine even when absolute times are not. In stub mode the pg driver is replaced by an in-memory responder, so only the library's own work is measured:
| Scenario | postgrelize µs/op | v6 ÷ postgrelize |
|---|---|---|
findByPk | 13.3 | 7.7× |
findAll 100 rows | 132.9 | 4.2× |
findAll 10 000 rows | 11 500 | 1.9× |
findAll 10 000 rows, raw: true | 1 014 | 1.3× |
findAll with include (belongsTo) | 242 | 3.1× |
findAll with include (hasMany) | 151 | 1.7× |
findAll with include (belongsToMany) | 167 | 1.9× |
findAndCountAll | 74 | 3.2× |
where with nested operators and a JSON path | 38 | 5.6× |
where with Op.in of 5 000 values | 124 | 18× |
create | 39 | 3.3× |
bulkCreate 100 / 1 000 rows | 434 / 5 053 | 3.1× / 2.6× |
save() of a changed instance | 17.3 | 6.7× |
| transaction with 3 queries | 81 | 5.1× |
build + toJSON | 5.4 | 0.9× |
| model definition | 45 | 9.8× |
Measured on a GitHub Actions runner (ubuntu-latest, Node 22; no database involved in stub mode) with the bench gate workflow on 2026-11-24: main after 1.8.3, median of three runs. Absolute numbers differ on your machine, the ratios are what to compare. The database-mode figures below come from an earlier measurement on a developer machine. Against a real database a statement spends most of its time in the network and in PostgreSQL, so the end-to-end difference is smaller: in database mode postgrelize used 1.1–2.4× less CPU than v6 per operation (raw: true reads of 10 000 rows about 1.1× less), and read 10 000 rows at about 255 000 rows/s (344 000 rows/s with raw: true).
Run it yourself:
PGZ_BENCH_STUB=1 npm run bench # library overhead only, no database
npm run bench # real PostgreSQL (PGZ_TEST_URL or the docker default)Tuning your application
Pool size
Every statement needs a pooled connection, so pool.max is the concurrency limit of one Node.js process. A value close to the number of cores of the database server divided by the number of application processes is a good start; more connections than the server can run in parallel only add queueing and memory use on the server.
export const createPool = (url: string): Sequelize =>
new Sequelize(url, {
logging: false,
// At most `max` statements run at once per Node.js process; extra calls wait for a connection.
pool: { max: 10, idle: 10_000, acquire: 30_000 },
});Load less
Instances cost allocation and change tracking; rows you do not need cost network and parsing time. For read-only paths:
// Only the columns you need, as plain objects: no instances, no change tracking.
const lean = await Reading.findAll({
attributes: ['id', 'value'],
where: { sensor: 's1' },
limit: 100,
raw: true,
});attributeslimits the columns PostgreSQL sends.raw: truereturns plain objects: no instance construction, getters or change tracking. It is about 3× faster than building instances for large reads.limit/offset, or a cursor (findEach, see Advanced queries), keep result sets bounded.
Let PostgreSQL aggregate:
// Aggregates and counts are computed by PostgreSQL; do not load rows to count them.
const total = await Reading.count({ where: { sensor: 's1' } });Write in bulk
One statement for many rows beats a loop of create calls by a wide margin, because every create is a round trip:
// One INSERT for all rows instead of one round trip per row.
const rows = Array.from({ length: 500 }, (_, index) => ({
sensor: `s${index % 5}`,
value: index,
}));
await Reading.bulkCreate(rows, { validate: false });bulkCreatesends one multi-rowINSERT. Passvalidate: falsewhen the rows are already trusted.- For imports of many thousands of rows use
Model.copyFrom(COPY ... FROM STDIN); see Bulk writes and COPY. - Use
UPDATE ... FROM,INSERT ... SELECTandMERGE(Bulk writes and COPY) to change data without moving it through Node.js. - Upserts:
onConflictinstead of "find, then create or update".
Transactions and includes
- Group dependent writes in one transaction (Transactions): one
BEGIN/COMMITpair instead of one implicit transaction per statement. - Prefer
includeover a query per parent row. JoinedhasManyandbelongsToManyincludes multiply result rows; for large hasMany collections useseparate: trueon the include (see Associations) or query the child model directly. - Large
Op.inlists are cheap to build, but PostgreSQL limits a statement to 65 535 bound values; split larger lists or join against aVALUESlist / subquery.
Read replicas
Read-only traffic can go to replicas (Sessions and scaling); the pool of each replica has its own limit.
Prepared statements postgrelize extension
By default every query is sent as an unnamed statement, so PostgreSQL parses and plans it on each call. The statementCache constructor option names repeated statements so each connection parses and plans them once:
export const createCachedPool = (url: string): Sequelize =>
new Sequelize(url, {
logging: false,
// Repeated SELECT / INSERT / UPDATE / DELETE texts are parsed once per connection.
statementCache: { max: 50 },
pool: { max: 10 },
});- Only
SELECT,INSERT,UPDATE,DELETEandWITHtexts of at most 8192 characters are named. Multi-statement texts, DDL,COPY,LISTEN/NOTIFYand cursors stay unnamed. The name ispgz_plus a hash of the SQL text; values are still bound as$nparameters. - Each connection names at most
maxstatements (default 100); further texts run unnamed. There is no eviction: statements disappear with the connection (pool.maxUsesandpool.idlerecycle them). Keeppool.maxbounded: server memory ispool.max × maxstatements. - Texts that vary per call (long
INlists, dynamicwhereshapes) fill the slots without being reused; the cache pays off for a small set of hot, repeated queries (findByPk, counts, single-row writes). - After a schema change that alters a result's columns, PostgreSQL rejects the cached statement with "cached plan must not change result type". Outside a transaction the query is retried once unnamed and that text stays unnamed on that connection; inside a transaction the error is thrown like any other.
- Not for PgBouncer in transaction pooling mode (without protocol-level prepared statement support). The gain is visible only against a real database; the
findByPk statementCachecase ofnpm run benchshows it.
What the library does for you
Query building, SQL text assembly and row mapping are optimised for the common paths: columns of a model are resolved once and reused, rows are converted without per-row lookups, hooks and validators that are not defined cost almost nothing, and identifier quoting is cached. None of it changes SQL text or Sequelize v6 behaviour. If you find a case where postgrelize is slower than v6, open an issue with the scenario; the benchmark in bench/scenarios.ts is the place to add it.