Need to check palloc() return value?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Need to check palloc() return value?
Date: 2005-02-16 03:13:55
Message-ID: 20050216031355.GA14421@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Do user-defined functions need to check palloc()'s return value,
or does return guarantee success? The latter appears to be the
case:

Datum
palloctest(PG_FUNCTION_ARGS)
{
int32 nbytes = PG_GETARG_INT32(0);
char *p;
ereport(INFO, (errmsg("calling palloc")));
p = palloc(nbytes);
ereport(INFO, (errmsg("palloc returned")));
PG_RETURN_INT32(nbytes);
}

SELECT palloctest(1000);
INFO: calling palloc
INFO: palloc returned
palloctest
------------
1000
(1 row)

\set VERBOSITY verbose
SELECT palloctest(1000000000);
INFO: 00000: calling palloc
LOCATION: palloctest, palloctest.c:34
ERROR: 53200: out of memory
DETAIL: Failed on request of size 1000000000.
LOCATION: AllocSetAlloc, aset.c:505

Control doesn't return to the user-defined function if the allocation
fails, so checking palloc()'s return value seems superfluous. Is it
safe to rely on this behavior?

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Greg Sabino Mullane 2005-02-16 03:51:12 Re: random record from small set
Previous Message Andrew Hall 2005-02-16 00:27:54 Re: Lost rows/data corruption?