Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:38:56+00:00 2026-05-27T18:38:56+00:00

I have a backend process that maintains state in a PostgreSQL database, which needs

  • 0

I have a backend process that maintains state in a PostgreSQL database, which needs to be visible to the frontend. I want to:

  • Properly handle the backend being stopped and started. This alone is as simple as clearing out the backend state tables on startup.

  • Guard against multiple instances of the backend trampling each other. There should only be one backend process, but if I accidentally start a second instance, I want to make sure either the first instance is killed, or the second instance is blocked until the first instance dies.

Solutions I can think of include:

  • Exploit the fact that my backend process listens on a port. If a second instance of the process tries to start, it will fail with “Address already in use”. I just have to make sure it does the listen step before connecting to the database and wiping out state tables.

  • Open a secondary connection and run the following:

    BEGIN;
    LOCK TABLE initech.backend_lock IN EXCLUSIVE MODE;
    

    Note: the reason for IN EXCLUSIVE MODE is that LOCK defaults to the AccessExclusive locking mode. This conflicts with the AccessShare lock acquired by pg_dump.

    Don’t commit. Leave the table locked until the program dies.

What’s a good pattern for maintaining a singleton backend process that maintains state in a PostgreSQL database? Ideally, I would acquire a lock for the duration of the connection, but LOCK TABLE cannot be used outside of a transaction.

Background

Consider an application with a “broker” process which talks to the database, and accepts connections from clients. Any time a client connects, the broker process adds an entry for it to the database. This provides two benefits:

  • The frontend can query the database to see what clients are connected.

  • When a row changes in another table called initech.objects, and clients need to know about it, I can create a trigger that generates a list of clients to notify of the change, writes it to a table, then uses NOTIFY to wake up the broker process.

    Without the table of connected clients, the application has to figure out what clients to notify. In my case, this turned out to be quite messy: store a copy of the initech.objects table in memory, and any time a row changes, dispatch the old row and new row to handlers that check if the row changed and act if it did. To do it efficiently involves creating “indexes” against both the table-stored-in-memory, and handlers interested in row changes. I’m making a poor replica of SQL’s indexing and querying capabilities in the broker program. I’d rather move this work to the database.

In summary, I want the broker process to maintain some of its state in the database. It vastly simplifies dispatching configuration changes to clients, but it requires that only one instance of the broker be connected to the database at a time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:38:57+00:00Added an answer on May 27, 2026 at 6:38 pm

    it can be done by advisory locks

    http://www.postgresql.org/docs/9.1/interactive/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a back-end that process about 500 invoices a day. I have
I have a backend class that has to raise an event with no arguments.
We have an SQL 2005 database backend for our website, currently about 10GB in
We have a system with an Oracle backend to which we have access (though
I have a PHP frontend and a C++ backend, and I need to be
I have a Silverlight application that communications with an ASP.NET backend through WCF. I
I have a working memoize decorator which uses Django's cache backend to remember the
I have a current process that exists in 2 MS Access databases that exports
I have an ASP .NET web application which on the backend is talking to
I have once ajax call which does connect to database, sets one flag in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.