PostgreSQL Development Essentials 1st Edition Kaur download pdf
PostgreSQL Development Essentials 1st Edition Kaur download pdf
com
https://ebookname.com/product/postgresql-development-
essentials-1st-edition-kaur/
OR CLICK BUTTON
DOWLOAD EBOOK
https://ebookname.com/product/game-development-essentials-an-
introduction-3rd-edition-jeannie-novak/
ebookname.com
https://ebookname.com/product/postgresql-high-availability-
cookbook-2nd-edition-shaun-m-thomas/
ebookname.com
https://ebookname.com/product/marmalade-sdk-mobile-game-development-
essentials-1st-new-edition-edition-sean-scaplehorn/
ebookname.com
https://ebookname.com/product/las-romanticas-susan-kirkpatrick/
ebookname.com
https://ebookname.com/product/english-irish-and-subversives-among-the-
dismal-scientists-1st-edition-noel-w-thompson/
ebookname.com
https://ebookname.com/product/between-morality-and-the-law-corruption-
anthropology-and-comparative-society-1st-edition-italo-pardo/
ebookname.com
https://ebookname.com/product/breaking-the-fourth-wall-direct-address-
in-the-cinema-1st-edition-tom-brown/
ebookname.com
https://ebookname.com/product/breadlines-knee-deep-in-wheat-food-
assistance-in-the-great-depression-nestle/
ebookname.com
The Oxford Handbook of the Word Oxford Handbooks John R.
Taylor
https://ebookname.com/product/the-oxford-handbook-of-the-word-oxford-
handbooks-john-r-taylor/
ebookname.com
PostgreSQL Development
Essentials
Manpreet Kaur
Baji Shaik
BIRMINGHAM - MUMBAI
PostgreSQL Development Essentials
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, without the prior written permission of the
publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the authors, nor Packt Publishing, and its
dealers and distributors will be held liable for any damages caused or alleged to be caused
directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
ISBN 978-1-78398-900-3
www.packtpub.com
Credits
Baji Shaik
Danny Sauer
Additionally, she is very creative and enjoys oil painting. She also has a youtube channel,
Oh so homemade, where she posts easy ways to make recycled crafts.
He comes from a small village named Vutukutu in Andhra Pradesh and currently lives in
Hyderabad. He likes to watch movies, read books, and write technical blogs. He loves to
spend time with family. He has tech-reviewed Troubleshooting PostgreSQL by Packt
Publishing. He is a certified PostgreSQL professional.
Thanks to my loving parents. Thanks to Packt Publishing for giving me this opportunity.
Special thanks to Izzat Contractor for choosing me, and Anish Sukumaran, Nitin Dasan,
and Sunith Shetty for working with me. Thanks to Dinesh Kumar for helping me write.
About the Reviewers
Daniel Durante started spending time with computers at the age of 12. He has built
applications for various sectors, such as the medical industry, universities, the
manufacturing industry, and the open source community. He mainly uses Golang, C, Node,
or PHP for developing web applications, frameworks, tools, embedded systems, and so
on. Some of his personal work can be found on GitHub and his personal website.
He has also worked on the PostgreSQL Developer's Guide, published by Packt Publishing.
I would like to thank my parents, brother, and friends, who’ve all put up with my insanity,
day in and day out. I would not be here today if it weren’t for their patience, guidance, and
love.
Danny Sauer has been a Linux sysadmin, software developer, security engineer, open
source advocate, and general computer geek at various companies for around 20 years. He
has administered, used, and programmed PostgreSQL for over half of that time. When he's
not building solutions in the digital world, he and his wife enjoy restoring their antique
home and teaching old cars new tricks.
www.PacktPub.com
eBooks, discount offers, and more
Did you know that Packt offers eBook versions of every book published, with PDF and
ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a
print book customer, you are entitled to a discount on the eBook copy. Get in touch with us
at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a
range of free newsletters and receive exclusive discounts and offers on Packt books and
eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book
library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Table of Contents
Preface 1
Chapter 1: Advanced SQL 5
Creating views 5
Deleting and replacing views 7
Materialized views 8
Why materialized views? 8
Read-only, updatable, and writeable materialized views 8
Read-only materialized views 9
Updatable materialized views 9
Writeable materialized views 10
Creating cursors 10
Using cursors 11
Closing a cursor 12
Using the GROUP BY clause 12
Using the HAVING clause 14
Parameters or arguments 14
Using the UPDATE operation clauses 15
Using the LIMIT clause 15
Using subqueries 16
Subqueries that return multiple rows 18
Correlated subqueries 18
Existence subqueries 19
Parameters or arguments 19
Using the Union join 20
Using the Self join 21
Using the Outer join 22
Left outer join 23
Right outer join 24
Full outer join 24
Summary 26
Chapter 2: Data Manipulation 27
Conversion between datatypes 27
Introduction to arrays 28
Array constructors 28
String_to_array() 31
Array_dims( ) 32
ARRAY_AGG() 32
ARRAY_UPPER() 34
Array_length() 34
Array slicing and splicing 34
UNNESTing arrays to rows 35
Introduction to JSON 37
Inserting JSON data in PostgreSQL 37
Querying JSON 38
Equality operation 38
Containment 38
Key/element existence 39
Outputting JSON 40
Using XML in PostgreSQL 41
Inserting XML data in PostgreSQL 41
Querying XML data 42
Composite datatype 42
Creating composite types in PostgreSQL 42
Altering composite types in PostgreSQL 44
Dropping composite types in PostgreSQL 45
Summary 45
Chapter 3: Triggers 46
Introduction to triggers 46
Adding triggers to PostgreSQL 47
Modifying triggers in PostgreSQL 52
Removing a trigger function 53
Creating a trigger function 54
Testing the trigger function 55
Viewing existing triggers 56
Summary 57
Chapter 4: Understanding Database Design Concepts 58
Basic design rules 58
The ability to solve the problem 58
The ability to hold the required data 59
The ability to support relationships 59
The ability to impose data integrity 59
The ability to impose data efficiency 59
The ability to accommodate future changes 59
Normalization 60
Anomalies in DBMS 60
[ ii ]
First normal form 62
Second normal form 62
Third normal form 63
Common patterns 64
Many-to-many relationships 64
Hierarchy 65
Recursive relationships 66
Summary 67
Chapter 5: Transactions and Locking 68
Defining transactions 68
ACID rules 69
Effect of concurrency on transactions 70
Transactions and savepoints 70
Transaction isolation 71
Implementing isolation levels 72
Dirty reads 72
Non-repeatable reads 73
Phantom reads 74
ANSI isolation levels 74
Transaction isolation levels 75
Changing the isolation level 75
Using explicit and implicit transactions 76
Avoiding deadlocks 76
Explicit locking 77
Locking rows 77
Locking tables 78
Summary 79
Chapter 6: Indexes and Constraints 81
Introduction to indexes and constraints 81
Primary key indexes 82
Unique indexes 83
B-tree indexes 84
Standard indexes 85
Full text indexes 86
Partial indexes 86
Multicolumn indexes 88
Hash indexes 89
GIN and GiST indexes 89
Clustering on an index 90
Foreign key constraints 91
[ iii ]
Unique constraints 92
Check constraints 94
NOT NULL constraints 95
Exclusion constraints 96
Summary 96
Chapter 7: Table Partitioning 97
Table partitioning 97
Partition implementation 102
Partitioning types 107
List partition 107
Managing partitions 109
Adding a new partition 109
Purging an old partition 110
Alternate partitioning methods 111
Method 1 111
Method 2 112
Constraint exclusion 114
Horizontal partitioning 116
PL/Proxy 117
Foreign inheritance 118
Summary 121
Chapter 8: Query Tuning and Optimization 122
Query tuning 122
Hot versus cold cache 123
Cleaning the cache 124
pg_buffercache 127
pg_prewarm 129
Optimizer settings for cached data 130
Multiple ways to implement a query 132
Bad query performance with stale statistics 134
Optimizer hints 136
Explain Plan 141
Generating and reading the Explain Plan 141
Simple example 142
More complex example 142
Query operators 143
Seq Scan 143
Index Scan 143
Sort 144
Unique 144
[ iv ]
LIMIT 144
Aggregate 144
Append 144
Result 144
Nested Loop 145
Merge Join 145
Hash and Hash Join 145
Group 145
Subquery Scan and Subplan 145
Tid Scan 145
Materialize 146
Setop 146
Summary 146
Chapter 9: PostgreSQL Extensions and Large Object Support 147
Creating an extension 147
Compiling extensions 149
Database links in PostgreSQL 150
Using binary large objects 153
Creating a large object 154
Importing a large object 154
Exporting a large object 155
Writing data to a large object 155
Server-side functions 155
Summary 156
Chapter 10: Using PHP in PostgreSQL 157
Postgres with PHP 157
PHP-to-PostgreSQL connections 158
Dealing with DDLs 161
DML operations 162
pg_query_params 163
pg_insert 164
Data retrieval 165
pg_fetch_all 165
pg_fetch_assoc 166
pg_fetch_result 167
Helper functions to deal with data fetching 168
pg_free_results 168
pg_num_rows 168
[v]
pg_num_fields 168
pg_field_name 168
pg_meta_data 168
pg_convert 169
UPDATE 171
DELETE 172
COPY 172
Summary 174
Chapter 11: Using Java in PostgreSQL 175
Making database connections to PostgreSQL using Java 175
Using Java to create a PostgreSQL table 178
Using Java to insert records into a PostgreSQL table 179
Using Java to update records into a PostgreSQL table 180
Using Java to delete records into a PostgreSQL table 181
Catching exceptions 182
Using prepared statements 184
Loading data using COPY 184
Connection properties 186
Summary 187
Index 188
[ vi ]
Preface
The purpose of this book is to teach you the fundamental practices and techniques of
database developers for programming database applications with PostgreSQL. It is targeted
to database developers using PostgreSQL who have basic experience developing database
applications with the system, but want a deeper understanding of how to implement
programmatic functions with PostgreSQL.
Chapter 2, Data Manipulation, provides you the ability to perform data type conversions
and perform JSON and XML operations in PostgreSQL.
Chapter 3, Triggers, explains how to perform trigger operations and use trigger functions in
PostgreSQL.
Chapter 5, Transactions and Locking, covers the effect of transactions and locking on the
database.The reader will also be able to understand isolation levels and understand multi-
version concurrency control behavior.
Chapter 6, Indexes And Constraints, provides knowledge about the different indexes and
constraints available in PostgreSQL. This knowledge will help the reader while coding and
the reader will be in a better position to choose among the different indexes and constraints
depending upon the requirement during the coding phase.
Chapter 8, Query Tuning and Optimization, provides knowledge about different mechanisms
and approaches available to tune a query. The reader will be able to utilize this knowledge
in order to write a optimal/efficient query or code.
Chapter 9, PostgreSQL Extensions and Large Object Support, will familiarize the reader with
the concept of extensions in PostgreSQL and also with the usage of large objects' datatypes
in PostgreSQL.
Chapter 10, Using PHP in PostgreSQL, covers the basics of performing database operations
in PostgreSQL using the PHP language, which helps reader to start with PHP code.
Chapter 11, Using Java in PostgreSQL, this chapter provides knowledge about database
connectivity using Java and creating/modifying objects using Java code. It also talks about
JDBC drivers.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds
of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions,
pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Database
views are created using the CREATE VIEW statement. "
[2]
Preface
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this
book—what you liked or disliked. Reader feedback is important for us as it helps us
develop titles that you will really get the most out of. To send us general feedback, simply
e-mail [email protected], and mention the book's title in the subject of your
message. If there is a topic that you have expertise in and you are interested in either
writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you
to get the most from your purchase.
[3]
Preface
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do
happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-
we would be grateful if you could report this to us. By doing so, you can save other readers
from frustration and help us improve subsequent versions of this book. If you find any
errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting
your book, clicking on the Errata Submission Form link, and entering the details of your
errata. Once your errata are verified, your submission will be accepted and the errata will
be uploaded to our website or added to any list of existing errata under the Errata section of
that title.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At
Packt, we take the protection of our copyright and licenses very seriously. If you come
across any illegal copies of our works in any form on the Internet, please provide us with
the location address or website name immediately so that we can pursue a remedy.
We appreciate your help in protecting our authors and our ability to bring you valuable
content.
Questions
If you have a problem with any aspect of this book, you can contact us
at [email protected], and we will do our best to address the problem.
[4]
Advanced SQL
1
This book is all about an open source software product, a relational database called
PostgreSQL. PostgreSQL is an advanced SQL database server, available on a wide range of
platforms. The purpose of this book is to teach database developers the fundamental
practices and techniques to program database applications with PostgreSQL.
Creating views
Understanding materialized views
Creating cursors
Using the GROUP BY clause
Using the HAVING clause
Understanding complex topics such as subqueries and joins
Creating views
A view is a virtual table based on the result set of an SQL statement. Just like a real table, a
view consist of rows and columns. The fields in a view are from one or more real tables in
the database. Generally speaking, a table has a set of definitions that physically stores data.
A view also has a set of definitions built on top of table(s) or other view(s) that does not
physically store data. The purpose of creating views is to make sure that the user does not
have access to all the data and is being restricted through a view. Also, it's better to create a
view if we have a query based on multiple tables so that we can use it straightaway rather
than writing a whole PSQL again and again.
Database views are created using the CREATE VIEW statement. Views can be created from a
single table or multiple tables, or another view.
Advanced SQL
You can then query this view as though it were a table. (In PostgreSQL, at the time of
writing, views are read-only by default.) You can SELECT data from a view just as you
would from a table and join it to other tables; you can also use WHERE clauses. Each time
you execute a SELECT query using the view, the data is rebuilt, so it is always up-to-date. It
is not a frozen copy stored at the time the view was created.
Let's create a view on supplier and order tables. But, before that, let's see what the structure
of the suppliers and orders table is:
CREATE TABLE suppliers
(supplier_id number primary key,
Supplier_name varchar(30),
Phone_number number);
CREATE TABLE orders
(order_number number primary key,
Supplier_id number references suppliers(supplier_id),
Quanity number,
Is_active varchar(10),
Price number);
CREATE VIEW active_supplier_orders AS
SELECT suppliers.supplier_id, suppliers.supplier_name orders.quantity,
orders.price
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id
WHERE suppliers.supplier_name = 'XYZ COMPANY'
[6]
Advanced SQL
And orders.active='TRUE';
The preceding example will create a virtual table based on the result set of the SELECT
statement. You can now query the PostgreSQL VIEW as follows:
SELECT * FROM active_supplier_orders;
If you want to replace an existing view with one that has the same name and returns the
same set of columns, you can use a CREATE OR REPLACE command.
CREATE OR REPLACE VIEW: This command helps modify the existing view.
SELECT: This command selects the columns that you want as part of the view.
FROM: This command gives the table name from where we can fetch the columns.
This may include one or more table names, since you have to create a view at the
top of multiple tables.
WHERE: This command provides the condition to restrict the data for a view. Also,
if you include multiple tables in the FROM clause, you can provide the joining
condition under the WHERE clause.
Let's modify a view, supplier_orders, by adding some more columns in the view. The
view was originally based on supplier and order tables having supplier_id,
supplier_name, quantity, and price. Let's also add order_number in the view.
[7]
Advanced SQL
Materialized views
A materialized view is a table that actually contains rows but behaves like a view. This has
been added in the PostgreSQL 9.3 version. A materialized view cannot subsequently be
directly updated, and the query used to create the materialized view is stored in exactly the
same way as the view's query is stored. As it holds the actual data, it occupies space as per
the filters that we applied while creating the materialized view.
You may notice that certain queries are very slow. You may have exhausted all the
techniques in the standard bag of techniques to speed up those queries. In the end, you will
realize that getting queries to run as fast as you want simply isn't possible without
completely restructuring the data.
Now, if you have an environment where you run the same type of SELECT query multiple
times against the same set of tables, then you can create a materialized view for SELECT so
that, on every run, this view does not go to the actual tables to fetch the data, which will
obviously reduce the load on them as you might be running a Data Manipulation
Language (DML) against your actual tables at the same time. So, basically, you take a view
and turn it into a real table that holds real data rather than a gateway to a SELECT query.
[8]
Advanced SQL
In a replication environment, a materialized table holds the table data and resides in a
different database. A table that has a materialized view on it is called a master table. The
master table resides on a master site and the materialized view resides on a materialized-
view site.
The CREATE MATERIALIZED VIEW command helps us create a materialized view. The
command acts in way similar to the CREATE VIEW command, which was explained in the
previous section.
This view is a read-only materialized view and will not reflect the changes to the master
site.
When we say “refreshing the materialized view,” we mean synchronizing the data in the
materialized view with data in its master table.
[9]
Advanced SQL
An updatable materialized view enables you to decrease the load on master sites because
users can make changes to data on the materialized view site.
Whenever changes are made in the suppliers_matview clause, it will reflect the changes
to the master sites during refresh.
Creating cursors
A cursor in PostgreSQL is a read-only pointer to a fully executed SELECT statement's result
set. Cursors are typically used within applications that maintain a persistent connection to
the PostgreSQL backend. By executing a cursor and maintaining a reference to its returned
result set, an application can more efficiently manage which rows to retrieve from a result
set at different times without re-executing the query with different LIMIT and OFFSET
clauses.
The four SQL commands involved with PostgreSQL cursors are DECLARE, FETCH, MOVE, and
CLOSE.
[ 10 ]
Advanced SQL
The DECLARE command both defines and opens a cursor, in effect defining the cursor in
memory, and then populates the cursor with information about the result set returned from
the executed query. A cursor may be declared only within an existing transaction block, so
you must execute a BEGIN command prior to declaring a cursor.
DECLARE cursorname is the name of the cursor to create. The optional BINARY keyword
causes the output to be retrieved in binary format instead of standard ASCII; this can be
more efficient, though it is only relevant to custom applications as clients such as psql are
not built to handle anything but text output. The INSENSITIVE and SCROLL keywords exist
to comply with the SQL standard, though they each define PostgreSQL's default behavior
and are never necessary. The INSENSITIVE SQL keyword exists to ensure that all data
retrieved from the cursor remains unchanged from other cursors or connections. As
PostgreSQL requires the cursors to be defined within transaction blocks, this behavior is
already implied. The SCROLL SQL keyword exists to specify that multiple rows at a time
can be selected from the cursor. This is the default in PostgreSQL, even if it is unspecified.
The CURSOR FOR query is the complete query and its result set will be accessible by the
cursor when executed.
The [FOR { READ ONLY | UPDATE [ OF column [, ...] ] } ] cursors may only be
defined as READ ONLY, and the FOR clause is, therefore, superfluous.
Let's begin a transaction block with the BEGIN keyword, and open a cursor named
order_cur with SELECT * FROM orders as its executed select statement:
BEGIN;
DECLARE order_cur CURSOR
FOR SELECT * FROM orders;
Once the cursor is successfully declared, it means that the rows retrieved by the query are
now accessible from the order_cur cursor.
Using cursors
In order to retrieve rows from the open cursor, we need to use the FETCH command. The
MOVE command moves the current location of the cursor within the result set and the CLOSE
command closes the cursor, freeing up any associated memory.
[ 11 ]
Advanced SQL
cursor is the name of the cursor from where we can retrieve row data. A cursor always
points to a current position in the executed statement's result set and rows can be retrieved
either ahead of the current location or behind it. The FORWARD and BACKWARD keywords
may be used to specify the direction, though the default is forward. The NEXT keyword (the
default) returns the next single row from the current cursor position. The PRIOR keyword
causes the single row preceding the current cursor position to be returned.
Let's consider an example that fetches the first four rows stored in the result set, pointed to
by the order_cur cursor. As a direction is not specified, FORWARD is implied. It then uses a
FETCH statement with the NEXT keyword to select the fifth row, and then another FETCH
statement with the PRIOR keyword to again select the fourth retrieved row.
FETCH 4 FROM order_cur;
Closing a cursor
You can use the CLOSE command to explicitly close an open cursor. A cursor can also be
implicitly closed if the transaction block that it resides within is committed with the COMMIT
command, or rolled back with the ROLLBACK command.
Here is the syntax for the CLOSE command, where Cursorname is the name of the cursor
intended to be closed:
CLOSE
Cursorname;
[ 12 ]
Random documents with unrelated
content Scribd suggests to you:
would be impossible for me now to go through it. If the weather
should grow hot, I may, at any time, go to the hills, the foot of which
is five or six miles distant. I drink goats’ milk, and have left off meat
since the cough came on, but design eating a little again at
dinner.”[436]
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com