If I write a function that does a 'tolower()' on each
character of a parameter passed into my function, do I
have to allocate a new text object and initialize it
and copy the data, or can I modify that object in
place and return it?

Modifying it in place will save the overhead of an
alloc call, and on a large database, that could add
up.

right now, I am allocating a new data buffer of
exactly the same size and it seems like a waste.

Secondly, if I modify the data length of an object,
as: VARDATA(field) = nn, making sure that 'nn' is
smaller than the origial length, with this affect the
memory management?

In short, can we modify text * objects passed to a C
function, and can we shrink the length field without
any ill effects?

(Doing so will reduce quite a bit of memory allocation
overhead I am doing.)


__________________________________________________
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Mon, 5 Feb 2001 12:13:56 -0800 (PST)
m w <mttf2000@yahoo.com>
Can we modify 'text *' passed to a C function?
m w <mttf2000@yahoo.com> writes:
> If I write a function that does a 'tolower()' on each
> character of a parameter passed into my function, do I
> have to allocate a new text object and initialize it
> and copy the data, or can I modify that object in
> place and return it?

You must NEVER alter a pass-by-reference parameter, of text or
any other data type.  If you do, you are probably altering data
in a disk buffer, thus changing the content of a tuple.

> Secondly, if I modify the data length of an object,
> as: VARDATA(field) = nn, making sure that 'nn' is
> smaller than the origial length, with this affect the
> memory management?

In a palloc'd temp object it's OK if the allocated size is more than
what you end up using, so yes you can set VARSIZE() to less than what
you allocated.  Some of the built-in functions do things this way to
simplify calculation of the palloc request size, IIRC.  Again, you
cannot scribble on a source object.

In 7.1, for toastable data types there are GETARG macros that guarantee
to return a writable copy, see PG_GETARG_TEXT_P_COPY etc.  I believe
tolower() and similar functions use this already.

			regards, tom lane

Mon, 05 Feb 2001 20:02:04 -0500
Tom Lane <tgl@sss.pgh.pa.us>
Re: Can we modify 'text *' passed to a C function? 
--- Tom Lane <tgl@sss.pgh.pa.us> wrote:
> m w <mttf2000@yahoo.com> writes:
> > If I write a function that does a 'tolower()' on
> each
> > character of a parameter passed into my function,
> do I
> > have to allocate a new text object and initialize
> it
> > and copy the data, or can I modify that object in
> > place and return it?
> 
> You must NEVER alter a pass-by-reference parameter,
> of text or
> any other data type.  If you do, you are probably
> altering data
> in a disk buffer, thus changing the content of a
> tuple.

Thanks, I was concerned that may be the case, but I
could never find a definitive answer on this.

__________________________________________________
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Mon, 5 Feb 2001 17:54:21 -0800 (PST)
m w <mttf2000@yahoo.com>
Re: Can we modify 'text *' passed to a C function? 