ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใƒขใƒ‹ใ‚ฟใƒชใƒณใ‚ฐใฏใ€ใ‚ฏใ‚จใƒชใƒกใƒˆใƒชใ‚ฏใ‚นใ€ใ‚ฏใ‚จใƒชใ‚ตใƒณใƒ—ใƒซใ€ๅฎŸ่กŒ่จˆ็”ปใ€ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใฎ็Šถๆ…‹ใ€ใƒ•ใ‚งใ‚คใƒซใ‚ชใƒผใƒใƒผใ€ใ‚คใƒ™ใƒณใƒˆใ‚’ๅ…ฌ้–‹ใ™ใ‚‹ใ“ใจใงใ€Postgres ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใ‚’่ฉณ็ดฐใซๅฏ่ฆ–ๅŒ–ใ—ใพใ™ใ€‚

Agent ใฏใ€่ชญใฟๅ–ใ‚Šๅฐ‚็”จใฎใƒฆใƒผใ‚ถใƒผใจใ—ใฆใƒญใ‚ฐใ‚คใƒณใ™ใ‚‹ใ“ใจใงใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใ‹ใ‚‰็›ดๆŽฅใƒ†ใƒฌใƒกใƒˆใƒชใƒผใ‚’ๅŽ้›†ใ—ใพใ™ใ€‚Postgres ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใงใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใƒขใƒ‹ใ‚ฟใƒชใƒณใ‚ฐใ‚’ๆœ‰ๅŠนใซใ™ใ‚‹ใซใฏใ€ไปฅไธ‹ใฎ่จญๅฎšใ‚’่กŒใฃใฆใใ ใ•ใ„ใ€‚

  1. ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ๆง‹ๆˆใ™ใ‚‹
  2. Agent ใซใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใธใฎใ‚ขใ‚ฏใ‚ปใ‚นใ‚’ไป˜ไธŽใ™ใ‚‹
  3. Agent ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹

ใฏใ˜ใ‚ใซ

ใ‚ตใƒใƒผใƒˆๅฏพ่ฑกใฎ PostgreSQL ใƒใƒผใ‚ธใƒงใƒณ
9.6ใ€10ใ€11ใ€12ใ€13ใ€14ใ€15ใ€16
ๅ‰ๆๆกไปถ
Postgres ใฎ่ฟฝๅŠ ๆไพ›ใƒขใ‚ธใƒฅใƒผใƒซใŒใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใฆใ„ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใปใจใ‚“ใฉใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซใงใฏใ€ใ“ใ‚Œใฏใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใงๅซใพใ‚Œใฆใ„ใพใ™ใŒใ€ใ‚ใพใ‚Šไธ€่ˆฌ็š„ใงใชใ„ใ‚คใƒณใ‚นใƒˆใƒผใƒซใงใฏใ€ใŠไฝฟใ„ใฎใƒใƒผใ‚ธใƒงใƒณใฎ postgresql-contrib ใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใฎ่ฟฝๅŠ ใ‚คใƒณใ‚นใƒˆใƒผใƒซใŒๅฟ…่ฆใซใชใ‚‹ๅ ดๅˆใŒใ‚ใ‚Šใพใ™ใ€‚
ใ‚ตใƒใƒผใƒˆๅฏพ่ฑกใฎ Agent ใƒใƒผใ‚ธใƒงใƒณ
7.36.1+
ใƒ‘ใƒ•ใ‚ฉใƒผใƒžใƒณใ‚นใธใฎๅฝฑ้Ÿฟ
ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใƒขใƒ‹ใ‚ฟใƒชใƒณใ‚ฐใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ Agent ใ‚ณใƒณใƒ•ใ‚ฃใ‚ฎใƒฅใƒฌใƒผใ‚ทใƒงใƒณใฏไฟๅฎˆ็š„ใงใ™ใŒใ€ๅŽ้›†้–“้š”ใ‚„ใ‚ฏใ‚จใƒชใฎใ‚ตใƒณใƒ—ใƒชใƒณใ‚ฐใƒฌใƒผใƒˆใชใฉใฎ่จญๅฎšใ‚’่ชฟๆ•ดใ™ใ‚‹ใ“ใจใงใ€ใ‚ˆใ‚Šใƒ‹ใƒผใ‚บใซๅˆใฃใŸใ‚‚ใฎใซใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ใƒฏใƒผใ‚ฏใƒญใƒผใƒ‰ใฎๅคงๅŠใซใŠใ„ใฆใ€Agent ใฏใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นไธŠใฎใ‚ฏใ‚จใƒชๅฎŸ่กŒๆ™‚้–“ใฎ 1 % ๆœชๆบ€ใ€ใŠใ‚ˆใณ CPU ใฎ 1 % ๆœชๆบ€ใ‚’ๅ ใ‚ใฆใ„ใพใ™ใ€‚

ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใƒขใƒ‹ใ‚ฟใƒชใƒณใ‚ฐใฏใ€ใƒ™ใƒผใ‚นใจใชใ‚‹ Agent ไธŠใฎใ‚คใƒณใƒ†ใ‚ฐใƒฌใƒผใ‚ทใƒงใƒณใจใ—ใฆๅ‹•ไฝœใ—ใพใ™ (ใƒ™ใƒณใƒใƒžใƒผใ‚ฏใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„)ใ€‚
ใƒ—ใƒญใ‚ญใ‚ทใ€ใƒญใƒผใƒ‰ใƒใƒฉใƒณใ‚ตใƒผใ€ใ‚ณใƒใ‚ฏใ‚ทใƒงใƒณใƒ—ใƒผใƒฉใƒผ
Datadog Agent ใฏใ€็›ฃ่ฆ–ๅฏพ่ฑกใฎใƒ›ใ‚นใƒˆใซ็›ดๆŽฅๆŽฅ็ถšใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ใ‚ปใƒซใƒ•ใƒ›ใ‚นใƒˆๅž‹ใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใฎๅ ดๅˆใฏใ€127.0.0.1 ใพใŸใฏใ‚ฝใ‚ฑใƒƒใƒˆใŒๆŽจๅฅจใ•ใ‚Œใพใ™ใ€‚Agent ใฏใ€ใƒ—ใƒญใ‚ญใ‚ทใ€ใƒญใƒผใƒ‰ใƒใƒฉใƒณใ‚ตใƒผใ€ใพใŸใฏ pgbouncer ใชใฉใฎใ‚ณใƒใ‚ฏใ‚ทใƒงใƒณใƒ—ใƒผใƒฉใƒผใ‚’ไป‹ใ—ใฆใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใซๆŽฅ็ถšใ™ในใใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚Agent ใŒๅฎŸ่กŒไธญใซ็•ฐใชใ‚‹ใƒ›ใ‚นใƒˆใซๆŽฅ็ถšใ™ใ‚‹ใจ (ใƒ•ใ‚งใ‚คใƒซใ‚ชใƒผใƒใƒผใ‚„ใƒญใƒผใƒ‰ใƒใƒฉใƒณใ‚ทใƒณใ‚ฐใชใฉใฎๅ ดๅˆ)ใ€Agent ใฏ 2 ใคใฎใƒ›ใ‚นใƒˆ้–“ใง็ตฑ่จˆๆƒ…ๅ ฑใฎๅทฎใ‚’่จˆ็ฎ—ใ—ใ€ไธๆญฃ็ขบใชใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’็”Ÿๆˆใ—ใพใ™ใ€‚
ใƒ‡ใƒผใ‚ฟใ‚ปใ‚ญใƒฅใƒชใƒ†ใ‚ฃใธใฎ้…ๆ…ฎ
Agent ใŒใŠๅฎขๆง˜ใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใ‹ใ‚‰ใฉใฎใ‚ˆใ†ใชใƒ‡ใƒผใ‚ฟใ‚’ๅŽ้›†ใ™ใ‚‹ใ‹ใ€ใพใŸใใฎใƒ‡ใƒผใ‚ฟใฎๅฎ‰ๅ…จๆ€งใ‚’ใฉใฎใ‚ˆใ†ใซ็ขบไฟใ—ใฆใ„ใ‚‹ใ‹ใซใคใ„ใฆใฏใ€ๆฉŸๅฏ†ๆƒ…ๅ ฑใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚

Postgres ่จญๅฎšใ‚’ๆง‹ๆˆใ™ใ‚‹

postgresql.conf ใซไปฅไธ‹ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใ‚’ๆง‹ๆˆใ—ใ€ใ‚ตใƒผใƒใƒผใ‚’ๅ†่ตทๅ‹•ใ™ใ‚‹ใจ่จญๅฎšใŒๆœ‰ๅŠนใซใชใ‚Šใพใ™ใ€‚ใ“ใ‚Œใ‚‰ใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€Postgres ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚

ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผๅ€ค่ชฌๆ˜Ž
shared_preload_librariespg_stat_statementspostgresql.queries.* ใƒกใƒˆใƒชใ‚ฏใ‚นใซๅฏพใ—ใฆๅฟ…่ฆใงใ™ใ€‚pg_stat_statements ๆ‹กๅผตๆฉŸ่ƒฝใ‚’ไฝฟ็”จใ—ใฆใ€ใ‚ฏใ‚จใƒชใƒกใƒˆใƒชใ‚ฏใ‚นใฎๅŽ้›†ใ‚’ๅฏ่ƒฝใซใ—ใพใ™ใ€‚
track_activity_query_size4096ใ‚ˆใ‚Šๅคงใใชใ‚ฏใ‚จใƒชใ‚’ๅŽ้›†ใ™ใ‚‹ใŸใ‚ใซๅฟ…่ฆใงใ™ใ€‚pg_stat_activity ใฎ SQL ใƒ†ใ‚ญใ‚นใƒˆใฎใ‚ตใ‚คใ‚บใ‚’ๆ‹กๅคงใ—ใพใ™ใ€‚ ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅ€คใฎใพใพใ ใจใ€1024 ๆ–‡ๅญ—ใ‚’่ถ…ใˆใ‚‹ใ‚ฏใ‚จใƒชใฏๅŽ้›†ใ•ใ‚Œใพใ›ใ‚“ใ€‚
pg_stat_statements.trackALLใ‚ชใƒ—ใ‚ทใƒงใƒณใงใ™ใ€‚ใ‚นใƒˆใ‚ขใƒ‰ใƒ—ใƒญใ‚ทใƒผใ‚ธใƒฃใ‚„้–ขๆ•ฐๅ†…ใฎใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใ‚’่ฟฝ่ทกใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚
pg_stat_statements.max10000ใ‚ชใƒ—ใ‚ทใƒงใƒณใงใ™ใ€‚pg_stat_statements ใง่ฟฝ่ทกใ™ใ‚‹ๆญฃ่ฆๅŒ–ใ•ใ‚ŒใŸใ‚ฏใ‚จใƒชใฎๆ•ฐใ‚’ๅข—ใ‚„ใ—ใพใ™ใ€‚ใ“ใฎ่จญๅฎšใฏใ€ๅคšใใฎ็•ฐใชใ‚‹ใ‚ฏใƒฉใ‚คใ‚ขใƒณใƒˆใ‹ใ‚‰ใ•ใพใ–ใพใช็จฎ้กžใฎใ‚ฏใ‚จใƒชใŒ้€ไฟกใ•ใ‚Œใ‚‹ๅคงๅฎน้‡ใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใซๆŽจๅฅจใ•ใ‚Œใพใ™ใ€‚
pg_stat_statements.track_utilityoffใ‚ชใƒ—ใ‚ทใƒงใƒณใ€‚PREPARE ใ‚„ EXPLAIN ใจใ„ใฃใŸใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃใ‚ณใƒžใƒณใƒ‰ใ‚’็„กๅŠนใซใ—ใพใ™ใ€‚ใ“ใฎๅ€คใ‚’ off ใซ่จญๅฎšใ™ใ‚‹ใจใ€SELECTใ€UPDATEใ€DELETE ใฎใ‚ˆใ†ใชใ‚ฏใ‚จใƒชใฎใฟใŒ่ฟฝ่ทกใ•ใ‚Œใพใ™ใ€‚
track_io_timingonใ‚ชใƒ—ใ‚ทใƒงใƒณใ€‚ใ‚ฏใ‚จใƒชใฎใƒ–ใƒญใƒƒใ‚ฏใฎ่ชญใฟๅ–ใ‚ŠใŠใ‚ˆใณๆ›ธใ่พผใฟๆ™‚้–“ใฎๅŽ้›†ใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚

Agent ใซใ‚ขใ‚ฏใ‚ปใ‚นใ‚’ไป˜ไธŽใ™ใ‚‹

Datadog Agent ใŒ็ตฑ่จˆใ‚„ใ‚ฏใ‚จใƒชใ‚’ๅŽ้›†ใ™ใ‚‹ใŸใ‚ใซใฏใ€ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚น ใ‚ตใƒผใƒใƒผใธใฎ่ชญใฟๅ–ใ‚Šๅฐ‚็”จใฎใ‚ขใ‚ฏใ‚ปใ‚นใŒๅฟ…่ฆใจใชใ‚Šใพใ™ใ€‚

Postgres ใŒ่ค‡่ฃฝใ•ใ‚Œใฆใ„ใ‚‹ๅ ดๅˆใ€ไปฅไธ‹ใฎ SQL ใ‚ณใƒžใƒณใƒ‰ใฏใ‚ฏใƒฉใ‚นใ‚ฟใƒผๅ†…ใฎใƒ—ใƒฉใ‚คใƒžใƒชใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใ‚ตใƒผใƒใƒผ (ใƒฉใ‚คใ‚ฟใƒผ) ใงๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚Agent ใŒๆŽฅ็ถšใ™ใ‚‹ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใ‚ตใƒผใƒใƒผไธŠใฎ PostgreSQL ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใ‚’้ธๆŠžใ—ใพใ™ใ€‚Agent ใฏใ€ใฉใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใซๆŽฅ็ถšใ—ใฆใ‚‚ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใ‚ตใƒผใƒใƒผไธŠใฎใ™ในใฆใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใ‹ใ‚‰ใƒ†ใƒฌใƒกใƒˆใƒชใƒผใ‚’ๅŽ้›†ใ™ใ‚‹ใ“ใจใŒใงใใ‚‹ใŸใ‚ใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎ postgres ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใ‚’ไฝฟ็”จใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚[ใใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใซๅฏพใ—ใฆใ€ๅ›บๆœ‰ใฎใƒ‡ใƒผใ‚ฟใซๅฏพใ™ใ‚‹ใ‚ซใ‚นใ‚ฟใƒ ใ‚ฏใ‚จใƒช]ใ‚’ AgentใงๅฎŸ่กŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ๅ ดๅˆใฎใฟๅˆฅใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใ‚’้ธๆŠžใ—ใฆใใ ใ•ใ„6ใ€‚

้ธๆŠžใ—ใŸใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใซใ€ใ‚นใƒผใƒ‘ใƒผใƒฆใƒผใ‚ถใƒผ (ใพใŸใฏๅๅˆ†ใชๆจฉ้™ใ‚’ๆŒใคไป–ใฎใƒฆใƒผใ‚ถใƒผ) ใจใ—ใฆๆŽฅ็ถšใ—ใพใ™ใ€‚ไพ‹ใˆใฐใ€้ธๆŠžใ—ใŸใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใŒ postgres ใงใ‚ใ‚‹ๅ ดๅˆใฏใ€ๆฌกใฎใ‚ˆใ†ใซๅฎŸ่กŒใ—ใฆ psql ใ‚’ไฝฟ็”จใ™ใ‚‹ postgres ใƒฆใƒผใ‚ถใƒผใจใ—ใฆๆŽฅ็ถšใ—ใพใ™ใ€‚

psql -h mydb.example.com -d postgres -U postgres

datadog ใƒฆใƒผใ‚ถใƒผใ‚’ไฝœๆˆใ—ใพใ™ใ€‚

CREATE USER datadog WITH password '<PASSWORD>';

datadog ใƒฆใƒผใ‚ถใƒผใซ้–ข้€ฃใƒ†ใƒผใƒ–ใƒซใธใฎๆจฉ้™ใ‚’ไป˜ไธŽใ—ใพใ™ใ€‚

ALTER ROLE datadog INHERIT;

ใ™ในใฆใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใซไปฅไธ‹ใฎใ‚นใ‚ญใƒผใƒžใ‚’ไฝœๆˆใ—ใพใ™ใ€‚

CREATE SCHEMA datadog;
GRANT USAGE ON SCHEMA datadog TO datadog;
GRANT USAGE ON SCHEMA public TO datadog;
GRANT pg_monitor TO datadog;
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

ใ™ในใฆใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใซไปฅไธ‹ใฎใ‚นใ‚ญใƒผใƒžใ‚’ไฝœๆˆใ—ใพใ™ใ€‚

CREATE SCHEMA datadog;
GRANT USAGE ON SCHEMA datadog TO datadog;
GRANT USAGE ON SCHEMA public TO datadog;
GRANT pg_monitor TO datadog;
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

ใ™ในใฆใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใซไปฅไธ‹ใฎใ‚นใ‚ญใƒผใƒžใ‚’ไฝœๆˆใ—ใพใ™ใ€‚

CREATE SCHEMA datadog;
GRANT USAGE ON SCHEMA datadog TO datadog;
GRANT USAGE ON SCHEMA public TO datadog;
GRANT SELECT ON pg_stat_database TO datadog;
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

ใ™ในใฆใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใซ้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใฆใ€Agent ใŒ pg_stat_activity ใŠใ‚ˆใณ pg_stat_statements ใฎๅ…จใ‚ณใƒณใƒ†ใƒณใƒ„ใ‚’่ชญใฟ่พผใ‚ใ‚‹ใ‚ˆใ†ใซใ—ใพใ™ใ€‚

CREATE OR REPLACE FUNCTION datadog.pg_stat_activity() RETURNS SETOF pg_stat_activity AS
  $$ SELECT * FROM pg_catalog.pg_stat_activity; $$
LANGUAGE sql
SECURITY DEFINER;
CREATE OR REPLACE FUNCTION datadog.pg_stat_statements() RETURNS SETOF pg_stat_statements AS
    $$ SELECT * FROM pg_stat_statements; $$
LANGUAGE sql
SECURITY DEFINER;
่ฟฝๅŠ ใฎใƒ†ใƒผใƒ–ใƒซใ‚’ใ‚ฏใ‚จใƒชใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใƒ‡ใƒผใ‚ฟๅŽ้›†ใพใŸใฏใ‚ซใ‚นใ‚ฟใƒ ใƒกใƒˆใƒชใ‚ฏใ‚นใฎๅ ดๅˆใฏใ€ใใ‚Œใ‚‰ใฎใƒ†ใƒผใƒ–ใƒซใฎ SELECT ๆจฉ้™ใ‚’ datadog ใƒฆใƒผใ‚ถใƒผใซไป˜ไธŽใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ไพ‹: grant SELECT on <TABLE_NAME> to datadog; ่ฉณ็ดฐใฏ PostgreSQL ใ‚ซใ‚นใ‚ฟใƒ ใƒกใƒˆใƒชใ‚ฏใ‚นใฎๅŽ้›†ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚

Agent ใŒๅฎŸ่กŒ่จˆ็”ปใ‚’ๅŽ้›†ใงใใ‚‹ใ‚ˆใ†ใซใ€ใ™ในใฆใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใซ้–ขๆ•ฐใ‚’ไฝœๆˆใ—ใพใ™ใ€‚

CREATE OR REPLACE FUNCTION datadog.explain_statement(
   l_query TEXT,
   OUT explain JSON
)
RETURNS SETOF JSON AS
$$
DECLARE
curs REFCURSOR;
plan JSON;

BEGIN
   OPEN curs FOR EXECUTE pg_catalog.concat('EXPLAIN (FORMAT JSON) ', l_query);
   FETCH curs INTO plan;
   CLOSE curs;
   RETURN QUERY SELECT plan;
END;
$$
LANGUAGE 'plpgsql'
RETURNS NULL ON NULL INPUT
SECURITY DEFINER;

ใƒ‘ใ‚นใƒฏใƒผใƒ‰ใ‚’ๅฎ‰ๅ…จใซไฟ็ฎก

Store your password using secret management software such as Vault. You can then reference this password as ENC[<SECRET_NAME>] in your Agent configuration files: for example, ENC[datadog_user_database_password]. See Secrets Management for more information.

The examples on this page use datadog_user_database_password to refer to the name of the secret where your password is stored. It is possible to reference your password in plain text, but this is not recommended.

ๆคœ่จผใ™ใ‚‹

ๆจฉ้™ใŒๆญฃใ—ใ„ใ“ใจใ‚’็ขบ่ชใ™ใ‚‹ใŸใ‚ใซใ€ไปฅไธ‹ใฎใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใฆใ€Agent ใƒฆใƒผใ‚ถใƒผใŒใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใซๆŽฅ็ถšใ—ใฆใ‚ณใ‚ขใƒ†ใƒผใƒ–ใƒซใ‚’่ชญใฟๅ–ใ‚‹ใ“ใจใŒใงใใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใพใ™ใ€‚

psql -h localhost -U datadog postgres -A \
  -c "select * from pg_stat_database limit 1;" \
  && echo -e "\e[0;32mPostgres connection - OK\e[0m" \
  || echo -e "\e[0;31mCannot connect to Postgres\e[0m"
psql -h localhost -U datadog postgres -A \
  -c "select * from pg_stat_activity limit 1;" \
  && echo -e "\e[0;32mPostgres pg_stat_activity read OK\e[0m" \
  || echo -e "\e[0;31mCannot read from pg_stat_activity\e[0m"
psql -h localhost -U datadog postgres -A \
  -c "select * from pg_stat_statements limit 1;" \
  && echo -e "\e[0;32mPostgres pg_stat_statements read OK\e[0m" \
  || echo -e "\e[0;31mCannot read from pg_stat_statements\e[0m"
psql -h localhost -U datadog postgres -A \
  -c "select * from pg_stat_database limit 1;" \
  && echo -e "\e[0;32mPostgres connection - OK\e[0m" \
  || echo -e "\e[0;31mCannot connect to Postgres\e[0m"
psql -h localhost -U datadog postgres -A \
  -c "select * from pg_stat_activity limit 1;" \
  && echo -e "\e[0;32mPostgres pg_stat_activity read OK\e[0m" \
  || echo -e "\e[0;31mCannot read from pg_stat_activity\e[0m"
psql -h localhost -U datadog postgres -A \
  -c "select * from pg_stat_statements limit 1;" \
  && echo -e "\e[0;32mPostgres pg_stat_statements read OK\e[0m" \
  || echo -e "\e[0;31mCannot read from pg_stat_statements\e[0m"

ใƒ‘ใ‚นใƒฏใƒผใƒ‰ใฎๅ…ฅๅŠ›ใ‚’ๆฑ‚ใ‚ใ‚‰ใ‚ŒใŸๅ ดๅˆใฏใ€datadog ใƒฆใƒผใ‚ถใƒผใ‚’ไฝœๆˆใ—ใŸใจใใซๅ…ฅๅŠ›ใ—ใŸใƒ‘ใ‚นใƒฏใƒผใƒ‰ใ‚’ไฝฟ็”จใ—ใฆใใ ใ•ใ„ใ€‚

Agent ใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซ

Datadog Agent ใ‚’ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ™ใ‚‹ใจใ€Postgres ใงใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใƒขใƒ‹ใ‚ฟใƒชใƒณใ‚ฐใซๅฟ…่ฆใช Postgres ใƒใ‚งใƒƒใ‚ฏใ‚‚ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ•ใ‚Œใพใ™ใ€‚Postgres ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใƒ›ใ‚นใƒˆใฎ Agent ใ‚’ใพใ ใ‚คใƒณใ‚นใƒˆใƒผใƒซใ—ใฆใ„ใชใ„ๅ ดๅˆใฏใ€Agent ใฎใ‚คใƒณใ‚นใƒˆใƒผใƒซๆ‰‹้ †ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚

  1. Agent ใฎ conf.d/postgres.d/conf.yaml ใƒ•ใ‚กใ‚คใƒซใ‚’็ทจ้›†ใ—ใฆใ€host / port ใ‚’ๆŒ‡ๅฎšใ—ใ€็›ฃ่ฆ–ใ™ใ‚‹ใƒ›ใ‚นใƒˆใ‚’่จญๅฎšใ—ใพใ™ใ€‚ไฝฟ็”จๅฏ่ƒฝใชใ™ในใฆใฎใ‚ณใƒณใƒ•ใ‚ฃใ‚ฎใƒฅใƒฌใƒผใ‚ทใƒงใƒณใ‚ชใƒ—ใ‚ทใƒงใƒณใซใคใ„ใฆใฏใ€ใ‚ตใƒณใƒ—ใƒซ postgres.d/conf.yaml ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚
init_config:
instances:
  - dbm: true
    host: localhost
    port: 5432
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    ## ใ‚ชใƒ—ใ‚ทใƒงใƒณ: `custom_queries` ใซๅฟ…่ฆใชๅ ดๅˆใฏใ€ๅˆฅใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใซๆŽฅ็ถšใ—ใพใ™
    # dbname: '<DB_NAME>'
init_config:
instances:
  - dbm: true
    host: localhost
    port: 5432
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    pg_stat_statements_view: datadog.pg_stat_statements()
    pg_stat_activity_view: datadog.pg_stat_activity()
    ## ใ‚ชใƒ—ใ‚ทใƒงใƒณ: `custom_queries` ใซๅฟ…่ฆใชๅ ดๅˆใฏใ€ๅˆฅใฎใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใซๆŽฅ็ถšใ—ใพใ™
    # dbname: '<DB_NAME>'

ๆณจ: ใƒ‘ใ‚นใƒฏใƒผใƒ‰ใซ็‰นๆฎŠๆ–‡ๅญ—ใŒๅซใพใ‚Œใ‚‹ๅ ดๅˆใฏใ€ๅ˜ไธ€ๅผ•็”จ็ฌฆใงๅ›ฒใ‚“ใงใใ ใ•ใ„ใ€‚

  1. Agent ใ‚’ๅ†่ตทๅ‹•ใ—ใพใ™ใ€‚

ใƒญใ‚ฐใฎๅŽ้›† (ใ‚ชใƒ—ใ‚ทใƒงใƒณ)

PostgreSQL ใฎใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใฎใƒญใ‚ฐใฏ stderr ใซ่จ˜้Œฒใ•ใ‚Œใ€ใƒญใ‚ฐใซ่ฉณ็ดฐใชๆƒ…ๅ ฑใฏๅซใพใ‚Œใพใ›ใ‚“ใ€‚ใƒญใ‚ฐ่กŒใฎใƒ—ใƒฌใƒ•ใ‚ฃใƒƒใ‚ฏใ‚นใซๆŒ‡ๅฎšใ•ใ‚ŒใŸ่ฉณ็ดฐใ‚’่ฟฝๅŠ ใ—ใฆใƒ•ใ‚กใ‚คใƒซใซ่จ˜้Œฒใ™ใ‚‹ใ“ใจใ‚’ใŠๅ‹งใ‚ใ—ใพใ™ใ€‚่ฉณ็ดฐใซใคใ„ใฆใฏใ€ใ“ใฎใƒˆใƒ”ใƒƒใ‚ฏใซ้–ขใ™ใ‚‹ PostgreSQL ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚

  1. ใƒญใ‚ฎใƒณใ‚ฐใฏใƒ•ใ‚กใ‚คใƒซ /etc/postgresql/<ใƒใƒผใ‚ธใƒงใƒณ>/main/postgresql.conf ๅ†…ใงๆง‹ๆˆใ•ใ‚Œใพใ™ใ€‚ใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆๅ‡บๅŠ›ใ‚’ๅซใ‚€้€šๅธธใฎใƒญใ‚ฐ็ตๆžœใฎๅ ดๅˆใ€ใƒญใ‚ฐใ‚ปใ‚ฏใ‚ทใƒงใƒณใฎๆฌกใฎใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎใ‚ณใƒกใƒณใƒˆใ‚’ๅค–ใ—ใพใ™ใ€‚

      logging_collector = on
      log_directory = 'pg_log'  # directory where log files are written,
                                # can be absolute or relative to PGDATA
      log_filename = 'pg.log'   # log file name, can include pattern
      log_statement = 'all'     # log all queries
      #log_duration = on
      log_line_prefix= '%m [%p] %d %a %u %h %c '
      log_file_mode = 0644
      ## For Windows
      #log_destination = 'eventlog'
    
  2. ่ฉณ็ดฐใชๆœŸ้–“ใƒกใƒˆใƒชใ‚ฏใ‚นใ‚’ๅŽ้›†ใ—ใ€Datadog ใ‚คใƒณใ‚ฟใƒผใƒ•ใ‚งใƒผใ‚นใงๆคœ็ดขๅฏ่ƒฝใซใ™ใ‚‹ใซใฏใ€ใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆ่‡ชไฝ“ใ‚’ไฝฟ็”จใ—ใฆใ‚คใƒณใƒฉใ‚คใƒณใงๆง‹ๆˆใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ไธŠ่จ˜ใฎไพ‹ใจๆŽจๅฅจใ‚ณใƒณใƒ•ใ‚ฃใ‚ฎใƒฅใƒฌใƒผใ‚ทใƒงใƒณใจใฎ้•ใ„ใซใคใ„ใฆใฏใ€ไปฅไธ‹ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚ใพใŸใ€log_statement ใ‚ชใƒ—ใ‚ทใƒงใƒณใจ log_duration ใ‚ชใƒ—ใ‚ทใƒงใƒณใฎไธกๆ–นใŒใ‚ณใƒกใƒณใƒˆใ‚ขใ‚ฆใƒˆใ•ใ‚Œใฆใ„ใ‚‹ใฎใงๆณจๆ„ใ—ใฆใใ ใ•ใ„ใ€‚ใ“ใฎใƒˆใƒ”ใƒƒใ‚ฏใซ้–ขใ™ใ‚‹่ญฐ่ซ–ใฏใ“ใกใ‚‰ใ‚’ใ”่ฆงใใ ใ•ใ„ใ€‚

    ใ“ใฎๆง‹ๆˆใฏใ™ในใฆใฎใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใ‚’ใƒญใ‚ฐใ—ใพใ™ใŒใ€ๅ‡บๅŠ›ใ‚’็‰นๅฎšใฎๆœŸ้–“ใ‚’ๆŒใคใ‚‚ใฎใซๆธ›ใ‚‰ใ™ใซใฏใ€log_min_duration_statement ใฎๅ€คใ‚’็›ฎ็š„ใฎๆœ€ๅฐๆœŸ้–“๏ผˆใƒŸใƒช็ง’ๅ˜ไฝ๏ผ‰ใซ่จญๅฎšใ—ใพใ™๏ผˆๅฎŒๅ…จใช SQL ใ‚นใƒ†ใƒผใƒˆใƒกใƒณใƒˆใฎใƒญใ‚ฐ่จ˜้ŒฒใŒ็ต„็น”ใฎใƒ—ใƒฉใ‚คใƒใ‚ทใƒผ่ฆไปถใซๆบ–ๆ‹ ใ—ใฆใ„ใ‚‹ใ“ใจใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„๏ผ‰ใ€‚

      log_min_duration_statement = 0    # -1 is disabled, 0 logs all statements
                                        # and their durations, > 0 logs only
                                        # statements running at least this number
                                        # of milliseconds
      #log_statement = 'all'
      #log_duration = on
    
  3. Datadog Agent ใงใ€ใƒญใ‚ฐใฎๅŽ้›†ใฏใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใง็„กๅŠนใซใชใฃใฆใ„ใพใ™ใ€‚ไปฅไธ‹ใฎใ‚ˆใ†ใซใ€datadog.yaml ใƒ•ใ‚กใ‚คใƒซใงใ“ใ‚Œใ‚’ๆœ‰ๅŠนใซใ—ใพใ™ใ€‚

    logs_enabled: true
    
  4. PostgreSQL ใฎใƒญใ‚ฐใฎๅŽ้›†ใ‚’้–‹ๅง‹ใ™ใ‚‹ใซใฏใ€ๆฌกใฎๆง‹ๆˆใƒ–ใƒญใƒƒใ‚ฏใ‚’ conf.d/postgres.d/conf.yaml ใƒ•ใ‚กใ‚คใƒซใซ่ฟฝๅŠ ใ—ใ€็ทจ้›†ใ—ใพใ™ใ€‚

    logs:
      - type: file
        path: "<LOG_FILE_PATH>"
        source: postgresql
        service: "<SERVICE_NAME>"
        #To handle multi line that starts with yyyy-mm-dd use the following pattern
        #log_processing_rules:
        #  - type: multi_line
        #    pattern: \d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])
        #    name: new_log_start_with_date
    

    service ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใจ path ใƒ‘ใƒฉใƒกใƒผใ‚ฟใƒผใฎๅ€คใ‚’ๅค‰ๆ›ดใ—ใ€็’ฐๅขƒใซๅˆใ‚ใ›ใฆๆง‹ๆˆใ—ใฆใใ ใ•ใ„ใ€‚ไฝฟ็”จๅฏ่ƒฝใชใ™ในใฆใฎๆง‹ๆˆใ‚ชใƒ—ใ‚ทใƒงใƒณใฎ่ฉณ็ดฐใซใคใ„ใฆใฏใ€ใ‚ตใƒณใƒ—ใƒซ postgres.d/conf.yaml ใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚

  5. Agent ใ‚’ๅ†่ตทๅ‹•ใ—ใพใ™ใ€‚

UpdateAzureIntegration

Agent ใฎ status ใ‚ตใƒ–ใ‚ณใƒžใƒณใƒ‰ใ‚’ๅฎŸ่กŒใ—ใ€Checks ใ‚ปใ‚ฏใ‚ทใƒงใƒณใง postgres ใ‚’ๆŽขใ—ใพใ™ใ€‚ใพใŸใฏใ€ใƒ‡ใƒผใ‚ฟใƒ™ใƒผใ‚นใฎใƒšใƒผใ‚ธใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚

Agent ใฎๆง‹ๆˆไพ‹

One agent connecting to multiple hosts

It is common to configure a single Agent host to connect to multiple remote database instances (see Agent installation architectures for DBM). To connect to multiple hosts, create an entry for each host in the Postgres integration config.

Datadog recommends using one Agent to monitor no more than 30 database instances.

Benchmarks show that one Agent running on a t4g.medium EC2 instance (2 CPUs and 4GB of RAM) can successfully monitor 30 RDS db.t3.medium instances (2 CPUs and 4GB of RAM).
init_config:
instances:
  - dbm: true
    host: example-service-primary.example-host.com
    port: 5432
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    tags:
      - 'env:prod'
      - 'team:team-discovery'
      - 'service:example-service'
  - dbm: true
    host: example-serviceโ€“replica-1.example-host.com
    port: 5432
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    tags:
      - 'env:prod'
      - 'team:team-discovery'
      - 'service:example-service'
  - dbm: true
    host: example-serviceโ€“replica-2.example-host.com
    port: 5432
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    tags:
      - 'env:prod'
      - 'team:team-discovery'
      - 'service:example-service'
    [...]

Monitoring multiple databases on a database host

Use the database_autodiscovery option to permit the Agent to discover all databases on your host to monitor. You can specify include or exclude fields to narrow the scope of databases discovered. See the sample postgres.d/conf.yaml for more details.

init_config:
instances:
  - dbm: true
    host: example-service-primary.example-host.com
    port: 5432
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    database_autodiscovery:
      enabled: true
      # Optionally, set the include field to specify
      # a set of databases you are interested in discovering
      include:
        - mydb.*
        - example.*
    tags:
      - 'env:prod'
      - 'team:team-discovery'
      - 'service:example-service'

Running custom queries

To collect custom metrics, use the custom_queries option. See the sample postgres.d/conf.yaml for more details.

init_config:
instances:
  - dbm: true
    host: localhost
    port: 5432
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    custom_queries:
    - metric_prefix: employee
      query: SELECT age, salary, hours_worked, name FROM hr.employees;
      columns:
        - name: custom.employee_age
          type: gauge
        - name: custom.employee_salary
           type: gauge
        - name: custom.employee_hours
           type: count
        - name: name
           type: tag
      tags:
        - 'table:employees'

Monitoring relation metrics for multiple databases

In order to collect relation metrics (such as postgresql.seq_scans, postgresql.dead_rows, postgresql.index_rows_read, and postgresql.table_size), the Agent must be configured to connect to each database (by default, the Agent only connects to the postgres database).

Specify a single โ€œDBMโ€ instance to collect DBM telemetry from all databases. Use the database_autodiscovery option to avoid specifying each database name.

init_config:
instances:
  # This instance is the "DBM" instance. It will connect to the
  # all logical databases, and send DBM telemetry from all databases
  - dbm: true
    host: example-service-primary.example-host.com
    port: 5432
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    database_autodiscovery:
      enabled: true
      exclude:
        - ^users$
        - ^inventory$
    relations:
      - relation_regex: .*
  # This instance only collects data from the `users` database
  # and collects relation metrics from tables prefixed by "2022_"
  - host: example-service-primary.example-host.com
    port: 5432
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    dbname: users
    dbstrict: true
    relations:
      - relation_regex: 2022_.*
        relkind:
          - r
          - i
  # This instance only collects data from the `inventory` database
  # and collects relation metrics only from the specified tables
  - host: example-service-primary.example-host.com
    port: 5432
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    dbname: inventory
    dbstrict: true
    relations:
      - relation_name: products
      - relation_name: external_seller_products

Collecting schemas

To enable this feature, use the collect_schemas option. You must also configure the Agent to connect to each logical database.

Use the database_autodiscovery option to avoid specifying each logical database. See the sample postgres.d/conf.yaml for more details.

init_config:
# This instance only collects data from the `users` database
# and collects relation metrics only from the specified tables
instances:
  - dbm: true
    host: example-service-primary.example-host.com
    port: 5432
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    dbname: users
    dbstrict: true
    collect_schemas:
      enabled: true
    relations:
      - products
      - external_seller_products
  # This instance detects every logical database automatically
  # and collects relation metrics from every table
  - dbm: true
    host: example-serviceโ€“replica-1.example-host.com
    port: 5432
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    database_autodiscovery:
      enabled: true
    collect_schemas:
      enabled: true
    relations:
      - relation_regex: .*

Working with hosts through a proxy

If the Agent must connect through a proxy such as the Cloud SQL Auth proxy, all telemetry is tagged with the hostname of the proxy rather than the database instance. Use the reported_hostname option to set a custom override of the hostname detected by the Agent.

init_config:
instances:
  - dbm: true
    host: localhost
    port: 5000
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    reported_hostname: example-service-primary
  - dbm: true
    host: localhost
    port: 5001
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    reported_hostname: example-service-replica-1

ใƒˆใƒฉใƒ–ใƒซใ‚ทใƒฅใƒผใƒ†ใ‚ฃใƒณใ‚ฐ

ใ‚คใƒณใƒ†ใ‚ฐใƒฌใƒผใ‚ทใƒงใƒณใจ Agent ใ‚’ๆ‰‹้ †้€šใ‚Šใซใ‚คใƒณใ‚นใƒˆใƒผใƒซใƒป่จญๅฎšใ—ใฆใ‚‚ๆœŸๅพ…้€šใ‚Šใซๅ‹•ไฝœใ—ใชใ„ๅ ดๅˆใฏใ€ใƒˆใƒฉใƒ–ใƒซใ‚ทใƒฅใƒผใƒ†ใ‚ฃใƒณใ‚ฐใ‚’ๅ‚็…งใ—ใฆใใ ใ•ใ„ใ€‚

ๅ‚่€ƒ่ณ‡ๆ–™

ใŠๅฝนใซ็ซ‹ใคใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆใ€ใƒชใƒณใ‚ฏใ‚„่จ˜ไบ‹: