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

  • SEARCH
  • Home
  • 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 6852915
In Process

The Archive Base Latest Questions

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

I have a stored procedure that is executed by remote clients on an ongoing

  • 0

I have a stored procedure that is executed by remote clients on an ongoing basis. From reading the docs I am under the impression that with the proper locking techniques I shouldn’t need to externally manage these clients and I would be free to have them run as often and as concurrently as they like.

This procedure updates and inserts to multiple tables. A skeleton outline of the procedure is below:

LOCK TABLE table1 IN EXCLUSIVE MODE;

LOOP
    UPDATE table1 SET "var1"="var1"+1, WHERE "var2"=var2;
    IF found THEN
        EXIT;
    END IF; 

    BEGIN
        INSERT INTO table1 ("col") VALUES (1) RETURNING "ID" INTO id;
        EXIT;
    EXCEPTION WHEN unique_violation THEN
        EXIT;
    END;
    EXIT;
END LOOP;

LOCK TABLE table2 IN EXCLUSIVE MODE;

LOOP
    BEGIN
        INSERT INTO table2 ("var3","var4") values (var3,var4);
        EXIT;
    EXCEPTION WHEN unique_violation THEN
        EXIT;
    END;
END LOOP;

There’s also a third table after this that is locked and updated/inserted in the same fashion as table1. Things are working as-is however a web application is also performing some slow select statements on the same tables and the exclusive locks cause large delays every so often. I realize exclusive locking should not be necessary however the myriad of locking types and which to apply to which tables in my procedure was a bit confusing so I went for this approach to get it going asap.

I have a few questions:

What is the best locking strategy for this procedure? I’d like to do it properly, avoid exclusive locks where I can to enable the web application to run selects without interference.

What kind of limitations will I face as the number of clients grows? Would it be better to abandon this approach entirely and have the data from all clients pushed to a central location and only run the procedure from there?

  • 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-27T01:26:56+00:00Added an answer on May 27, 2026 at 1:26 am

    Let the database handle locking for you. PostgreSQL, as well as every other database, has locking code that will lock the appropriate rows for modification as needed. PostgreSQL uses something called Multi-Version Concurrency Control that effectively means readers will never block or be blocked by writers.

    With your current solution, as the number of clients grow, you’ll see locking increase until your application becomes completely unusable. Again, let the database manage locking for you – it is very effective at resolving locks as needed.

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

Sidebar

Related Questions

I have a stored procedure that executes much faster from Sql Server Management Studio
I have a stored procedure that returns values from a temp table. In my
I have a query in a stored procedure that needs to be executed on
I have an stored procedure that get executed after insert and update. I need
I have stored procedure that insanely times out every single time it's called from
I have a report that renders data returned from a stored procedure. Using profiler
I've written a stored procedure that takes 15 min when executed from Management Studio.
I have a stored procedure on SQLserver that is executed by Matlab. The procedure
I have a stored procedure that returns multiple tables. How can I execute and
I have a strange, sporadic issue. I have stored procedure that returns back 5

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.