Thanks for your assistance guys, and special thanks to Greg for his blog page. This is exactly my problem which is resolved using dynamic SQL. I can now see how the function was behaving badly, and have been able to fix the query. Once again, thanks guys - and Happy New Year from a snow-covered part of the UK. chris -- Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general