Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1glwdD-0003Lw-GX for pgsql-performance@arkaria.postgresql.org; Tue, 22 Jan 2019 14:02:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1glwdC-00011V-6n for pgsql-performance@arkaria.postgresql.org; Tue, 22 Jan 2019 14:02:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1glwdB-00011O-Rs for pgsql-performance@lists.postgresql.org; Tue, 22 Jan 2019 14:02:22 +0000 Received: from mail-io1-xd42.google.com ([2607:f8b0:4864:20::d42]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1glwd1-0000lw-18 for pgsql-performance@lists.postgresql.org; Tue, 22 Jan 2019 14:02:21 +0000 Received: by mail-io1-xd42.google.com with SMTP id k2so19159023iog.7 for ; Tue, 22 Jan 2019 06:02:10 -0800 (PST) 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:in-reply-to:user-agent; bh=9DjOcuH7II9I7jX1dUpR8sXhUaEaxYhFbdpJGU55Kww=; b=DsWazz8x3xfKhs0yz6c8ExMAda/JbFplpPn9I9wA4brTpmckdq1reONlcJVTZsPqt0 funVtQI07UvtFulDFQI9xTrnwDilMSpRYRgRDCHRJUTYRH0BjRmX8fA/DnjhUqOnsxo9 V22+5akA8yXtVykx15n/+xz4nI3NWVcgUB1+eB9ccpM6n1iGuj/U+SCk5eR+7mtAlEgh X5QQWNuru4p6p7wRMYGrXPmkbvah4d0GqIDe10nQ5nwhKwU3npUNR5ceB57vspubPrqL 17Jp39UtCt4mnTjzqSrDKGFbegUbuFaLLnDUz6oil7GjqVuDk0E8IgWFtxzy/KBA/zER xrIg== 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:in-reply-to:user-agent; bh=9DjOcuH7II9I7jX1dUpR8sXhUaEaxYhFbdpJGU55Kww=; b=S4Rwju19ln16zM/b5XsvruiUNf3ym9NaUppHkwW/fecArRuss+z8dxjElCURooDEfX f9u+utpXEyxoNeNIgVsofty2yqETSm4+noGLk+d1g2fy9IZfgZ24Nste3H7edqjj2pDU 8Dse6psPFuR9O1rjNTS5ue5B7B1sULlsnPmFyPnTGT8mHdGSSUj6fjIy7mPMpU15QVSE N5UIaddCa4UxXNeJOfbe9RncqDDEuA+TtXGLYB5ROET/NfsgyAPr5JboC1Gj4j6XJJiy 1d0ND6etzGYFBDDwhaB9WHv6QaZxtPRxJ3j3OI7ENW9OwPRCB71jZNUkNYaNrQAhs7Id 14Vw== X-Gm-Message-State: AJcUukeeUhE/VuOI/wyOnGsQUuGqH7RUTXsHmOlplZojDIfyNu19tY6x ass8u3UC55mFvMZQtSbRgy8Xmv/XvtI= X-Google-Smtp-Source: ALg8bN4noPRtWhpwqpRPAW3FQSF539B+sF20sbJ4X0ww01PUjkxd+hfw4JToo7ToVXXfOfNwUwJkKA== X-Received: by 2002:a5d:9756:: with SMTP id c22mr19043513ioo.146.1548165727882; Tue, 22 Jan 2019 06:02:07 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id 196sm7487723itu.33.2019.01.22.06.02.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Jan 2019 06:02:07 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id AF2A5801CF8; Tue, 22 Jan 2019 08:02:05 -0600 (CST) Date: Tue, 22 Jan 2019 08:02:05 -0600 From: Justin Pryzby To: Mickael van der Beek Cc: pgsql-performance@lists.postgresql.org Subject: Re: Very long query planning times for database with lots of partitions Message-ID: <20190122140205.GC20937@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Jan 22, 2019 at 02:44:29PM +0100, Mickael van der Beek wrote: > Hey everyone, > > I have a PostgreSQL 10 database that contains two tables which both have > two levels of partitioning (by list and using a single value). Meaning that > a partitioned table gets repartitioned again. > > The data in my use case is stored on 5K to 10K partitioned tables (children > and grand-children of the two tables mentioned above) depending on usage > levels. > > Three indexes are set on the grand-child partition. The partitioning > columns are not covered by them. > (I don't believe that it is needed to index partition columns no?) > > With this setup, I experience queries that have very slow planning times > but fast execution times. > Even for simple queries where only a couple partitions are searched on and > the partition values are hard-coded. > > Researching the issue, I thought that the linear search in use by > PostgreSQL 10 to find the partition table metadata was the cause. > > cf: https://blog.2ndquadrant.com/partition-elimination-postgresql-11/ > > So I decided to try ou PostgreSQL 11 which included the two aforementioned > fixes: > > https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=499be013de65242235ebdde06adb08db887f0ea5 > https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=9fdb675fc5d2de825414e05939727de8b120ae81 Those reduce the CPU time needed, but that's not the most significant issue. For postgres up through 11, including for relkind=p, planning requires 1) stat()ing every 1GB file for every partition, even those partitions which are eventually excluded by constraints or partition bounds ; AND, 2) open()ing every index on every partition, even if it's excluded later. Postgres 12 is expected to resolve this and allow "many" (perhaps 10k) of partitions: https://commitfest.postgresql.org/20/1778/ I think postgres through 11 would consider 1000 partitions to be "too many". You *might* be able to mitigate the high cost of stat()ing tables by ensuring that the table metadata stays in OS cache, by running something like: find /var/lib/pgsql /tablespace -ls You *might* be able to mitigate the high cost of open()ing the indices by keeping their first page in cache (preferably postgres buffer cache)..either by running a cronjob to run explain, or perhaps something like pg_prewarm on the indices. (I did something like this for our largest customers to improve performance as a stopgap). Justin