|Don't ask to ask, just ask
Since I store the PID value of the process in a database, this PID might be re-used by the operating system when deleting the record at startup.
There is no way to handle 100% of the termination cases at the time of termination no matter what you do (e.g. your machine suddenly loses power and the UPS fails), so you must have code that detects stale records at some future point in time. There are several ways of doing this. The canonical way to verify a pid is to record it in a lock file that the process keeps locked while it is running. When you verify the pid, if you can lock the file, the process has obviously died. Another more generic way could be to write a separate "checkpoint" record that you remove in your END block. If you start up and the checkpoint record exists, you remove the stale PID record the that checkpoint record points to as well as the checkpoint record.