Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id C324D1337C08 for ; Thu, 7 Apr 2011 18:25:06 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 03447-03 for ; Thu, 7 Apr 2011 21:24:47 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pz0-f46.google.com (mail-pz0-f46.google.com [209.85.210.46]) by mail.postgresql.org (Postfix) with ESMTP id 43A571337C24 for ; Thu, 7 Apr 2011 18:22:23 -0300 (ADT) Received: by pzk9 with SMTP id 9so1014755pzk.19 for ; Thu, 07 Apr 2011 14:22:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=/htQh55GJ9NgfUbyJ/lSzYOpEsuUOc/xgr67Owy44TQ=; b=FZn2vnekiXO4KAOA+cCmZo4eXP2AY0kQUg8mW2UqucI7FksMewmNiVb5VMg+8nQv25 CnH68mJKLuPXrEXieKc21AW2miD/QJVX2RqM7mmFWUbwKHpDc1pm2YEJZWvUjtd3IiGU bQ9YF3cckY2C3G5ppbb052SeNGh3QoQbJC8+Q= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=UQYlmycJyKv1mE+UJ0XxrWoRFBosCiECCjwnisTsIfbPeGPPEP70wdzIXFJjMneWmh ZmXXXk14JqBUy0EbxyjDzxaCpyGmYeHU1fike2OzenKkisRAjnE8Fgs1pDMdGT7KUCrA N0vFOxTeObgT3wguUDOugmHud6aiV4WrYs+Bw= MIME-Version: 1.0 Received: by 10.142.135.21 with SMTP id i21mr1093795wfd.418.1302211342931; Thu, 07 Apr 2011 14:22:22 -0700 (PDT) Received: by 10.68.51.161 with HTTP; Thu, 7 Apr 2011 14:22:22 -0700 (PDT) In-Reply-To: <1302208932097-4289407.post@n5.nabble.com> References: <1302208932097-4289407.post@n5.nabble.com> Date: Thu, 7 Apr 2011 16:22:22 -0500 Message-ID: Subject: Re: PostgreSQL backend process high memory usage issue From: Merlin Moncure To: Shianmiin Cc: pgsql-general@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001 X-Spam-Level: X-Archive-Number: 201104/284 X-Sequence-Number: 174640 On Thu, Apr 7, 2011 at 3:42 PM, Shianmiin wrote: > Hi there, > > We are evaluating using PostgreSQL to implement a multitenant database, > Currently we are running some tests on single-database-multiple-schema mo= del > (basically, all tenants have the same set of database objects under then = own > schema within the same database). > The application will maintain a connection pool that will be shared among > all tenants/schemas. > > e.g. If the database has 500 tenants/schemas and each tenants has 200 > tables/views, > the total number of tables/views will be 500 * 200 =3D 100,000. > > Since the connection pool will be used by all tenants, eventually each > connection will hit all the tables/views. > > In our tests, when the connection hits more views, we found the memory us= age > of the backend process increases quite fast and most of them are private > memory. > Those memory will be hold until the connection is closed. > > We have a test case that one backend process uses more the 30GB memory an= d > eventually get an out of memory error. > > To help understand the issue, I wrote code to create a simplified test ca= ses > =A0- MTDB_destroy: used to clear tenant schemas > =A0- MTDB_Initialize: used to create a multitenant DB > =A0- MTDB_RunTests: simplified test case, basically select from all tenan= t > views one by one. > > The tests I've done was on PostgreSQL 9.0.3 on CentOS 5.4 > To make sure I have a clean environment, I re-created database cluster an= d > leave majority configurations as default, > (the only thing I HAVE to change is to increase "max_locks_per_transactio= n" > since MTDB_destroy needs to drop many objects.) > > This is what I do to reproduce the issue: > 1. create a new database > 2. create the three functions using the code attached > 3. connect to the new created db and run the initialize scripts > > -- Initialize > select MTDB_Initialize('tenant', 100, 100, true); > -- not sure if vacuum analyze is useful here, I just run it > vacuum analyze; > -- check the tables/views created > select table_schema, table_type, count(*) from information_schema.tables > where table_schema like 'tenant%' group by table_schema, table_type order= by > table_schema, table_type; > > 4. open another connection to the new created db and run the test scripts > > -- get backend process id for current connection > SELECT pg_backend_pid(); > > -- open a linux console and run ps -p =A0and watch VIRT, RES and SHR > > -- run tests > select MTDB_RunTests('tenant', 1); don't write your test environment this way. postgresql functions !=3D stored procedure. Your entire suite of tests is running in single transaction context which is very problematic and not a real test of any reasonably written application. Convert your test suite into a sql/per/bash/etc script that is run into the database through libpq or sql. merlin