You probably want to write a function (PL/PgSQL would be best) which
returns a row of a type matching the typical union row. If all the
tables are identical, you could just pick one and have the function
return a row of that type. If they are different, and you are selecting
just a subset of fields from each table, then you may need to create a
"dummy table" of the appropriate type.
This is what I did in a similar circumstance; I needed to loop through
all tables matching "draft_[0-9]{5}" (that is, draft_00000 through
draft_99999) and return each row). However, I also needed to "tack on"
an extra column in each row so I could tell what table it came from.
Rather than use the table name, I used the integer corresponding to the
table name's suffix.
create table "all_drafts" ("editor_id" INTEGER) inherits ("draft_template");
create or replace function fn_all_drafts() returns setof all_drafts
as
'declare
t record;
r all_drafts%ROWTYPE;
draft_curs REFCURSOR;
editor_id INTEGER;
begin
for t in select relname from pg_class where relname ~
''^draft_[0-9]{5}$'' loop
editor_id := substring(t.relname from ''[0-9]{5}$'') :: integer;
open draft_curs for execute ''select * from '' ||
quote_ident(t.relname);
loop
fetch draft_curs into r;
exit when not found;
r.editor_id := editor_id;
return next r;
end loop;
close draft_curs;
end loop;
return;
end;' language 'plpgsql';
--
Jeff Boes vox 269.226.9550 ext 24
Database Engineer fax 269.349.9076
Nexcerpt, Inc.
http://www.nexcerpt.com
...Nexcerpt... Extend your Expertise