I have a table that looks basically like this:

```
id | redirectid | data

```

where the redirectid is an id to another row. Basically if a row is selected, and it has a redirectid, then the redirectid data should be used in it's place. There may be multiple redirects until redirectid is NULL. Essentially these redirects form a linked list in the table. What I'd like to know is, given an id, is it possible to set up a sql query that will iterate through all the possible redirects and return the id at the end of the "list"?

This is using Postgresql 8.3 and I'd like to do everything in on sql query if possible (rather than iterate in my code).