Re: update inside function does not use the index

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: Johannes <jotpe(at)posteo(dot)de>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: update inside function does not use the index
Date: 2015-11-16 17:19:30
Message-ID: 17585.1447694370@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 11/16/2015 08:03 AM, Johannes wrote:
>>> In every loop I execute an update with a where LIKE condition, which
>>> relates to my current cursor position:
>>> FOR i IN SELECT id, level_ids, path_names||'%' as path_names from x LOOP
>>> update x set path_ids[i.level] = id where path_names like i.path_names;

Probably the problem is that the planner is unable to fold i.path_names
to a constant, so it can't derive an indexscan condition from the LIKE
clause.

A little bit of experimentation says that that will work if "i" is
declared with a named rowtype, but not if it's declared RECORD. This
might or might not be something we could fix, but in the meantime I'd
try

DECLARE i x%rowtype;

FOR i IN SELECT * FROM x LOOP
update x set path_ids[i.level] = id where path_names like (i.path_names || '%');

which while it might look less "constant" is actually more so from the
planner's perspective, because there is no question of whether "i" has
got a field of that name.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message S McGraw 2015-11-16 17:25:28 Re: Importing directly from BCP files
Previous Message Adrian Klaver 2015-11-16 16:57:38 Re: update inside function does not use the index