Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fYEov-0006Ii-OR for pgsql-performance@arkaria.postgresql.org; Wed, 27 Jun 2018 18:05:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fYEor-00029Y-Nb for pgsql-performance@arkaria.postgresql.org; Wed, 27 Jun 2018 18:05:29 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fYEor-00029K-88 for pgsql-performance@lists.postgresql.org; Wed, 27 Jun 2018 18:05:29 +0000 Received: from mail-it0-x242.google.com ([2607:f8b0:4001:c0b::242]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fYEoj-0003mJ-Jv for pgsql-performance@postgresql.org; Wed, 27 Jun 2018 18:05:27 +0000 Received: by mail-it0-x242.google.com with SMTP id v83-v6so8729545itc.3 for ; Wed, 27 Jun 2018 11:05:21 -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:content-transfer-encoding:in-reply-to :user-agent; bh=K7trTgdPj5FM6HQPIUYN/ivU1eqVsZxdnjWxGI1Mdq4=; b=dX1pE7HbQAaMtMUG1teLXW2ZzbHcDwcfjpxvN3ePBNUmWoYxCKChgMDjdas2namZtk b2iBIJINxEXcymAN/vLzLKVUKNHhllOGKQACDX9qMm8vSyqaRsZqC+6KEP+ZqbYKCFaY 5yLtARNOznjsVUK2pobttHKSmu1I8xIXBiRph/9TFqyfRnU2Xm15rU5pDUHkxaozDzUR 7yXECfEssfNl5A0sg4omzOtgdUu3oxo5guwZy4AFlVLGCp4e3hNwq02OF863Ghg27CPf mn0br0BaNKBXcPh9+16wCYxcugLCItwAHaix6hZz08uXxdVRV1rMUapcxjdNZt0N3xfN FT4Q== 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:content-transfer-encoding :in-reply-to:user-agent; bh=K7trTgdPj5FM6HQPIUYN/ivU1eqVsZxdnjWxGI1Mdq4=; b=cGynkc7lhHNgkU/xG/Jf7RZl1DeVybr+sB3JhXlETCn2PA3/faUJCC0qGC7MtVthv1 F8sVEUPM+PT3byMQv7HzGvtPoUIXliG3dGbs6FfTfE4qj+Jr1OWfBxNZuqogcYhZ3KUi vze96i7y47ilMKWGBIR+3Ym0rN12hUK3CB9JU/QYlY1zfvy6KIWCU0mcUWnZokqMHUkW cnIDa60quMAqZAbjoBhl0xCA2FVims8fLhrmylYd2oYMNNmBKbSM/uRvgyygyeFt80jr dmVWsIcU9yeFgKWuiTkdnMkx4OGJj0IF1IxHfeznusH/niXKdHqhNZiL9euH1XMs0CIw Gnyg== X-Gm-Message-State: APt69E216ZJYlGpyZ+EotRkcA1Qjgy5+ZpdIov4st9LJw5rsu4WYyG51 qJFhODTmz1jRHrjTt7Ztux7a2A== X-Google-Smtp-Source: AAOMgpfJ0mD+bBhGa2lt//nCwDEVOTygBLoCbX0g+3awSkg/veDYrQI7Bm7u9Q20wpH0A7LTRN+kJQ== X-Received: by 2002:a02:6543:: with SMTP id u64-v6mr5932790jab.71.1530122719991; Wed, 27 Jun 2018 11:05:19 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id b100-v6sm2675781itd.16.2018.06.27.11.05.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Jun 2018 11:05:19 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id BD99B80203D; Wed, 27 Jun 2018 13:05:18 -0500 (CDT) Date: Wed, 27 Jun 2018 13:05:18 -0500 From: Justin Pryzby To: David Wheeler Cc: "pgsql-performance@postgresql.org" Subject: Re: Queue table that quickly grows causes query planner to choose poor plan Message-ID: <20180627180518.GF3554@telsasoft.com> References: <64CC9527-C642-4AED-844B-70E7BC996A1B@dgitsystems.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <64CC9527-C642-4AED-844B-70E7BC996A1B@dgitsystems.com> User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, Jun 27, 2018 at 03:45:26AM +0000, David Wheeler wrote: > Hi All, > > I’m having performance trouble with a particular set of queries. It goes a bit like this > > 1) queue table is initially empty, and very narrow (1 bigint column) > 2) we insert ~30 million rows into queue table > 3) we do a join with queue table to delete from another table (delete from a using queue where a.id = queue.id), but postgres stats say that queue table is empty, so it uses a nested loop over all 30 million rows, taking forever If it's within a transaction, then autovacuum couldn't begin to help until it commits. (And if it's not, then it'll be slow on its own). It seems to me that you can't rely on autoanalyze to finish between commiting step 2 and beginning step 3. So you're left with options like: SET enable_nestloop=off; or manual ANALZYE (or I guess VACUUM would be adequate to set reltuples). Maybe you can conditionalize that: if inserted>9: ANALYZE queue. Justin