question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Drastic difference between pg and pg-native performance inserting bytea data

See original GitHub issue

Schema:

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();

test.jpg

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:closed
  • Created 5 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
sehropecommented, Jul 1, 2018

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.

0reactions
charmandercommented, Jul 1, 2018

Are you saying you did require('pg-native') instead of require('pg').native?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found