Re: Fdw cleanup

From: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "'Feng Tian *EXTERN*'" <ftian(at)vitessedata(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fdw cleanup
Date: 2015-12-14 08:30:01
Message-ID: A737B7A37273E048B164557ADEF4A58B5377557A@ntex2010a.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Feng Tian wrote:
> I need some help to understand foreign table error handling.
>
> For a query on foreign table, ExecInitForeignScan is called, which in turn calls the BeginForeignScan
> hook. Inside this hook, I allocated some resource,
>
>
> node->fdw_state = allocate_resource(...);
>
> If everything goes well, ExecEndForeignScan will call call my EndForeignScan hook, inside the hook, I
> free the resource.
>
> free_resource(node->fdw_state);
>
> However, if during the execution an error happened, seems to me that EndForeignScan will not be called
> (traced using gdb). So my question is, is Begin/End the right place for allocate/free resources?
> If it is not, what is the right way to do this?

If the resource is memory, use "palloc" to allocate it, and PostgreSQL
will take care of it automatically.

Other than that, you could call RegisterXactCallback() and register a callback
that will be called upon transaction end. In the callback function you can
free the resource if it was not already freed by EndForeignScan.

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Kyotaro HORIGUCHI 2015-12-14 08:34:18 Re: [PoC] Asynchronous execution again (which is not parallel)
Previous Message Craig Ringer 2015-12-14 08:19:33 Re: [PATCH] Logical decoding support for sequence advances