PostgreSQL-How does a long running select query behave when its source tables are updated?

Posted on 2007-10-11
Last Modified: 2012-06-27
I'm wondering if a long running select query would restart processing if its source tables were modified during the run of the query.  We acquired this query from a vendor and it runs on PostgreSQL 7.4.6.  One example run of the query today took 7 minutes to return information on 4 invoices.  I'm not currently trying to alter the query because additional code would have to be rewritten and that's not an option.  However, I'm wondering if other activity in the source tables would cause the select query to restart processing.  If so, I could make it so the other activity would be avoided on days when this query would be running frequently.  I have not found any lock type being specified in the vendor's code, so I'm assuming it's using the default method (Access Share?).  So, what happens to a select query that is running when its source tables are being updated and inserted into?
Question by:kreilkamp
    LVL 22

    Expert Comment

    by:earth man2
    the answer to the question is indeterminate... !
    if an update to some data is committed whilst the select is running then the query results are altered.
    This is a feature of multi-user databases and why table locks should be used if you need to cut off any pending updates to your data.
    LVL 19

    Accepted Solution

    I'm pretty sure the answer is nothing will happen.  PostgreSQL uses versioning to keep just such a thing from happening.  Old versions of a changed record are kept around in the database and isolated from each other by storing a Transaction ID on each one.  A query will not read versions of rows newer than the transaction ID of the query itself.

    Vacuuming is the process that removes old copies of rows from the database.  If you don't vacuum a database that has significant amounts of updating going on, you just keep building up old versions of records.


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    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.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    745 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

    16 Experts available now in Live!

    Get 1:1 Help Now