Re: text datum VARDATA and strings

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Reece Hart <reece(at)harts(dot)net>
Cc: pgsql-general(at)postgresql(dot)org, Michael Enke <michael(dot)enke(at)wincor-nixdorf(dot)com>
Subject: Re: text datum VARDATA and strings
Date: 2006-08-14 19:51:22
Message-ID: 13746.1155585082@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-general

Reece Hart <reece(at)harts(dot)net> writes:
> On Mon, 2006-08-14 at 11:27 -0400, Tom Lane wrote:
>> The usual way to get a C string from a TEXT datum is to call textout,
>> eg
>> str = DatumGetCString(DirectFunctionCall1(textout, datumval));

> Yikes! I've been accessing VARDATA text data like Michael for years
> (code below). I account for length and don't expect null-termination,
> but I don't use anything like Tom's suggestion above.

Sure, that works. The problem with what Michael was doing was that he
was passing the string to elog, which expects a null-terminated string.
Possibly I should have written "usual way to get a null-terminated string"
above, just to be clear.

> 1) I based everything I did on examples lifted nearly verbatim from a
> 7.x manual, and I bet Michael did similarly. I've never heard of
> DatumGetCString, DirectFunctionCall1, or textout. Are these and other
> treasures documented somewhere?

Whose 7.x manual? This stuff has been there since we invented the
"version 1" function call convention, which was 7.3 or before. There
is some documentation in the SGML docs, but really we kind of expect you
to look at the standard built-in functions to see how things are done...

> 2) Does DatumGetCString(DirectFunctionCall1(textout, datumval)) do
> something other than null terminate a string?

At the moment that's all it does ... assuming that you've already
detoasted the text datum ... but it's not impossible that someday
it will do something different. For instance, sooner or later we are
going to support multiple locales/encodings within a single database,
and I wouldn't be surprised if that involves sticking extra data into
text values. So it's best not to assume that you know what is inside a
text datum, if possible,

> 3) Is there any reason to believe that the code below is problematic?

The only thing I'd suggest is that checking for a null return from
palloc is a waste of effort. It doesn't return to you if it runs
out of memory.

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Reece Hart 2006-08-14 22:11:49 Re: text datum VARDATA and strings
Previous Message Reece Hart 2006-08-14 18:04:30 text datum VARDATA and strings

Browse pgsql-general by date

  From Date Subject
Next Message Berend Tober 2006-08-14 20:08:07 Re: Best approach for a "gap-less" sequence
Previous Message Jeff Davis 2006-08-14 19:31:02 select updates pg_stat_database