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 1qdsI6-0052Gh-Mc for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Sep 2023 13:09:54 +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 1qdsI5-00AF4D-57 for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Sep 2023 13:09:52 +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 1qdsI4-00AF42-QI for pgsql-hackers@lists.postgresql.org; Wed, 06 Sep 2023 13:09:52 +0000 Received: from mail-ot1-x334.google.com ([2607:f8b0:4864:20::334]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qdsI1-003Q9S-2O for pgsql-hackers@lists.postgresql.org; Wed, 06 Sep 2023 13:09:52 +0000 Received: by mail-ot1-x334.google.com with SMTP id 46e09a7af769-6c09d760cb9so827154a34.2 for ; Wed, 06 Sep 2023 06:09:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694005787; x=1694610587; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=zPZVGZ30AjLhI6Djin2n3D+u2seJbmVzXZ4uDCzutEI=; b=d8HSDdZFzVO13q3uYZPcUKF1z//Tl5tawpMzvSyDPMwYeejcYpjNur3nM8Li0nhgKW NWJFyUOUSiVckrQLn3k8JnaVrpLne0DaD1cxqwgJks7mwpOfLde2TwPtt4+2oYBzmrLa S1kUVDI0E9Sj0RZXEkTqr8W326g3wkcUP3tXXQ/OmeABUiWP7inJt3BSnhW+AqljcXdD 193UPSsN9cIQY5ep5BQqGScrrJX+6hCsEga01kY/xyjUGbroS610nHAbLavGQxHKzL3Y bFMvbndXx0TOHtmsD/uAr2RxyZGYEa4T/wyb07Q44sN3bOg4mmSAxoVB3to46b9XVs79 cf2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1694005787; x=1694610587; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zPZVGZ30AjLhI6Djin2n3D+u2seJbmVzXZ4uDCzutEI=; b=E0gzRBB8X9ukWeSRv7fKFGshsjmNP6on4rCzQq0bJh4fKaHBz+/HJiy56kKXSiVeIg omxq6WfD6tj723pMyzuOuFblQg+Mpvzgrwsu3OwUojn8IHTX4taNLr4mLTMrMG3/5mp4 uPFuUXKtTLeZsRUOi3kPeHyC5vQu779O0oNLeDovHX1hdbAEZ1km7NYwNByUTQ++AUBK kOhz70A7XdH5Dcj+a+fmMo618uUoGdICTjFPcaBP2NMWp3uR6cOIoRCQWNDrzC4KdMO2 t3qkfhfQLvB7XQgdbx6GkRF5c9AOoj0gNOYUytMyT2NrBrs45v/oyFj2q2+1Oq3M4jLq XGaA== X-Gm-Message-State: AOJu0Yye9r4K6dRFB6cZob89odPFkRQbrBA413fzryIBfQso+1j0GYfc r07hHy+pV41jZh5tBZcxt8VqT4gNVxGQQeBgPqg= X-Google-Smtp-Source: AGHT+IGPlbs2uRzjX+WAjwPV2v3/VSuY4lBS1Gd0pMLctwKtLTPK5DzOt0McbTaCuP5kQyxkkNFIwldIh17CTeCFnag= X-Received: by 2002:a05:6358:5e01:b0:134:ed9b:15a7 with SMTP id q1-20020a0563585e0100b00134ed9b15a7mr2387311rwn.30.1694005786878; Wed, 06 Sep 2023 06:09:46 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Ashutosh Bapat Date: Wed, 6 Sep 2023 18:39:35 +0530 Message-ID: Subject: Re: Optimize planner memory consumption for huge arrays To: Lepikhov Andrei Cc: pgsql-hackers@lists.postgresql.org, =?UTF-8?B?0JXQstCz0LXQvdC40Lkg0JHRgNC10LTQvdGP?= 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 Hi Lepikhov, Thanks for using my patch and I am glad that you found it useful. On Mon, Sep 4, 2023 at 10:56=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 optimizer signi= ficantly. 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 consumes= about 250 bytes for each integer element. I guess the numbers you mentioned in init.sql are total memory used by the planner (as reported by the patch in the thread) when planning that query and not memory consumed by Const nodes themselves. Am I right? I think the measurements need to be explained better and also the realistic scenario you are trying to oprimize. I guess, the reason you think that partitioning will increase the memory consumed is because each partition will have the clause translated for it. Selectivity estimation for each partition will create those many Const nodes and hence consume memory. Am I right? Can you please measure the memory consumed with and without your patch. > > It is maybe not a problem most of the time. However, in the case of parti= tions, memory consumption multiplies by each partition. Such a corner case = looks weird, but the fix is simple. So, why not? With vectorized operations becoming a norm these days, it's possible to have thousands of element in array of an ANY or IN clause. Also will be common to have thousands of partitions. But I think what we need to do here is to write a selectivity estimation function which takes an const array and return selectivity without requiring to create a Const node for each element. > > The diff in the attachment is proof of concept showing how to reduce wast= ing of memory. Having benchmarked a bit, I didn't find any overhead. > You might want to include your benchmarking results as well. --=20 Best Wishes, Ashutosh Bapat