Primary Postgres database process (postmaster) eating all CPU

I’ve got a 2-container install on a DO 8GB droplet that is behaving very strangely.

There is a postmaster (EDIT: now there are two of them) processing eating 100% CPU.
Sidekiq is running, but the Dashboard complains that it’s not checking for updates.

There are some logs like

  PG::ConnectionBad (FATAL: remaining connection slots are reserved for non-replication superuser connections ) /var/www/discourse/vendor/bundle/ruby/2.4.0/gems/pg-0.21.0/lib/pg.rb:56:in `initialize'

and

Job exception: FATAL: remaining connection slots are reserved for non-replication superuser connections	

The data container has:

  db_shared_buffers: "2GB"
  db_work_mem: "40MB"

There are 4 unicorn workers in the web container (same as # processors).

Plugins:

          - git clone https://github.com/discourse/docker_manager.git
          #- git clone https://github.com/SumatoSoft/discourse-adplugin.git
          #- git clone https://github.com/davidcelis/new_relic-discourse.git
          - git clone https://github.com/discourse/discourse-cakeday.git
          - git clone https://github.com/ekkans/lrqdo-editor-plugin-discourse.git
          #- git clone https://github.com/davidtaylorhq/discourse-whos-online.git
          - git clone https://github.com/pmusaraj/discourse-onesignal.git

Memory:

KiB Mem :  8174936 total,   169976 free,  1288084 used,  6716876 buff/cache
KiB Swap:  2097148 total,  2094304 free,     2844 used.  4369992 avail Mem 

The postgresql connection limit needs to be increased. That will cause the database as a whole to use more memory, but based on the free output you’ve got plenty that could be used if required. I’d double the current value, and review errors and resource consumption.

Uh. Where is that changed?

You mean this?

  db_work_mem: "80MB"

I did that, but I’m still getting a 502 error on the admin dashboard.

The other issue is that this site is using cloudflare with no caching (I’m told). I have included the cloudflare template, but I still suspect something is wrong with cloudflare.

It’s the max_connections parameter in postgresql.conf. I don’t see a tunable for that in discourse_docker, so I suspect you’ll need to play games with a pups exec stanza to make the edit.

As for Cloudflare, all the cloudflare template does it make it so that IP addresses get fixed after going through Cloudflare proxying. It doesn’t do anything to make Cloudflare cache. You might want to keep that in a separate topic, rather than mix them together in here.

Not one for playing games when they’re not necessary, I went into the data container, edited postgresql.conf by hand, doubled max_connections (from 100 to 200) and, LO! it seems that all is well.

I don’t understand just why I’ve not encountered this before or why this is the solution here. The database doesn’t seem that big and the traffic doesn’t seem that high.

Edit: I have played the games and won!

If anyone else cares. . . stick this in data.yml in hooks in the after_postgres section. I put it after the -exec section.

    # double max_connections to 200
    - replace:
        filename: "/etc/postgresql/9.5/main/postgresql.conf"
        from: /#?max_connections *=.*/
        to: "max_connections = 200"

오래된 스레드를 다시 올리는 건 죄송합니다.

@pfaffman 이 방법으로 포스트마스터가 CPU 사용률을 과도하게 높이는 문제를 해결하셨나요?

postgresql.conf(/var/discourse/shared/standalone/postgres_data/postgresql.conf)에서 max connections를 직접 수정하고 ./launcher rebuild app을 실행해 보았지만, 달라진 점을 느끼지 못했습니다.

문제가 사라진 것 같습니다

Postgres에 더 많은 메모리를 할당해보고, 다시 줄여보기도 했습니다. 스왑을 추가한 것이 도움이 된 것 같습니다(그래서 Postgres에 할당하는 메모리를 줄여본 것입니다). 도움이 되었을 수 있는 또 다른 작업은 데이터베이스를 백업하고 복원한 것입니다. 아니면 아무 효과가 없었을 수도 있고요.

완벽한 해결책은 아니지만, 제가 시도해 본 것들은 위와 같습니다. :confused:

2.5.0.beta5 업데이트를 설치한 후에도 나에게서 같은 문제가 발생하기 시작했습니다. 하나씩 더 많은 postmaster 프로세스가 생성되며, 각 프로세스가 가능한 한 많은 CPU를 사용하다가 완료되기까지 몇 분씩 걸리는 경우가 있습니다. 이로 인해 서버의 AWS 크레딧이 서서히 소진되고, 전체 포럼이 느려지거나 심지어 사용할 수 없게 됩니다.

max_connections를 늘리거나 앱을 재빌드해도 효과가 없었습니다.

2.5.0beta5로 업데이트하기 전까지는 이런 현상을 본 적이 없습니다. 어디서부터 확인해야 할지 힌트를 주실 수 있나요?

어제 포럼을 2.5.0.beta5로 업데이트한 이후로, 포럼이 느리고 응답이 없습니다. 상단에 몇 개의 postmaster 작업이 CPU의 90~100%를 사용 중입니다. 이로 인해 포럼의 여러 부분이 타임아웃되고 사용자에게 502 오류를 반환하고 있습니다.

해당 작업들은 계속 발생하고 사라지지만, 작업이 실행되는 동안 포럼을 제대로 사용하기 어렵습니다.

이것은 Postgres 12 업그레이드의 마무리 단계가 아닌가요? PG10에서 PG12로 마이그레이션한 후 내부적으로 정리해야 할 작업이 있는 것 같습니다. 이런 상황이 하루 이상 지속되나요?

지금까지 13시간이 경과했습니다.

또한 확인차 말씀드리면, 저는 PostgreSQL 10에서 12로 업그레이드했습니다 (10으로 유지하는 것도 선택 사항이므로 명확히 하고 싶었습니다).

이것이 관련이 있는지 확신은 없지만, 사용자 요약 페이지로 이동할 때마다 CPU 사용률이 90% 이상으로 급등하고 항상 502 오류로 종료됩니다. 프로필의 다른 섹션은 느리긴 하지만 작동하는 것 같습니다.

오늘 하루 동안 상황을 지켜보며 스스로 해결되는지 확인한 후 여기에 업데이트하겠습니다.

마이그레이션 후 일부 정리 작업이 필요할 수 있습니다. 여기 공식 업그레이드 토픽을 확인하고 첫 번째 게시물을 자세히 읽어보시면, 상세 정보와 권장 단계가 나와 있습니다 – PostgreSQL 12 update

참고로, 저도 같은 문제를 겪었는데 이렇게 해결했습니다:

@codinghorhor와 @markersocial에게 안내해 주셔서 감사합니다. 하루가 넘게 지나면서 상황이 정상으로 돌아온 것 같습니다. 저는 그저 기다리고만 있었습니다.

상황을 계속 지켜보며 502 에러가 다시 발생하지 않는지 확인해 보겠습니다. (비수기 시간대에는 사용자가 적어 발생하는 문제일 수도 있습니다.)

다시 발생한다면 여러분이 나열해 주신 단계를 시도해 보겠습니다.