Drastic difference between pg and pg-native performance inserting bytea data
See original GitHub issueSchema:
CREATE TABLE test (
id SERIAL PRIMARY KEY,
data bytea
);
CREATE UNIQUE INDEX test_pkey ON test(id int4_ops);
test.js
:
import fs from 'fs';
import pg from 'pg';
const image = fs.readFileSync('./test.jpg');
const main = async () => {
const client = new pg.native.Client();
await client.connect();
let index = 1000;
while (index--) {
await client.query('INSERT INTO "test" (data) VALUES ($1) RETURNING id', [
image
]);
}
await client.end();
};
main();
Evaluation:
$ time node ./test.js
Result with pg
:
0.88s user
0.39s system
0% cpu
2:37.37 total
Result with pg-native
:
4.95s user
0.16s system
19% cpu
26.118 total
That is a huge difference. What is the overhead?
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (12 by maintainers)
Top Results From Across the Web
Using PostgreSQL CURRENT_DATE function with Node and ...
Drastic difference between pg and pg-native performance inserting bytea data. Schema: CREATE TABLE test ( id SERIAL PRIMARY KEY, data bytea ); ...
Read more >Binary data performance in PostgreSQL - CYBERTEC
In my benchmark, retrieving binary objects from the database is roughly ten times slower that reading them from files in a file system....
Read more >Fermi Estimates On Postgres Performance - Citus Data
The answer is: it depends. The performance you can expect from single node Postgres comes down to your workload, both on inserts and...
Read more >Re: Performance of ByteA: ascii vs binary - PostgreSQL
I tested a ByteA column. > > If I used ascii data the tests took 52 seconds. > If I used random binary...
Read more >Key Metrics for Amazon RDS PostgreSQL Monitoring - Datadog
PostgreSQL maintains data reliability by logging each transaction in the WAL on the primary server, and writing it to disk periodically. In ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes that was my mistake trying this out. Using the the .native approach works fine for the getting promises return values.
Real issue is that libpq expects a string[] for the query parameters and pg-native doesn’t perform any type conversions. It blindly passes the params array to libpq which somehow is interpreted as a zero length string for the parameter value (so it inserts a zero length value). The performance difference is because the native version isn’t actually inserting anything so it’s not a valid comparison, just a bug in native.
Should either be documented as insisting on string[] parameters (and throw an error if not that) or use the same type conversions as the pure js driver.
Are you saying you did
require('pg-native')
instead ofrequire('pg').native
?