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.

how to avoid pg to parse a result object

See original GitHub issue

I’m using api-service as a proxy between PostgreSQL and client so JSON.parse result from PostgreSQL and JSON.stringify the result to client is unnecessary and only eat service time. Could I switch off parsing response from PostgreSQL into JSON?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
mattzeunertcommented, Oct 5, 2020

It’s about 5000 rows with about 1kb of JSON each. I didn’t run the test with pg, I just ran explain analyze on the queries with and without ::text, and somehow the text conversion did slow it down a lot. (I wonder if it’s just because Postgres doesn’t normally count the serialization of the row data as part of the execution time?)

I eventually found a way to prevent parsing using the types property:

const types = require("pg").types;
res = await con.query({
  text: `SELECT values FROM table`,
  types: {
    getTypeParser(oid, ...rest) {
      if (oid === types.builtins.JSON || oid === types.builtins.JSONB) {
        return (v) => v;
      }
      return types.getTypeParser(oid, ...rest);
    },
  },
});
1reaction
budarincommented, Feb 2, 2020

thanks !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parsing a PostgreSQL result object in a Rails app
Is there any built-in PG way to do this, or a workaround, or anything really? Here is the query I'm using: SELECT row_to_json(row(company_name, ......
Read more >
Understanding PG Result object - Kiprosh Blogs
Understanding PG Result object. In rails one of the way to execute raw query is use of ActiveRecord::Base.connection.execute.
Read more >
Work Easily With JSON Using PostgreSQL Parse JSON - Learn
You can parse the JSON objects using PostgreSQL's JSON operators and functions, and then insert the values into your desired PostgreSQL tables ...
Read more >
Queries – node-postgres
This will inform the result parser to bypass collecting rows into a JavaScript object, and instead will return each row as an array...
Read more >
9.15. JSON Functions and Operators - PostgreSQL
Function Return Type Example Example Result array_to_json(anyarray ) json array_to_json(''::int) row_to_json(record ) json row_to_json(row(1,'foo')) to_json(anyelement) json to_json('Fred said "Hi."'::text) "Fred said \"Hi.\""
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