hi,
after having ripped my hair out for months over this &*$#! "1 is not a valid PostgreSQL link resource" bug, I have found a solution which I want to share with the community. Hope this helps someone out of future desperation. 😉
The problem is that, sometimes, when various subsequent connections are made by a script, suddenly and reasonless, one of the connections is of type "Unknown", which results in the error: "1 is not a valid PostgreSQL link resource".
As a workaround, you can encapsulate the connect statement in a while loop:
while(!is_resource($conn) || (get_resource_type($conn) != "pgsql link")
$conn = pg_connect(...);
ie, as long as the resource type is "unknown", try again to connect. in my tests, it succeeded on second try.
this bug is supposed to be fixed in newer php versions but it's good to know there's a way to work around.
matto