Book Image

Troubleshooting PostgreSQL

Book Image

Troubleshooting PostgreSQL

Overview of this book

Table of Contents (17 chapters)
Troubleshooting PostgreSQL
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Understanding prepared queries


This section is all about prepared queries. What is a prepared query? Consider the following example:

SELECT * FROM website WHERE domain = 'www.cybertec.at';

PostgreSQL will parse the query to check for syntax errors and so on. Then it will go through the rewrite system and hit the PostgreSQL optimizer, which then comes up with a good plan. Finally, the executor will perform all the steps selected by the query planner. If this type of query is executed over and over again for different domains, the parsing and planning process will also have to run over and over again, which is very time consuming.

Prepared queries try to solve exactly that problem. The idea is that the backend process will cache the plan and reuse it just in case it is needed again. The beauty is that sending the parameters instead of the complete query can be enough. Usually, prepared statements are happening behind the scenes when desired (this is usually done through the driver if the driver...