title: PostgreSQL tuple format
body: <p>Is there any document describing the tuple format that PostgreSQL server adheres to? The official documentation appears arcane about this.</p>
  
  <p>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.</p>
  
  <p>I am asking this following my initial attempt at implementing <a href="https://github.com/vitaly-t/pg-tuple">pg-tuple</a>, a parser that's still missing today, to be able to parse PostgreSQL tuples within Node.js</p>
  
  <hr>
  
  <p><strong>Examples</strong></p>
  
  <pre><code>create type type_A as (
     a int,
     b text
  );
  </code></pre>
  
  <ul>
  <li>with a simple text: <code>(1,hello)</code></li>
  <li>with a complex text: <code>(1,"hello world!")</code></li>
  </ul>
  
  <hr>
  
  <pre><code>create type type_B as (
     c type_A,
     d type_A[]
  );
  </code></pre>
  
  <ul>
  <li><p>simple-value array: <code>{"(2,two)","(3,three)"}</code></p></li>
  <li><p>for <code>type_B[]</code> we can get:</p></li>
  </ul>
  
  <p><code>{"(\"(7,inner)\",\"{\"\"(88,eight-1)\"\",\"\"(99,nine-2)\"\"}\")","(\"(77,inner)\",\"{\"\"(888,eight-3)\"\",\"\"(999,nine-4)\"\"}\")"}</code></p>
  
  <p>It gets even more complex for multi-dimensional arrays of composite types.</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Since it feels like there is no specification at all, <a href="https://github.com/vitaly-t/pg-tuple/tree/master/spec">I have started working on reversing it</a>. Not sure if it can be done fully though, because <a href="https://github.com/vitaly-t/pg-tuple/blob/master/spec/playground.md">from some initial examples</a> it is often unclear what formatting rules are applied.</p>