Performance optimizations

This provides some minor optimization tweaks, fixes a few tests so they can run properly in isolation, and makes a minor fix to the pg instrumentation to support pg 4.x.