Factor out advisory locking code into advisory_lock()

Also, some further small fixes of error messages, comments,
marking dubious kludges (e.g. strchr() to determine schema name).