Thanks @ScottR . I had tried something similar before but found out it doesn't work with Postgres due to converting the query to a prepared statement.
There is a setting that can be disabled but I was a bit loathe to do that as that allows for sql injection.
See Problem with SQL query and parameters - #2 by ScottR and '{{}}' cannot be used when using the DO-END statement in SQL
Not sure where is any other way though?