Troubleshooting PostgreSQL - Sample Chapter
Troubleshooting PostgreSQL - Sample Chapter
ee
Sa
pl
Troubleshooting PostgreSQL
First of all, I have to say "thank you" to you, for deciding to give this little book
a chance as you are about to read this preface. I hope this book will give you valuable
insights, and I sincerely hope that it will contribute to successful work on PostgreSQL.
I did my best to include all of the information I think is needed to make you greatly
successful and efficient when it comes to your daily business.
In my 15 years as a professional PostgreSQL consultant and commercial supporter at my
company, Cybertec Schnig & Schnig GmbH (www.postgresql-support.de),
I have seen countless setupsbig, small, medium, complex, and simple. Over the past
couple of years, I have assembled the 'best of problems,' which I would like to present
in this book.
The book you are looking at is unlike all other books. It has not been written in some
tutorial-style way. It really focuses on common problems and their solutions. Each
problem described will include some background information, why it happens, how
it can be solved, and what you can do to avoid it. In this way, I hope to solve and
fix your problems as quickly and efficiently as possible.
I truly hope that you find this little book useful, and that it contains
all you need to make your setup work.
Installing PostgreSQL
In this chapter, we will cover what can go wrong during the installation process and
what can be done to avoid those things from happening. At the end of the chapter,
you should be able to avoid all of the pitfalls, traps, and dangers you might face
during the setup process.
For this chapter, I have compiled some of the core problems that I have seen over the
years, as follows:
At the end of the chapter, you should be able to install PostgreSQL and protect
yourself against the most common issues popping up immediately after installation.
[1]
Installing PostgreSQL
The last digit is the so-called minor release. When a new minor release is issued,
it generally means that some bugs have been fixed (for example, some time zone
changes, crashes, and so on). There will never be new features, missing functions,
or changes of that sort in a minor release. The same applies to something truly
importantthe storage format. It won't change with a new minor release.
These little facts have a wide range of consequences. As the binary format and
the functionality are unchanged, you can simply upgrade your binaries, restart
PostgreSQL, and enjoy your improved minor release.
When the digit in the middle changes, things get a bit more complex. A changing
middle digit is called a major release. It usually happens around once a year and
provides you with significant new functionality. If this happens, we cannot just
stop or start the database anymore to replace the binaries. In this case, we face a
real migration process, which will be discussed later on in this book.
If the first digit changes, something really important has happened. Examples of
such important events were introductions of SQL (6.0), the Windows port (8.0),
streaming replication (9.0), and so on. Technically, there is no difference between
the first and the second digitthey mean the same thing to the end user. However,
a migration process is needed.
The question that now arises is this: if you have a choice, which version of PostgreSQL
should you use? Well, in general, it is a good idea to take the latest stable release. In
PostgreSQL, every version number following the design patterns I just outlined is a
stable release.
As of PostgreSQL 9.4, the PostgreSQL community provides
fixes for versions as old as PostgreSQL 9.0. So, if you are
running an older version of PostgreSQL, you can still enjoy
bug fixes and so on.
Installing from source is not too hard to do. However, this chapter will focus on
installing binary packages only. Nowadays, most people (not including me) like
to install PostgreSQL from binary packages because it is easier and faster.
[2]
Chapter 1
Basically, two types of binary packages are common these days: RPM
(Red Hat-based) and DEB (Debian-based). In this chapter, installing these
two types of packages will be discussed.
First of all, PostgreSQL 9.4 is installed. Then a so-called database instance is created
(initdb). Next, the service is enabled to make sure that it is always there after a
reboot, and finally, the postgresql-9.4 service is started.
The term database instance is an important concept. It basically
describes an entire PostgreSQL environment (setup). A database
instance is fired up when PostgreSQL is started. Databases are
part of a database instance.
[3]
Installing PostgreSQL
So, in the case of Debian Wheezy, the following line would be useful:
deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main
Once we have added the repository, we can import the signing key:
$# wget --quiet -O - \
https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add OK
Voil! Things are mostly done. In the next step, the repository information can
be updated:
apt-get update
If no error is issued by the operating system, it means you have successfully installed
PostgreSQL. The beauty here is that PostgreSQL will fire up automatically after a
restart. A simple database instance has also been created for you.
If everything has worked as expected, you can give it a try and log in to the database:
root@chantal:~# su - postgres
$ psql postgres
psql (9.4.1)
Type "help" for help.
postgres=#
[4]
Chapter 1
HINT: This error usually means that PostgreSQL's request for a shared
memory segment exceeded your kernel's SHMMAX parameter. You can either
reduce the request size or reconfigure the kernel with larger SHMMAX. To reduce
the request size (currently 1122263040 bytes), reduce PostgreSQL's shared
memory usage, perhaps by reducing shared_buffers or max_connections.
If the request size is already small, it's possible that it is less than your
kernel's SHMMIN parameter, in which case raising the request size or
reconfiguring SHMMIN is called for.
The PostgreSQL documentation contains more information about
shared memory configuration.
If you are facing a message like this, it means that the kernel does not provide you
with enough shared memory to satisfy your needs. Where does this need for shared
memory come from? Back in the old days, PostgreSQL stored a lot of stuff, such
as the I/O cache (shared_buffers, locks, autovacuum-related information and a lot
more), in the shared memory. Traditionally, most Linux distributions have had
a tight grip on the memory, and they don't issue large shared memory segments;
for example, Red Hat has long limited the maximum amount of shared memory
available to applications to 32 MB. For most applications, this is not enough to run
PostgreSQL in a useful wayespecially not if performance does matter (and it
usually does).
[5]
Installing PostgreSQL
To fix this problem, you have to adjust kernel parameters. Managing Kernel Resources
of the PostgreSQL Administrator's Guide will tell you exactly why we have to adjust
kernel parameters.
For more information, check out the PostgreSQL documentation at http://www.
postgresql.org/docs/9.4/static/kernel-resources.htm.
This chapter describes all the kernel parameters that are relevant to PostgreSQL.
Note that every operating system needs slightly different values here (for open files,
semaphores, and so on).
Since not all operating systems can be covered in this little book, only Linux and Mac
OS X will be discussed here in detail.
In this example, shmmax and shmall have been adjusted to 16 GB. Note that shmmax
is in bytes while shmall is in 4k blocks. The kernel will now provide you with a great
deal of shared memory.
Also, there is more; to handle concurrency, PostgreSQL needs something called
semaphores. These semaphores are also provided by the operating system. The
following kernel variables are available:
Don't change these variables unless you really have to. Changes can be made with
sysctl, as was shown for the shared memory.
[6]
Chapter 1
Mac OS X is somewhat nasty to configure. The reason is that you have to set all five
parameters to make this work. Otherwise, your changes will be silently ignored, and
this can be really painful.
In addition to that, it has to be assured that SHMMAX is an exact multiple of 4096. If it is
not, trouble is near.
If you want to change these parameters on the fly, recent versions of OS X provide a
systcl command just like Linux. Here is how it works:
sysctl -w kern.sysv.shmmax
sysctl -w kern.sysv.shmmin
sysctl -w kern.sysv.shmmni
sysctl -w kern.sysv.shmseg
sysctl -w kern.sysv.shmall
hard
nofile
1024
postgres
soft
nofile
1024
This example says that the postgres user can have up to 1,024 open files per session.
Note that this is only important for large systems; open files won't hurt an
average setup.
[7]
Installing PostgreSQL
Chapter 1
If you are planning to load data into a new database, ensure that the encoding or
character set of the data is the same as that of the database. Otherwise, you may
face ugly surprises.
To create a database using the correct locale, check out the syntax of CREATE DATABASE:
test=# \h CREATE DATABASE
Command:
CREATE DATABASE
Description: create a new database
Syntax:
CREATE DATABASE name
[ [ WITH ] [ OWNER [=] user_name ]
[ TEMPLATE [=] template ]
[ ENCODING [=] encoding ]
[ LC_COLLATE [=] lc_collate ]
[ LC_CTYPE [=] lc_ctype ]
[ TABLESPACE [=] tablespace_name ]
[ CONNECTION LIMIT [=] connlimit ] ]
ENCODING and the LC* settings are used here to define the proper encoding for your
new database.
[9]
Installing PostgreSQL
Summary
In this chapter, you learned how to install PostgreSQL using binary packages.
Some of the most common problems and pitfalls, including encoding-related
issues, checksums, and versioning were discussed.
In the next chapter, we'll deal with data structures and common issues related to them.
[ 10 ]
www.PacktPub.com
Stay Connected: