I have a table in PostgreSQL which consists of 2 columns, first ID and second ID. Every entry in it means that there's a relation between first and second ID, and it can be guaranteed that first ID will always be larger than the second ID.
My goal is to process the table so that it can detect networks (multiple IDs that relates to one another), and change every relations of that networks in the table so that first ID is the large IDs in the network and the second ID is always the smallest ID in the network.
Example:
D->C , C->B , B->A , F->E , H->G
Will become:
D->A , C->A , B->A , F->E , H->G
Another example:
D->C , D->B , D->A
Will become:
D->A , C->A , B->A
How to do this using SQL or a Postgres procedural language?
Edit : The PostgreSQL version that i'm using is 9.4. The table consists of column id1 (integer) and id2 (integer), with both of them as primary key.
As to how to conclude that A is the smallest in the set of second example (A,B,C), I used this query to determine the smallest id2
SELECT id1, MIN(id2) FROM table GROUP BY id1