I have a directed graph in a Postgres database defined with these relations:
CREATE TABLE node (
id int4 NOT NULL,
"name" varchar NULL,
CONSTRAINT pk_node PRIMARY KEY (id),
CONSTRAINT unq_node_name UNIQUE ("name"),
);
CREATE TABLE link (
id int4 NOT NULL,
"name" varchar NULL,
id_node_from int4 NULL,
id_node_to int4 NULL,
CONSTRAINT pk_link PRIMARY KEY (id),
CONSTRAINT unq_link_name UNIQUE ("name"),
CONSTRAINT fk_link_node_from FOREIGN KEY (id_node_from) REFERENCES node(id),
CONSTRAINT fk_link_node_to FOREIGN KEY (id_node_to) REFERENCES node(id)
);
Given a node n, I would like to obtain the set of nodes from which it is possible to reach n traversing the directed graph.
How can it be made with a single SQL query?