Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qd6gN-002Cng-GQ for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Sep 2023 10:19:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qd6gM-00AGVb-A9 for pgsql-hackers@arkaria.postgresql.org; Mon, 04 Sep 2023 10:19:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qd6gL-00AGVN-R6 for pgsql-hackers@lists.postgresql.org; Mon, 04 Sep 2023 10:19:45 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qd6gI-002xxH-6s for pgsql-hackers@lists.postgresql.org; Mon, 04 Sep 2023 10:19:45 +0000 Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com [66.111.4.228]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) (Authenticated sender: a.lepikhov@postgrespro.ru) by mail.postgrespro.ru (Postfix/587) with ESMTPSA id 30C60E20E63; Mon, 4 Sep 2023 13:19:40 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1693822780; bh=Bg07L4ASg5iMPHa9Ux3xaP7zW4IjZ2IMnPH4vRnqN5A=; h=User-Agent:Message-Id:In-Reply-To:References:Date:From:To:Cc: Subject:From; b=k1ngfa+68tw0tP4aBe0JEWs1ERMx7GsNaOrZrTfs/l411LEZYedFxivDhhg1c7Ky8 EmKDD5Jnc7zvbkmdBpu423UR6FiNIw5xmRw+RPO5gHwrkFBmNZYOWD/b2+ORoZv/6P 4XlZiDpZduwp689hY8z8Jj6rTK3jf1i+GoVxu1zKLBQlCT/vjGO/zCIWwrAvayBs2D ZzEapDXaZrF/vbdAzq4bxGVwSIQVj64JSuUo66A8x4BYHgV5sBcm+x+GBxhUDGM4D8 LtYrzpB9tM/7/kMQFrs2Z4UnhvJz34AUifcEqugTBCvuCTWL7Hbm1zz8Jb7svvh7mv romgK8E60e9ng== Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailauth.nyi.internal (Postfix) with ESMTP id D36E427C005B; Mon, 4 Sep 2023 06:19:37 -0400 (EDT) Received: from imap53 ([10.202.2.103]) by compute1.internal (MEProxy); Mon, 04 Sep 2023 06:19:37 -0400 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudegkedgvdekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfgjfhffhffvvefutgfgse htqhertderreejnecuhfhrohhmpedfnfgvphhikhhhohhvucetnhgurhgvihdfuceorgdr lhgvphhikhhhohhvsehpohhsthhgrhgvshhprhhordhruheqnecuggftrfgrthhtvghrnh epkedvffekgeevleeugeeuieduveehieekjeelffffvdegteevheeijefgueehkeefnecu vehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheplhgvphhikh hhohhvodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqddufeelgeehudejhedv qdeftddtiedtkeefvddqrgdrlhgvphhikhhhohhvpeepphhoshhtghhrvghsphhrohdrrh husehfrghsthhmrghilhdrtghomh X-ME-Proxy: Feedback-ID: ie8b149e1:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 5DD4788005E; Mon, 4 Sep 2023 06:19:37 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-701-g9b2f44d3ee-fm-20230823.001-g9b2f44d3 Mime-Version: 1.0 Message-Id: <8498b480-51f8-4827-ac3a-c0c87872810c@app.fastmail.com> In-Reply-To: References: Date: Mon, 04 Sep 2023 17:19:05 +0700 From: "Lepikhov Andrei" To: "Dilip Kumar" Cc: pgsql-hackers@lists.postgresql.org, =?UTF-8?Q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9_=D0=91=D1=80=D0=B5?= =?UTF-8?Q?=D0=B4=D0=BD=D1=8F?= Subject: Re: Optimize planner memory consumption for huge arrays Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Sep 4, 2023, at 3:37 PM, Dilip Kumar wrote: > On Mon, Sep 4, 2023 at 11:58=E2=80=AFAM Lepikhov Andrei > wrote: >> >> Hi, hackers, >> >> Looking at the planner behaviour with the memory consumption patch [1= ], I figured out that arrays increase memory consumption by the optimize= r significantly. See init.sql in attachment. >> The point here is that the planner does small memory allocations for = each element during estimation. As a result, it looks like the planner c= onsumes about 250 bytes for each integer element. >> >> It is maybe not a problem most of the time. However, in the case of p= artitions, memory consumption multiplies by each partition. Such a corne= r case looks weird, but the fix is simple. So, why not? >> >> The diff in the attachment is proof of concept showing how to reduce = wasting of memory. Having benchmarked a bit, I didn't find any overhead. > > + Const *c =3D makeConst(nominal_element_type, > + -1, > + nominal_element_collation, > + elmlen, > + elem_values[i], > + elem_nulls[i], > + elmbyval); > + > + args =3D list_make2(leftop, c); > if (is_join_clause) > s2 =3D DatumGetFloat8(FunctionCall5Coll(&oprselproc, > clause->inputcollid, > @@ -1984,7 +1985,8 @@ scalararraysel(PlannerInfo *root, > ObjectIdGetDatum(operator), > PointerGetDatum(args), > Int32GetDatum(varRelid))); > - > + list_free(args); > + pfree(c); > > Maybe you can just use list_free_deep, instead of storing the constant > in a separate variable. As I see, the first element in the array is leftop, which is used on oth= er iterations. So, we can't use list_free_deep here. --=20 Regards, Andrei Lepikhov