Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jVGhy-0008OK-8X for pgsql-performance@arkaria.postgresql.org; Sun, 03 May 2020 15:39:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jVGhP-0004gl-Ks for pgsql-performance@arkaria.postgresql.org; Sun, 03 May 2020 15:38:35 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jVGhP-0004ge-AH for pgsql-performance@lists.postgresql.org; Sun, 03 May 2020 15:38:35 +0000 Received: from mail-qk1-x741.google.com ([2607:f8b0:4864:20::741]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jVGhL-00007B-HB for pgsql-performance@postgresql.org; Sun, 03 May 2020 15:38:33 +0000 Received: by mail-qk1-x741.google.com with SMTP id q7so14147814qkf.3 for ; Sun, 03 May 2020 08:38:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=/tQOuJaHkIoknbHrHjgJDUD8duhFB7+0DwcxkbN3WPQ=; b=USiHoRYecyF9tjSQn7lYn9M+DgV/XEgrLa9j91+VyLpiGozZb2teCHD5R/vrhbRz4R hrDNuSyn0AHMahCWYftGYzfANz4gnQ1VaYppwb93nCgMCj9wvzFK8pVn7XAzMEArvmR4 oyrtnm6VPVxsJpNjvEWHwX1EQOGFzkY4PHI0uPozJLl7lXcVoVaGe+YzhcoTS/pLMK6f 9LrSdJme/l6jNxKh+xc+yMAd5Ot6MBpaQFbjponpIWQV4weOVTuQYM5YcOVbqiW/yCj9 sp9AqeC04GuZ2LoXDmop/bgV5dLTYyL2wRhAY4hw3W72IejS7NI5L7qOIiiwU3IANvv7 VRSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=/tQOuJaHkIoknbHrHjgJDUD8duhFB7+0DwcxkbN3WPQ=; b=p8VAG/LuCL5e7jlthJp0dPu26wY9bPeXI+NwphHkGcCymXOZ2BWdstsNSutgoKQHH2 jIjnVnGlkCGfO1he5iCY6Hz5wHIDBiaKb826OemdLGV6YUr7z3aUZUOerdure24nCJGG v3zzW58kKp6DZX18jVpZv6dNEZlpOojB4migo6V8iK5/N8UMChkln1vr9he9Hp7nNBUE /IVaaAwHYwExlK7nhBGvN35v3sol5qTSVbPtTF78dszzcb4G5tiIsrFGA1JQE2+nO9r2 t9eeVTqcEQ7g5YvBbOgEjIiICB3rdpdzN/7014kYJl2xTBg3MG47DpLh/4EcaNE2Y2+k 3kAQ== X-Gm-Message-State: AGi0Pua+FuQXeXp9esj3L5OwcMCIe17mNJ5qbwMMwRbPQDu3t5op/gyY SJPw9cX8T8501kuTnCjtvlH51A== X-Google-Smtp-Source: APiQypJfrIZGTPGo9ua9rOeaz6PkPmFgxKg4k31sYCzZ+x8stqm1B8RQrQ3t7LpxhUOJrlxzupcUyw== X-Received: by 2002:a37:9d08:: with SMTP id g8mr12365954qke.138.1588520308604; Sun, 03 May 2020 08:38:28 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id t15sm8555078qtc.64.2020.05.03.08.38.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 03 May 2020 08:38:27 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id 2545A800A79; Sun, 3 May 2020 10:38:26 -0500 (CDT) Date: Sun, 3 May 2020 10:38:26 -0500 From: Justin Pryzby To: James Thompson Cc: pgsql-performance@postgresql.org Subject: Re: Please help! Query jumps from 1s -> 4m Message-ID: <20200503153825.GK28974@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sun, May 03, 2020 at 09:58:27AM +0100, James Thompson wrote: > Hi, > > Hoping someone can help with this performance issue that's been driving a > few of us crazy :-) Any guidance greatly appreciated. > > A description of what you are trying to achieve and what results you > expect.: > - I'd like to get an understanding of why the following query (presented > in full, but there are specific parts that are confusing me) starts off > taking ~second in duration but 'switches' to taking over 4 minutes. Does it "switch" abruptly or do you get progressively slower queries ? If it's abrupt following the 5th execution, I guess you're hitting this: https://www.postgresql.org/message-id/Pine.BSO.4.64.0802131404090.6785@leary.csoft.net https://www.postgresql.org/message-id/A737B7A37273E048B164557ADEF4A58B50FB8D5E@ntex2010i.host.magwien.gv.at > - we initially saw this behaviour for the exact same sql with a different > index that resulted in an index scan. To try and fix the issue we've > created an additional index with additional included fields so we now have > Index Only Scans, but are still seeing the same problem. > Segments of interest: > 1. -> Index Only Scan using table1_typea_include_uniqueid_col16_idx on > table1 table1alias1 (cost=0.56..17.25 rows=1 width=60) (actual > time=110.539..123828.134 rows=67000 loops=1) > Index Cond: (col20 = $2005) > Filter: (((col3 = $2004) OR (col3 IS NULL)) AND ((col8)::text = ANY > ((ARRAY[$1004, ..., $2003])::text[]))) > Rows Removed by Filter: 2662652 > Heap Fetches: 6940 > Buffers: shared hit=46619 read=42784 written=52 > If I run the same queries now: > Index Only Scan using table1_typea_include_uniqueid_col16_idx on table1 > table1alias1 (cost=0.56..2549.69 rows=69 width=36) > (actual time=1.017..1221.375 rows=67000 loops=1) > Heap Fetches: 24 > Buffers: shared hit=2849 read=2483 It looks to me like you're getting good performance following a vacuum, when Heap Fetches is low. So you'd want to run vacuum more often, like: | ALTER TABLE table1 SET (autovacuum_vacuum_scale_factor=0.005). But maybe I've missed something - you showed the bad query plan, but not the good one, and I wonder if they may be subtly different, and that's maybe masked by the replaced identifiers. -- Justin