Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wapww-0000VY-8V for pgsql-performance@arkaria.postgresql.org; Thu, 17 Apr 2014 17:18:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Wapwv-0002F0-8p for pgsql-performance@arkaria.postgresql.org; Thu, 17 Apr 2014 17:18:09 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wapwt-0002Et-V5 for pgsql-performance@postgresql.org; Thu, 17 Apr 2014 17:18:08 +0000 Received: from smtp08.smtpout.orange.fr ([80.12.242.130] helo=smtp.smtpout.orange.fr) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wapwl-0000Yf-W5 for pgsql-performance@postgresql.org; Thu, 17 Apr 2014 17:18:07 +0000 Received: from axege.com ([82.127.232.162]) by mwinf5d31 with ME id r5Hx1n00D3WtTCJ035Hxg8; Thu, 17 Apr 2014 19:17:58 +0200 X-ME-Helo: axege.com X-ME-Date: Thu, 17 Apr 2014 19:17:58 +0200 X-ME-IP: 82.127.232.162 Received: from localhost (localhost [127.0.0.1]) by axege.com (Postfix) with ESMTP id 3D8A4500FA for ; Thu, 17 Apr 2014 19:17:57 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at myhostname="axege.com." Received: from axege.com ([127.0.0.1]) by localhost (axege.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Sm1OYz4Sa9WE for ; Thu, 17 Apr 2014 19:17:48 +0200 (CEST) Received: from [10.0.0.75] (unknown [10.0.0.75]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by axege.com (Postfix) with ESMTPSA id 653E64FF10 for ; Thu, 17 Apr 2014 19:17:48 +0200 (CEST) Message-ID: <53500CBC.9030002@axege.com> Date: Thu, 17 Apr 2014 19:17:48 +0200 From: Franck Routier User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Fast distinct not working as expected References: <534FEF1D.5010706@axege.com> In-Reply-To: Content-Type: multipart/alternative; boundary="------------080403070304010300020406" X-Pg-Spam-Score: -1.1 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org This is a multi-part message in MIME format. --------------080403070304010300020406 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi, > > That is not equivalent to a distinct. There must be more to it than that. Indeed, this query is used in a loop: CREATE OR REPLACE FUNCTION small_distinct(IN tablename character varying, IN fieldname character varying, IN sample anyelement DEFAULT ''::character varying) RETURNS SETOF anyelement AS $BODY$ BEGIN EXECUTE 'SELECT '||fieldName||' FROM '||tableName||' ORDER BY '||fieldName ||' LIMIT 1' INTO result; WHILE result IS NOT NULL LOOP RETURN NEXT; EXECUTE 'SELECT '||fieldName||' FROM '||tableName ||' WHERE '||fieldName||' > $1 ORDER BY ' || fieldName || ' LIMIT 1' INTO result USING result; END LOOP; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100 ROWS 1000; Since we have the problem, some iteration of the query are still quick (< 1ms), but others are long (> 5s). > > > > > > > My best guess would be that the index got stuffed full of entries for > rows that are not visible, either because they are not yet committed, > or have been deleted but are not yet vacuumable. Do you have any > long-lived transactions? There has been a delete on the table (about 20% of the records). Then a manual VACUUM. We have recreated the index, but it did not help. In the explain analyze output, the index scan begins at 5798.912. What can be happening before that ? Index Scan using vsn_idx on dwhinv (cost=0.00..302591122.05 rows=267473826 width=12) (actual time=5798.912..5798.912 rows=1 loops=1) (Notice the delay is not planning itself, as explain is instantaneous) > > > > - postgresql Version 8.4 > > > Newer versions have better diagnostic tools. An explain (analyze, > buffers) would be nice, especially with track_io_timing on. Yep, we certainly would like to, but this is a distant prod box, with no access to an online upgrade source, and no planned upgrade for now :-(( Regards, Franck --------------080403070304010300020406 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit Hi,

That is not equivalent to a distinct.  There must be more to it than that.
Indeed, this query is used in a loop:

CREATE OR REPLACE FUNCTION small_distinct(IN tablename character varying, IN fieldname character varying, IN sample anyelement DEFAULT ''::character varying)
  RETURNS SETOF anyelement AS
$BODY$
BEGIN
   EXECUTE 'SELECT '||fieldName||' FROM '||tableName||' ORDER BY '||fieldName
      ||' LIMIT 1'  INTO result;
   WHILE result IS NOT NULL LOOP
      RETURN NEXT;
      EXECUTE 'SELECT '||fieldName||' FROM '||tableName
         ||' WHERE '||fieldName||' > $1 ORDER BY ' || fieldName || ' LIMIT 1'
         INTO result USING result;
   END LOOP;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
  ROWS 1000;


Since we have the problem, some iteration of the query are still quick (< 1ms), but others are long (> 5s).
 




My best guess would be that the index got stuffed full of entries for rows that are not visible, either because they are not yet committed, or have been deleted but are not yet vacuumable.  Do you have any long-lived transactions?
There has been a delete on the table (about 20% of the records). Then a manual VACUUM.
We have recreated the index, but it did not help.

In the explain analyze output, the index scan begins at 5798.912. What can be happening before that ?
Index Scan using vsn_idx on dwhinv  (cost=0.00..302591122.05
rows=267473826 width=12) (actual time=5798.912..5798.912 rows=1 loops=1)
(Notice the delay is not planning itself, as explain is instantaneous)

 

- postgresql Version 8.4

Newer versions have better diagnostic tools.  An explain (analyze, buffers)  would be nice, especially with track_io_timing on.
Yep, we certainly would like to, but this is a distant prod box, with no access to an online upgrade source, and no planned upgrade for now :-((

Regards,
Franck
--------------080403070304010300020406--