Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Is there any document describing the tuple format that PostgreSQL server adheres to? The official documentation appears arcane about this.

A single tuple seems simple enough to figure out, but when it comes to arrays of tuples, arrays of composite tuples, and finally nested arrays of composite tuples, it is impossible to be certain about the format simply by looking at the output.

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 tuples within Node.js


Examples

create type type_A as (
   a int,
   b text
);
  • with a simple text: (1,hello)
  • with a complex text: (1,"hello world!")

create type type_B as (
   c type_A,
   d type_A[]
);
  • simple-value array: {"(2,two)","(3,three)"}

  • for type_B[] we can get:

{"(\"(7,inner)\",\"{\"\"(88,eight-1)\"\",\"\"(99,nine-2)\"\"}\")","(\"(77,inner)\",\"{\"\"(888,eight-3)\"\",\"\"(999,nine-4)\"\"}\")"}

It gets even more complex for multi-dimensional arrays of composite types.


UPDATE

Since it feels like there is no specification at all, I have started working on reversing it. Not sure if it can be done fully though, because from some initial examples it is often unclear what formatting rules are applied.

share|improve this question

This question has an open bounty worth +200 reputation from vitaly-t ending in 2 hours.

This question has not received enough attention.

1  
Asking out of curiosity - are you doing it because of the need or just for science? There are things like json and hstore in Postgres. – Kamil G. Sep 15 at 22:15
1  
@KamilG. this comes out of practical needs from the community of developers. I am also the author of pg-promise. So no, it is not academical. – vitaly-t Sep 15 at 22:18
1  
I don't quite unerstand. Do you want to know how tuples are stored on disk? Or how a tuple looks when output as string? Something else? – Laurenz Albe Sep 16 at 7:42
    
I'm asking about the tuple strings format that PostgreSQL outputs. – vitaly-t Sep 16 at 13:38
1  
Here are the relevant docs for composites and arrays, which seem to explain the quoting rules pretty thoroughly. There's nothing fancy going on when you combine these; nested elements are converted to strings, and then quoted / escaped like any other string (so for example, select row('{1,2}'::int[]) is indistinguishable from select row('{1,2}'::text)). – Nick Barnes Sep 19 at 13:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.