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.

Parsing PG Tuples

See original GitHub issue

Has a way been explored to parse Postgres Tuples into JavaScript objects?

I have a query that must select a tuple (i.e. it can’t be flattened), say in the form of a composite type:

create type custom_type as (
  a int,
  b text
);

When you do something like:

select x::custom_type as x from y;

You get an object in pg that looks like:

{
  "x": "(2,'Hello, world!')"
}

Has a library been developed to parse this format? Is there a way to get this information in a better format from postgres? In my scenario I’ve queried pg_catalog for all the database type information, so I know the type OIDs of the tuple attributes.

Another option to explore is calling to_json or a similar function in the select statement, so:

select to_json(x::custom_type) as x from y;

…but if I do that I’m afraid that what I get back will be incompatible with what pg-types parses. Is there any guarantee that I’ll only get back, say strings, so I can run the pg-types parsers anyway on the returned JSON object?

Thanks!


In case I’m falling victim to the XY problem for a more complete picture I’m the author of PostGraphQL and I’m trying to add support for Postgres compound types in general.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
vitaly-tcommented, Sep 11, 2016

I had another look at it within pg-types. The library is definitely getting it as text. However, the type Id i’m getting is 73804, which is a very high number:

Field {
       name: 'data',
       tableID: 73807,
       columnID: 2,
       dataTypeID: 73804,
       dataTypeSize: -1,
       dataTypeModifier: -1,
       format: 'text' }

@brianc my guess is that high type Id-s represent custom types. It would be great if pg-types could support ranges of type id-s for parsers, so we could say something like typeId > 65535 => my custom type parser, or something like that.

If this is the case then it may be possible to integrate automatic parsing for tuples, although it wouldn’t be trivial, because the format itself isn’t.

Either way, you would need a proper parser for this. And I don’t know of any module that can do it today.

0reactions
FbNcommented, Apr 29, 2022

If someone land here a simple parser that auto load types from Postgres https://www.npmjs.com/package/pg-tuple-types

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python - Tuples - Tutorialspoint
A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists...
Read more >
PostgreSQL tuple format - Stack Overflow
I am asking this following my initial attempt at implementing pg-tuple, a parser that's still missing today, to be able to parse PostgreSQL ......
Read more >
Using tuples of items | Modern Python Cookbook
What's the best way to represent simple (x,y) and (r,g,b) groups of values? How can we keep things which are pairs such as...
Read more >
postgresql tuple as function argument - Google Groups
also my PG database doesn't know about the jsonb_object_agg function from sqlalchemy import * ... isn't parsing out the tuple. Using postgresql.
Read more >
Documentation: 15: 8.16. Composite Types - PostgreSQL
In fact, it's so much like selecting from a table name that you often have to use parentheses to keep from confusing the...
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