[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2007-10-11
Medium Priority
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
ID: 20064389
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

grant300 earned 2000 total points
ID: 20078723
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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: http://www.postgresql.org/ (http://www.postgresql.org/) 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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month19 days, 2 hours left to enroll

834 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