Re: Can we modify 'text *' passed to a C function?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: m w <mttf2000(at)yahoo(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Can we modify 'text *' passed to a C function?
Date: 2001-02-06 01:02:04
Message-ID: 18988.981421324@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

m w <mttf2000(at)yahoo(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Panon, Paul-Andre 2001-02-06 01:04:53 ADD CONSTRAINT ... FOREIGN KEY and custom data type.
Previous Message Nic Ferrier 2001-02-06 00:58:07 Re: [HACKERS] Re: syslog logging setup broken?