Open source databases play a major role in today's Web environments. Especially for complex Web applications, PostgreSQL is a good choice. To use PostgreSQL efficiently, the database should be configured to satisfy the demands of a modern IT environment. Depending on the kind of application you want to run, the best settings for your database might vary. In this section a few basic rules will be presented. 10.4.1 MemoryTuning PostgreSQL's memory management is an important task. The first thing is to find out if the applications you are going to run contain complex queries or a set of simple queries. If you run only a few complex queries, it won't be necessary to change the maximum number of connections allowed, but it will be necessary to increase the memory PostgreSQL is allowed to use for internal sorts. Sort buffers are a tricky thing because if many concurrent sorts are running on the system, a lot of memory could be allocated, and no memory would be left for other operations. On the other hand, small sort buffers will reduce the performance of your system because a lot of I/O power is needed. In general, the more memory your computer has, the better it is for your database. Caching can make a database perform really fast, so it is recommended to use a lot of cache and many shared buffers. However, make sure that the system does not swap because swapping is much slower than working with temporary sort files. This is an important point, and many people think that the operating system is always faster than the database, which is not true. To monitor the memory that is currently used by your system, we recommend working with tools like top and free (these tools are available on Linux; if you are using other operating systems, check out the manual). 10.4.2 Configuring the Linux Kernel for PostgreSQLAfter PostgreSQL has been configured, it is recommended to change the configuration of the Linux kernel as well. When the database is working under heavy load, some operating system resource limits can easily be exceeded and problems occur because of wrong configuration of the underlying operating system. These problems can easily be avoided by changing the most important settings of the kernel. In this book only the kernel settings of Linux operating systems will be covered because configuring other systems is beyond the scope of this book and how to change parameters can be looked up in the manual of the operating system. Table 10.1 shows the basic values that can be defined and a reasonable estimation of these parameters.
Table 10.1 has been taken from the developers docs of PostgreSQL. All parameters of the Linux kernel can be changed at runtime and don't require a reboot. To change a parameter, a simple echo command can be used: [root@notebook root]# echo 1 > /proc/sys/kernel/shmmni Simply redirect the output of the echo command to the appropriate location in the proc filesystem and the changes will be made immediately. 10.4.3 General RecommendationsWhen running PostgreSQL as a Web database, we recommend changing some default settings. Keep in mind that these changes are not required, but daily work with the database has proven that it can be useful to make these changes:
Depending on your system, additional changes might be useful. In many situations the settings that have just been described have been useful. |