title: PostgreSQL's libpq: Encoding for binary transport of ARRAY[]-data?
body: <p>after hours of documentations/boards/mailinglists and no progress I may ask you: How do I 'encode' my data to use it for binary transport using libpq's <code>PQexecParams(.)</code> ?</p>
  
  <p>Simple variables are just in big endian order:</p>
  
  <pre><code>PGconn *conn;
  PGresult *res;
  char *paramValues[1];
  int paramLengths[1];
  int paramFormats[1];
  
  conn = PQconnectdb(CONNINFO);
  
  // -- (1) -- send a float value
  float val_f = 0.12345678901234567890; // float precision: ~7 decimal digits
  // alloc some memory &amp; write float (in big endian) into
  paramValues[0] = (char *) malloc(sizeof(val_f));
  *((uint32_t*) paramValues[0]) = htobe32(*((uint32_t*) &amp;val_f)); // host to big endian
  
  paramLengths[0] = sizeof(val_f);
  paramFormats[0] = 1; // binary
  
  res = PQexecParams(conn, "SELECT $1::real ;", //
          1, // number parameters
          NULL, // let the backend deduce param type
          paramValues, //
          paramLengths, //
          paramFormats, //
          0); // return text
  printf("sent float: %s \n", PQgetvalue(res, 0, 0));
  // --&gt; sent float: 0.123457
  </code></pre>
  
  <p>and like this also double, int, etc ...</p>
  
  <p>But how about ARRAYs?</p>
  
  <pre><code>    float vals_f[] = {1.23, 9.87};
      // alloc some memory
      paramValues[0] = (char *) malloc(sizeof(float) * 2);
  
  //  ???? paramValues[0] = ??????
  
      paramLengths[0] = sizeof(float) * 2;
      paramFormats[0] = 1; // binary
  
  
      res = PQexecParams(conn, "SELECT $1::real[] ;", //
              1, // number parameters
              NULL, // let the backend deduce param type
              paramValues, //
              paramLengths, //
              paramFormats, //
              0); // return text
      printf("sent float array: %s \n", PQgetvalue(res, 0, 0));
  </code></pre>
  
  <p>Is there any working example of transfering ARRAY data in PostgreSQL's binary format?
  The code in <code>backend/utils/adt/</code> doesn't help me much (except I now know there is a ARRAYTYPE, but not how to use them) :-(</p>
  
  <p>I just need a function <code>char* to_PQbin(float [] input, int length)</code> for passing to <code>paramValues[.]</code> ... </p>
  
  <p>Thanks a lot,
  Tebas</p>
  
  <p>PS: What is the suggested way of converting simple variables (rather than my <code>htobe32(.)</code>)?</p>