Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 33F661AEBA99 for ; Tue, 24 Jan 2012 17:34:23 -0400 (AST) Received: from mail.prospectiv.com ([155.212.59.212] helo=stan.prospectivdirect.com) by makus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Rpo0R-0002Qt-Ke for pgsql-performance@postgresql.org; Tue, 24 Jan 2012 21:34:23 +0000 Received: (qmail 17422 invoked from network); 24 Jan 2012 21:34:06 -0000 Received: from unknown (HELO ?192.168.1.15?) (192.168.1.15) by 0 with SMTP; 24 Jan 2012 21:34:06 -0000 Subject: Re: Cursor fetch performance issue From: Tony Capobianco To: Pavel Stehule Cc: Andy Colson , pgsql-performance@postgresql.org In-Reply-To: References: <1327437700.1968.10.camel@tony1.localdomain> <1327438657.1968.15.camel@tony1.localdomain> <4F1F1E7A.4080403@squeakycode.net> Content-Type: text/plain; charset="UTF-8" Date: Tue, 24 Jan 2012 16:34:06 -0500 Message-ID: <1327440846.1968.20.camel@tony1.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.32.3 (2.32.3-1.fc14) Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/68 X-Sequence-Number: 46080 Here's the explain: pg=# explain select getMemberAdminPrevious_sp(247815829, 1,'test.email@hotmail.com', 'Email', 'Test'); QUERY PLAN ------------------------------------------ Result (cost=0.00..0.26 rows=1 width=0) (1 row) Time: 1.167 ms There was discussion of 'LIKE' v. '=' and wildcard characters are not being entered into the $1 parameter. This is not generating a sql string. I feel it's something to do with the fetch of the refcursor. The cursor is a larger part of a function: CREATE OR REPLACE FUNCTION PUBLIC.GETMEMBERADMINPREVIOUS_SP2 ( p_memberid IN numeric, p_websiteid IN numeric, p_emailaddress IN varchar, p_firstname IN varchar, p_lastname IN varchar) RETURNS refcursor AS $$ DECLARE ref refcursor; l_sysdateid numeric; BEGIN l_sysdateid := sysdateid(); if (p_memberid != 0) then if (p_emailaddress IS NOT NULL) then OPEN ref FOR SELECT m.memberid, m.websiteid, m.emailaddress, m.firstname, m.lastname, m.regcomplete, m.emailok FROM members m WHERE m.emailaddress LIKE p_emailaddress AND m.changedate_id < l_sysdateid ORDER BY m.emailaddress, m.websiteid; end if; end if; Return ref; EXCEPTION WHEN NO_DATA_FOUND THEN Return null; END; $$ LANGUAGE 'plpgsql'; On Tue, 2012-01-24 at 22:17 +0100, Pavel Stehule wrote: > Hello > > > > > So, is getMemberAdminPrevious_sp2() preparing a statement with wildcards? > > > > SELECT m.memberid, m.websiteid, m.emailaddress, > > m.firstname, m.lastname, m.regcomplete, m.emailok > > FROM members m > > WHERE m.emailaddress LIKE $1 > > AND m.changedate_id < $2 > > ORDER BY m.emailaddress, m.websiteid; > > > > Or is it creating the string and executing it: > > > > sql = 'SELECT m.memberid, m.websiteid, m.emailaddress, ' > > || ' m.firstname, m.lastname, m.regcomplete, m.emailok ' > > || ' FROM members m > > || ' WHERE m.emailaddress LIKE ' || arg1 > > || ' AND m.changedate_id < ' || arg2 > > || ' ORDER BY m.emailaddress, m.websiteid '; > > execute(sql); > > > > Maybe its the planner doesnt plan so well with $1 arguments vs actual > > arguments thing. > > > > sure, it could be blind optimization problem in plpgsql. Maybe you > have to use a dynamic SQL - OPEN FOR EXECUTE stmt probably > > http://www.postgresql.org/docs/9.1/interactive/plpgsql-cursors.html > > Regards > > Pavel Stehule > > > -Andy > > > > >