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 1nx9EC-0002ut-FG for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Jun 2022 15:28:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nx9EB-0001Pn-4Z for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Jun 2022 15:28:43 +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 1nx9EA-0001Pe-MC for pgsql-hackers@lists.postgresql.org; Fri, 03 Jun 2022 15:28:42 +0000 Received: from mail-il1-x12c.google.com ([2607:f8b0:4864:20::12c]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nx9E7-0000QN-FO for pgsql-hackers@postgresql.org; Fri, 03 Jun 2022 15:28:41 +0000 Received: by mail-il1-x12c.google.com with SMTP id h18so7121058ilj.7 for ; Fri, 03 Jun 2022 08:28:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=FzGu3bVeszgpYuGNwY2X6jwRE5lUui6HG8XwGs4Z6Gc=; b=qeeIUwclz3IIkkZMDkYnE97t4UOoJ/8fJ6X1guL0PWcu4FCbcZrsvhoC9Jijs3e6en SivTrHwwezikbNmo/MlXnALurnrrYqurDB5w4hSGKXJ+8kDBncKw2XN4AJMUZULYB9sw VeKoSlxy/weO8dG7mZS87Jq2JVjrUIeB/vuGT8cpw/bMP5gnDtHuRJZXs68N6KvafOf2 DEXDTKqmU3LRTbZ1A0FFLPYW24hmEiRIta6wZqx01XtCkxk5xp5YRXG0RHRzXdIYqLq5 82WskAD1bbI2U1wnEjDQqidti9nvTn56qmA8mvipS48wdlBqU01lqdRuPqTvOCnb/5Q3 4HtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=FzGu3bVeszgpYuGNwY2X6jwRE5lUui6HG8XwGs4Z6Gc=; b=dCuehgBeJLxTbw7ZPaIRaXHbbdtWN2tAu3g8I4j4qR+NkBbsh+rD9EumNi/fUssXtE GTjrFnw0Mu9/xw/Vxv9Dj7taOI+Ff1N83ITXb5J7sY2zMqiK53EtX7t3r8W+rpz7jy6K +32sP80nbAcZ673BOtqnM5n7EG45ahpfkOqMQExeHDA8hmTE2fw4Im3dJP9eS7BqDuSu RBpWtQ74GcD8aji2dytNQpTMfEnjnrOZP9gjnTpMBnAVFgwDoqExLtKSL+xX8uMIZ9ze pkp8qufhupDmH0mslURF2E5Hf707NtAdneR4mPkiw1EAkbShkcsNFu7I8e9T43vMvmkP CFkg== X-Gm-Message-State: AOAM531xE2iMrpuxhGSbTgEm89WhuyFkhXk8toC28UXc2/wKYhE2su/f RSHfi9wQqSgw1F3Y22ksSFZEdQ== X-Google-Smtp-Source: ABdhPJx7cfWVkr6FpPNEdOiYGDSyVsFHoMiVi/M7t5Zw90yjH5DKoz4xxJMDC0eujJrQ90NVWw57DA== X-Received: by 2002:a02:605e:0:b0:331:5cb4:7db0 with SMTP id d30-20020a02605e000000b003315cb47db0mr6234934jaf.204.1654270118733; Fri, 03 Jun 2022 08:28:38 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id f5-20020a022405000000b0032b3a7817bdsm2621335jaa.129.2022.06.03.08.28.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Jun 2022 08:28:38 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 86B4E8009F4; Fri, 3 Jun 2022 10:28:37 -0500 (CDT) Date: Fri, 3 Jun 2022 10:28:37 -0500 From: Justin Pryzby To: Robert Haas Cc: Tom Lane , Michael Paquier , Tomas Vondra , pgsql-hackers@postgresql.org Subject: Re: should check interrupts in BuildRelationExtStatistics ? Message-ID: <20220603152837.GO29853@telsasoft.com> References: <20220509000108.GQ28830@telsasoft.com> <3592964.1652067393@sss.pgh.pa.us> 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: Archived-At: Precedence: bulk On Mon, May 09, 2022 at 09:11:37AM -0400, Robert Haas wrote: > On Sun, May 8, 2022 at 11:36 PM Tom Lane wrote: > > Michael Paquier writes: > > > How long can the backend remain unresponsive? I don't think that > > > anybody would object to the addition of some CHECK_FOR_INTERRUPTS() in > > > areas where it would be efficient to make the shutdown quicker, but > > > we need to think carefully about the places where we'd want to add > > > these. > > > > CHECK_FOR_INTERRUPTS is really quite cheap, just a test-and-branch. > > I wouldn't put it in a *very* tight loop, but one test per row > > processed while gathering stats is unlikely to be a problem. > > +1. If we're finding things stalling that would be fixed by adding > CHECK_FOR_INTERRUPTS(), we should generally just add it. In the > unlikely event that this causes a performance problem, we can try to > figure out some other solution, but not responding to interrupts isn't > the right way to economize. Reproduce the problem for ndistinct and dependencies like: DROP TABLE t; CREATE TABLE t AS SELECT i A,1+i B,2+i C,3+i D,4+i E,5+i F, now() AS ts FROM generate_series(1.0, 99999.0)i; VACUUM t; DROP STATISTICS stxx; CREATE STATISTICS stxx (ndistinct) ON mod(a,14),mod(b,15),mod(c,16),mod(d,17),mod(e,18),mod(f,19) FROM t; ANALYZE VERBOSE t; Maybe this should actually call vacuum_delay_point(), like compute_scalar_stats(). For MCV, there seems to be no issue, since those functions are being called (but only for expressional stats). But maybe I've just failed to make a large enough, non-expressional MCV list for the problem to be apparent. The patch is WIP, but whatever we end up with should probably be backpatched at least to v14, where expressional indexes were introduced, since they're likely to have more columns, and are slower to compute. diff --git a/src/backend/statistics/dependencies.c b/src/backend/statistics/dependencies.c index e8f71567b4e..e5538dcd4e1 100644 --- a/src/backend/statistics/dependencies.c +++ b/src/backend/statistics/dependencies.c @@ -19,6 +19,7 @@ #include "catalog/pg_statistic_ext.h" #include "catalog/pg_statistic_ext_data.h" #include "lib/stringinfo.h" +#include "miscadmin.h" #include "nodes/nodeFuncs.h" #include "nodes/nodes.h" #include "nodes/pathnodes.h" @@ -383,6 +384,8 @@ statext_dependencies_build(StatsBuildData *data) MVDependency *d; MemoryContext oldcxt; + CHECK_FOR_INTERRUPTS(); + /* release memory used by dependency degree calculation */ oldcxt = MemoryContextSwitchTo(cxt); diff --git a/src/backend/statistics/mcv.c b/src/backend/statistics/mcv.c index dd67b19b6fa..9db1d0325cd 100644 --- a/src/backend/statistics/mcv.c +++ b/src/backend/statistics/mcv.c @@ -269,6 +269,8 @@ statext_mcv_build(StatsBuildData *data, double totalrows, int stattarget) SortItem **freqs; int *nfreqs; + // CHECK_FOR_INTERRUPTS(); + /* used to search values */ tmp = (MultiSortSupport) palloc(offsetof(MultiSortSupportData, ssup) + sizeof(SortSupportData)); diff --git a/src/backend/statistics/mvdistinct.c b/src/backend/statistics/mvdistinct.c index 6ade5eff78c..3b739ab7ca0 100644 --- a/src/backend/statistics/mvdistinct.c +++ b/src/backend/statistics/mvdistinct.c @@ -29,6 +29,7 @@ #include "catalog/pg_statistic_ext.h" #include "catalog/pg_statistic_ext_data.h" #include "lib/stringinfo.h" +#include "miscadmin.h" #include "statistics/extended_stats_internal.h" #include "statistics/statistics.h" #include "utils/fmgrprotos.h" @@ -114,6 +115,8 @@ statext_ndistinct_build(double totalrows, StatsBuildData *data) MVNDistinctItem *item = &result->items[itemcnt]; int j; + CHECK_FOR_INTERRUPTS(); + item->attributes = palloc(sizeof(AttrNumber) * k); item->nattributes = k;