I'm working on a daemon which does intermittent bursts of work and commits some of it to a PostgreSQL database. These bursts can sometimes be 10-15 minutes apart and the current bane of my existence is
DBD::Pg::db do failed: server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.
I assume this is a timeout issue, but all searches I've done (here, google, etc.) have just turned up information on using signals to set DBI/query timeouts, not how to prevent them. I have also attempted in vain to locate any way of changing/disabling connection idle timeouts via DBI, DBD::Pg, and the Postgres configuration.

I am also frequently seeing

message type 0x43 arrived from server while idle message type 0x5a arrived from server while idle

The best I've been able to achieve so far is to do a $dbh->ping || initialize_database_connection(); right before going into my main processing, which mostly works, but, race conditions being what they are, I still get the DBD::Pg::db error on occasion when $dbh goes stale between the ping and the next query. I had actually been hoping that whatever test DBD::Pg->ping does would itself be enough to keep the connection alive, but no such luck.

Any suggestions on how to keep this db connection from going bad on me?


Update: After a suggestion from samtregar that this might not actually be a timeout issue, it turned out that I wasn't being careful enough when mixing fork and DBI. Thanks to all who offered ideas along the way.

In reply to Preventing database handles from going stale by dsheroh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.