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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:59:06+00:00 2026-05-24T05:59:06+00:00

I’m trying to insert some files into a Postgres database. Since lots of duplication

  • 0

I’m trying to insert some files into a Postgres database. Since lots of duplication is expected, we put the files themselves into the file table, then link them to the section of the database we’re using with the output_file table. As the file table is also referenced by tables other than output_file (for example, the similar input_file table), one of its columns is a reference count, which is updated by a trigger when rows are inserted into output_file (and the other tables, too, although they aren’t being used at the times the problem occurs).

CREATE TABLE file
(
    file_id serial PRIMARY KEY,
    --other columns
    occurences integer NOT NULL DEFAULT 0
);

CREATE TABLE output_file
(
    output_file_id serial PRIMARY KEY,
    --other columns
    file_id integer REFERENCES file NOT NULL
);

CREATE OR REPLACE FUNCTION file_insert() RETURNS opaque AS '
    BEGIN
        UPDATE file
        SET occurences = occurences + 1
        WHERE file.file_id = NEW.file_id;

        RETURN NEW;
    END;
' LANGUAGE plpgsql;

CREATE TRIGGER output_file_insert AFTER INSERT
    ON output_file FOR EACH ROW
    EXECUTE PROCEDURE file_insert();

The code that inserts the files is shown below, and is all one transaction.

private void insertFiles(Set<File> files){
    SortedSet<Integer> outputFileIDs = new TreeSet<Integer>();
    PreparedStatement fileExistsStatement = getFileExistsStatement();
    for(File file : files) {
        try {
            int fileID = -1;

            ResultSet rs = /* Query to see if file already present in file table */

            if(rs.next()) {
                // File found
                fileID = rs.getInt(1);
            }

            if(fileID < 0) {
                /* File does not exist, upload it */

                rs = /* Query to get file ID */
                fileID = rs.getInt(1);
            }

            outputFileIDs.add(fileID);
        }
        catch(FileNotFoundException e){
            /* handle errors */
        }
    }

    Iterator<Integer> it = outputFileIDs.iterator();
    while(it.hasNext()){
        /* Insert reference in output file table */
        PreparedStatement outputFileStatement = "INSERT INTO output_file (file_id, /*...*/) VALUES (?, /*...*/);";
        outputFileStatement.setInt(1, it.next());
        outputFileStatement.executeUpdate();
    }
}

My problem is that code deadlocks (exception shown below) a lot. It’ll chunter along fairly happily for a while, then deadlocks will start happening all over the place, to the extent that nothing makes it into the database at all when we roll back and retry. I’m mystified as to why it’s deadlocking in the first place, though. The file IDs are stored in a sorted set, and so the locks on the file table should be acquired in a consistent order for all transactions, as suggested in the Postgres manual, preventing any deadlock. What am I doing wrong? Does Postgres run its triggers in an undefined order?

org.postgresql.util.PSQLException: ERROR: deadlock detected
    Detail: Process 8949 waits for ShareLock on transaction 256629; blocked by process 8924.
Process 8924 waits for ExclusiveLock on tuple (4148,40) of relation 30265 of database 16384; blocked by process 8949.
    Hint: See server log for query details.
    Where: SQL statement "UPDATE file
            SET occurences = occurences + 1
            WHERE file.file_id = NEW.file_id"
PL/pgSQL function "file_insert" line 2 at SQL statement
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
    at [outputFileStatement.executeUpdate(), above]

[edit] As requested by axtavt, transactions are managed by the code which calls the method shown.

public void run(){
    /* connection.setAutoCommit(false) has already been called elsewhere */
    try{
        boolean committed = false;
        boolean deadlocked = false;

        synchronized(connection){
            do{
                deadlocked = false;
                try {
                    /* insert lots of other stuff */

                    if(!files.isEmpty()){
                        insertFiles(files);
                    }

                    /* insert some more stuff */

                    connection.commit();
                    committed = true;

                    closeStatements();
                }
                catch(PSQLException e){
                    if(e.getSQLState() != null){
                        if(e.getSQLState().equals("40P01")){
                            /* Log the fact that we're deadlocked */
                            deadlocked = true;
                        }
                        else{
                            throw e;
                        }
                    }
                    else{
                        throw e;
                    }
                }
                finally {
                    try {
                        if(!committed) {
                            connection.rollback();
                        }
                    }
                    catch (SQLException e) {
                        /* Log exceptions */
                    }
                }
            }while(deadlocked);
        }

    }
    catch(Exception e){
        /* Log exceptions */
    }
    finally{
        try {
            connection.close();
        }
        catch (SQLException e) {
            /* Log exceptions */
        }
    }
}
  • 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-24T05:59:07+00:00Added an answer on May 24, 2026 at 5:59 am

    Here’s what is going on (I suspect):

    A file tuple (let’s say file.file_id = 1) already exists.

    Process A inserts a new output_file with output_file.file_id=1. This acquires a sharelock on the file tuple with file.file_id = 1.

    Process B inserts a new output_file with output_file.file_id=1. This acquires a aharelock on the file tuple with file.file_id = 1 (more than one transaction can hold a sharelock on a single tuple).

    Process A then runs the trigger, which attempts to update the file tuple with file.file_id=1. It can’t promote its sharelock to an exclusive lock, since another transaction (process B) is holding a sharelock. So, it waits.

    Process B then runs its trigger, which attempts to update the file tuple with file.file_id=1. It can’t promote its sharelock, for the same reason, so it waits… and then, we have a deadlock.

    To fix, before you insert the new tuple, to a SELECT … FOR UPDATE on the file tuple that will be the new parent; that will create the exclusive lock on that row, so the other transactions will queue up right from the start.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.