Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id E437B632741 for ; Fri, 3 Sep 2010 07:58:21 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 34063-05 for ; Fri, 3 Sep 2010 10:58:13 +0000 (UTC) X-Greylist: delayed 00:07:15.398033 by SQLgrey-1.7.6 Received: from mail-gw0-f46.google.com (mail-gw0-f46.google.com [74.125.83.46]) by mail.postgresql.org (Postfix) with ESMTP id 8F2086318F3 for ; Fri, 3 Sep 2010 07:58:14 -0300 (ADT) Received: by gwj17 with SMTP id 17so589552gwj.19 for ; Fri, 03 Sep 2010 03:58:13 -0700 (PDT) Received: by 10.100.207.7 with SMTP id e7mr706074ang.52.1283511056810; Fri, 03 Sep 2010 03:50:56 -0700 (PDT) Received: from [192.168.0.110] (ppp246-215.static.internode.on.net [203.122.246.215]) by mx.google.com with ESMTPS id k11sm2503787ani.30.2010.09.03.03.50.55 (version=SSLv3 cipher=RC4-MD5); Fri, 03 Sep 2010 03:50:56 -0700 (PDT) Subject: procedure help between databases From: Daniel Sobey To: pgsql-sql@postgresql.org Content-Type: text/plain; charset="UTF-8" Date: Fri, 03 Sep 2010 20:19:46 +0930 Message-ID: <1283510986.14678.35.camel@ubuntu-laptop.BlueCrystal.local> Mime-Version: 1.0 X-Mailer: Evolution 2.28.3 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201009/9 X-Sequence-Number: 34540 Hello list, I need some help in writing a trigger / procedure. I have two databases, one for last.fm and one for musicbrainz. What I would like to do is when i submit a song into last.fm, connect to the musicbrainz and get an id for the song. Both are using postgres and i can use a db link to query from one to the other. The query I need to run on the musicbrainz database is: select track.name, artist.name,album.name,album.gid,track.gid,artist.gid from track,artist,albumjoin,album where artist.id = track.artist and albumjoin.track = track.id and albumjoin.album=album.id and artist.name='The Beatles' and track.name='Yellow Submarine' and album.name='Revolver'; I can then perform the following query that uses the database link: select * from dblink ('dbname=musicbrainz queries_db','select track.name, artist.name,album.name,album.gid,track.gid,artist.gid from track,artist,albumjoin,album where artist.id = track.artist and albumjoin.track = track.id and albumjoin.album=album.id and artist.name=''The Beatles'' and track.name=''Yellow Submarine'' and album.name=''Revolver'';') as t1(track character varying(255),artist character varying(255),album character varying(255), trackid character(36),artistid character(36),albumid character(36)); Now i want to turn this query into a trigger so when i insert into a table in the libre.fm database it performs the above query and inserts it into some tables in the libre.fm database. What i have so far is as below but i am not sure the best way to call a function and then insert the results in a table. If anyone could point me to some examples i would appreciate it. create or replace function mb_lookup(varchar,varchar,varchar) returns integer as $$ declare track alias for $1; artist alias for $2; album alias for $3; abc record; begin for abc in select * from dblink ('dbname=musicbrainz_db','select track.name, artist.name,album.name,album.gid,track.gid,artist.gid from track,artist,albumjoin,album where artist.id = track.artist and albumjoin.track = track.id and albumjoin.album=album.id and artist.name=artist and track.name=track and album.name=album;') as t1(track character varying(255),artist character varying(255),album character varying(255), trackid character(36),artistid character(36),albumid character(36)) LOOP END LOOP; return 1; end; $$ language 'plpgsql';