Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bB2A0-0003mr-Te for pgsql-bugs@arkaria.postgresql.org; Thu, 09 Jun 2016 15:46:21 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bB29z-0006z0-Ef for pgsql-bugs@arkaria.postgresql.org; Thu, 09 Jun 2016 15:46:19 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bB29y-0006yK-A4 for pgsql-bugs@postgresql.org; Thu, 09 Jun 2016 15:46:18 +0000 Received: from andy.depesz.com ([88.198.47.100] helo=depesz.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bB29t-0001Bk-Sl for pgsql-bugs@postgresql.org; Thu, 09 Jun 2016 15:46:17 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20140207; h=Content-Type:MIME-Version:Reply-To:Message-ID:Subject:To: Sender:From:Date:Cc:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=wEgyQjM0p1mM4HeNFBAjdoQiSN4uuIrmkLUifStohRs=; b=tXnfgYTzrwXyqfjq4b5GjxA8C2 OomjDMIDQs/tBnioCjG1SWZD05UkDxO2d5DTAZO6kfS+uaEyNQtenAVK0edbEPexrEL3au4CUwUH+ Fl8sp9+4narwAYREFj/pscxab; Received: from andy.depesz.com ([88.198.47.100] helo=depesz.com) by depesz.com with esmtpa (Exim 4.87) (envelope-from ) id 1bB29q-00074d-D9 for pgsql-bugs@postgresql.org; Thu, 09 Jun 2016 17:46:10 +0200 Date: Thu, 9 Jun 2016 17:46:10 +0200 From: hubert depesz lubaczewski To: pgsql-bugs@postgresql.org Subject: PostgreSQL (9.3 and 9.6) eats all memory when using many tables Message-ID: <20160609154610.GA20600@depesz.com> Reply-To: depesz@depesz.com MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline User-Agent: Mutt/1.6.0 (2016-04-01) X-Pg-Spam-Score: -3.4 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Hi, so, we are running 9.3.10 in production, but I tested it in 9.6, and the problem seems to be there too in 9.6, though to much lesser extent. In our database we have ~ 70000 tables (~ 180 tables in ~ 400 schemas). So far we used application in such a way that each connection could use only tables from single schema. But then, we switched situation to where single connection (very long, as it's reused thanks to pgbouncer) can effectively query tables from all schemas. And this caused memory usage to explode, to the point that our server wasn't able to handle it (64gb of mem gone). I checked logs, and other stuff, and came with synthetic test, using perl: ======================================================= #!/usr/bin/env perl use strict; use DBI; use Data::Dumper; my $dbh = DBI->connect("dbi:Pg:dbname=depesz"); my $backend_pid = $dbh->selectall_arrayref("select pg_backend_pid()")->[0]->[0]; system("ps nh uww -p $backend_pid"); my $t = [ map { $_->[0] } @{ $dbh->selectall_arrayref("SELECT oid::regclass from pg_class where relkind = 'r'") } ]; my $len = scalar @{ $t }; my $i = 0; while (1) { my $use_table = $t->[ $i % $len ]; my $limit = 1 + ( $i % 3 ); $dbh->prepare("select ${i}::int4 as a, ?::int4 as a, * from $use_table limit $limit")->execute( $i ); $i++; if ( 0 == $i % 1000) { print "$i:"; system("ps nh uww -p $backend_pid"); } } ======================================================= This effectively does: select 1, 1, * from limit <1..3> for each table. on 9.3 after ~ 35000 tables, pg process grew by 1GB, and it wasn't shared buffers, as smaps showed that the memory was anonymous. In 9.6, after 35000 tables it grew by ~ 71MB. Which is much better, but still not really optimal. The same situation happens when I was *not* using prepared statements on server side. Basically it looks that postgresql "caches" query plans? parsed elements? for queries, but doesn't put any kind of limit to size of this cache. Which means that if our app is using LOTS of different queries, the memory usage will grow in time. This, plus the fact that this cache is not shared, means that with non-trivial numbers of tables, and not-trivial numbers of backends, it will use all of server memory as soon as enough different queries/tables will get used. What can we do about it, aside from having less tables and moving to newer Pg? Best regards, depesz -- The best thing about modern society is how easy it is to avoid contact with it. http://depesz.com/ -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs