node-postgres: Problems with bytea performance

I originally opened this as part of a knex issue: https://github.com/tgriesser/knex/issues/2052 but was directed here instead.

I have a simple query:

SELECT glb FROM models where assets_id = 16 LIMIT 1;

glb is a bytea column and the value I’m retrieving is 28630077 bytes (~27MB) (models contains a single row in this example). The query takes 13305 ms to run and the Node process (not the DB process) maxes out the CPU while the query is running). If I query for the assets_id column instead of the glb column, it only takes 2 ms.

Running the same query with the same data from the psql command line completes almost immediately:

time psql -A -c "SELECT glb FROM models where assets_id = 16 LIMIT 1;" master postgres > out.glb

real    0m0.679s
user    0m0.000s
sys     0m0.031s

I also tested the same query in pg-native and it completed in ~450ms, but using pg-native isn’t an option for me at this time (though I might have to re-evaluate that depending on where this issue goes).

Here’s the table definition for completeness.

CREATE TABLE public.models
(
  assets_id integer NOT NULL,
  glb bytea NOT NULL,
  CONSTRAINT models_pkey PRIMARY KEY (assets_id),
  CONSTRAINT models_assets_id_foreign FOREIGN KEY (assets_id)
      REFERENCES public.assets (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE
)
WITH (
  OIDS=FALSE
);
ALTER TABLE public.models
  OWNER TO postgres;

Finally, I thought maybe it was a performance issue in the type parser, but all of the time is taken up by the query and then the typeParser completes almost instantly at the end.

Am I doing something wrong? Or is there a performance issue with bytea issues? I’d be happy to debug this further myself if someone can point me in the correct direction. Thanks in advance.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 4
  • Comments: 22 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks for your help y’all! Much appreciated!

OK, thanks. I’ll do a pass myself and see if there is anything obvious I can add. Thanks for the help on this by the way.

@vitaly-t, I have a fix for the packet-reader problem that I mentioned in https://github.com/brianc/node-postgres/issues/1286#issuecomment-300565369.

I’ve gone from ~13 seconds to ~450ms, matching the native time!