Re: PL/Python prepare example's use of setdefault

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, Jonathan Rogers <jrogers(at)socialserve(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: PL/Python prepare example's use of setdefault
Date: 2014-10-15 21:56:59
Message-ID: 22234.1413410219@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com> writes:
> On 10/15/2014 02:39 PM, Jonathan Rogers wrote:
>> I was just reading the PL/Python docs section "42.7.1 Database Access
>> Functions" and saw this example:
>>
>> CREATE FUNCTION usesavedplan() RETURNS trigger AS $$
>> plan = SD.setdefault("plan", plpy.prepare("SELECT 1"))
>> # rest of function
>> $$ LANGUAGE plpythonu;
>>
>> The above example uses the plpy.prepare() function, reusing the result
>> across function calls uses setdefault(). Unfortunately, since
>> setdefault() is a method on dict objects, the values passed to it must
>> be evaluated before it can be called. Therefore, plpy.prepare() will be
>> called every time usesavedplan() executes whether a result already
>> exists in the SD dict or not.
>>
>> Can anyone clarify what occurs when plpy.prepare() is called? Is it
>> worth using a Python conditional to determine whether to call it rather
>> than using SD.setdefault()?

> Like in the older documentation?:

Hm ... this was changed in commit 6f6b46c9c0ca3d96. Peter, did
you consider efficiency here?

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jonathan Rogers 2014-10-15 21:58:37 Re: PL/Python prepare example's use of setdefault
Previous Message Adrian Klaver 2014-10-15 21:51:06 Re: PL/Python prepare example's use of setdefault