Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TTDRC-0005SD-Eh for pgsql-sql@postgresql.org; Tue, 30 Oct 2012 15:09:06 +0000 Received: from nm9.bullet.mail.ac4.yahoo.com ([98.139.52.206]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TTDR9-0004AC-HO for pgsql-sql@postgresql.org; Tue, 30 Oct 2012 15:09:05 +0000 Received: from [98.139.52.197] by nm9.bullet.mail.ac4.yahoo.com with NNFMP; 30 Oct 2012 15:09:02 -0000 Received: from [76.13.13.46] by tm10.bullet.mail.ac4.yahoo.com with NNFMP; 30 Oct 2012 15:09:02 -0000 Received: from [127.0.0.1] by smtp107.prem.mail.ac4.yahoo.com with NNFMP; 30 Oct 2012 15:09:02 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1351609742; bh=C+XyrnbK1bDS6NlRMa3zyffNPNvS4WPf+VHqsicsrS0=; h=X-Yahoo-Newman-Id:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=OcLWOiRH4k0xvmt8ri1rUf9x2SreIOKucXxPA6zPsXKLFJrPW1J4IwK0lCX4hIhFC4WPsrf64lNORnvIfMawFes+a8MjyI8AJrGDzoFSFg4B2+c/Z8AaeK7yaQRUN8b9N7uTH7P4uWY0ODj4DsJyVId9nJESgbRkrRI6N8CKVSA= X-Yahoo-Newman-Id: 799020.32822.bm@smtp107.prem.mail.ac4.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: 1MjaNbkVM1meVaM4Wmso.Y8d6XjgkItS.n2Ny6K36cfrmkd TiVGyMJgkh7R76omglGxFb5Rji_x4qAT5iaAr2R3CXOY6SK26USvMxpj1WQr p.cAn1iBuNaS1V8owfenqxhCXCOKXcaTDiuFTXpS.SDkR8K8X28GTY7YlsQj 2hPLp3iOqAMM4t6ikYZWFhNmkSr.n7ln7te7kC.HLVCIpfwhHvD..SC_zfhY WG.HJGEwxwbE70wgpnB3SEW00XG28P49c3k3AsqqOudwK_1tpSacy_DDCBzJ SPxd.ReNz6OVhRj2MZEzyR2xpDtbfecurcWB5Q7iyxtUuFvFIIdX4KwDmyYd FCcZv_ZBIoNR9y4vUCtrFWN91iO4yIbA.Z4tfBB9qXzDGnxtrfCrcNpVBXAC wEgcd_cvVXyZdJZGkN4aMtvEkGTSxRAWYTGMThvq0zW3IqJ0A7q6stq1ipXo _ki3X X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp107.prem.mail.ac4.yahoo.com with SMTP; 30 Oct 2012 08:09:02 -0700 PDT From: "David Johnston" To: "'jan zimmek'" , References: <47098D72-27B8-4634-8010-066B4765FE91@web.de> In-Reply-To: <47098D72-27B8-4634-8010-066B4765FE91@web.de> Subject: Re: replace text occurrences loaded from table Date: Tue, 30 Oct 2012 11:08:46 -0400 Message-ID: <00b001cdb6b0$75c2c350$614849f0$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQJsxXgZyF3EqAsIWw6Ph0L/2iAXSpaT0dfg Content-Language: en-us X-Pg-Spam-Score: -0.6 (/) X-Archive-Number: 201210/61 X-Sequence-Number: 36932 > -----Original Message----- > From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql- > owner@postgresql.org] On Behalf Of jan zimmek > Sent: Tuesday, October 30, 2012 7:45 AM > To: pgsql-sql@postgresql.org > Subject: [SQL] replace text occurrences loaded from table > > hello, > > i am actually trying to replace all occurences in a text column with some > value, but the occurrences to replace are defined in a table. this is a > simplified version of my schema: > > create temporary table tmp_vars as select var from > (values('ABC'),('XYZ'),('VAR123')) entries (var); create temporary table > tmp_messages as select message from (values('my ABC is XYZ'),('the XYZ is > very VAR123')) messages (message); > > select * from tmp_messages; > > my ABC is XYZ -- row 1 > the XYZ is very VAR123 -- row 2 > > now i need to somehow update the rows in tmp_messages, so that after the > update i get the following: > > select * from tmp_messages; > > my XXX is XXX -- row 1 > the XXX is very XXX -- row 2 > > i have implemented a solution in plpgsql by doing a nested for-loop over > tmp_vars and tmp_messages, but i would like to know if there is a more > efficient way to solve this problem ? > You may want to consider creating an alternating regular expression and using "regexp_replace(...)" one time per message instead of "replace(...)" three times Not Tested: regexp_replace(message, 'ABC|XYZ|VAR123', 'XXX', 'g') This should at least reduce the amount of overhead checking each expression against each message would incur. If you need even better performance you would need to find some way to "index" the message contents so that for each expression the index can be used to quickly identify the subset of messages that are going to be altered. The full-text-search capabilities of PostgreSQL will probably help here though I am not familiar with them personally. Since you have not shared the true context of your request no alternatives can be suggested. Also, your ability to implement certain algorithms is influenced by the version of PostgreSQL that you are running and which you have also not provided. David J.