Re: interaction with postgres defined types in custom c functions

Поиск
Список
Период
Сортировка
От Gregory Stark
Тема Re: interaction with postgres defined types in custom c functions
Дата
Msg-id 87bqebcp6x.fsf@oxford.xeocode.com
обсуждение исходный текст
Ответ на interaction with postgres defined types in custom c functions  ("Jason Nerothin" <jasonnerothin@gmail.com>)
Список pgsql-general
"Jason Nerothin" <jasonnerothin@gmail.com> writes:

> PG_FUNCTION_INFO_V1(par);
> Datum par(PG_FUNCTION_ARGS) // pete and repeat! takes text struct pointer
> {
>   text *t = PG_GETARG_TEXT_P(0);
>   text *new_t = (text*) palloc(VARSIZE(t));
>   VARATT_SIZEP(new_t) = VARSIZE(t);
>   memcpy((void*) VARDATA(new_t),(void*)VARDATA(t),VARSIZE(t) - VARHDRSZ);
>   char *ptr = (char*) VARDATA(new_t);
>   *ptr = 'Q'; // NO EFFECT
>   PG_RETURN_TEXT(new_t);
> }

That looks correct to me (well aside from it being an error to define ptr in
the middle of a block in C and replacing the first character with Q will be
wrong if the string is 0-bytes or the first byte is a multibyte character).

> The "no effect" comment applies to trying to rewrite to the same memory,
> write from some static type I define, etc, etc.

I think you've simplified away the mistake in your code. Have you actually
tried the above code? Send an actual example and describe what actually
happens.

The only thing I wonder about is whether you're compiling with optimizations
and without -fno-strict-aliasing which may be necessary for Postgres's code
style to compile correctly. What compiler are you compiling with?

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


В списке pgsql-general по дате отправления:

Предыдущее
От: "Pavel Stehule"
Дата:
Сообщение: Re: ERROR: a column definition list is required for functions returning "record"
Следующее
От: Tim Tassonis
Дата:
Сообщение: Re: The speed problem of Varchar vs. Char