Enclosed please find the patch that the libpq support 'prefer-read' feature.
If the target_session_attrs is set to 'prefer-read', the patch will connect to server and send 'SHOW transaction_read_only' query to check the server being 'read-only' or not. If server is 'read-write' then it will try next server address. If all connections for 'read-only' get failed it will try to connect to the master server.