Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 2FC8C16AF4E5 for ; Mon, 30 Jul 2012 12:06:37 -0300 (ADT) Received: from moutng.kundenserver.de ([212.227.17.10]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SvrYH-00006h-6L for pgsql-bugs@postgresql.org; Mon, 30 Jul 2012 15:06:36 +0000 Received: from awork2.localnet (p5DDC7A9E.dip.t-dialin.net [93.220.122.158]) by mrelayeu.kundenserver.de (node=mreu3) with ESMTP (Nemesis) id 0MOYWP-1Sqf1G1LFd-005mrw; Mon, 30 Jul 2012 17:06:15 +0200 From: Andres Freund Organization: 2ndquadrant.com To: pgsql-bugs@postgresql.org Subject: Re: BUG #6785: Memory Leak in plpgsql Date: Mon, 30 Jul 2012 17:06:13 +0200 User-Agent: KMail/1.13.7 (Linux/3.5.0-rc6-andres-00196-gfdb1335; KDE/4.8.4; x86_64; ; ) Cc: andervalbh@gmail.com References: In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <201207301706.13963.andres@2ndquadrant.com> X-Provags-ID: V02:K0:A2mCXqIR3f2/wG1537irAzIpO7WHLcEmlscHkTxkaYz f1+zVcDzxQIftNpyfhEMbUd3xBjRPEFjUasVe4AKQDXelC0qRR EwWatunD6KOVyVWbrViu0YjYDqAcdm4ok3y414SB814fyrYyLB eGjL2dLxxjo5tiOubGJmlTDrBrU0EU1FMBWhhjS1DHgcCKure+ rIiDpmPmXCO7jy9Z5AXbXNMV/RFDooZNzoYbDpOifzs6GOcPVq JgE7UBcBAE4Qn/y9B50qOYeh/67FTqc7SkK760gaLBVR9ufSpR /71Gd2qU5WS9uI+PBRuCBoTYNiua6C/kiXy7AlOZUvI1ry6Mmr F063UmYTc+slS+dZLBUxENAS5TcVNFdDN7bcRKArS X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201207/230 X-Sequence-Number: 33706 Hi, On Monday, July 30, 2012 03:15:37 PM andervalbh@gmail.com wrote: > we recently had a memory exhaustion in the PostgreSQL server of the > company, after a scan found a likely memory leak when using a plpgsql > function. > The problem occurred on an IBM x3400 server with 12G, CentOS 5.5 and > PostgreSQL 9.1.4. The leak occurs when a variable declared type of a table > column and when the > column or the table ceases to exist. > Follow the steps for the simulation: > > create table tbl_test > (cod integer); > > CREATE OR REPLACE FUNCTION citgis.fct_test() > RETURNS void AS > $body$ > DECLARE > v_cod tbl_test.cod%type; > BEGIN > return; > END; > $body$ > LANGUAGE 'plpgsql'; > > drop table tbl_test; > > > test=# select pg_backend_pid(); > pg_backend_pid > ---------------- > 6465 > > Initial memory > --------------------------------------------------------------------------- > --------------------------------------------- PID USER PR NI VIRT > SWAP RES CODE DATA SHR S P %CPU %MEM TIME+ COMMAND > 6465 postgres 15 0 1194m 1.2g 14m 4684 12m 3748 S 1 0.0 0.1 > 0:00.08 postgres: postgres test [local] idle > 6465 postgres 15 0 1203m 1.2g 23m 4684 21m 3748 S 1 0.0 0.2 > 0:00.17 postgres: postgres test [local] idle > 6465 postgres 15 0 1212m 1.2g 32m 4684 30m 3748 S 1 3.0 0.3 > 0:00.26 postgres: postgres test [local] idle > 6465 postgres 16 0 1221m 1.2g 41m 4684 39m 3756 S 1 0.0 0.3 > 0:00.36 postgres: postgres test [local] idle > --------------------------------------------------------------------------- > --------------------------------------------- I don't think youve found a memory leak here. I tested several thousand iterations of this and the memory usage tops out a 93MB. What you see is probably some memory fragmentation and that some copy-on-write page mappings are only gradually brought into place. Greetings, Andres -- Andres Freund http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services