Long Running UPDATE Query - plpgsql

Posted on 2006-04-24
Last Modified: 2008-02-01
I have an UPDATE query that updates one int4 column of a table with around 220,000 rows in it. The subquery to pull the new value of this column executes in approximately 10 seconds. When I run the UPDATE query:

UPDATE mytable
SET mycol = sq.mycol
      ... ) sq

It takes somewhere in the area of 5 minutes to execute. I think when Postgres gets around to running COMMIT is where locking begins to occur, and all of my SQL statements begin backing up.

This table is pretty heavily hit on a live website - so things get backed up pretty quickly - and if it gets too bad, I have no choice but to restart the database.

I am running this statement from with in plpgsql function, and I have even tried looping through running smaller UPDATE statements:
   UPDATE ...

This seems to run better, as it slowly degrades performance, whereas the first one would run fairly quickly, and then at one point everything would start backing up. I attempted to explicitly COMMIT smaller blocks of records, but plpgsql doesn't allow for this.

A few of questions, any of which might lead me to a good solution:
1) Is there just an overall better way to approach this, in plpgsql?
2) Is there some way to tell the function's execution to pause for a number of seconds to allow some backed up queries to complete execution?
3) Is there a way to use a READ UNCOMMITED isolation level or disable locking for this execution? I don't care about any issues associated with dirty/phantom reads while dealing with this table.

Thanks in advance!
Question by:InterWorks
    LVL 15

    Accepted Solution


    EXPLAIN UPDATE mytable
    SET mycol = sq.mycol
    FROM (
          ... ) sq
    WHERE =;

    and post the results here.

    Also, are and of the same type, and what's their type?

    READ UNCOMMITED won't help: " The SQL standard defines two additional levels, READ UNCOMMITTED and REPEATABLE READ. In PostgreSQL READ UNCOMMITTED is treated as READ COMMITTED, while REPEATABLE READ is treated as SERIALIZABLE." (
    LVL 61

    Assisted Solution

    Do joined columns have INDEX associated?
    LVL 22

    Assisted Solution

    by:earth man2
    Are you encountering deadlock, rather than bottleneck.
    LVL 1

    Author Comment

    Sorry for lack of response. Yes they're of the same type and it's an indexed btree primary key on mytable.

    We brought in a high level postgres guy to tune the configuration. Apparently a simple UPDATE on this table inserts an entirely new row and removes the old one - we were running out of workspace memory - so it resorted to running most of this query in swapfiles on disk. That has been corrected, but this query still takes too long and locks too much. We're breaking the query up into smaller blocks with an outside language so we can use smaller transactions, from what I've heard, PL/PGSQL cannot do this.

    All points brought up were valid, so I'll split the points. Thanks for the responses.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: ( This requires some add-o…
    Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
    Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now