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 1mQbDu-00054K-TI for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Sep 2021 20:09:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mQbDt-0000xt-7l for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Sep 2021 20:09:37 +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 1mQbDs-0000xl-Rv for pgsql-hackers@lists.postgresql.org; Wed, 15 Sep 2021 20:09:37 +0000 Received: from mail-io1-xd34.google.com ([2607:f8b0:4864:20::d34]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mQbDn-0004K9-M8 for pgsql-hackers@postgresql.org; Wed, 15 Sep 2021 20:09:35 +0000 Received: by mail-io1-xd34.google.com with SMTP id a15so5086238iot.2 for ; Wed, 15 Sep 2021 13:09: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:mime-version:content-disposition :user-agent; bh=SlWsWlkMR/QT0dCiirmM+fcKgfNXWa3A2izIk2rxYPw=; b=furiPFLTGpkYhn8U0wcw0YAxkW3vI1a5Jw45Xpgmer7RKKQFueiGkEGEMSr3475IWk 96VLcjbOB/edpFTKeoPS+FQXjMu5Vi6F5f/oUdjTl3bq3HgWEwJItwS0Ma9IfbtakOl3 YmX5hXtBTNuA7KCCIYmCdom1vrjxv7C08/LLjMYZwnEtxaE9TZbhba/hGSWqVqAq1dAa NuyidqrEqTcq2K/qm+nPhVFxkTyr+gflCX/qG/yozaS0BR7YEbFfgdepGjQKLHhaxZyP UOdiFmxVblwxW8ObrVnvkCE5cRFgdpnzaoGBiHKdPa/VzPdVRgZKYQ52DuFZ2p7HxYC/ prwg== 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:mime-version :content-disposition:user-agent; bh=SlWsWlkMR/QT0dCiirmM+fcKgfNXWa3A2izIk2rxYPw=; b=jWCAv8x+B9enCs/PsW3/2Iihd4Fn5Yai8JfFIQ7MiuPgernGBrchxnLf+TkLphXY1i Mo/LKTD5r3IOO+ywaGp89pNWjHWbLPR/V6jdYzIHbDSNChuZMtPkV/bJ9tClTFbfHphB qBnkfTBw5WL5aGHo6d93hJ2pgX7tVo+pxO+qTUe0vDRDgi+N0oCV0WWi1AINoezDUZMf 4irGieTA9Fj7Tw1EWo6I7+vWqEqixx6hQ6lLShNJjzmLnsuPnXtq5w5Ej48v48YbwrtM Al8hNZpDiC9SEb4RaT7jgZGVd4kNQNjWBCmAr6XoZDNR/79zdrVAxtdlrBVKUjiohUuC RlwQ== X-Gm-Message-State: AOAM531z93nvQCRvbAgddWPrQklCL96x/8dGcDgfLIK/BVEbDawT3/Qr 1mJ3AI7gUfKJvYpZsZiGHJqa012BwnxPIQ== X-Google-Smtp-Source: ABdhPJwnCFm98BrW217xPYdAGcK+cEBknunc6D6veTqA0jki2wbvG4cG+q91W28Vfejexpp74R3Icw== X-Received: by 2002:a05:6638:3046:: with SMTP id u6mr1493315jak.35.1631736570066; Wed, 15 Sep 2021 13:09:30 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id c4sm509575ioo.2.2021.09.15.13.09.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 15 Sep 2021 13:09:29 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 491D7800F18; Wed, 15 Sep 2021 15:09:28 -0500 (CDT) Date: Wed, 15 Sep 2021 15:09:28 -0500 From: Justin Pryzby To: Tomas Vondra Cc: pgsql-hackers@postgresql.org Subject: mem context is not reset between extended stats Message-ID: <20210915200928.GP831@telsasoft.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="kXdP64Ggrk/fb43R" Content-Disposition: inline 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 --kXdP64Ggrk/fb43R Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Memory allocation appeared be O(1) WRT the number of statistics objects, which was not expected to me. This is true in v13 (and probably back to v10). It seems to work fine to reset the memory context within the loop, so long as the statslist is allocated in the parent context. |DROP TABLE t; CREATE TABLE t AS SELECT i, i+1 AS a, i+2 AS b, i+3 AS c, i+4 AS d, i+5 AS e FROM generate_series(1,99999)i; |SELECT format('CREATE STATISTICS sta%s (ndistinct) ON a,(1+b),(2+c),(3+d),(4+e) FROM t', a) FROM generate_series(1,9)a\gexec |SET log_statement_stats=on; SET client_min_messages=debug; ANALYZE t; |=> 369432 kB max resident size |SELECT format('CREATE STATISTICS sta%s (ndistinct) ON a,b,c,d,e FROM t', a) FROM generate_series(1,33)a\gexec |SET log_statement_stats=on; SET client_min_messages=debug; ANALYZE t; |=> 1284368 kB max resident size --kXdP64Ggrk/fb43R Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-stx-do-not-leak-memory-for-each-stats-obj.patch" From 6ae4d059f2ed8baf2af92ec0847458055147383c Mon Sep 17 00:00:00 2001 From: Justin Pryzby Date: Wed, 15 Sep 2021 14:38:49 -0500 Subject: [PATCH] stx: do not leak memory for each stats obj --- src/backend/statistics/extended_stats.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index 63549757ec..d9387ceeb6 100644 --- a/src/backend/statistics/extended_stats.c +++ b/src/backend/statistics/extended_stats.c @@ -136,14 +136,14 @@ BuildRelationExtStatistics(Relation onerel, double totalrows, if (!natts) return; + pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock); + statslist = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel)); + cxt = AllocSetContextCreate(CurrentMemoryContext, "BuildRelationExtStatistics", ALLOCSET_DEFAULT_SIZES); oldcxt = MemoryContextSwitchTo(cxt); - pg_stext = table_open(StatisticExtRelationId, RowExclusiveLock); - statslist = fetch_statentries_for_relation(pg_stext, RelationGetRelid(onerel)); - /* report this phase */ if (statslist != NIL) { @@ -245,14 +245,12 @@ BuildRelationExtStatistics(Relation onerel, double totalrows, pgstat_progress_update_param(PROGRESS_ANALYZE_EXT_STATS_COMPUTED, ++ext_cnt); - /* free the build data (allocated as a single chunk) */ - pfree(data); + MemoryContextReset(cxt); } - table_close(pg_stext, RowExclusiveLock); - MemoryContextSwitchTo(oldcxt); MemoryContextDelete(cxt); + table_close(pg_stext, RowExclusiveLock); } /* -- 2.17.0 --kXdP64Ggrk/fb43R--