Idle (pooled) PostGIS connections remain in transactions, and although all queries are readonly, some DDL queries like ALTER TABLE to hang indefinitely while the in-transaction connections are active. Probably having something to do with a transaction's guarantee of presenting the user with a consistent view of the database.
http://lists.osgeo.org/pipermail/mapserver-users/2008-May/055977.html
The proposed patch enters a transaction before the DECLARE BINARY CURSOR call and issues ROLLBACK after the FETCH ALL resultset is processed. So idle connections are no longer in a transaction.
It passed my basic tests. I made some rather heavy changes to the end of msPOSTGISLayerGetShape, collapsing two mutually exclusive CLOSE [cursor] call paths into a single one. The previous code paths were somewhat hard to follow. I hope I preserved the logic correctly.
I'm pretty sure I corrected a bug where CLOSE [cursor] was not being called when in msPOSTGISLayerGetShape, shape->type == MS_SHAPE_NULL.