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 1n9hQu-0000yX-5A for pgsql-admin@arkaria.postgresql.org; Tue, 18 Jan 2022 05:53:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n9hQs-00060P-TO for pgsql-admin@arkaria.postgresql.org; Tue, 18 Jan 2022 05:53:26 +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 1n9hQs-00060A-J3 for pgsql-admin@lists.postgresql.org; Tue, 18 Jan 2022 05:53:26 +0000 Received: from admin.sibptus.ru ([2001:19f0:5001:21dc::10]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n9hQp-0006ze-L5 for pgsql-admin@lists.postgresql.org; Tue, 18 Jan 2022 05:53:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sibptus.ru; s=20181118; h=In-Reply-To:Message-ID:Subject:To:From:Date; bh=3IIeslE1x+xB5RctQ7DgsVk5UqqPPpuqKBnwPz/6dcA=; b=RhzwvXLhKAs2s82yRG5LzZgoEq A5h7OuYUdiAGud7M0ofZtp4ki5VDxnft78RBRkUrKiS7VC2tspVXU/EhZ0lnYHWzb2BDkRKG/WStN QEEjBHfta3xU39vgZXpRPEC0PeNM0zUXANuopguwcF6peR4CnFTAXDmqfa5G0mD5urts=; Received: from vas by admin.sibptus.ru with local (Exim 4.95 (FreeBSD)) (envelope-from ) id 1n9hQn-000B39-B2; Tue, 18 Jan 2022 05:53:21 +0000 Date: Tue, 18 Jan 2022 05:53:21 +0000 From: Victor Sudakov To: Tom Lane Cc: pgsql-admin@lists.postgresql.org, Ron Subject: Re: Large number of partitions of a table Message-ID: References: <3919502.1642393668@sss.pgh.pa.us> <671340a5-1960-e5dc-1d09-4b7f36cda2c8@gmail.com> <254601.1642480213@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <254601.1642480213@sss.pgh.pa.us> X-PGP-Key: http://admin.sibptus.ru/~vas/ X-PGP-Fingerprint: 10E3 1171 1273 E007 C2E9 3532 0DA4 F259 9B5E C634 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Tom Lane wrote: > Victor Sudakov writes: > > What's the worst thing to happen if someone runs "SELECT COUNT(*) FROM t" where t has 10000 partitions? > > > 1. The backend will crash? > > Well, yeah, actually. An ill-advised query will blow out your backend's > memory consumption, potentially leading to a SIGKILL from the dreaded OOM > killer[1] (if you're on Linux), resulting in a backend crash and cluster > restart. Why should "SELECT COUNT(*) FROM t" ever consume more than work_mem even if t has 10000 partitions? I've just run it with 3000 partitions and it does not even create temporary files let alone consume any significant amount of RAM. It is very difficult for me to even imagine a query that would consume work_mem*N where N>4 or something. It has to be a very sophisticated query though documentation says this is possible. And the reasonable amount of work_mem could be around 32MB. Even considering parallelism, how would a query to a table with 10000 partitions consume enough RAM to cause OOM, what is the mechanism? > > > 3. Only this particular query (spanning multiple partitions) will be very slow? > > Guaranteed. > > > 4. ? > > You could run the shared lock table to full or nearly full, causing > concurrent queries to fail all the while your SELECT COUNT(*) is > leisurely proceeding towards completion. What's the limit on the shared lock table? > > Also, what if it is not a SELECT but an UPDATE query spanning multiple partitions? Does it make any difference? > > On v14, no. Before that, you have the O(N^2) planner issues > I mentioned upthread. > > All of these things can be worked around, but you have to ask > yourself if the benefits you get from using more rather than > fewer partitions are worth fighting these sorts of fires. Thank you for the valuable comments! > > [1] https://www.postgresql.org/docs/current/kernel-resources.html#LINUX-MEMORY-OVERCOMMIT Reading this, I'm not sure if getting a "Cannot allocate memory" error is really any better for Postgres than being killed by the OOM killer. I'm not arguing here but just stating that I genuinely don't know. -- Victor Sudakov VAS4-RIPE http://vas.tomsk.ru/ 2:5005/49@fidonet