Search Postgresql Archives

Re: convert binary string to datum

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"Ron Peterson" <ron.peterson@xxxxxxxxxxxxxx> writes:

> Is this a legitimate/blessed way to go about it?
>
> aval = (bytea *)palloc( len + VARHDRSZ );
> VARATT_SIZEP(aval) = len + VARHDRSZ;
> memcpy( VARDATA(aval), myrawdata, len );
> values[0] = PointerGetDatum(aval);
> ...etc
> tuple = heap_formtuple( tupdesc, values, &isNull );

Yes, assuming that your tuple descriptor there does in fact have a varlena
data type in column 1. And normally you would define your own datatype and not
use bytea. Personally I'm not entirely clear why we don't just use void* for
text and bytea though.

Postgres 8.3 has a different macro api here though. If you want to
future-proof your code you could do (put the macro definition somewhere in
your private header file after including postgres.h).

#ifndef SET_VARSIZE
#define SET_VARSIZE(v,l) (VARATT_SIZEP(v) = (l))
#endif

aval = (bytea *)palloc( len + VARHDRSZ );
SET_VARSIZE(aval, len + VARHDRSZ);
memcpy( VARDATA(aval), myrawdata, len );
values[0] = PointerGetDatum(aval);
...etc
tuple = heap_formtuple( tupdesc, values, &isNull );

Also, make sure you use VARSIZE to refer to the size header at all times,
don't refer to it directly. And unless you mark it with storage plain always
detoast it before working with an argument or anything from heap_deform_tuple.
In postgres we normally put pg_detoast_datum() directly into the DatumGetFoo()
and PG_GETARG_FOO_P() macros.


-- 
  Gregory Stark
  EnterpriseDB          http://www.enterprisedb.com

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
       subscribe-nomail command to majordomo@xxxxxxxxxxxxxx so that your
       message can get through to the mailing list cleanly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux