Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dXbuV-0008Di-Un for pgsql-hackers@arkaria.postgresql.org; Tue, 18 Jul 2017 23:28:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dXbuU-0005xh-CH for pgsql-hackers@arkaria.postgresql.org; Tue, 18 Jul 2017 23:28:10 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dXbsy-0003G6-3i for pgsql-hackers@postgreSQL.org; Tue, 18 Jul 2017 23:26:36 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dXbsv-0001bs-1c for pgsql-hackers@postgreSQL.org; Tue, 18 Jul 2017 23:26:35 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v6INQU3Y031270; Tue, 18 Jul 2017 19:26:30 -0400 From: Tom Lane To: Justin Pryzby cc: Simon Riggs , pgsql-hackers@postgreSQL.org Subject: Re: [GENERAL] huge RAM use in multi-command ALTER of table heirarchy In-reply-to: <20170706180420.GH30252@telsasoft.com> References: <20170706180420.GH30252@telsasoft.com> Comments: In-reply-to Justin Pryzby message dated "Thu, 06 Jul 2017 13:04:20 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <31268.1500420390.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 18 Jul 2017 19:26:30 -0400 Message-ID: <31269.1500420390@sss.pgh.pa.us> List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org Justin Pryzby writes: > I've seen this before while doing SET STATISTICS on a larger number of co= lumns > using xargs, but just came up while doing ADD of a large number of column= s. > Seems to be roughly linear in number of children but superlinear WRT colu= mns. > I think having to do with catalog update / cache invalidation with many > ALTERs*children*columns? I poked into this a bit. The operation is necessarily roughly O(N^2) in the number of columns, because we rebuild the affected table's relcache entry after each elementary ADD COLUMN operation, and one of the principal components of that cost is reading all the pg_attribute entries. However, that should only be a time cost not a space cost. Eventually I traced the O(N^2) space consumption to RememberToFreeTupleDescAtEOX, which seems to have been introduced in Simon's commit e5550d5fe, and which strikes me as a kluge of the first magnitude. Unless I am missing something, that function's design concept can fairly be characterized as "let's leak memory like there's no tomorrow, on the off chance that somebody somewhere is ignoring basic coding rules". I tried ripping that out, and the peak space consumption of your example (with 20 child tables and 1600 columns) decreased from ~3GB to ~200MB. Moreover, the system still passes make check-world, so it's not clear to me what excuse this code has to live. It's probably a bit late in the v10 cycle to be taking any risks in this area, but I'd vote for ripping out RememberToFreeTupleDescAtEOX as soon as the v11 cycle opens, unless someone can show an example of non-broken coding that requires it. (And if so, there ought to be a regression test incorporating that.) regards, tom lane --=20 Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers