Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you dig through the headers you'll find this in <code>server/c.h</code>:</p> <pre><code>/* ---------------- * Variable-length datatypes all share the 'struct varlena' header. *... */ struct varlena { char vl_len_[4]; /* Do not touch this field directly! */ char vl_dat[1]; }; #define VARHDRSZ ((int32) sizeof(int32)) /* * These widely-used datatypes are just a varlena header and the data bytes. * There is no terminating null or anything like that --- the data length is * always VARSIZE(ptr) - VARHDRSZ. */ typedef struct varlena bytea; typedef struct varlena text; </code></pre> <p>so there's your definition of the <code>text</code> data type. Note this rather important part of the comment:</p> <blockquote> <p>There is no terminating null or anything like that</p> </blockquote> <p>That indicates that you absolutely do not want to treat <code>fileName-&gt;data</code> as a C string unless you like segfaults. You need a way to convert a <code>text</code> to a nul-terminated C string that you can hand to <code>stat</code>; there is a function for that: <code>text_to_cstring</code>.</p> <p>The only documentation for <code>text_to_cstring</code> that I can find is <a href="http://doxygen.postgresql.org/varlena_8c_source.html#l00121" rel="nofollow noreferrer">this comment in the source</a>:</p> <pre><code>/* * text_to_cstring * * Create a palloc'd, null-terminated C string from a text value. * * We support being passed a compressed or toasted text value. * This is a bit bogus since such values shouldn't really be referred to as * "text *", but it seems useful for robustness. If we didn't handle that * case here, we'd need another routine that did, anyway. */ </code></pre> <p>There is also <a href="http://www.postgresql.org/docs/current/static/spi-examples.html" rel="nofollow noreferrer">an example that uses it</a>:</p> <pre><code>char *command; /*...*/ /* Convert given text object to a C string */ command = text_to_cstring(sql); /*...*/ pfree(command); </code></pre> <p>You should be able to do something like this:</p> <pre><code>struct stat buf; char *fileName = text_to_cstring(PG_GETARG_TEXT_P(0)); int i = stat(fileName, &amp;buf); pfree(fileName); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload