Simon,
p_object_type and p_context_id both have the "alias for $9;" attached to them. That may be it...
Generally, one of the best ways to look for clues when debugging pl[pg]sql is to have an emacs shell ( M-x shell - https://openacs.org/education/psets/advice#Emacs ) running on the side, where you have done a "tail -f your-aolserver-error-log", and look at the error message generated as you request the page with the offending statement - and a couple of rows before the error is often helpful.
Increasing the level of verbosity of the debug messages in the error log by manipulating postgresql.conf is sometimes necessary, but usually not, in my experience.
HTH.