PostgreSQL启动大页支持很简单,只需要配置几个东西。
例子:
.1. postgresql.conf
huge_pages = on
shared_buffers = 8GB # 使用8G内存
.2. 操作系统
配置grub.conf,加入如下,重启系统
numa=off
transparent_hugepage=never
或
在/etc/rc.local中加入下面的几行,然后重启操作系统:
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
.3. 配置大页数量
cat /proc/meminfo |grep -i huge
AnonHugePages: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
每页2MB,一共需要8GB,所以配置为4096
vi /etc/sysctl.conf
vm.nr_hugepages = 4096
启动数据库,如果发现报这个错误,
huge TLB pages not supported on this platform
说明系统编译时,检测到OS不支持大页,那么怎么解决呢?
src/backend/port/sysv_shmem.c
src/backend/port/pg_shmem.c
/*
* Creates an anonymous mmap()ed shared memory segment.
*
* Pass the requested size in *size. This function will modify *size to the
* actual size of the allocation, if it ends up allocating a segment that is
* larger than requested.
*/
#ifndef EXEC_BACKEND
static void *
CreateAnonymousSegment(Size *size)
{
Size allocsize = *size;
void *ptr = MAP_FAILED;
int mmap_errno = 0;
#ifndef MAP_HUGETLB
if (huge_pages == HUGE_PAGES_ON)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("huge TLB pages not supported on this platform")));
#else
......
实际上MAP_HUGETLB是在系统的/usr/include/sys/mman.h 中定义的,如果没有定义,PG就不支持大页。
#include <bits/mman.h>
在文件 /usr/include/bits/mman.h 中加入以下行,重新编译PostgreSQL即可。
vi /usr/include/bits/mman.h
#define MAP_HUGETLB 0x40000 /* Create huge page mapping. */
重新编译PostgreSQL后,重启数据库。
[参考]
.1. http://www.postgresql.org/message-id/flat/54B9C41D.10109@jcvi.org#54B9C41D.10109@jcvi.org
.2. http://terryebase.com/?p=161