Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WQCJK-00009R-KI for pgsql-bugs@arkaria.postgresql.org; Wed, 19 Mar 2014 08:57:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WQCJK-0000kH-5m for pgsql-bugs@arkaria.postgresql.org; Wed, 19 Mar 2014 08:57:18 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WQCJJ-0000kB-Cs for pgsql-bugs@postgresql.org; Wed, 19 Mar 2014 08:57:17 +0000 Received: from mail-ee0-x22f.google.com ([2a00:1450:4013:c00::22f]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WQCJA-0007am-U7 for pgsql-bugs@postgresql.org; Wed, 19 Mar 2014 08:57:17 +0000 Received: by mail-ee0-f47.google.com with SMTP id b15so6346974eek.34 for ; Wed, 19 Mar 2014 01:57:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=BYorNJLu4yazGs0pOcsVl/fs/I67nBoRHG65RegAphI=; b=pRv+XPpMubDY86dnPmRj/xymdc++xGMwiiQz8cL7/VjVs4s6818WZEM/+w5ot3NGDL s2gxTeDA+XQK9vQpZTQeHZVOueQ/mcaLNKwPF2TpHm8qz30fKVhH1lWNpuoHppROdRfi 7a06Vi2nYEWI3OzqYQ9e0OoD59PIVpgSR11/ItMGZ6jVmCPQL/xz7zyJcgRwugLQ3cVO N9RcEWc0eYi7fPRTEKiPVfvGyHamZyiOoZflKd0J0Qpf541e2jym2kxDGtCpxe4QbUSx i5+jG9G1EGflL5Sbb6rlVhwgh3yHJoHkisyoDEQb2b2ahxsmy45gNjESjJqtgBF+Q0aZ lh9w== X-Received: by 10.14.6.1 with SMTP id 1mr19108197eem.71.1395219427146; Wed, 19 Mar 2014 01:57:07 -0700 (PDT) Received: from localhost (ip-46-232-148-34.static.adsl.cheapnet.it. [46.232.148.34]) by mx.google.com with ESMTPSA id 44sm41024845eek.30.2014.03.19.01.57.04 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 19 Mar 2014 01:57:05 -0700 (PDT) Date: Wed, 19 Mar 2014 09:57:02 +0100 From: Sandro Santilli To: Tom Lane Cc: Craig Ringer , Florian Pflug , pgsql-bugs@postgresql.org Subject: Re: Leaking regexp_replace in 9.3.1 ? (was: [HACKERSUninterruptable regexp_replace in 9.3.1 ?) Message-ID: <20140319085702.GF4042@localhost> References: <20140221091759.GK6924@localhost> <530774E5.5040105@2ndquadrant.com> <53077EED.2000907@2ndquadrant.com> <3663.1393002213@sss.pgh.pa.us> <646.1393031856@sss.pgh.pa.us> <20140226150113.GD9740@localhost> <20140318170401.GE29859@localhost> <32100.1395186092@sss.pgh.pa.us> <20140319075359.GA4042@localhost> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20140319075359.GA4042@localhost> User-Agent: Mutt/1.5.21 (2010-09-15) X-Pg-Spam-Score: -1.9 (-) 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 A valgrind view on the matter (over a single call, in --single mode): ==21240== 6,748 (120 direct, 6,628 indirect) bytes in 1 blocks are definitely lost in loss record 203 of 239 ==21240== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==21240== by 0x64A200: newdfa.isra.4 (rege_dfa.c:307) ==21240== by 0x64A4C3: getsubdfa (regexec.c:285) ==21240== by 0x64B80A: cdissect (regexec.c:673) ==21240== by 0x64C802: pg_regexec (regexec.c:473) ==21240== by 0x6EBB3A: RE_wchar_execute (regexp.c:269) ==21240== by 0x6EBDC8: setup_regexp_matches (regexp.c:926) ==21240== by 0x6ECB10: regexp_matches (regexp.c:809) ==21240== by 0x5A641D: ExecMakeFunctionResult (execQual.c:1795) ==21240== by 0x5A8D6C: ExecProject (execQual.c:5261) ==21240== by 0x5BB471: ExecResult (nodeResult.c:155) ==21240== by 0x5A1E77: ExecProcNode (execProcnode.c:373) ==21240== ==21240== 6,748 (120 direct, 6,628 indirect) bytes in 1 blocks are definitely lost in loss record 204 of 239 ==21240== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==21240== by 0x64A200: newdfa.isra.4 (rege_dfa.c:307) ==21240== by 0x64A4C3: getsubdfa (regexec.c:285) ==21240== by 0x64B7EE: cdissect (regexec.c:671) ==21240== by 0x64BDE9: cdissect (regexec.c:692) ==21240== by 0x64C802: pg_regexec (regexec.c:473) ==21240== by 0x6EBB3A: RE_wchar_execute (regexp.c:269) ==21240== by 0x6EBDC8: setup_regexp_matches (regexp.c:926) ==21240== by 0x6ECB10: regexp_matches (regexp.c:809) ==21240== by 0x5A641D: ExecMakeFunctionResult (execQual.c:1795) ==21240== by 0x5A8D6C: ExecProject (execQual.c:5261) ==21240== by 0x5BB471: ExecResult (nodeResult.c:155) ==21240== ==21240== 6,748 (120 direct, 6,628 indirect) bytes in 1 blocks are definitely lost in loss record 205 of 239 ==21240== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==21240== by 0x64A200: newdfa.isra.4 (rege_dfa.c:307) ==21240== by 0x64A4C3: getsubdfa (regexec.c:285) ==21240== by 0x64B80A: cdissect (regexec.c:673) ==21240== by 0x64B46B: cdissect (regexec.c:633) ==21240== by 0x64B8AA: cdissect (regexec.c:689) ==21240== by 0x64BDE9: cdissect (regexec.c:692) ==21240== by 0x64C802: pg_regexec (regexec.c:473) ==21240== by 0x6EBB3A: RE_wchar_execute (regexp.c:269) ==21240== by 0x6EBDC8: setup_regexp_matches (regexp.c:926) ==21240== by 0x6ECB10: regexp_matches (regexp.c:809) ==21240== by 0x5A641D: ExecMakeFunctionResult (execQual.c:1795) ==21240== ==21240== 6,748 (120 direct, 6,628 indirect) bytes in 1 blocks are definitely lost in loss record 206 of 239 ==21240== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==21240== by 0x64A200: newdfa.isra.4 (rege_dfa.c:307) ==21240== by 0x64A4C3: getsubdfa (regexec.c:285) ==21240== by 0x64B7EE: cdissect (regexec.c:671) ==21240== by 0x64BDE9: cdissect (regexec.c:692) ==21240== by 0x64B46B: cdissect (regexec.c:633) ==21240== by 0x64B8AA: cdissect (regexec.c:689) ==21240== by 0x64BDE9: cdissect (regexec.c:692) ==21240== by 0x64C802: pg_regexec (regexec.c:473) ==21240== by 0x6EBB3A: RE_wchar_execute (regexp.c:269) ==21240== by 0x6EBDC8: setup_regexp_matches (regexp.c:926) ==21240== by 0x6ECB10: regexp_matches (regexp.c:809) ==21240== ==21240== 6,748 (120 direct, 6,628 indirect) bytes in 1 blocks are definitely lost in loss record 207 of 239 ==21240== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==21240== by 0x64A200: newdfa.isra.4 (rege_dfa.c:307) ==21240== by 0x64A4C3: getsubdfa (regexec.c:285) ==21240== by 0x64B97C: cdissect (regexec.c:975) ==21240== by 0x64B8AA: cdissect (regexec.c:689) ==21240== by 0x64BDE9: cdissect (regexec.c:692) ==21240== by 0x64B46B: cdissect (regexec.c:633) ==21240== by 0x64B8AA: cdissect (regexec.c:689) ==21240== by 0x64BDE9: cdissect (regexec.c:692) ==21240== by 0x64C802: pg_regexec (regexec.c:473) ==21240== by 0x6EBB3A: RE_wchar_execute (regexp.c:269) ==21240== by 0x6EBDC8: setup_regexp_matches (regexp.c:926) ==21240== ==21240== 16,928 bytes in 1 blocks are definitely lost in loss record 227 of 239 ==21240== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==21240== by 0x64A41E: newdfa.isra.4 (rege_dfa.c:289) ==21240== by 0x64A4C3: getsubdfa (regexec.c:285) ==21240== by 0x64B80A: cdissect (regexec.c:673) ==21240== by 0x64BDE9: cdissect (regexec.c:692) ==21240== by 0x64C802: pg_regexec (regexec.c:473) ==21240== by 0x6EBB3A: RE_wchar_execute (regexp.c:269) ==21240== by 0x6EBDC8: setup_regexp_matches (regexp.c:926) ==21240== by 0x6ECB10: regexp_matches (regexp.c:809) ==21240== by 0x5A641D: ExecMakeFunctionResult (execQual.c:1795) ==21240== by 0x5A8D6C: ExecProject (execQual.c:5261) ==21240== by 0x5BB471: ExecResult (nodeResult.c:155) ==21240== ==21240== 16,928 bytes in 1 blocks are definitely lost in loss record 228 of 239 ==21240== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==21240== by 0x64A41E: newdfa.isra.4 (rege_dfa.c:289) ==21240== by 0x64A4C3: getsubdfa (regexec.c:285) ==21240== by 0x64B7EE: cdissect (regexec.c:671) ==21240== by 0x64B46B: cdissect (regexec.c:633) ==21240== by 0x64B8AA: cdissect (regexec.c:689) ==21240== by 0x64BDE9: cdissect (regexec.c:692) ==21240== by 0x64C802: pg_regexec (regexec.c:473) ==21240== by 0x6EBB3A: RE_wchar_execute (regexp.c:269) ==21240== by 0x6EBDC8: setup_regexp_matches (regexp.c:926) ==21240== by 0x6ECB10: regexp_matches (regexp.c:809) ==21240== by 0x5A641D: ExecMakeFunctionResult (execQual.c:1795) ==21240== ==21240== 16,928 bytes in 1 blocks are definitely lost in loss record 229 of 239 ==21240== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==21240== by 0x64A41E: newdfa.isra.4 (rege_dfa.c:289) ==21240== by 0x64A4C3: getsubdfa (regexec.c:285) ==21240== by 0x64B80A: cdissect (regexec.c:673) ==21240== by 0x64BDE9: cdissect (regexec.c:692) ==21240== by 0x64B46B: cdissect (regexec.c:633) ==21240== by 0x64B8AA: cdissect (regexec.c:689) ==21240== by 0x64BDE9: cdissect (regexec.c:692) ==21240== by 0x64C802: pg_regexec (regexec.c:473) ==21240== by 0x6EBB3A: RE_wchar_execute (regexp.c:269) ==21240== by 0x6EBDC8: setup_regexp_matches (regexp.c:926) ==21240== by 0x6ECB10: regexp_matches (regexp.c:809) ==21240== ==21240== 16,928 bytes in 1 blocks are definitely lost in loss record 230 of 239 ==21240== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==21240== by 0x64A41E: newdfa.isra.4 (rege_dfa.c:289) ==21240== by 0x64A4C3: getsubdfa (regexec.c:285) ==21240== by 0x64B512: cdissect (regexec.c:903) ==21240== by 0x64BAF8: cdissect (regexec.c:1050) ==21240== by 0x64B8AA: cdissect (regexec.c:689) ==21240== by 0x64BDE9: cdissect (regexec.c:692) ==21240== by 0x64B46B: cdissect (regexec.c:633) ==21240== by 0x64B8AA: cdissect (regexec.c:689) ==21240== by 0x64BDE9: cdissect (regexec.c:692) ==21240== by 0x64C802: pg_regexec (regexec.c:473) ==21240== by 0x6EBB3A: RE_wchar_execute (regexp.c:269) ==21240== ==21240== LEAK SUMMARY: ==21240== definitely lost: 69,064 bytes in 12 blocks ==21240== indirectly lost: 38,143 bytes in 102 blocks ==21240== possibly lost: 0 bytes in 0 blocks ==21240== still reachable: 1,317,683 bytes in 342 blocks --strk; On Wed, Mar 19, 2014 at 08:53:59AM +0100, Sandro Santilli wrote: > On Tue, Mar 18, 2014 at 07:41:32PM -0400, Tom Lane wrote: > > Sandro Santilli writes: > > > Tom: I saw you pushed a fix for the interruptability with this: > > > https://github.com/postgres/postgres/commit/f5f21315d25ffcbfe7c6a3fa6ffaad54d31bcde0 > > > > > But we also noticed a memory leak in the regepx_replace call, did you notice > > > that in your tests ? Same regexp as reported. Do you need another testcase ? > > > > I see no memory growth while running your original example against HEAD. > > This is what I'm running: > > SELECT regexp_matches('test', $REG$((?:[^'"$;]+|"[^"]*"|'(?:[^']*|'')*'|(\$[^$]*\$).*?\2)+)$REG$, 'g' ); > > And this is what I see as the process memory: > > PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND > 5359 ? Ss 0:00 0 5693 172446 2908 0.0 postgres: strk strk [local] idle > 5359 ? Ss 0:00 0 5693 172718 4224 0.0 postgres: strk strk [local] idle > 5359 ? Ss 0:00 0 5693 173250 4488 0.0 postgres: strk strk [local] idle > 5359 ? Ss 0:00 0 5693 173670 5016 0.0 postgres: strk strk [local] idle > 5359 ? Ss 0:00 0 5693 174070 5280 0.0 postgres: strk strk [local] idle > 5359 ? Ss 0:00 0 5693 174494 5544 0.0 postgres: strk strk [local] idle > 5359 ? Ss 0:00 0 5693 174762 5808 0.0 postgres: strk strk [local] idle > 5359 ? Ss 0:00 0 5693 175690 6600 0.0 postgres: strk strk [local] idle > 5359 ? Ss 0:00 0 5693 179006 9240 0.0 postgres: strk strk [local] idle > > This is as of commit 63817f86b57fc3d29b57787bca9d786218b7ee25 > > Or, simpler, from this state: > > PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND > 5359 ? Ss 0:00 0 5693 1274962 869832 5.3 postgres: strk strk [local] idle > > I run this: > > SELECT count(*) from ( > select regexp_matches( 'test', $REG$((?:[^'"$;]+|"[^"]*"|'(?:[^']*|'')*'|(\$[^$]*\$).*?\2)+)$REG$, 'g' ) > FROM generate_series(1, 10000) > ) as f; > > And get here: > > PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND > 5359 ? Ss 0:00 0 5693 1274962 869832 5.3 postgres: strk strk [local] idle > 5359 ? Rs 0:00 0 5693 1555894 1090536 6.6 postgres: strk strk [local] SELECT > 5359 ? Ss 0:00 0 5693 2269682 1650744 10.0 postgres: strk strk [local] idle > > The memory is only released on session close. > This is with generate_series going up to 1e5: > > PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND > 5359 ? Ss 0:00 0 5693 2269682 1650744 10.0 postgres: strk strk [local] idle > 5359 ? Rs 0:00 0 5693 2300726 1675560 10.2 postgres: strk strk [local] SELECT > 5359 ? Rs 0:01 0 5693 6438626 4923024 30.0 postgres: strk strk [local] SELECT > 5359 ? Rs 0:02 0 5693 10591778 8182368 50.0 postgres: strk strk [local] SELECT > 5359 ? Ss 0:03 0 5693 12216566 9457488 57.8 postgres: strk strk [local] idle > > Up to 1e6 the story ends with a crash, after: > > PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND > 5359 ? Ss 0:03 0 5693 12216566 9457488 57.8 postgres: strk strk [local] idle > 5359 ? Rs 0:04 0 5693 15666674 12166392 74.3 postgres: strk strk [local] SELECT > 5359 ? Rs 0:05 0 5693 19403254 15099056 92.3 postgres: strk strk [local] SELECT > 5359 ? Rs 0:06 0 5693 20284546 15366412 93.9 postgres: strk strk [local] SELECT > 5359 ? Rs 0:07 1 5693 21665438 15333312 93.7 postgres: strk strk [local] SELECT > 5359 ? Rs 0:08 1 5693 23065426 15295472 93.5 postgres: strk strk [local] SELECT > 5359 ? Rs 0:09 1 5693 24468334 15256592 93.2 postgres: strk strk [local] SELECT > 5359 ? Rs 0:10 1 5693 25869382 15225544 93.0 postgres: strk strk [local] SELECT > 5359 ? Rs 0:11 1 5693 27280514 15210012 92.9 postgres: strk strk [local] SELECT > 5359 ? Rs 0:12 1 5693 28684350 15173964 92.7 postgres: strk strk [local] SELECT > 5359 ? Ds 0:12 1 5693 29683014 15100680 92.3 postgres: strk strk [local] SELECT > 5359 ? Rs 0:13 1 5693 30298130 15051580 92.0 postgres: strk strk [local] SELECT > 5359 ? Rs 0:13 1 5693 30979690 15088992 92.2 postgres: strk strk [local] SELECT > 5359 ? Rs 0:13 1 5693 31758466 15139792 92.5 postgres: strk strk [local] SELECT > 5359 ? Ds 0:14 1 5693 32406206 15111128 92.3 postgres: strk strk [local] SELECT > 5359 ? Ds 0:14 1 5693 33045722 15131676 92.5 postgres: strk strk [local] SELECT > 5359 ? Ds 0:14 1 5693 33479010 15135036 92.5 postgres: strk strk [local] SELECT > 5359 ? Rs 0:15 1 5693 33847174 15146748 92.6 postgres: strk strk [local] SELECT > 5359 ? Rs 0:15 1 5693 34186830 15131648 92.5 postgres: strk strk [local] SELECT > 5359 ? Rs 0:15 1 5693 34339878 15119460 92.4 postgres: strk strk [local] SELECT > 5359 ? Ds 0:15 1 5693 34502474 15139816 92.5 postgres: strk strk [local] SELECT > 5359 ? Ds 0:15 1 5693 34625554 15143068 92.5 postgres: strk strk [local] SELECT > 5359 ? Rs 0:15 1833 0 0 0 0.0 [postgres] > > The connection to the server was lost. Attempting reset: Failed. > !> > > The configure switches: > > ./configure \ > --prefix=/home/postgresql-9.3.4 \ > --with-libxml \ > --with-python \ > --enable-nls \ > --enable-cassert \ > --enable-debug \ > --with-ossp-uuid > > --strk; > > ()  ASCII ribbon campaign -- Keep it simple ! > /\  http://strk.keybit.net/rants/ascii_mails.txt -- ()  ASCII ribbon campaign -- Keep it simple ! /\  http://strk.keybit.net/rants/ascii_mails.txt -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs