Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id F343B632D64 for ; Fri, 12 Feb 2010 08:57:01 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 21379-07 for ; Fri, 12 Feb 2010 12:56:26 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-bw0-f212.google.com (mail-bw0-f212.google.com [209.85.218.212]) by mail.postgresql.org (Postfix) with ESMTP id AE816632C31 for ; Fri, 12 Feb 2010 08:56:50 -0400 (AST) Received: by bwz4 with SMTP id 4so2001127bwz.22 for ; Fri, 12 Feb 2010 04:56:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:from:date:message-id :subject:to:content-type; bh=H2HUaknBmt9XmJm+NpbXr/iZwikJAPZiIcrRSOxr1qs=; b=NvgniljLlwTtd3RyVnvUOWwtJ2Pl/OYYmUudse0g8i0JJnYOqDFP8cvDhEYP3jBxOm ZE8V7JNxcnWwfZgEctoFVdb8z2K2clvhOK0JdgWAbc/SzcZ1H1aaT3Szxs/SCzxg49I2 +mI15Ces176NULWEIxAClDekbFOmqvqEeBbxs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:from:date:message-id:subject:to:content-type; b=ARkHrQ53St4P1qWIuL5wvur5sEoXykvUYo7rQ2OAkxgxvzJsWgSmY33Dmq7yNTozHb 90AFDIw3O7zgpoGiuN4pWqZEngvb+04ap9LyXcfSz8uP2DZ2w6rKPFjkL0NU6t6bZaba JhV9ENls4JNR5xSwI/gl1t27vF63700SjjP0U= MIME-Version: 1.0 Received: by 10.204.18.212 with SMTP id x20mr877952bka.9.1265979408717; Fri, 12 Feb 2010 04:56:48 -0800 (PST) From: Luiz Angelo Daros de Luca Date: Fri, 12 Feb 2010 10:54:58 -0200 Message-ID: <8b96e3d21002120454q77b1d72eq38a91b7f486e8f29@mail.gmail.com> Subject: Immutable table functions To: pgsql-performance@postgresql.org Content-Type: multipart/alternative; boundary=000325557d76f3198b047f66cc59 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.598 tagged_above=-10 required=5 tests=BAYES_00=-2.599, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201002/196 X-Sequence-Number: 37617 --000325557d76f3198b047f66cc59 Content-Type: text/plain; charset=ISO-8859-1 Hello guys, I don't know if this is the correct list. Correct me if I'm wrong. I have a directed graph, or better, a tree in postgresql 8.3. One table are the nodes and another one are the connections. Given any node, I need to get all nodes down to it(to the leafs) that have relation with anotherTable. Also, this connection change on time, so I have a connectionDate and a disconnectionDate for each connection (which might be null to represent open interval). This way, I wrote a pgsql function (I rename the tables and columns to generic names). These are the tables and the function: CREATE TABLE node ( id_node integer NOT NULL, CONSTRAINT node_pkey PRIMARY KEY (id_node) ); CREATE TABLE anotherTable ( id_anotherTable integer NOT NULL, id_node integer NOT NULL, CONSTRAINT anothertable_pkey PRIMARY KEY (id_anotherTable), CONSTRAINT anothertable_node_fkey FOREIGN KEY (id_node) REFERENCES node. id_node MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ); CREATE TABLE connection ( id_connection integer NOT NULL, down integer NOT NULL, up integer NOT NULL, connectionDate timestamp with time zone, disconnectionDate timestamp with time zone, CONSTRAINT connection_pkey PRIMARY KEY (id_connection), CONSTRAINT down_fkey FOREIGN KEY (down) REFERENCES (id_node) REFERENCES node. id_node MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT up_fkey FOREIGN KEY (up) REFERENCES (id_node) REFERENCES node. id_node MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION); CREATE TABLE observation ( id_observation integer NOT NULL, id_node integer NOT NULL, date timestamp with time zone, CONSTRAINT observation_pkey PRIMARY KEY (id_observation), CONSTRAINT observation_node_fkey FOREIGN KEY (id_node) REFERENCES node. id_node MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ); CREATE OR REPLACE FUNCTION get_nodes_related_to_anothertable(integer,timestamp with time zone) RETURNS SETOF integer AS 'DECLARE _id ALIAS FOR $1; _date ALIAS FOR $2; _conn RECORD; BEGIN return query SELECT 1 FROM anothertable WHERE id_node = _id; FOR _ conn IN SELECT * FROM connection c where c.up = _id LOOP if _conn. connectionDate > _date then continue; end if; if _conn. disconnectionDate < _data then continue; end if; return query SELECT * from get_nodes_related_to_anothertable(_conn.down, _date); END LOOP; END' LANGUAGE 'plpgsql' IMMUTABLE; And I use it on my SELECT: SELECT * FROM (SELECT id_node, date FROM observation ) root_node_obs, node, anotherTable WHERE anotherTable.id_node = node.id_node AND node.id_node IN ( select * from get_nodes_related_to_anothertable(root_node_obs .id_node,root_node_obs .date)); Even with IMMUTABLE on the function, postgresql executes the function many times with the same parameters. In a single run: select * from get_nodes_related_to_anothertable(236,now()); it returns 5 rows and runs in 27ms. But in the bigger SELECT, it take 5s to each observation row (and I may have some :-) ). I know that IN generally is not optimization-friendly but I don't know how to use my function without it. Any clues guys? Thanks, --000325557d76f3198b047f66cc59 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

Hello guys,


I don't know if this is the correct list. Correct me if I'm wrong.

I have a directed graph, or better, a tree in postgresql 8.3. One table are the nodes and another one are the connections. Given any node, I need to get all nodes down to it(to the leafs) that have relation with anotherTable. Also, this connection change on time, so I have a connectionDate and a disconnectionDate for each connection (which might be null to represent open interval). This way, I wrote a pgsql function (I rename the tables and columns to generic names). These are the tables and the function:


CREATE TABLE node (

id_node integer NOT NULL,

CONSTRAINT node_pkey PRIMARY KEY (id_node)

);

CREATE TABLE anotherTable

(

=20 id_anotherTable integer NOT NULL,

id_node integer NOT NULL,

=20 CONSTRAINT anothertable_pkey PRIMARY KEY (id_anotherTable),

=20 CONSTRAINT anothertable_node_fkey FOREIGN KEY (id_node) REFERENCES node. id_node

=20 MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION

);

CREATE TABLE connection

(

=20 id_connection integer NOT NULL,

down integer NOT NULL,

up integer NOT NULL,

=20 connectionDate timestamp with time zone,

=20 disconnectionDate timestamp with time zone,

=20 CONSTRAINT connection_pkey PRIMARY KEY (id_connection),

=20 CONSTRAINT down_fkey FOREIGN KEY (down)

=20 REFERENCES (id_node) REFERENCES node. id_node MATCH SIMPLE

ON UPDATE NO ACTION ON DELETE NO ACTION,

CONSTRAINT up_fkey FOREIGN KEY (up)

=20 REFERENCES (id_node) REFERENCES node. id_node MATCH SIMPLE

ON UPDATE NO ACTION ON DELETE NO ACTION);

CREATE TABLE observation

(

=20 id_observation integer NOT NULL,

id_node integer NOT NULL,

date timestamp with time zone,

=20 CONSTRAINT observation_pkey PRIMARY KEY (id_observation),

=20 CONSTRAINT observation_node_fkey FOREIGN KEY (id_node) REFERENCES node. id_node

=20 MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION

);


CREATE OR REPLACE FUNCTION get_nodes_related_to_anothertable(integer,timestamp with time zone) RETURNS SETOF integer AS 'DECLARE
=A0=A0=A0 _id ALIAS FOR $1;
=A0=A0=A0 _date ALIAS FOR $2;
=A0=A0=A0 _conn RECORD;
BEGIN
=A0=A0=A0 return query SELECT 1 FROM anothertable WHERE id_node =3D _id;
=A0=A0=A0 FOR _ conn IN SELECT * FROM connection c where c.up =3D _id LOOP
=A0=A0=A0 =A0=A0=A0 if _conn. connectionDate > _date then
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 continue;
=A0=A0=A0 =A0=A0=A0 end if;
=A0=A0=A0 =A0=A0=A0 if _conn. disconnectionDate < _data then
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 continue;
=A0=A0=A0 =A0=A0=A0 end if;
=A0=A0=A0 =A0=A0=A0 return query SELECT * from get_nodes_related_to_anothertable(_conn.down, _date);
=A0=A0=A0 END LOOP;
END' LANGUAGE 'plpgsql' IMMUTABLE;


And I use it on my SELECT:


SELECT

*

FROM

(SELECT

id_node,

date

FROM

observation

) root_node_obs,

node,

anotherTable

WHERE

anotherTable.id_node =3D node.id_node

AND

node.id_node IN (

select * from get_nodes_related_to_anothertable(root_node_obs .id_node,root_node_obs .date));


Even with IMMUTABLE on the function, postgresql executes the function many times with the same parameters. In a single run:


select * from get_nodes_related_to_anothertable(236,now());


it returns 5 rows and runs in 27ms. But in the bigger SELECT, it take 5s to each observation row (and I may have some :-) ).


I know that IN generally is not optimization-friendly but I don't know how to use my function without it.

Any clues guys?


Thanks,



--000325557d76f3198b047f66cc59--