I had a client caught by this today, and I've been caught by it a few times myself. If you construct dynamic SQL for use in PLPGSQL then you will usually want to use quote_literal() to quote the value properly in the SQL. But quote_literal() is a STRICT function, so if its argument is NULL then so is its result. This can make your whole dynamic SQL string NULL, resulting in your getting errors like:
The way I get around the problem is to wrap the call to quote_literal() in a call to coalesce() if the value being quoted might be NULL, like this:
ERROR: query string argument of EXECUTE is null
The way I get around the problem is to wrap the call to quote_literal() in a call to coalesce() if the value being quoted might be NULL, like this:
coalesce(quote_literal(your_variable),'NULL')