Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id CCD557E7FA9 for ; Sun, 11 Dec 2011 20:46:05 -0400 (AST) Received: from mail.anarazel.de ([217.115.131.40]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RZu1r-0001SH-51 for pgsql-bugs@postgresql.org; Mon, 12 Dec 2011 00:46:04 +0000 Received: by mail.anarazel.de (Postfix, from userid 108) id 8D65D448007; Mon, 12 Dec 2011 01:45:50 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on mail X-Spam-Level: X-Spam-Status: No, score=-4.4 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=ham version=3.2.5 Received: from anarazel.de (e178247048.adsl.alicedsl.de [85.178.247.48]) (Authenticated sender: andres@anarazel.de) by mail.anarazel.de (Postfix) with ESMTPSA id 2653F448007; Mon, 12 Dec 2011 01:45:47 +0100 (CET) Received: by anarazel.de (Postfix, from userid 1000) id E4AF71C004D; Mon, 12 Dec 2011 01:45:43 +0100 (CET) From: Andres Freund To: pgsql-bugs@postgresql.org, Jochen Erwied Subject: Re: Postgresql 9.1.2 - abnormal memory usage Date: Mon, 12 Dec 2011 01:45:42 +0100 User-Agent: KMail/1.13.7 (Linux/3.2.0-rc5-andres; KDE/4.6.5; x86_64; ; ) References: <396904450.20111211221529@erwied.eu> <201112120045.23211.andres@anarazel.de> In-Reply-To: <201112120045.23211.andres@anarazel.de> MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_26U5OjyJjXHXJHh" Message-Id: <201112120145.42463.andres@anarazel.de> X-Pg-Spam-Score: -4.1 (----) X-Archive-Number: 201112/73 X-Sequence-Number: 31884 --Boundary-00=_26U5OjyJjXHXJHh Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hi, On Monday, December 12, 2011 12:45:23 AM Andres Freund wrote: > On Sunday, December 11, 2011 10:15:29 PM Jochen Erwied wrote: > > The following script can be used to show the behaviour: > > > > create database pgtest; > > \c pgtest > > create table test(var inet); > > insert into test(var) '127.0.0.0'::inet+generate_series(0,256*256*256-1); > > create index test_ix on test(var); > > I can reproduce the issue. The memory is being spent at only one place: > > TopMemoryContext: 68688 total in 10 blocks; 2736 free (10 chunks); 65952 > used TopTransactionContext: 8192 total in 1 blocks; 4856 free (0 chunks); > 3336 used > PortalMemory: 8192 total in 1 blocks; 7888 free (0 chunks); 304 used > PortalHeapMemory: 15360 total in 4 blocks; 10640 free (17 chunks); 4720 > used > TupleSort: 6283429096 total in 723 blocks; 1424704 free (0 chunks); > 6282004392 used In 3b8161723c645853021b57330dd2ea0484ec6131 Heikki made DatumGetInetP unpack toasted values. Unfortunately the btree support functions for the inet type didn't free memory which they have to do in contrast to about everything else. I fixed a few more functions than strictly necessary but I guess thats ok. Jochen, could you try the attached patch? Andres --Boundary-00=_26U5OjyJjXHXJHh Content-Type: text/x-patch; charset="UTF-8"; name="0001-Release-memory-in-inet-functions.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="0001-Release-memory-in-inet-functions.patch" =46rom b641f68a96138162935756ed708367e2f162f0a2 Mon Sep 17 00:00:00 2001 =46rom: Andres Freund Date: Mon, 12 Dec 2011 01:38:42 +0100 Subject: [PATCH] Release memory in inet functions This is now neccessary because since a recent commit ("Make DatumGetInetP() unpack inet datums") those functions actually allocate memory. Thats not allowed in btree support functions. =2D-- src/backend/utils/adt/network.c | 77 +++++++++++++++++++++++++++++------= =2D--- 1 files changed, 58 insertions(+), 19 deletions(-) diff --git a/src/backend/utils/adt/network.c b/src/backend/utils/adt/networ= k.c index e929d6b..7ee2d2b 100644 =2D-- a/src/backend/utils/adt/network.c +++ b/src/backend/utils/adt/network.c @@ -460,7 +460,11 @@ network_cmp(PG_FUNCTION_ARGS) inet *a1 =3D PG_GETARG_INET_P(0); inet *a2 =3D PG_GETARG_INET_P(1); =20 =2D PG_RETURN_INT32(network_cmp_internal(a1, a2)); + int32 r =3D network_cmp_internal(a1, a2); + + PG_FREE_IF_COPY(a1, 0); + PG_FREE_IF_COPY(a2, 1); + PG_RETURN_INT32(r); } =20 /* @@ -472,7 +476,11 @@ network_lt(PG_FUNCTION_ARGS) inet *a1 =3D PG_GETARG_INET_P(0); inet *a2 =3D PG_GETARG_INET_P(1); =20 =2D PG_RETURN_BOOL(network_cmp_internal(a1, a2) < 0); + bool r =3D network_cmp_internal(a1, a2) < 0; + + PG_FREE_IF_COPY(a1, 0); + PG_FREE_IF_COPY(a2, 1); + PG_RETURN_BOOL(r); } =20 Datum @@ -481,7 +489,11 @@ network_le(PG_FUNCTION_ARGS) inet *a1 =3D PG_GETARG_INET_P(0); inet *a2 =3D PG_GETARG_INET_P(1); =20 =2D PG_RETURN_BOOL(network_cmp_internal(a1, a2) <=3D 0); + bool r =3D network_cmp_internal(a1, a2) <=3D 0; + + PG_FREE_IF_COPY(a1, 0); + PG_FREE_IF_COPY(a2, 1); + PG_RETURN_BOOL(r); } =20 Datum @@ -490,7 +502,11 @@ network_eq(PG_FUNCTION_ARGS) inet *a1 =3D PG_GETARG_INET_P(0); inet *a2 =3D PG_GETARG_INET_P(1); =20 =2D PG_RETURN_BOOL(network_cmp_internal(a1, a2) =3D=3D 0); + bool r =3D network_cmp_internal(a1, a2) =3D=3D 0; + + PG_FREE_IF_COPY(a1, 0); + PG_FREE_IF_COPY(a2, 1); + PG_RETURN_BOOL(r); } =20 Datum @@ -499,7 +515,11 @@ network_ge(PG_FUNCTION_ARGS) inet *a1 =3D PG_GETARG_INET_P(0); inet *a2 =3D PG_GETARG_INET_P(1); =20 =2D PG_RETURN_BOOL(network_cmp_internal(a1, a2) >=3D 0); + bool r =3D network_cmp_internal(a1, a2) >=3D 0; + + PG_FREE_IF_COPY(a1, 0); + PG_FREE_IF_COPY(a2, 1); + PG_RETURN_BOOL(r); } =20 Datum @@ -508,7 +528,11 @@ network_gt(PG_FUNCTION_ARGS) inet *a1 =3D PG_GETARG_INET_P(0); inet *a2 =3D PG_GETARG_INET_P(1); =20 =2D PG_RETURN_BOOL(network_cmp_internal(a1, a2) > 0); + bool r =3D network_cmp_internal(a1, a2) > 0; + + PG_FREE_IF_COPY(a1, 0); + PG_FREE_IF_COPY(a2, 1); + PG_RETURN_BOOL(r); } =20 Datum @@ -517,7 +541,11 @@ network_ne(PG_FUNCTION_ARGS) inet *a1 =3D PG_GETARG_INET_P(0); inet *a2 =3D PG_GETARG_INET_P(1); =20 =2D PG_RETURN_BOOL(network_cmp_internal(a1, a2) !=3D 0); + bool r =3D network_cmp_internal(a1, a2) !=3D 0; + + PG_FREE_IF_COPY(a1, 0); + PG_FREE_IF_COPY(a2, 1); + PG_RETURN_BOOL(r); } =20 /* @@ -541,14 +569,17 @@ network_sub(PG_FUNCTION_ARGS) { inet *a1 =3D PG_GETARG_INET_P(0); inet *a2 =3D PG_GETARG_INET_P(1); + bool r =3D false; =20 if (ip_family(a1) =3D=3D ip_family(a2)) { =2D PG_RETURN_BOOL(ip_bits(a1) > ip_bits(a2) =2D && bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) =3D=3D 0); + r =3D ip_bits(a1) > ip_bits(a2) + && bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) =3D=3D 0; } =20 =2D PG_RETURN_BOOL(false); + PG_FREE_IF_COPY(a1, 0); + PG_FREE_IF_COPY(a2, 1); + PG_RETURN_BOOL(r); } =20 Datum @@ -556,14 +587,17 @@ network_subeq(PG_FUNCTION_ARGS) { inet *a1 =3D PG_GETARG_INET_P(0); inet *a2 =3D PG_GETARG_INET_P(1); + bool r =3D false; =20 if (ip_family(a1) =3D=3D ip_family(a2)) { =2D PG_RETURN_BOOL(ip_bits(a1) >=3D ip_bits(a2) =2D && bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) =3D=3D 0); + r =3D ip_bits(a1) >=3D ip_bits(a2) + && bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) =3D=3D 0; } =20 =2D PG_RETURN_BOOL(false); + PG_FREE_IF_COPY(a1, 0); + PG_FREE_IF_COPY(a2, 1); + PG_RETURN_BOOL(r); } =20 Datum @@ -574,11 +608,13 @@ network_sup(PG_FUNCTION_ARGS) =20 if (ip_family(a1) =3D=3D ip_family(a2)) { =2D PG_RETURN_BOOL(ip_bits(a1) < ip_bits(a2) =2D && bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) =3D=3D 0); + r =3D ip_bits(a1) < ip_bits(a2) + && bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) =3D=3D 0; } =20 =2D PG_RETURN_BOOL(false); + PG_FREE_IF_COPY(a1, 0); + PG_FREE_IF_COPY(a2, 1); + PG_RETURN_BOOL(r); } =20 Datum @@ -586,14 +622,17 @@ network_supeq(PG_FUNCTION_ARGS) { inet *a1 =3D PG_GETARG_INET_P(0); inet *a2 =3D PG_GETARG_INET_P(1); + bool r =3D false; =20 if (ip_family(a1) =3D=3D ip_family(a2)) { =2D PG_RETURN_BOOL(ip_bits(a1) <=3D ip_bits(a2) =2D && bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) =3D=3D 0); + r =3D ip_bits(a1) <=3D ip_bits(a2) + && bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) =3D=3D 0; } =20 =2D PG_RETURN_BOOL(false); + PG_FREE_IF_COPY(a1, 0); + PG_FREE_IF_COPY(a2, 1); + PG_RETURN_BOOL(r); } =20 /* =2D-=20 1.7.6.409.ge7a85.dirty --Boundary-00=_26U5OjyJjXHXJHh--