Query: SELECT xmlelement(name s_a, xmlagg( xmlelement(name s_b, xmlattributes( sq.id AS i ), (SELECT xmlagg( xmlelement(name s_c, xmlattributes( su.id AS i ) ) ORDER BY table_id,id ) FROM __table_to_table AS su WHERE su.table_id=sq.id ) ) ) ) FROM __table AS sq
No result given in: "PostgreSQL 9.2.3 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-52), 64-bit" A result is given in: "PostgreSQL 9.2.1, compiled by Visual C++ build 1600, 32-bit" Both installations tested with same DB.