pgstat.h 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /* ----------
  2. * pgstat.h
  3. *
  4. * Definitions for the PostgreSQL statistics collector daemon.
  5. *
  6. * Copyright (c) 2001-2016, PostgreSQL Global Development Group
  7. *
  8. * src/include/pgstat.h
  9. * ----------
  10. */
  11. #ifndef PGSTAT_H
  12. #define PGSTAT_H
  13. #include "datatype/timestamp.h"
  14. #include "fmgr.h"
  15. #include "libpq/pqcomm.h"
  16. #include "portability/instr_time.h"
  17. #include "postmaster/pgarch.h"
  18. #include "storage/barrier.h"
  19. #include "storage/proc.h"
  20. #include "utils/hsearch.h"
  21. #include "utils/relcache.h"
  22. /* ----------
  23. * Paths for the statistics files (relative to installation's $PGDATA).
  24. * ----------
  25. */
  26. #define PGSTAT_STAT_PERMANENT_DIRECTORY "pg_stat"
  27. #define PGSTAT_STAT_PERMANENT_FILENAME "pg_stat/global.stat"
  28. #define PGSTAT_STAT_PERMANENT_TMPFILE "pg_stat/global.tmp"
  29. /* Default directory to store temporary statistics data in */
  30. #define PG_STAT_TMP_DIR "pg_stat_tmp"
  31. /* Values for track_functions GUC variable --- order is significant! */
  32. typedef enum TrackFunctionsLevel
  33. {
  34. TRACK_FUNC_OFF,
  35. TRACK_FUNC_PL,
  36. TRACK_FUNC_ALL
  37. } TrackFunctionsLevel;
  38. /* ----------
  39. * The types of backend -> collector messages
  40. * ----------
  41. */
  42. typedef enum StatMsgType
  43. {
  44. PGSTAT_MTYPE_DUMMY,
  45. PGSTAT_MTYPE_INQUIRY,
  46. PGSTAT_MTYPE_TABSTAT,
  47. PGSTAT_MTYPE_TABPURGE,
  48. PGSTAT_MTYPE_DROPDB,
  49. PGSTAT_MTYPE_RESETCOUNTER,
  50. PGSTAT_MTYPE_RESETSHAREDCOUNTER,
  51. PGSTAT_MTYPE_RESETSINGLECOUNTER,
  52. PGSTAT_MTYPE_AUTOVAC_START,
  53. PGSTAT_MTYPE_VACUUM,
  54. PGSTAT_MTYPE_ANALYZE,
  55. PGSTAT_MTYPE_ARCHIVER,
  56. PGSTAT_MTYPE_BGWRITER,
  57. PGSTAT_MTYPE_FUNCSTAT,
  58. PGSTAT_MTYPE_FUNCPURGE,
  59. PGSTAT_MTYPE_RECOVERYCONFLICT,
  60. PGSTAT_MTYPE_TEMPFILE,
  61. PGSTAT_MTYPE_DEADLOCK
  62. } StatMsgType;
  63. /* ----------
  64. * The data type used for counters.
  65. * ----------
  66. */
  67. typedef int64 PgStat_Counter;
  68. /* ----------
  69. * PgStat_TableCounts The actual per-table counts kept by a backend
  70. *
  71. * This struct should contain only actual event counters, because we memcmp
  72. * it against zeroes to detect whether there are any counts to transmit.
  73. * It is a component of PgStat_TableStatus (within-backend state) and
  74. * PgStat_TableEntry (the transmitted message format).
  75. *
  76. * Note: for a table, tuples_returned is the number of tuples successfully
  77. * fetched by heap_getnext, while tuples_fetched is the number of tuples
  78. * successfully fetched by heap_fetch under the control of bitmap indexscans.
  79. * For an index, tuples_returned is the number of index entries returned by
  80. * the index AM, while tuples_fetched is the number of tuples successfully
  81. * fetched by heap_fetch under the control of simple indexscans for this index.
  82. *
  83. * tuples_inserted/updated/deleted/hot_updated count attempted actions,
  84. * regardless of whether the transaction committed. delta_live_tuples,
  85. * delta_dead_tuples, and changed_tuples are set depending on commit or abort.
  86. * Note that delta_live_tuples and delta_dead_tuples can be negative!
  87. * ----------
  88. */
  89. typedef struct PgStat_TableCounts
  90. {
  91. PgStat_Counter t_numscans;
  92. PgStat_Counter t_tuples_returned;
  93. PgStat_Counter t_tuples_fetched;
  94. PgStat_Counter t_tuples_inserted;
  95. PgStat_Counter t_tuples_updated;
  96. PgStat_Counter t_tuples_deleted;
  97. PgStat_Counter t_tuples_hot_updated;
  98. bool t_truncated;
  99. PgStat_Counter t_delta_live_tuples;
  100. PgStat_Counter t_delta_dead_tuples;
  101. PgStat_Counter t_changed_tuples;
  102. PgStat_Counter t_blocks_fetched;
  103. PgStat_Counter t_blocks_hit;
  104. } PgStat_TableCounts;
  105. /* Possible targets for resetting cluster-wide shared values */
  106. typedef enum PgStat_Shared_Reset_Target
  107. {
  108. RESET_ARCHIVER,
  109. RESET_BGWRITER
  110. } PgStat_Shared_Reset_Target;
  111. /* Possible object types for resetting single counters */
  112. typedef enum PgStat_Single_Reset_Type
  113. {
  114. RESET_TABLE,
  115. RESET_FUNCTION
  116. } PgStat_Single_Reset_Type;
  117. /* ------------------------------------------------------------
  118. * Structures kept in backend local memory while accumulating counts
  119. * ------------------------------------------------------------
  120. */
  121. /* ----------
  122. * PgStat_TableStatus Per-table status within a backend
  123. *
  124. * Many of the event counters are nontransactional, ie, we count events
  125. * in committed and aborted transactions alike. For these, we just count
  126. * directly in the PgStat_TableStatus. However, delta_live_tuples,
  127. * delta_dead_tuples, and changed_tuples must be derived from event counts
  128. * with awareness of whether the transaction or subtransaction committed or
  129. * aborted. Hence, we also keep a stack of per-(sub)transaction status
  130. * records for every table modified in the current transaction. At commit
  131. * or abort, we propagate tuples_inserted/updated/deleted up to the
  132. * parent subtransaction level, or out to the parent PgStat_TableStatus,
  133. * as appropriate.
  134. * ----------
  135. */
  136. typedef struct PgStat_TableStatus
  137. {
  138. Oid t_id; /* table's OID */
  139. bool t_shared; /* is it a shared catalog? */
  140. struct PgStat_TableXactStatus *trans; /* lowest subxact's counts */
  141. PgStat_TableCounts t_counts; /* event counts to be sent */
  142. } PgStat_TableStatus;
  143. /* ----------
  144. * PgStat_TableXactStatus Per-table, per-subtransaction status
  145. * ----------
  146. */
  147. typedef struct PgStat_TableXactStatus
  148. {
  149. PgStat_Counter tuples_inserted; /* tuples inserted in (sub)xact */
  150. PgStat_Counter tuples_updated; /* tuples updated in (sub)xact */
  151. PgStat_Counter tuples_deleted; /* tuples deleted in (sub)xact */
  152. bool truncated; /* relation truncated in this (sub)xact */
  153. PgStat_Counter inserted_pre_trunc; /* tuples inserted prior to truncate */
  154. PgStat_Counter updated_pre_trunc; /* tuples updated prior to truncate */
  155. PgStat_Counter deleted_pre_trunc; /* tuples deleted prior to truncate */
  156. int nest_level; /* subtransaction nest level */
  157. /* links to other structs for same relation: */
  158. struct PgStat_TableXactStatus *upper; /* next higher subxact if any */
  159. PgStat_TableStatus *parent; /* per-table status */
  160. /* structs of same subxact level are linked here: */
  161. struct PgStat_TableXactStatus *next; /* next of same subxact */
  162. } PgStat_TableXactStatus;
  163. /* ------------------------------------------------------------
  164. * Message formats follow
  165. * ------------------------------------------------------------
  166. */
  167. /* ----------
  168. * PgStat_MsgHdr The common message header
  169. * ----------
  170. */
  171. typedef struct PgStat_MsgHdr
  172. {
  173. StatMsgType m_type;
  174. int m_size;
  175. } PgStat_MsgHdr;
  176. /* ----------
  177. * Space available in a message. This will keep the UDP packets below 1K,
  178. * which should fit unfragmented into the MTU of the loopback interface.
  179. * (Larger values of PGSTAT_MAX_MSG_SIZE would work for that on most
  180. * platforms, but we're being conservative here.)
  181. * ----------
  182. */
  183. #define PGSTAT_MAX_MSG_SIZE 1000
  184. #define PGSTAT_MSG_PAYLOAD (PGSTAT_MAX_MSG_SIZE - sizeof(PgStat_MsgHdr))
  185. /* ----------
  186. * PgStat_MsgDummy A dummy message, ignored by the collector
  187. * ----------
  188. */
  189. typedef struct PgStat_MsgDummy
  190. {
  191. PgStat_MsgHdr m_hdr;
  192. } PgStat_MsgDummy;
  193. /* ----------
  194. * PgStat_MsgInquiry Sent by a backend to ask the collector
  195. * to write the stats file(s).
  196. *
  197. * Ordinarily, an inquiry message prompts writing of the global stats file,
  198. * the stats file for shared catalogs, and the stats file for the specified
  199. * database. If databaseid is InvalidOid, only the first two are written.
  200. *
  201. * New file(s) will be written only if the existing file has a timestamp
  202. * older than the specified cutoff_time; this prevents duplicated effort
  203. * when multiple requests arrive at nearly the same time, assuming that
  204. * backends send requests with cutoff_times a little bit in the past.
  205. *
  206. * clock_time should be the requestor's current local time; the collector
  207. * uses this to check for the system clock going backward, but it has no
  208. * effect unless that occurs. We assume clock_time >= cutoff_time, though.
  209. * ----------
  210. */
  211. typedef struct PgStat_MsgInquiry
  212. {
  213. PgStat_MsgHdr m_hdr;
  214. TimestampTz clock_time; /* observed local clock time */
  215. TimestampTz cutoff_time; /* minimum acceptable file timestamp */
  216. Oid databaseid; /* requested DB (InvalidOid => shared only) */
  217. } PgStat_MsgInquiry;
  218. /* ----------
  219. * PgStat_TableEntry Per-table info in a MsgTabstat
  220. * ----------
  221. */
  222. typedef struct PgStat_TableEntry
  223. {
  224. Oid t_id;
  225. PgStat_TableCounts t_counts;
  226. } PgStat_TableEntry;
  227. /* ----------
  228. * PgStat_MsgTabstat Sent by the backend to report table
  229. * and buffer access statistics.
  230. * ----------
  231. */
  232. #define PGSTAT_NUM_TABENTRIES \
  233. ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - 3 * sizeof(int) - 2 * sizeof(PgStat_Counter)) \
  234. / sizeof(PgStat_TableEntry))
  235. typedef struct PgStat_MsgTabstat
  236. {
  237. PgStat_MsgHdr m_hdr;
  238. Oid m_databaseid;
  239. int m_nentries;
  240. int m_xact_commit;
  241. int m_xact_rollback;
  242. PgStat_Counter m_block_read_time; /* times in microseconds */
  243. PgStat_Counter m_block_write_time;
  244. PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
  245. } PgStat_MsgTabstat;
  246. /* ----------
  247. * PgStat_MsgTabpurge Sent by the backend to tell the collector
  248. * about dead tables.
  249. * ----------
  250. */
  251. #define PGSTAT_NUM_TABPURGE \
  252. ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
  253. / sizeof(Oid))
  254. typedef struct PgStat_MsgTabpurge
  255. {
  256. PgStat_MsgHdr m_hdr;
  257. Oid m_databaseid;
  258. int m_nentries;
  259. Oid m_tableid[PGSTAT_NUM_TABPURGE];
  260. } PgStat_MsgTabpurge;
  261. /* ----------
  262. * PgStat_MsgDropdb Sent by the backend to tell the collector
  263. * about a dropped database
  264. * ----------
  265. */
  266. typedef struct PgStat_MsgDropdb
  267. {
  268. PgStat_MsgHdr m_hdr;
  269. Oid m_databaseid;
  270. } PgStat_MsgDropdb;
  271. /* ----------
  272. * PgStat_MsgResetcounter Sent by the backend to tell the collector
  273. * to reset counters
  274. * ----------
  275. */
  276. typedef struct PgStat_MsgResetcounter
  277. {
  278. PgStat_MsgHdr m_hdr;
  279. Oid m_databaseid;
  280. } PgStat_MsgResetcounter;
  281. /* ----------
  282. * PgStat_MsgResetsharedcounter Sent by the backend to tell the collector
  283. * to reset a shared counter
  284. * ----------
  285. */
  286. typedef struct PgStat_MsgResetsharedcounter
  287. {
  288. PgStat_MsgHdr m_hdr;
  289. PgStat_Shared_Reset_Target m_resettarget;
  290. } PgStat_MsgResetsharedcounter;
  291. /* ----------
  292. * PgStat_MsgResetsinglecounter Sent by the backend to tell the collector
  293. * to reset a single counter
  294. * ----------
  295. */
  296. typedef struct PgStat_MsgResetsinglecounter
  297. {
  298. PgStat_MsgHdr m_hdr;
  299. Oid m_databaseid;
  300. PgStat_Single_Reset_Type m_resettype;
  301. Oid m_objectid;
  302. } PgStat_MsgResetsinglecounter;
  303. /* ----------
  304. * PgStat_MsgAutovacStart Sent by the autovacuum daemon to signal
  305. * that a database is going to be processed
  306. * ----------
  307. */
  308. typedef struct PgStat_MsgAutovacStart
  309. {
  310. PgStat_MsgHdr m_hdr;
  311. Oid m_databaseid;
  312. TimestampTz m_start_time;
  313. } PgStat_MsgAutovacStart;
  314. /* ----------
  315. * PgStat_MsgVacuum Sent by the backend or autovacuum daemon
  316. * after VACUUM
  317. * ----------
  318. */
  319. typedef struct PgStat_MsgVacuum
  320. {
  321. PgStat_MsgHdr m_hdr;
  322. Oid m_databaseid;
  323. Oid m_tableoid;
  324. bool m_autovacuum;
  325. TimestampTz m_vacuumtime;
  326. PgStat_Counter m_live_tuples;
  327. PgStat_Counter m_dead_tuples;
  328. } PgStat_MsgVacuum;
  329. /* ----------
  330. * PgStat_MsgAnalyze Sent by the backend or autovacuum daemon
  331. * after ANALYZE
  332. * ----------
  333. */
  334. typedef struct PgStat_MsgAnalyze
  335. {
  336. PgStat_MsgHdr m_hdr;
  337. Oid m_databaseid;
  338. Oid m_tableoid;
  339. bool m_autovacuum;
  340. bool m_resetcounter;
  341. TimestampTz m_analyzetime;
  342. PgStat_Counter m_live_tuples;
  343. PgStat_Counter m_dead_tuples;
  344. } PgStat_MsgAnalyze;
  345. /* ----------
  346. * PgStat_MsgArchiver Sent by the archiver to update statistics.
  347. * ----------
  348. */
  349. typedef struct PgStat_MsgArchiver
  350. {
  351. PgStat_MsgHdr m_hdr;
  352. bool m_failed; /* Failed attempt */
  353. char m_xlog[MAX_XFN_CHARS + 1];
  354. TimestampTz m_timestamp;
  355. } PgStat_MsgArchiver;
  356. /* ----------
  357. * PgStat_MsgBgWriter Sent by the bgwriter to update statistics.
  358. * ----------
  359. */
  360. typedef struct PgStat_MsgBgWriter
  361. {
  362. PgStat_MsgHdr m_hdr;
  363. PgStat_Counter m_timed_checkpoints;
  364. PgStat_Counter m_requested_checkpoints;
  365. PgStat_Counter m_buf_written_checkpoints;
  366. PgStat_Counter m_buf_written_clean;
  367. PgStat_Counter m_maxwritten_clean;
  368. PgStat_Counter m_buf_written_backend;
  369. PgStat_Counter m_buf_fsync_backend;
  370. PgStat_Counter m_buf_alloc;
  371. PgStat_Counter m_checkpoint_write_time; /* times in milliseconds */
  372. PgStat_Counter m_checkpoint_sync_time;
  373. } PgStat_MsgBgWriter;
  374. /* ----------
  375. * PgStat_MsgRecoveryConflict Sent by the backend upon recovery conflict
  376. * ----------
  377. */
  378. typedef struct PgStat_MsgRecoveryConflict
  379. {
  380. PgStat_MsgHdr m_hdr;
  381. Oid m_databaseid;
  382. int m_reason;
  383. } PgStat_MsgRecoveryConflict;
  384. /* ----------
  385. * PgStat_MsgTempFile Sent by the backend upon creating a temp file
  386. * ----------
  387. */
  388. typedef struct PgStat_MsgTempFile
  389. {
  390. PgStat_MsgHdr m_hdr;
  391. Oid m_databaseid;
  392. size_t m_filesize;
  393. } PgStat_MsgTempFile;
  394. /* ----------
  395. * PgStat_FunctionCounts The actual per-function counts kept by a backend
  396. *
  397. * This struct should contain only actual event counters, because we memcmp
  398. * it against zeroes to detect whether there are any counts to transmit.
  399. *
  400. * Note that the time counters are in instr_time format here. We convert to
  401. * microseconds in PgStat_Counter format when transmitting to the collector.
  402. * ----------
  403. */
  404. typedef struct PgStat_FunctionCounts
  405. {
  406. PgStat_Counter f_numcalls;
  407. instr_time f_total_time;
  408. instr_time f_self_time;
  409. } PgStat_FunctionCounts;
  410. /* ----------
  411. * PgStat_BackendFunctionEntry Entry in backend's per-function hash table
  412. * ----------
  413. */
  414. typedef struct PgStat_BackendFunctionEntry
  415. {
  416. Oid f_id;
  417. PgStat_FunctionCounts f_counts;
  418. } PgStat_BackendFunctionEntry;
  419. /* ----------
  420. * PgStat_FunctionEntry Per-function info in a MsgFuncstat
  421. * ----------
  422. */
  423. typedef struct PgStat_FunctionEntry
  424. {
  425. Oid f_id;
  426. PgStat_Counter f_numcalls;
  427. PgStat_Counter f_total_time; /* times in microseconds */
  428. PgStat_Counter f_self_time;
  429. } PgStat_FunctionEntry;
  430. /* ----------
  431. * PgStat_MsgFuncstat Sent by the backend to report function
  432. * usage statistics.
  433. * ----------
  434. */
  435. #define PGSTAT_NUM_FUNCENTRIES \
  436. ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
  437. / sizeof(PgStat_FunctionEntry))
  438. typedef struct PgStat_MsgFuncstat
  439. {
  440. PgStat_MsgHdr m_hdr;
  441. Oid m_databaseid;
  442. int m_nentries;
  443. PgStat_FunctionEntry m_entry[PGSTAT_NUM_FUNCENTRIES];
  444. } PgStat_MsgFuncstat;
  445. /* ----------
  446. * PgStat_MsgFuncpurge Sent by the backend to tell the collector
  447. * about dead functions.
  448. * ----------
  449. */
  450. #define PGSTAT_NUM_FUNCPURGE \
  451. ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
  452. / sizeof(Oid))
  453. typedef struct PgStat_MsgFuncpurge
  454. {
  455. PgStat_MsgHdr m_hdr;
  456. Oid m_databaseid;
  457. int m_nentries;
  458. Oid m_functionid[PGSTAT_NUM_FUNCPURGE];
  459. } PgStat_MsgFuncpurge;
  460. /* ----------
  461. * PgStat_MsgDeadlock Sent by the backend to tell the collector
  462. * about a deadlock that occurred.
  463. * ----------
  464. */
  465. typedef struct PgStat_MsgDeadlock
  466. {
  467. PgStat_MsgHdr m_hdr;
  468. Oid m_databaseid;
  469. } PgStat_MsgDeadlock;
  470. /* ----------
  471. * PgStat_Msg Union over all possible messages.
  472. * ----------
  473. */
  474. typedef union PgStat_Msg
  475. {
  476. PgStat_MsgHdr msg_hdr;
  477. PgStat_MsgDummy msg_dummy;
  478. PgStat_MsgInquiry msg_inquiry;
  479. PgStat_MsgTabstat msg_tabstat;
  480. PgStat_MsgTabpurge msg_tabpurge;
  481. PgStat_MsgDropdb msg_dropdb;
  482. PgStat_MsgResetcounter msg_resetcounter;
  483. PgStat_MsgResetsharedcounter msg_resetsharedcounter;
  484. PgStat_MsgResetsinglecounter msg_resetsinglecounter;
  485. PgStat_MsgAutovacStart msg_autovacuum;
  486. PgStat_MsgVacuum msg_vacuum;
  487. PgStat_MsgAnalyze msg_analyze;
  488. PgStat_MsgArchiver msg_archiver;
  489. PgStat_MsgBgWriter msg_bgwriter;
  490. PgStat_MsgFuncstat msg_funcstat;
  491. PgStat_MsgFuncpurge msg_funcpurge;
  492. PgStat_MsgRecoveryConflict msg_recoveryconflict;
  493. PgStat_MsgDeadlock msg_deadlock;
  494. } PgStat_Msg;
  495. /* ------------------------------------------------------------
  496. * Statistic collector data structures follow
  497. *
  498. * PGSTAT_FILE_FORMAT_ID should be changed whenever any of these
  499. * data structures change.
  500. * ------------------------------------------------------------
  501. */
  502. #define PGSTAT_FILE_FORMAT_ID 0x01A5BC9D
  503. /* ----------
  504. * PgStat_StatDBEntry The collector's data per database
  505. * ----------
  506. */
  507. typedef struct PgStat_StatDBEntry
  508. {
  509. Oid databaseid;
  510. PgStat_Counter n_xact_commit;
  511. PgStat_Counter n_xact_rollback;
  512. PgStat_Counter n_blocks_fetched;
  513. PgStat_Counter n_blocks_hit;
  514. PgStat_Counter n_tuples_returned;
  515. PgStat_Counter n_tuples_fetched;
  516. PgStat_Counter n_tuples_inserted;
  517. PgStat_Counter n_tuples_updated;
  518. PgStat_Counter n_tuples_deleted;
  519. TimestampTz last_autovac_time;
  520. PgStat_Counter n_conflict_tablespace;
  521. PgStat_Counter n_conflict_lock;
  522. PgStat_Counter n_conflict_snapshot;
  523. PgStat_Counter n_conflict_bufferpin;
  524. PgStat_Counter n_conflict_startup_deadlock;
  525. PgStat_Counter n_temp_files;
  526. PgStat_Counter n_temp_bytes;
  527. PgStat_Counter n_deadlocks;
  528. PgStat_Counter n_block_read_time; /* times in microseconds */
  529. PgStat_Counter n_block_write_time;
  530. TimestampTz stat_reset_timestamp;
  531. TimestampTz stats_timestamp; /* time of db stats file update */
  532. /*
  533. * tables and functions must be last in the struct, because we don't write
  534. * the pointers out to the stats file.
  535. */
  536. HTAB *tables;
  537. HTAB *functions;
  538. } PgStat_StatDBEntry;
  539. /* ----------
  540. * PgStat_StatTabEntry The collector's data per table (or index)
  541. * ----------
  542. */
  543. typedef struct PgStat_StatTabEntry
  544. {
  545. Oid tableid;
  546. PgStat_Counter numscans;
  547. PgStat_Counter tuples_returned;
  548. PgStat_Counter tuples_fetched;
  549. PgStat_Counter tuples_inserted;
  550. PgStat_Counter tuples_updated;
  551. PgStat_Counter tuples_deleted;
  552. PgStat_Counter tuples_hot_updated;
  553. PgStat_Counter n_live_tuples;
  554. PgStat_Counter n_dead_tuples;
  555. PgStat_Counter changes_since_analyze;
  556. PgStat_Counter blocks_fetched;
  557. PgStat_Counter blocks_hit;
  558. TimestampTz vacuum_timestamp; /* user initiated vacuum */
  559. PgStat_Counter vacuum_count;
  560. TimestampTz autovac_vacuum_timestamp; /* autovacuum initiated */
  561. PgStat_Counter autovac_vacuum_count;
  562. TimestampTz analyze_timestamp; /* user initiated */
  563. PgStat_Counter analyze_count;
  564. TimestampTz autovac_analyze_timestamp; /* autovacuum initiated */
  565. PgStat_Counter autovac_analyze_count;
  566. } PgStat_StatTabEntry;
  567. /* ----------
  568. * PgStat_StatFuncEntry The collector's data per function
  569. * ----------
  570. */
  571. typedef struct PgStat_StatFuncEntry
  572. {
  573. Oid functionid;
  574. PgStat_Counter f_numcalls;
  575. PgStat_Counter f_total_time; /* times in microseconds */
  576. PgStat_Counter f_self_time;
  577. } PgStat_StatFuncEntry;
  578. /*
  579. * Archiver statistics kept in the stats collector
  580. */
  581. typedef struct PgStat_ArchiverStats
  582. {
  583. PgStat_Counter archived_count; /* archival successes */
  584. char last_archived_wal[MAX_XFN_CHARS + 1]; /* last WAL file
  585. * archived */
  586. TimestampTz last_archived_timestamp; /* last archival success time */
  587. PgStat_Counter failed_count; /* failed archival attempts */
  588. char last_failed_wal[MAX_XFN_CHARS + 1]; /* WAL file involved in
  589. * last failure */
  590. TimestampTz last_failed_timestamp; /* last archival failure time */
  591. TimestampTz stat_reset_timestamp;
  592. } PgStat_ArchiverStats;
  593. /*
  594. * Global statistics kept in the stats collector
  595. */
  596. typedef struct PgStat_GlobalStats
  597. {
  598. TimestampTz stats_timestamp; /* time of stats file update */
  599. PgStat_Counter timed_checkpoints;
  600. PgStat_Counter requested_checkpoints;
  601. PgStat_Counter checkpoint_write_time; /* times in milliseconds */
  602. PgStat_Counter checkpoint_sync_time;
  603. PgStat_Counter buf_written_checkpoints;
  604. PgStat_Counter buf_written_clean;
  605. PgStat_Counter maxwritten_clean;
  606. PgStat_Counter buf_written_backend;
  607. PgStat_Counter buf_fsync_backend;
  608. PgStat_Counter buf_alloc;
  609. TimestampTz stat_reset_timestamp;
  610. } PgStat_GlobalStats;
  611. /* ----------
  612. * Backend states
  613. * ----------
  614. */
  615. typedef enum BackendState
  616. {
  617. STATE_UNDEFINED,
  618. STATE_IDLE,
  619. STATE_RUNNING,
  620. STATE_IDLEINTRANSACTION,
  621. STATE_FASTPATH,
  622. STATE_IDLEINTRANSACTION_ABORTED,
  623. STATE_DISABLED
  624. } BackendState;
  625. /* ----------
  626. * Wait Classes
  627. * ----------
  628. */
  629. typedef enum WaitClass
  630. {
  631. WAIT_UNDEFINED,
  632. WAIT_LWLOCK_NAMED,
  633. WAIT_LWLOCK_TRANCHE,
  634. WAIT_LOCK,
  635. WAIT_BUFFER_PIN
  636. } WaitClass;
  637. /* ----------
  638. * Command type for progress reporting purposes
  639. * ----------
  640. */
  641. typedef enum ProgressCommandType
  642. {
  643. PROGRESS_COMMAND_INVALID,
  644. PROGRESS_COMMAND_VACUUM
  645. } ProgressCommandType;
  646. #define PGSTAT_NUM_PROGRESS_PARAM 10
  647. /* ----------
  648. * Shared-memory data structures
  649. * ----------
  650. */
  651. /*
  652. * PgBackendSSLStatus
  653. *
  654. * For each backend, we keep the SSL status in a separate struct, that
  655. * is only filled in if SSL is enabled.
  656. */
  657. typedef struct PgBackendSSLStatus
  658. {
  659. /* Information about SSL connection */
  660. int ssl_bits;
  661. bool ssl_compression;
  662. char ssl_version[NAMEDATALEN]; /* MUST be null-terminated */
  663. char ssl_cipher[NAMEDATALEN]; /* MUST be null-terminated */
  664. char ssl_clientdn[NAMEDATALEN]; /* MUST be null-terminated */
  665. } PgBackendSSLStatus;
  666. /* ----------
  667. * PgBackendStatus
  668. *
  669. * Each live backend maintains a PgBackendStatus struct in shared memory
  670. * showing its current activity. (The structs are allocated according to
  671. * BackendId, but that is not critical.) Note that the collector process
  672. * has no involvement in, or even access to, these structs.
  673. * ----------
  674. */
  675. typedef struct PgBackendStatus
  676. {
  677. /*
  678. * To avoid locking overhead, we use the following protocol: a backend
  679. * increments st_changecount before modifying its entry, and again after
  680. * finishing a modification. A would-be reader should note the value of
  681. * st_changecount, copy the entry into private memory, then check
  682. * st_changecount again. If the value hasn't changed, and if it's even,
  683. * the copy is valid; otherwise start over. This makes updates cheap
  684. * while reads are potentially expensive, but that's the tradeoff we want.
  685. *
  686. * The above protocol needs the memory barriers to ensure that the
  687. * apparent order of execution is as it desires. Otherwise, for example,
  688. * the CPU might rearrange the code so that st_changecount is incremented
  689. * twice before the modification on a machine with weak memory ordering.
  690. * This surprising result can lead to bugs.
  691. */
  692. int st_changecount;
  693. /* The entry is valid iff st_procpid > 0, unused if st_procpid == 0 */
  694. int st_procpid;
  695. /* Times when current backend, transaction, and activity started */
  696. TimestampTz st_proc_start_timestamp;
  697. TimestampTz st_xact_start_timestamp;
  698. TimestampTz st_activity_start_timestamp;
  699. TimestampTz st_state_start_timestamp;
  700. /* Database OID, owning user's OID, connection client address */
  701. Oid st_databaseid;
  702. Oid st_userid;
  703. SockAddr st_clientaddr;
  704. char *st_clienthostname; /* MUST be null-terminated */
  705. /* Information about SSL connection */
  706. bool st_ssl;
  707. PgBackendSSLStatus *st_sslstatus;
  708. /* current state */
  709. BackendState st_state;
  710. /* application name; MUST be null-terminated */
  711. char *st_appname;
  712. /* current command string; MUST be null-terminated */
  713. char *st_activity;
  714. /*
  715. * Command progress reporting. Any command which wishes can advertise
  716. * that it is running by setting st_progress_command,
  717. * st_progress_command_target, and st_progress_param[].
  718. * st_progress_command_target should be the OID of the relation which the
  719. * command targets (we assume there's just one, as this is meant for
  720. * utility commands), but the meaning of each element in the
  721. * st_progress_param array is command-specific.
  722. */
  723. ProgressCommandType st_progress_command;
  724. Oid st_progress_command_target;
  725. int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM];
  726. } PgBackendStatus;
  727. /*
  728. * Macros to load and store st_changecount with the memory barriers.
  729. *
  730. * pgstat_increment_changecount_before() and
  731. * pgstat_increment_changecount_after() need to be called before and after
  732. * PgBackendStatus entries are modified, respectively. This makes sure that
  733. * st_changecount is incremented around the modification.
  734. *
  735. * Also pgstat_save_changecount_before() and pgstat_save_changecount_after()
  736. * need to be called before and after PgBackendStatus entries are copied into
  737. * private memory, respectively.
  738. */
  739. #define pgstat_increment_changecount_before(beentry) \
  740. do { \
  741. beentry->st_changecount++; \
  742. pg_write_barrier(); \
  743. } while (0)
  744. #define pgstat_increment_changecount_after(beentry) \
  745. do { \
  746. pg_write_barrier(); \
  747. beentry->st_changecount++; \
  748. Assert((beentry->st_changecount & 1) == 0); \
  749. } while (0)
  750. #define pgstat_save_changecount_before(beentry, save_changecount) \
  751. do { \
  752. save_changecount = beentry->st_changecount; \
  753. pg_read_barrier(); \
  754. } while (0)
  755. #define pgstat_save_changecount_after(beentry, save_changecount) \
  756. do { \
  757. pg_read_barrier(); \
  758. save_changecount = beentry->st_changecount; \
  759. } while (0)
  760. /* ----------
  761. * LocalPgBackendStatus
  762. *
  763. * When we build the backend status array, we use LocalPgBackendStatus to be
  764. * able to add new values to the struct when needed without adding new fields
  765. * to the shared memory. It contains the backend status as a first member.
  766. * ----------
  767. */
  768. typedef struct LocalPgBackendStatus
  769. {
  770. /*
  771. * Local version of the backend status entry.
  772. */
  773. PgBackendStatus backendStatus;
  774. /*
  775. * The xid of the current transaction if available, InvalidTransactionId
  776. * if not.
  777. */
  778. TransactionId backend_xid;
  779. /*
  780. * The xmin of the current session if available, InvalidTransactionId if
  781. * not.
  782. */
  783. TransactionId backend_xmin;
  784. } LocalPgBackendStatus;
  785. /*
  786. * Working state needed to accumulate per-function-call timing statistics.
  787. */
  788. typedef struct PgStat_FunctionCallUsage
  789. {
  790. /* Link to function's hashtable entry (must still be there at exit!) */
  791. /* NULL means we are not tracking the current function call */
  792. PgStat_FunctionCounts *fs;
  793. /* Total time previously charged to function, as of function start */
  794. instr_time save_f_total_time;
  795. /* Backend-wide total time as of function start */
  796. instr_time save_total;
  797. /* system clock as of function start */
  798. instr_time f_start;
  799. } PgStat_FunctionCallUsage;
  800. /* ----------
  801. * GUC parameters
  802. * ----------
  803. */
  804. extern bool pgstat_track_activities;
  805. extern bool pgstat_track_counts;
  806. extern int pgstat_track_functions;
  807. extern PGDLLIMPORT int pgstat_track_activity_query_size;
  808. extern char *pgstat_stat_directory;
  809. extern char *pgstat_stat_tmpname;
  810. extern char *pgstat_stat_filename;
  811. /*
  812. * BgWriter statistics counters are updated directly by bgwriter and bufmgr
  813. */
  814. extern PgStat_MsgBgWriter BgWriterStats;
  815. /*
  816. * Updated by pgstat_count_buffer_*_time macros
  817. */
  818. extern PgStat_Counter pgStatBlockReadTime;
  819. extern PgStat_Counter pgStatBlockWriteTime;
  820. /* ----------
  821. * Functions called from postmaster
  822. * ----------
  823. */
  824. extern Size BackendStatusShmemSize(void);
  825. extern void CreateSharedBackendStatus(void);
  826. extern void pgstat_init(void);
  827. extern int pgstat_start(void);
  828. extern void pgstat_reset_all(void);
  829. extern void allow_immediate_pgstat_restart(void);
  830. #ifdef EXEC_BACKEND
  831. extern void PgstatCollectorMain(int argc, char *argv[]) pg_attribute_noreturn();
  832. #endif
  833. /* ----------
  834. * Functions called from backends
  835. * ----------
  836. */
  837. extern void pgstat_ping(void);
  838. extern void pgstat_report_stat(bool force);
  839. extern void pgstat_vacuum_stat(void);
  840. extern void pgstat_drop_database(Oid databaseid);
  841. extern void pgstat_clear_snapshot(void);
  842. extern void pgstat_reset_counters(void);
  843. extern void pgstat_reset_shared_counters(const char *);
  844. extern void pgstat_reset_single_counter(Oid objectid, PgStat_Single_Reset_Type type);
  845. extern void pgstat_report_autovac(Oid dboid);
  846. extern void pgstat_report_vacuum(Oid tableoid, bool shared,
  847. PgStat_Counter livetuples, PgStat_Counter deadtuples);
  848. extern void pgstat_report_analyze(Relation rel,
  849. PgStat_Counter livetuples, PgStat_Counter deadtuples,
  850. bool resetcounter);
  851. extern void pgstat_report_recovery_conflict(int reason);
  852. extern void pgstat_report_deadlock(void);
  853. extern void pgstat_initialize(void);
  854. extern void pgstat_bestart(void);
  855. extern void pgstat_report_activity(BackendState state, const char *cmd_str);
  856. extern void pgstat_report_tempfile(size_t filesize);
  857. extern void pgstat_report_appname(const char *appname);
  858. extern void pgstat_report_xact_timestamp(TimestampTz tstamp);
  859. extern const char *pgstat_get_wait_event(uint32 wait_event_info);
  860. extern const char *pgstat_get_wait_event_type(uint32 wait_event_info);
  861. extern const char *pgstat_get_backend_current_activity(int pid, bool checkUser);
  862. extern const char *pgstat_get_crashed_backend_activity(int pid, char *buffer,
  863. int buflen);
  864. extern void pgstat_progress_start_command(ProgressCommandType cmdtype,
  865. Oid relid);
  866. extern void pgstat_progress_update_param(int index, int64 val);
  867. extern void pgstat_progress_update_multi_param(int nparam, const int *index,
  868. const int64 *val);
  869. extern void pgstat_progress_end_command(void);
  870. extern PgStat_TableStatus *find_tabstat_entry(Oid rel_id);
  871. extern PgStat_BackendFunctionEntry *find_funcstat_entry(Oid func_id);
  872. extern void pgstat_initstats(Relation rel);
  873. /* ----------
  874. * pgstat_report_wait_start() -
  875. *
  876. * Called from places where server process needs to wait. This is called
  877. * to report wait event information. The wait information is stored
  878. * as 4-bytes where first byte repersents the wait event class (type of
  879. * wait, for different types of wait, refer WaitClass) and the next
  880. * 3-bytes repersent the actual wait event. Currently 2-bytes are used
  881. * for wait event which is sufficient for current usage, 1-byte is
  882. * reserved for future usage.
  883. *
  884. * NB: this *must* be able to survive being called before MyProc has been
  885. * initialized.
  886. * ----------
  887. */
  888. static inline void
  889. pgstat_report_wait_start(uint8 classId, uint16 eventId)
  890. {
  891. volatile PGPROC *proc = MyProc;
  892. uint32 wait_event_val;
  893. if (!pgstat_track_activities || !proc)
  894. return;
  895. wait_event_val = classId;
  896. wait_event_val <<= 24;
  897. wait_event_val |= eventId;
  898. /*
  899. * Since this is a four-byte field which is always read and written as
  900. * four-bytes, updates are atomic.
  901. */
  902. proc->wait_event_info = wait_event_val;
  903. }
  904. /* ----------
  905. * pgstat_report_wait_end() -
  906. *
  907. * Called to report end of a wait.
  908. *
  909. * NB: this *must* be able to survive being called before MyProc has been
  910. * initialized.
  911. * ----------
  912. */
  913. static inline void
  914. pgstat_report_wait_end(void)
  915. {
  916. volatile PGPROC *proc = MyProc;
  917. if (!pgstat_track_activities || !proc)
  918. return;
  919. /*
  920. * Since this is a four-byte field which is always read and written as
  921. * four-bytes, updates are atomic.
  922. */
  923. proc->wait_event_info = 0;
  924. }
  925. /* nontransactional event counts are simple enough to inline */
  926. #define pgstat_count_heap_scan(rel) \
  927. do { \
  928. if ((rel)->pgstat_info != NULL) \
  929. (rel)->pgstat_info->t_counts.t_numscans++; \
  930. } while (0)
  931. #define pgstat_count_heap_getnext(rel) \
  932. do { \
  933. if ((rel)->pgstat_info != NULL) \
  934. (rel)->pgstat_info->t_counts.t_tuples_returned++; \
  935. } while (0)
  936. #define pgstat_count_heap_fetch(rel) \
  937. do { \
  938. if ((rel)->pgstat_info != NULL) \
  939. (rel)->pgstat_info->t_counts.t_tuples_fetched++; \
  940. } while (0)
  941. #define pgstat_count_index_scan(rel) \
  942. do { \
  943. if ((rel)->pgstat_info != NULL) \
  944. (rel)->pgstat_info->t_counts.t_numscans++; \
  945. } while (0)
  946. #define pgstat_count_index_tuples(rel, n) \
  947. do { \
  948. if ((rel)->pgstat_info != NULL) \
  949. (rel)->pgstat_info->t_counts.t_tuples_returned += (n); \
  950. } while (0)
  951. #define pgstat_count_buffer_read(rel) \
  952. do { \
  953. if ((rel)->pgstat_info != NULL) \
  954. (rel)->pgstat_info->t_counts.t_blocks_fetched++; \
  955. } while (0)
  956. #define pgstat_count_buffer_hit(rel) \
  957. do { \
  958. if ((rel)->pgstat_info != NULL) \
  959. (rel)->pgstat_info->t_counts.t_blocks_hit++; \
  960. } while (0)
  961. #define pgstat_count_buffer_read_time(n) \
  962. (pgStatBlockReadTime += (n))
  963. #define pgstat_count_buffer_write_time(n) \
  964. (pgStatBlockWriteTime += (n))
  965. extern void pgstat_count_heap_insert(Relation rel, int n);
  966. extern void pgstat_count_heap_update(Relation rel, bool hot);
  967. extern void pgstat_count_heap_delete(Relation rel);
  968. extern void pgstat_count_truncate(Relation rel);
  969. extern void pgstat_update_heap_dead_tuples(Relation rel, int delta);
  970. extern void pgstat_init_function_usage(FunctionCallInfoData *fcinfo,
  971. PgStat_FunctionCallUsage *fcu);
  972. extern void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu,
  973. bool finalize);
  974. extern void AtEOXact_PgStat(bool isCommit);
  975. extern void AtEOSubXact_PgStat(bool isCommit, int nestDepth);
  976. extern void AtPrepare_PgStat(void);
  977. extern void PostPrepare_PgStat(void);
  978. extern void pgstat_twophase_postcommit(TransactionId xid, uint16 info,
  979. void *recdata, uint32 len);
  980. extern void pgstat_twophase_postabort(TransactionId xid, uint16 info,
  981. void *recdata, uint32 len);
  982. extern void pgstat_send_archiver(const char *xlog, bool failed);
  983. extern void pgstat_send_bgwriter(void);
  984. /* ----------
  985. * Support functions for the SQL-callable functions to
  986. * generate the pgstat* views.
  987. * ----------
  988. */
  989. extern PgStat_StatDBEntry *pgstat_fetch_stat_dbentry(Oid dbid);
  990. extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid);
  991. extern PgBackendStatus *pgstat_fetch_stat_beentry(int beid);
  992. extern LocalPgBackendStatus *pgstat_fetch_stat_local_beentry(int beid);
  993. extern PgStat_StatFuncEntry *pgstat_fetch_stat_funcentry(Oid funcid);
  994. extern int pgstat_fetch_stat_numbackends(void);
  995. extern PgStat_ArchiverStats *pgstat_fetch_stat_archiver(void);
  996. extern PgStat_GlobalStats *pgstat_fetch_global(void);
  997. #endif /* PGSTAT_H */