Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UBQeR-0005AE-Is for pgsql-performance@arkaria.postgresql.org; Fri, 01 Mar 2013 14:09:31 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UBQeQ-0001m2-Ts for pgsql-performance@arkaria.postgresql.org; Fri, 01 Mar 2013 14:09:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UBQeO-0001lb-SM for pgsql-performance@postgresql.org; Fri, 01 Mar 2013 14:09:28 +0000 Received: from mail-lb0-f179.google.com ([209.85.217.179]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UBQeM-0007hL-7A for pgsql-performance@postgresql.org; Fri, 01 Mar 2013 14:09:28 +0000 Received: by mail-lb0-f179.google.com with SMTP id j14so2293719lbo.38 for ; Fri, 01 Mar 2013 06:09:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=C3UcFATNG7i4KkkPlfXNThnky9SlAWeWY139HxGPqLg=; b=VnlP+T9hOcEDZ9VVeNx2wywXUhoklNwXtwgDhXhjIkN5JvklL+TBEHxti5GZHFZYJQ Vovh41nDT+DGDNOXt5Q8H8NNRL9wfeaNYO/Jt6FtZzWU528G+Bl+ljtY37aocGUtWYip zmrghw1hmVfDG3qO514GciG40XDliGUKMBuhWAJSgip8IbvU1pOJlkt/wL9CIC6TivXC EbQx+NhWdaPbh8O8rHc16O2w8i2TyUu1FElTCPIsEcghNW1bx4LTNDuiBnRkr5ubGBwZ ktzpeMPB+5iD1nsdC/mxyVyW79QP3Vyb6R2XmLIizLv5mW45bOUCegv2NKS1pIXID/dT W3mw== MIME-Version: 1.0 X-Received: by 10.152.104.18 with SMTP id ga18mr9254723lab.33.1362146965338; Fri, 01 Mar 2013 06:09:25 -0800 (PST) Received: by 10.112.88.164 with HTTP; Fri, 1 Mar 2013 06:09:25 -0800 (PST) In-Reply-To: <51306444.1000302@esitelsrl.it> References: <512738B3.4070309@esitelsrl.it> <51306444.1000302@esitelsrl.it> Date: Fri, 1 Mar 2013 08:09:25 -0600 Message-ID: Subject: Re: xmlconcat performance From: Merlin Moncure To: Davide Berra Cc: pgsql-performance@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On Fri, Mar 1, 2013 at 2:18 AM, Davide Berra wrote: > Il 28/02/2013 18:48, Merlin Moncure ha scritto: > >> On Fri, Feb 22, 2013 at 3:21 AM, Davide Berra >> wrote: >>> >>> I got a problem with the performance of a PL/PGsql stored procedure >>> outputting an xml. >>> >>> Server version: PostgreSQL 8.3.6 on i686-pc-linux-gnu, compiled by GCC >>> gcc >>> (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46) >>> CPU: Intel(R) Core(TM) i3 CPU 540 @ 3.07GHz >>> RAM installed: 4GB >>> Hard Disk: Seagate 500Gb SATA 2 >>> >>> This is a simplified content of the function showing the xmlconcat >>> behaviour. >>> >>> CREATE OR REPLACE FUNCTION test_function (v_limit int) >>> RETURNS xml AS >>> $BODY$ >>> DECLARE >>> v_xml xml; >>> BEGIN >>> >>> FOR i IN 1..v_limit LOOP >>> v_xml := xmlconcat(v_xml, xmlelement(name content, 'aaaaaaa')); >>> END LOOP; >>> >>> RETURN v_xml ; >>> END >>> $BODY$ >>> LANGUAGE 'plpgsql' SECURITY DEFINER ; >>> >>> >>> As long as the v_limit parameter grows (and then the size of the output >>> xml, >>> the time needed increase exponentially. >>> Look at this examples: >>> >>> pang=# explain analyze select test_function(1000); >>> QUERY PLAN >>> >>> -------------------------------------------------------------------------------------- >>> Result (cost=0.00..0.26 rows=1 width=0) (actual time=65.430..65.431 >>> rows=1 >>> loops=1) >>> Total runtime: 65.457 ms >>> (2 rows) >>> >>> pang=# explain analyze select test_function(5000); >>> QUERY PLAN >>> >>> ---------------------------------------------------------------------------------------- >>> Result (cost=0.00..0.26 rows=1 width=0) (actual time=473.318..473.318 >>> rows=1 loops=1) >>> Total runtime: 473.340 ms >>> (2 rows) >>> >>> pang=# explain analyze select test_function(15000); >>> QUERY PLAN >>> >>> ------------------------------------------------------------------------------------------ >>> Result (cost=0.00..0.26 rows=1 width=0) (actual >>> time=4044.903..4044.904 >>> rows=1 loops=1) >>> Total runtime: 4044.928 ms >>> (2 rows) >>> >>> pang=# explain analyze select test_function(50000); >>> QUERY PLAN >>> >>> -------------------------------------------------------------------------------------------- >>> Result (cost=0.00..0.26 rows=1 width=0) (actual >>> time=94994.337..94994.369 >>> rows=1 loops=1) >>> Total runtime: 94994.396 ms >>> (2 rows) >>> >>> I already tried to update to 8.3.23 service version but i didn't see any >>> improvement. >>> >>> Do you have any suggestion about how to increase the performance of >>> xmlconcat? >>> >>> My need is to use stored procedures that calls xmlconcat more than 50000 >>> times, but it is unacceptable 94 seconds to complete the job. >>> >>> Thanks in advance >> >> typically for high performance string manipulation you have to do >> things on more purely textual level and manipulate through arrays to >> get really good performance. iterative string concatenation is >> typically wrong approach -- you have to think in set terms. >> >> also your database version is obsolete -- time to start thinking about >> upgrade. >> >> merlin >> > Thank you for the reply Merlin but i don't fully get what you mean. (sorry, > i'm not a PostgreSQL expert) > How would you change the above example function in order to improve > performance? > What do you mean with "manipulate through arrays"? well arrays, or simple aggregation. for example: select string_agg(v, '') from (select 'aaaaaaa'::text as v from generate_series(1,50000)) q; runs in ~ 30 ms. merlin -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance