Question about error handling with UDF written in C

From: Terry Chong <198back(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Question about error handling with UDF written in C
Date: 2013-03-19 19:57:36
Message-ID: CAPx7QHGggiA6rEMO8VpJyF3wooxD-o21JOmUQ80A76NWZ7tTbg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hello, I am trying to create a UDF in C that returns multiple rows. I
followed the examples in the contrib directory which does the following:

1 /* create a function context for cross-call persistence */
2 funcctx = SRF_FIRSTCALL_INIT();
3
4 /* switch to memory context appropriate for multiple function calls
*/
5 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
6
7 /* Prepare the inter call array for returning the data */
8 someVariable = palloc(size);
9
10 /* total number of tuples to be returned */
11 funcctx->max_calls = NUM_OUTPUT_VARIABLES;
12 funcctx->user_fctx = someVariable;
13
14 /* Build a tuple descriptor for our result type */
15 if (get_call_result_type(fcinfo, NULL, &tupdesc) !=
TYPEFUNC_COMPOSITE)
16 ereport(ERROR,
17 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
18 errmsg("function returning record called in context "
19 "that cannot accept type record")));

My question is that if we detect an error at line 15 and we get into the
ereport code, would the memory allocated under multi_call_memory_ctx gets
cleaned up properly? The document only states that the memory allocated
under multi_call_memory_ctx will be cleaned up by SRF_RETURN_DONE, but what
about error situations such as the above?

Thanks

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2013-03-19 20:05:01 Re: Question about error handling with UDF written in C
Previous Message Tom Lane 2013-03-19 13:57:47 Re: Roles and passwds