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 7165153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:11:37+00:00 2026-05-28T14:11:37+00:00

Sometimes postgresql raise error deadlocks. In trigger for table setted FOR UPDATE. Table comment:

  • 0

Sometimes postgresql raise error deadlocks.

In trigger for table setted FOR UPDATE.

Table comment:

http://pastebin.com/L1a8dbn4

Log (INSERT sentences is cutted):

2012-01-26 17:21:06 MSK ERROR:  deadlock detected
2012-01-26 17:21:06 MSK DETAIL:  Process 2754 waits for ExclusiveLock on tuple (40224,15) of relation 735493 of database 734745; blocked by process 2053.
Process 2053 waits for ShareLock on transaction 25162240; blocked by process 2754.
Process 2754: INSERT INTO comment (user_id, content_id, reply_id, text) VALUES (1756235868, 935967, 11378142, 'text1') RETURNING comment.id;
Process 2053: INSERT INTO comment (user_id, content_id, reply_id, text) VALUES (4071267066, 935967, 11372945, 'text2') RETURNING comment.id;
2012-01-26 17:21:06 MSK HINT:  See server log for query details.
2012-01-26 17:21:06 MSK CONTEXT:  SQL statement "SELECT comments_count FROM content WHERE content.id = NEW.content_id FOR UPDATE"
PL/pgSQL function "increase_comment_counter" line 5 at SQL statement
2012-01-26 17:21:06 MSK STATEMENT:  INSERT INTO comment (user_id, content_id, reply_id, text) VALUES (1756235868, 935967, 11378142, 'text1') RETURNING comment.id;

And trigger on table comment:

CREATE OR REPLACE FUNCTION increase_comment_counter() RETURNS TRIGGER AS $$
DECLARE
comments_count_var INTEGER;
BEGIN
    SELECT INTO comments_count_var comments_count FROM content WHERE content.id = NEW.content_id FOR UPDATE;
    UPDATE content SET comments_count = comments_count_var + 1, last_comment_dt = now()  WHERE content.id = NEW.content_id;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;



CREATE TRIGGER increase_comment_counter_trigger AFTER INSERT ON comment FOR EACH ROW EXECUTE PROCEDURE increase_comment_counter();

Why it can happens?

Thanks!

  • 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-28T14:11:37+00:00Added an answer on May 28, 2026 at 2:11 pm

    These are two comments being inserted with the same content_id. Merely inserting the comment will take out a SHARE lock on the content row, in order to stop another transaction deleting that row until the first transaction has completed.

    However, the trigger then goes on to upgrade the lock to EXCLUSIVE, and this can be blocked by a concurrent transaction performing the same process. Consider the following sequence of events:

    Txn 2754                      Txn 2053
    Insert Comment
                                  Insert Comment
    Lock Content#935967 SHARE
      (performed by fkey)
                                  Lock Content#935967 SHARE
                                    (performed by fkey)
    Trigger
    Lock Content#935967 EXCLUSIVE
    (blocks on 2053's share lock)
                                  Trigger
                                  Lock Content#935967 EXCLUSIVE
                                  (blocks on 2754's share lock)
    

    So- deadlock.

    One solution is to immediately take an exclusive lock on the content row before inserting the comment. i.e.

    SELECT 1 FROM content WHERE content.id = 935967 FOR UPDATE
    INSERT INTO comment(.....)
    

    Another solution is simply to avoid this “cached counts” pattern completely, except where you can prove it is necessary for performance. If so, consider keeping the cached count somewhere other than the content table– e.g. a dedicated table for the counter. That will also cut down on the update traffic to the content table every time a comment gets added. Or maybe just re-select the count and use memcached in the application. There’s no getting round the fact that wherever you store this cached count is going to be a choke point, it has to be updated safely.

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

Sidebar

Related Questions

I've installed PostgreSQL and PostGIS, and now I'm trying to follow these instructions: http://docs.djangoproject.com/en/dev/ref/contrib/gis/install/#spatialdb-template
Sometimes I need to insert into the table some null values, or update them
Sometimes, not often, I see these lines in the error log: [Sun Feb 20
Sometimes when copying stuff into PostgreSQL I get errors that there's invalid byte sequences.
sometimes I get the script execution error, where it takes over 30 seconds. It's
Sometimes I got error saying that unrecognized selector objcType was sent to an NSDate
I have a postgresql table storing posts from an online forum. Each post belongs
Sometimes when I'm editing page or control the .designer files stop being updated with
Sometimes IE6 will render the text of a <ul> list the same color as
Sometimes I need to quickly extract some arbitrary data from XML files to put

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.