Debian Codegraph @

doc/src/sgml/pgbuffercache.sgml

raw
1 <!-- doc/src/sgml/pgbuffercache.sgml -->
2
3 <sect1 id="pgbuffercache" xreflabel="pg_buffercache">
4 <title>pg_buffercache &mdash; inspect <productname>PostgreSQL</productname>
5 buffer cache state</title>
6
7 <indexterm zone="pgbuffercache">
8 <primary>pg_buffercache</primary>
9 </indexterm>
10
11 <para>
12 The <filename>pg_buffercache</filename> module provides a means for
13 examining what's happening in the shared buffer cache in real time.
14 It also offers a low-level way to evict data from it, for testing
15 purposes.
16 </para>
17
18 <indexterm>
19 <primary>pg_buffercache_pages</primary>
20 </indexterm>
21
22 <indexterm>
23 <primary>pg_buffercache_numa</primary>
24 </indexterm>
25
26 <indexterm>
27 <primary>pg_buffercache_summary</primary>
28 </indexterm>
29
30 <indexterm>
31 <primary>pg_buffercache_usage_counts</primary>
32 </indexterm>
33
34 <indexterm>
35 <primary>pg_buffercache_evict</primary>
36 </indexterm>
37
38 <indexterm>
39 <primary>pg_buffercache_evict_relation</primary>
40 </indexterm>
41
42 <indexterm>
43 <primary>pg_buffercache_evict_all</primary>
44 </indexterm>
45
46 <para>
47 This module provides the <function>pg_buffercache_pages()</function>
48 function (wrapped in the <structname>pg_buffercache</structname> view), the
49 <function>pg_buffercache_numa_pages()</function> function (wrapped in the
50 <structname>pg_buffercache_numa</structname> view), the
51 <function>pg_buffercache_summary()</function> function, the
52 <function>pg_buffercache_usage_counts()</function> function, the
53 <function>pg_buffercache_evict()</function> function, the
54 <function>pg_buffercache_evict_relation()</function> function and the
55 <function>pg_buffercache_evict_all()</function> function.
56 </para>
57
58 <para>
59 The <function>pg_buffercache_pages()</function> function returns a set of
60 records, each row describing the state of one shared buffer entry. The
61 <structname>pg_buffercache</structname> view wraps the function for
62 convenient use.
63 </para>
64
65 <para>
66 The <function>pg_buffercache_numa_pages()</function> function provides
67 <acronym>NUMA</acronym> node mappings for shared buffer entries. This
68 information is not part of <function>pg_buffercache_pages()</function>
69 itself, as it is much slower to retrieve.
70 The <structname>pg_buffercache_numa</structname> view wraps the function for
71 convenient use.
72 </para>
73
74 <para>
75 The <function>pg_buffercache_summary()</function> function returns a single
76 row summarizing the state of the shared buffer cache.
77 </para>
78
79 <para>
80 The <function>pg_buffercache_usage_counts()</function> function returns a set
81 of records, each row describing the number of buffers with a given usage
82 count.
83 </para>
84
85 <para>
86 By default, use of the above functions is restricted to superusers and roles
87 with privileges of the <literal>pg_monitor</literal> role. Access may be
88 granted to others using <command>GRANT</command>.
89 </para>
90
91 <para>
92 The <function>pg_buffercache_evict()</function> function allows a block to
93 be evicted from the buffer pool given a buffer identifier. Use of this
94 function is restricted to superusers only.
95 </para>
96
97 <para>
98 The <function>pg_buffercache_evict_relation()</function> function allows all
99 unpinned shared buffers in the relation to be evicted from the buffer pool
100 given a relation identifier. Use of this function is restricted to
101 superusers only.
102 </para>
103
104 <para>
105 The <function>pg_buffercache_evict_all()</function> function allows all
106 unpinned shared buffers to be evicted in the buffer pool. Use of this
107 function is restricted to superusers only.
108 </para>
109
110 <sect2 id="pgbuffercache-pg-buffercache">
111 <title>The <structname>pg_buffercache</structname> View</title>
112
113 <para>
114 The definitions of the columns exposed by the view are shown in <xref linkend="pgbuffercache-columns"/>.
115 </para>
116
117 <table id="pgbuffercache-columns">
118 <title><structname>pg_buffercache</structname> Columns</title>
119 <tgroup cols="1">
120 <thead>
121 <row>
122 <entry role="catalog_table_entry"><para role="column_definition">
123 Column Type
124 </para>
125 <para>
126 Description
127 </para></entry>
128 </row>
129 </thead>
130
131 <tbody>
132 <row>
133 <entry role="catalog_table_entry"><para role="column_definition">
134 <structfield>bufferid</structfield> <type>integer</type>
135 </para>
136 <para>
137 ID, in the range 1..<varname>shared_buffers</varname>
138 </para></entry>
139 </row>
140
141 <row>
142 <entry role="catalog_table_entry"><para role="column_definition">
143 <structfield>relfilenode</structfield> <type>oid</type>
144 (references <link linkend="catalog-pg-class"><structname>pg_class</structname></link>.<structfield>relfilenode</structfield>)
145 </para>
146 <para>
147 Filenode number of the relation
148 </para></entry>
149 </row>
150
151 <row>
152 <entry role="catalog_table_entry"><para role="column_definition">
153 <structfield>reltablespace</structfield> <type>oid</type>
154 (references <link linkend="catalog-pg-tablespace"><structname>pg_tablespace</structname></link>.<structfield>oid</structfield>)
155 </para>
156 <para>
157 Tablespace OID of the relation
158 </para></entry>
159 </row>
160
161 <row>
162 <entry role="catalog_table_entry"><para role="column_definition">
163 <structfield>reldatabase</structfield> <type>oid</type>
164 (references <link linkend="catalog-pg-database"><structname>pg_database</structname></link>.<structfield>oid</structfield>)
165 </para>
166 <para>
167 Database OID of the relation
168 </para></entry>
169 </row>
170
171 <row>
172 <entry role="catalog_table_entry"><para role="column_definition">
173 <structfield>relforknumber</structfield> <type>smallint</type>
174 </para>
175 <para>
176 Fork number within the relation; see
177 <filename>common/relpath.h</filename>
178 </para></entry>
179 </row>
180
181 <row>
182 <entry role="catalog_table_entry"><para role="column_definition">
183 <structfield>relblocknumber</structfield> <type>bigint</type>
184 </para>
185 <para>
186 Page number within the relation
187 </para></entry>
188 </row>
189
190 <row>
191 <entry role="catalog_table_entry"><para role="column_definition">
192 <structfield>isdirty</structfield> <type>boolean</type>
193 </para>
194 <para>
195 Is the page dirty?
196 </para></entry>
197 </row>
198
199 <row>
200 <entry role="catalog_table_entry"><para role="column_definition">
201 <structfield>usagecount</structfield> <type>smallint</type>
202 </para>
203 <para>
204 Clock-sweep access count
205 </para></entry>
206 </row>
207
208 <row>
209 <entry role="catalog_table_entry"><para role="column_definition">
210 <structfield>pinning_backends</structfield> <type>integer</type>
211 </para>
212 <para>
213 Number of backends pinning this buffer
214 </para></entry>
215 </row>
216 </tbody>
217 </tgroup>
218 </table>
219
220 <para>
221 There is one row for each buffer in the shared cache. Unused buffers are
222 shown with all fields null except <structfield>bufferid</structfield>. Shared system
223 catalogs are shown as belonging to database zero.
224 </para>
225
226 <para>
227 Because the cache is shared by all the databases, there will normally be
228 pages from relations not belonging to the current database. This means
229 that there may not be matching join rows in <structname>pg_class</structname> for
230 some rows, or that there could even be incorrect joins. If you are
231 trying to join against <structname>pg_class</structname>, it's a good idea to
232 restrict the join to rows having <structfield>reldatabase</structfield> equal to
233 the current database's OID or zero.
234 </para>
235
236 <para>
237 Since buffer manager locks are not taken to copy the buffer state data that
238 the view will display, accessing <structname>pg_buffercache</structname> view
239 has less impact on normal buffer activity but it doesn't provide a consistent
240 set of results across all buffers. However, we ensure that the information of
241 each buffer is self-consistent.
242 </para>
243 </sect2>
244
245 <sect2 id="pgbuffercache-pg-buffercache-numa">
246 <title>The <structname>pg_buffercache_numa</structname> View</title>
247
248 <para>
249 The definitions of the columns exposed by the view are shown in <xref linkend="pgbuffercache-numa-columns"/>.
250 </para>
251
252 <table id="pgbuffercache-numa-columns">
253 <title><structname>pg_buffercache_numa</structname> Columns</title>
254 <tgroup cols="1">
255 <thead>
256 <row>
257 <entry role="catalog_table_entry"><para role="column_definition">
258 Column Type
259 </para>
260 <para>
261 Description
262 </para></entry>
263 </row>
264 </thead>
265
266 <tbody>
267 <row>
268 <entry role="catalog_table_entry"><para role="column_definition">
269 <structfield>bufferid</structfield> <type>integer</type>
270 </para>
271 <para>
272 ID, in the range 1..<varname>shared_buffers</varname>
273 </para></entry>
274 </row>
275
276 <row>
277 <entry role="catalog_table_entry"><para role="column_definition">
278 <structfield>os_page_num</structfield> <type>bigint</type>
279 </para>
280 <para>
281 number of OS memory page for this buffer
282 </para></entry>
283 </row>
284
285 <row>
286 <entry role="catalog_table_entry"><para role="column_definition">
287 <structfield>numa_node</structfield> <type>int</type>
288 </para>
289 <para>
290 ID of <acronym>NUMA</acronym> node
291 </para></entry>
292 </row>
293
294 </tbody>
295 </tgroup>
296 </table>
297
298 <para>
299 As <acronym>NUMA</acronym> node ID inquiry for each page requires memory pages
300 to be paged-in, the first execution of this function can take a noticeable
301 amount of time. In all the cases (first execution or not), retrieving this
302 information is costly and querying the view at a high frequency is not recommended.
303 </para>
304
305 <warning>
306 <para>
307 When determining the <acronym>NUMA</acronym> node, the view touches
308 all memory pages for the shared memory segment. This will force
309 allocation of the shared memory, if it wasn't allocated already,
310 and the memory may get allocated in a single <acronym>NUMA</acronym>
311 node (depending on system configuration).
312 </para>
313 </warning>
314
315 </sect2>
316
317 <sect2 id="pgbuffercache-summary">
318 <title>The <function>pg_buffercache_summary()</function> Function</title>
319
320 <para>
321 The definitions of the columns exposed by the function are shown in <xref linkend="pgbuffercache-summary-columns"/>.
322 </para>
323
324 <table id="pgbuffercache-summary-columns">
325 <title><function>pg_buffercache_summary()</function> Output Columns</title>
326 <tgroup cols="1">
327 <thead>
328 <row>
329 <entry role="catalog_table_entry"><para role="column_definition">
330 Column Type
331 </para>
332 <para>
333 Description
334 </para></entry>
335 </row>
336 </thead>
337
338 <tbody>
339 <row>
340 <entry role="catalog_table_entry"><para role="column_definition">
341 <structfield>buffers_used</structfield> <type>int4</type>
342 </para>
343 <para>
344 Number of used shared buffers
345 </para></entry>
346 </row>
347
348 <row>
349 <entry role="catalog_table_entry"><para role="column_definition">
350 <structfield>buffers_unused</structfield> <type>int4</type>
351 </para>
352 <para>
353 Number of unused shared buffers
354 </para></entry>
355 </row>
356
357 <row>
358 <entry role="catalog_table_entry"><para role="column_definition">
359 <structfield>buffers_dirty</structfield> <type>int4</type>
360 </para>
361 <para>
362 Number of dirty shared buffers
363 </para></entry>
364 </row>
365
366 <row>
367 <entry role="catalog_table_entry"><para role="column_definition">
368 <structfield>buffers_pinned</structfield> <type>int4</type>
369 </para>
370 <para>
371 Number of pinned shared buffers
372 </para></entry>
373 </row>
374
375 <row>
376 <entry role="catalog_table_entry"><para role="column_definition">
377 <structfield>usagecount_avg</structfield> <type>float8</type>
378 </para>
379 <para>
380 Average usage count of used shared buffers
381 </para></entry>
382 </row>
383 </tbody>
384 </tgroup>
385 </table>
386
387 <para>
388 The <function>pg_buffercache_summary()</function> function returns a
389 single row summarizing the state of all shared buffers. Similar and more
390 detailed information is provided by the
391 <structname>pg_buffercache</structname> view, but
392 <function>pg_buffercache_summary()</function> is significantly cheaper.
393 </para>
394
395 <para>
396 Like the <structname>pg_buffercache</structname> view,
397 <function>pg_buffercache_summary()</function> does not acquire buffer
398 manager locks. Therefore concurrent activity can lead to minor inaccuracies
399 in the result.
400 </para>
401 </sect2>
402
403 <sect2 id="pgbuffercache-usage-counts">
404 <title>The <function>pg_buffercache_usage_counts()</function> Function</title>
405
406 <para>
407 The definitions of the columns exposed by the function are shown in
408 <xref linkend="pgbuffercache_usage_counts-columns"/>.
409 </para>
410
411 <table id="pgbuffercache_usage_counts-columns">
412 <title><function>pg_buffercache_usage_counts()</function> Output Columns</title>
413 <tgroup cols="1">
414 <thead>
415 <row>
416 <entry role="catalog_table_entry"><para role="column_definition">
417 Column Type
418 </para>
419 <para>
420 Description
421 </para></entry>
422 </row>
423 </thead>
424
425 <tbody>
426 <row>
427 <entry role="catalog_table_entry"><para role="column_definition">
428 <structfield>usage_count</structfield> <type>int4</type>
429 </para>
430 <para>
431 A possible buffer usage count
432 </para></entry>
433 </row>
434
435 <row>
436 <entry role="catalog_table_entry"><para role="column_definition">
437 <structfield>buffers</structfield> <type>int4</type>
438 </para>
439 <para>
440 Number of buffers with the usage count
441 </para></entry>
442 </row>
443
444 <row>
445 <entry role="catalog_table_entry"><para role="column_definition">
446 <structfield>dirty</structfield> <type>int4</type>
447 </para>
448 <para>
449 Number of dirty buffers with the usage count
450 </para></entry>
451 </row>
452
453 <row>
454 <entry role="catalog_table_entry"><para role="column_definition">
455 <structfield>pinned</structfield> <type>int4</type>
456 </para>
457 <para>
458 Number of pinned buffers with the usage count
459 </para></entry>
460 </row>
461 </tbody>
462 </tgroup>
463 </table>
464
465 <para>
466 The <function>pg_buffercache_usage_counts()</function> function returns a
467 set of rows summarizing the states of all shared buffers, aggregated over
468 the possible usage count values. Similar and more detailed information is
469 provided by the <structname>pg_buffercache</structname> view, but
470 <function>pg_buffercache_usage_counts()</function> is significantly cheaper.
471 </para>
472
473 <para>
474 Like the <structname>pg_buffercache</structname> view,
475 <function>pg_buffercache_usage_counts()</function> does not acquire buffer
476 manager locks. Therefore concurrent activity can lead to minor inaccuracies
477 in the result.
478 </para>
479 </sect2>
480
481 <sect2 id="pgbuffercache-pg-buffercache-evict">
482 <title>The <function>pg_buffercache_evict()</function> Function</title>
483 <para>
484 The <function>pg_buffercache_evict()</function> function takes a buffer
485 identifier, as shown in the <structfield>bufferid</structfield> column of
486 the <structname>pg_buffercache</structname> view. It returns information
487 about whether the buffer was evicted and flushed. The buffer_evicted
488 column is true on success, and false if the buffer wasn't valid, if it
489 couldn't be evicted because it was pinned, or if it became dirty again
490 after an attempt to write it out. The buffer_flushed column is true if the
491 buffer was flushed. This does not necessarily mean that buffer was flushed
492 by us, it might be flushed by someone else. The result is immediately out
493 of date upon return, as the buffer might become valid again at any time due
494 to concurrent activity. The function is intended for developer testing
495 only.
496 </para>
497 </sect2>
498
499 <sect2 id="pgbuffercache-pg-buffercache-evict-relation">
500 <title>The <function>pg_buffercache_evict_relation()</function> Function</title>
501 <para>
502 The <function>pg_buffercache_evict_relation()</function> function is very
503 similar to the <function>pg_buffercache_evict()</function> function. The
504 difference is that the <function>pg_buffercache_evict_relation()</function>
505 takes a relation identifier instead of buffer identifier. It tries to
506 evict all buffers for all forks in that relation.
507
508 It returns the number of evicted buffers, flushed buffers and the number of
509 buffers that could not be evicted. Flushed buffers haven't necessarily
510 been flushed by us, they might have been flushed by someone else. The
511 result is immediately out of date upon return, as buffers might immediately
512 be read back in due to concurrent activity. The function is intended for
513 developer testing only.
514 </para>
515 </sect2>
516
517 <sect2 id="pgbuffercache-pg-buffercache-evict-all">
518 <title>The <function>pg_buffercache_evict_all()</function> Function</title>
519 <para>
520 The <function>pg_buffercache_evict_all()</function> function is very
521 similar to the <function>pg_buffercache_evict()</function> function. The
522 difference is, the <function>pg_buffercache_evict_all()</function> function
523 does not take an argument; instead it tries to evict all buffers in the
524 buffer pool. It returns the number of evicted buffers, flushed buffers and
525 the number of buffers that could not be evicted. Flushed buffers haven't
526 necessarily been flushed by us, they might have been flushed by someone
527 else. The result is immediately out of date upon return, as buffers might
528 immediately be read back in due to concurrent activity. The function is
529 intended for developer testing only.
530 </para>
531 </sect2>
532
533 <sect2 id="pgbuffercache-sample-output">
534 <title>Sample Output</title>
535
536 <screen>
537 regression=# SELECT n.nspname, c.relname, count(*) AS buffers
538 FROM pg_buffercache b JOIN pg_class c
539 ON b.relfilenode = pg_relation_filenode(c.oid) AND
540 b.reldatabase IN (0, (SELECT oid FROM pg_database
541 WHERE datname = current_database()))
542 JOIN pg_namespace n ON n.oid = c.relnamespace
543 GROUP BY n.nspname, c.relname
544 ORDER BY 3 DESC
545 LIMIT 10;
546
547 nspname | relname | buffers
548 ------------+------------------------+---------
549 public | delete_test_table | 593
550 public | delete_test_table_pkey | 494
551 pg_catalog | pg_attribute | 472
552 public | quad_poly_tbl | 353
553 public | tenk2 | 349
554 public | tenk1 | 349
555 public | gin_test_idx | 306
556 pg_catalog | pg_largeobject | 206
557 public | gin_test_tbl | 188
558 public | spgist_text_tbl | 182
559 (10 rows)
560
561
562 regression=# SELECT * FROM pg_buffercache_summary();
563 buffers_used | buffers_unused | buffers_dirty | buffers_pinned | usagecount_avg
564 --------------+----------------+---------------+----------------+----------------
565 248 | 2096904 | 39 | 0 | 3.141129
566 (1 row)
567
568
569 regression=# SELECT * FROM pg_buffercache_usage_counts();
570 usage_count | buffers | dirty | pinned
571 -------------+---------+-------+--------
572 0 | 14650 | 0 | 0
573 1 | 1436 | 671 | 0
574 2 | 102 | 88 | 0
575 3 | 23 | 21 | 0
576 4 | 9 | 7 | 0
577 5 | 164 | 106 | 0
578 (6 rows)
579 </screen>
580 </sect2>
581
582 <sect2 id="pgbuffercache-authors">
583 <title>Authors</title>
584
585 <para>
586 Mark Kirkwood <email>[email protected]</email>
587 </para>
588
589 <para>
590 Design suggestions: Neil Conway <email>[email protected]</email>
591 </para>
592
593 <para>
594 Debugging advice: Tom Lane <email>[email protected]</email>
595 </para>
596 </sect2>
597
598 </sect1>