PQfsize isn't returning the wrong value.
If you run the query below you will also get the answer 8, because internally in your version of Postgres timestamp is stored as an 8 byte (64 bit) integer.
select typlen from pg_type where oid = 'timestamp'::regtype::oid;
As per the documentation you linked PQfsize returns the space allocated for this field in a database tuple, in other words the size of the server's binary representation of the data type.
PQgetlength Returns the length of a field (attribute) in bytes. Tuple and field indices start at 0.
This is the actual data length for the particular data value, that is the size of the object pointed to by PQgetvalue. Note that for ASCII-represented values, this size has little to do with the binary size reported by PQfsize
.
In order to declare arrays that don't have fixed size, you the size of the array before creating it using PQgetlength
(which should return 19) and by specifying to get the data as text by having PQexecParams
having resultFormat
as 0. If you are just using PQexec
then data is returned in text form by default.
An example of using libpq is avaliable here.
To modify this example so that instead just printing the value, it makes a character array from the length of the data and copies the data to the array you could replace the for loop with something like
for (i = 0; i < PQntuples(res); i++)
{
int length = PQgetlength(res, i, 0);
char result[length];
printf("%s\n", PQgetvalue(res, i, 0));
strncpy (result,PQgetvalue(res, i, 0),length);
}
You would also need to add #include <string.h>
to the example.