1、

2、 内存占用分析

2.1 查看数据库的共享内存参数shared_buffers

 

show shared_buffers;
 shared_buffers 
----------------
 32GB
(1 row)

 

2.2 top 命令查看进程内存占用

  使用top命令查看PostgreSQL数据库服务进程占用内存情况,从以下图中可以看到进程(pid=45286)占到了机器总内存的25.9%(对应物理机的总内存是128G),进一步查看(pid=45286)进程占用内存比较高的原因:

 2.3 查看进程对应的服务

  (pid=45286)进程是checkpointer进程,checkpointer进程占用了很高的内存,实际上这里看到的这个内存并不是这个进程实际占用的真实内存,原因是这里显示的内存包括了共享内存,而共享内存是在很多的进程之间共享的,不能算作(pid=45286)进程的内存占用。

  列VSZ是虚拟内存并不是实际占用的内存,RSS虽然是实际的内存,但是包括了共享的部分,而top命令中看到进程内存的百分比就是RSS内存占用总内存的百分比。

ps -ef|grep 45286 |grep -v grep
postgres 45286 45282  0  2020 ?        05:33:30 postgres: checkpointer
ps aux --sort=-rss |head -10
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
postgres 45286  0.0 25.8 35072428 33997064 ?   Ss    2020 333:30 postgres: checkpointer   
postgres 45287  0.0  8.3 35067396 10929444 ?   Ss    2020  49:22 postgres: background writer
postgres 23784 30.4  0.8 35091120 1118520 ?    Rs   21:42   4:13 postgres: inofa greenerp2018 192.168.2.193(62975) SELECT
postgres 24074 41.8  0.8 35091636 1095144 ?    Rs   21:47   4:04 postgres: inofa greenerp2018 192.168.2.193(63056) SELECT
postgres 24737 38.2  0.8 35090844 1090260 ?    Rs   21:54   0:51 postgres: inofa greenerp2018 192.168.2.193(63202) SELECT
postgres 24837 70.3  0.8 35091552 1078752 ?    Rs   21:55   1:03 postgres: inofa greenerp2018 192.168.2.193(63223) SELECT
postgres 24368 44.3  0.8 35093552 1064208 ?    Rs   21:50   2:50 postgres: inofa greenerp2018 192.168.2.193(63124) SELECT
postgres 24835 52.3  0.8 35093740 1057660 ?    Rs   21:55   0:47 postgres: inofa greenerp2018 192.168.2.193(63222) SELECT
postgres 45282  0.0  0.8 35064744 1054188 ?    S     2020  84:47 /usr/pgsql-11.7/bin/postgres -D /data/ssd_pg_data/sale_pgdata_11

2.4 查看进程占用的实际内存

  查看PostgreSQL进程占用的内存,应该是把共享内存剔除掉,可以通过cat /proc/pid/smaps文件查看对应进程占用的实际内存,Pss表示把共享内存平分到各个进程上的内存,这实际上是这个进程占用的实际内存:

cat /proc/12047/smaps |more
00400000-00ad1000 r-xp 00000000 fd:01 42123205                           /usr/pgsql-11.7/bin/postgres
Size:               6980 kB
Rss:                 528 kB
Pss:                  67 kB
Shared_Clean:        512 kB
Shared_Dirty:          0 kB
Private_Clean:        16 kB
Private_Dirty:         0 kB
Referenced:          528 kB
Anonymous:             0 kB
AnonHugePages:         0 kB
Swap:                  0 kB
KernelPageSize:        4 kB
MMUPageSize:           4 kB
Locked:                0 kB
VmFlags: rd ex mr mw me dw sd 
00cd0000-00cd1000 r--p 006d0000 fd:01 42123205                           /usr/pgsql-11.7/bin/postgres
Size:                  4 kB
Rss:                   4 kB
Pss:                   0 kB
Shared_Clean:          0 kB
Shared_Dirty:          4 kB
Private_Clean:         0 kB
Private_Dirty:         0 kB
...
...
...

2.5 使用smem查看进程占用的实际内存

  如果完全不包括共享部分的内存,可以看上面“USS”列显示的内存。

  • VSS- Virtual Set Size 虚拟内存(包含共享库占用的内存)
  • RSS- Resident Set Size 实际使用物理内存(包含共享库占用的内存)
  • PSS- Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
  • USS- Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)
smem -t -r  | head -20
  PID User     Command                         Swap      USS      PSS      RSS 
12035 postgres postgres: startup   recover        0   120800  5012485 10186640 
12047 postgres postgres: checkpointer             0   104684  4995433 10115820 
12032 postgres /usr/pgsql-11.7/bin/postgre        0   510344   664110  1054460 
28643 postgres postgres: inofa greenerp201        0   433236   512166   773388 
27838 postgres postgres: inofa greenerp201        0    19568   282248   914964 
27462 postgres postgres: inofa greenerp201        0    19820   279398   913364 
27913 postgres postgres: inofa greenerp201        0    10812   271796   906096 
12048 postgres postgres: background writer        0      168    68658   264096 
27139 postgres postgres: inofa greenerp201        0    26880    67974   229700 
27732 postgres postgres: inofa greenerp201        0    14272    36483   104760 
27264 postgres postgres: inofa greenerp201        0    15512    29736    90616 
28234 postgres postgres: inofa greenerp201        0    13904    23913    66956 
27861 postgres postgres: inofa greenerp201        0     7216    21375    61788 
27287 postgres postgres: inofa greenerp201        0     9236    18343    67220 
28654 postgres postgres: inofa greenerp201        0     8124    15647    70764 
  493 root     /usr/lib/systemd/systemd-jo        0     5760    13507    27548 
 1211 root     ../pyenv/bin/python clup_ag        0    13424    13469    14524 
  780 root     /sbin/dhclient -d -q -sf /u        0    12484    12680    14824 
  887 postgres postgres: inofa greenerp201        0    10104    12438    35852