Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i0rWO-0000jU-Ec for pgsql-performance@arkaria.postgresql.org; Thu, 22 Aug 2019 18:09:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i0rWN-0003qs-1J for pgsql-performance@arkaria.postgresql.org; Thu, 22 Aug 2019 18:09:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i0rWM-0003ql-Oy for pgsql-performance@lists.postgresql.org; Thu, 22 Aug 2019 18:09:14 +0000 Received: from mail-io1-xd43.google.com ([2607:f8b0:4864:20::d43]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i0rWG-0005vt-4s for pgsql-performance@lists.postgresql.org; Thu, 22 Aug 2019 18:09:14 +0000 Received: by mail-io1-xd43.google.com with SMTP id j5so13763085ioj.8 for ; Thu, 22 Aug 2019 11:09:07 -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=+A2T1U5c1DiywAdHXMgSDhtjyMiyJ1OpwkzUP75cE48=; b=cHYae2rKyl3PeC4MHSOitjwhYH1cC1t4J4jRU+SUFpJ2a7MhpU/OrKbLeC5EyrWIoA L3Zx7f51z5eIiOCj86xPhJDS9f73uEBAg1HBYRYZ7tR90FW98BtZdgYsH7cfkMZJKgY7 L7DBFSOtGagn7DKK58dCLvdL5R0kn6J67LkLTKJ5cpLN6+uAd20FJpoBId5lCOPlycWB fF+htGiR2EEZAUOcoyca/rtEoK4yDRdON2Vu4pBGIf+0kB/ktdQdsPDB3+Y0URZ8lIf+ SsVfnd5vi+RKd8V5lqR1NxYiLfChA5nOH1N1z2o0HYcnN2hstxN3HcfubfxnzGSzvq8q T/hQ== 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=+A2T1U5c1DiywAdHXMgSDhtjyMiyJ1OpwkzUP75cE48=; b=BvG9TTjtQOM5YznVMpU+wE2wnbBu3K2kP+ws6dQd1nzi2pKidtIhEOdjUi5v3YmvcJ fP2oQBcF/3PV+S5RoN9G1FYTdhrrL9djDHJ3Mwtqok+unuJgHvTON2g/ENYGgxc/JErj k+6i3R+t3Z1+oVh6AZtq+W2yzbJy4I5PApvzkWxoq6ttMaE0tzLGrJjKj9tJoj5BpjtW bdvPmu8KMV2TPkhIgzShAAhignpsOsjWEKyDzYobGMRw+a6/2sigMf3FIgVypUdSmBdb v6gygahTJcxjIcaH9QDD00CwkEgUYbIuB4fME1y0RYuncktqvmmvjcaDPUuyLKeQbIUM 7trg== X-Gm-Message-State: APjAAAWxxib6zAawkL944RixPKgfw9UTVl8i5XosD8DUBAdJdrhtiPwo 4RZdKPZpX4UuKlYk0Xlh0WIo7LjzqZY= X-Google-Smtp-Source: APXvYqwdGg1UyntaXJ9ldZbeDWyoKGAeQiBY1kLbUQt0il9h5HXNYGi3gOQXx7eRaIQfgakZdLOTFA== X-Received: by 2002:a6b:3b57:: with SMTP id i84mr1198792ioa.145.1566497043486; Thu, 22 Aug 2019 11:04:03 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id p3sm183322iom.7.2019.08.22.11.04.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Aug 2019 11:04:01 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id F31F9801537; Thu, 22 Aug 2019 13:04:00 -0500 (CDT) Date: Thu, 22 Aug 2019 13:04:00 -0500 From: Justin Pryzby To: Marco Colli Cc: pgsql-performance@lists.postgresql.org Subject: Re: Extremely slow count (simple query, with index) Message-ID: <20190822180400.GI15332@telsasoft.com> References: <20190822131910.GC15332@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190822131910.GC15332@telsasoft.com> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Aug 22, 2019 at 07:54:57PM +0200, Marco Colli wrote: > I have completely solved (from 17s to 1s) by running this command: > vacuum analyze subscriptions; Thanks for following though. On Thu, Aug 22, 2019 at 08:19:10AM -0500, Justin Pryzby wrote: > You can see it used the same index in both cases, and the index scan was > reasonably fast (compared to your goal), but the heap component was slow. > > I suggest to run VACUUM FREEZE on the table, to try to encourage index only > scan. If that works, you should condider setting aggressive autovacuum I should've used a better word, since aggressive means something specific. Perhaps just: "parameter to encourage more frequent autovacuums". > parameter, at least for the table: > ALTER TABLE subscriptions SET (AUTOVACUUM_VACUUM_SCALE_FACTOR=0.005); > -- And possibly lower value of autovacuum_freeze_max_age > > Or, running manual vacuum possibly during quiet hours (possibly setting > vacuum_freeze_table_age to encourage aggressive vacuum). I think my reference to autovacuum_freeze_max_age and vacuum_freeze_table_age were incorrect; what's important is "relallvisible" and not "relfrozenxid". And xid wraparound isn't at issue here. > > Even an approximate count would be enough. > > You can SELECT reltuples FROM pg_class WHERE oid='subscriptions'::oid, but its Should be: oid='subscriptions'::regclass > accuracy depends on frequency of vacuum (and if a large delete/insert happened > since the most recent vacuum/analyze). Justin