gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
14KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>68.1. Database File Layout</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="storage.html" title="Chapter 68. Database Physical Storage" /><link rel="next" href="storage-toast.html" title="68.2. TOAST" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">68.1. Database File Layout</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="storage.html" title="Chapter 68. Database Physical Storage">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="storage.html" title="Chapter 68. Database Physical Storage">Up</a></td><th width="60%" align="center">Chapter 68. Database Physical Storage</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="storage-toast.html" title="68.2. TOAST">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="STORAGE-FILE-LAYOUT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">68.1. Database File Layout</h2></div></div></div><p>
  3. This section describes the storage format at the level of files and
  4. directories.
  5. </p><p>
  6. Traditionally, the configuration and data files used by a database
  7. cluster are stored together within the cluster's data
  8. directory, commonly referred to as <code class="varname">PGDATA</code> (after the name of the
  9. environment variable that can be used to define it). A common location for
  10. <code class="varname">PGDATA</code> is <code class="filename">/var/lib/pgsql/data</code>. Multiple clusters,
  11. managed by different server instances, can exist on the same machine.
  12. </p><p>
  13. The <code class="varname">PGDATA</code> directory contains several subdirectories and control
  14. files, as shown in <a class="xref" href="storage-file-layout.html#PGDATA-CONTENTS-TABLE" title="Table 68.1. Contents of PGDATA">Table 68.1</a>. In addition to
  15. these required items, the cluster configuration files
  16. <code class="filename">postgresql.conf</code>, <code class="filename">pg_hba.conf</code>, and
  17. <code class="filename">pg_ident.conf</code> are traditionally stored in
  18. <code class="varname">PGDATA</code>, although it is possible to place them elsewhere.
  19. </p><div class="table" id="PGDATA-CONTENTS-TABLE"><p class="title"><strong>Table 68.1. Contents of <code class="varname">PGDATA</code></strong></p><div class="table-contents"><table class="table" summary="Contents of PGDATA" border="1"><colgroup><col /><col /></colgroup><thead><tr><th>
  20. Item
  21. </th><th>Description</th></tr></thead><tbody><tr><td><code class="filename">PG_VERSION</code></td><td>A file containing the major version number of <span class="productname">PostgreSQL</span></td></tr><tr><td><code class="filename">base</code></td><td>Subdirectory containing per-database subdirectories</td></tr><tr><td><code class="filename">current_logfiles</code></td><td>File recording the log file(s) currently written to by the logging
  22. collector</td></tr><tr><td><code class="filename">global</code></td><td>Subdirectory containing cluster-wide tables, such as
  23. <code class="structname">pg_database</code></td></tr><tr><td><code class="filename">pg_commit_ts</code></td><td>Subdirectory containing transaction commit timestamp data</td></tr><tr><td><code class="filename">pg_dynshmem</code></td><td>Subdirectory containing files used by the dynamic shared memory
  24. subsystem</td></tr><tr><td><code class="filename">pg_logical</code></td><td>Subdirectory containing status data for logical decoding</td></tr><tr><td><code class="filename">pg_multixact</code></td><td>Subdirectory containing multitransaction status data
  25. (used for shared row locks)</td></tr><tr><td><code class="filename">pg_notify</code></td><td>Subdirectory containing LISTEN/NOTIFY status data</td></tr><tr><td><code class="filename">pg_replslot</code></td><td>Subdirectory containing replication slot data</td></tr><tr><td><code class="filename">pg_serial</code></td><td>Subdirectory containing information about committed serializable transactions</td></tr><tr><td><code class="filename">pg_snapshots</code></td><td>Subdirectory containing exported snapshots</td></tr><tr><td><code class="filename">pg_stat</code></td><td>Subdirectory containing permanent files for the statistics
  26. subsystem</td></tr><tr><td><code class="filename">pg_stat_tmp</code></td><td>Subdirectory containing temporary files for the statistics
  27. subsystem</td></tr><tr><td><code class="filename">pg_subtrans</code></td><td>Subdirectory containing subtransaction status data</td></tr><tr><td><code class="filename">pg_tblspc</code></td><td>Subdirectory containing symbolic links to tablespaces</td></tr><tr><td><code class="filename">pg_twophase</code></td><td>Subdirectory containing state files for prepared transactions</td></tr><tr><td><code class="filename">pg_wal</code></td><td>Subdirectory containing WAL (Write Ahead Log) files</td></tr><tr><td><code class="filename">pg_xact</code></td><td>Subdirectory containing transaction commit status data</td></tr><tr><td><code class="filename">postgresql.auto.conf</code></td><td>A file used for storing configuration parameters that are set by
  28. <code class="command">ALTER SYSTEM</code></td></tr><tr><td><code class="filename">postmaster.opts</code></td><td>A file recording the command-line options the server was
  29. last started with</td></tr><tr><td><code class="filename">postmaster.pid</code></td><td>A lock file recording the current postmaster process ID (PID),
  30. cluster data directory path,
  31. postmaster start timestamp,
  32. port number,
  33. Unix-domain socket directory path (empty on Windows),
  34. first valid listen_address (IP address or <code class="literal">*</code>, or empty if
  35. not listening on TCP),
  36. and shared memory segment ID
  37. (this file is not present after server shutdown)</td></tr></tbody></table></div></div><br class="table-break" /><p>
  38. For each database in the cluster there is a subdirectory within
  39. <code class="varname">PGDATA</code><code class="filename">/base</code>, named after the database's OID in
  40. <code class="structname">pg_database</code>. This subdirectory is the default location
  41. for the database's files; in particular, its system catalogs are stored
  42. there.
  43. </p><p>
  44. Note that the following sections describe the behavior of the builtin
  45. <code class="literal">heap</code> <a class="link" href="tableam.html" title="Chapter 60. Table Access Method Interface Definition">table access method</a>,
  46. and the builtin <a class="link" href="indexam.html" title="Chapter 61. Index Access Method Interface Definition">index access methods</a>. Due
  47. to the extensible nature of <span class="productname">PostgreSQL</span>, other
  48. access methods might work differently.
  49. </p><p>
  50. Each table and index is stored in a separate file. For ordinary relations,
  51. these files are named after the table or index's <em class="firstterm">filenode</em> number,
  52. which can be found in <code class="structname">pg_class</code>.<code class="structfield">relfilenode</code>. But
  53. for temporary relations, the file name is of the form
  54. <code class="literal">t<em class="replaceable"><code>BBB</code></em>_<em class="replaceable"><code>FFF</code></em></code>, where <em class="replaceable"><code>BBB</code></em>
  55. is the backend ID of the backend which created the file, and <em class="replaceable"><code>FFF</code></em>
  56. is the filenode number. In either case, in addition to the main file (a/k/a
  57. main fork), each table and index has a <em class="firstterm">free space map</em> (see <a class="xref" href="storage-fsm.html" title="68.3. Free Space Map">Section 68.3</a>), which stores information about free space available in
  58. the relation. The free space map is stored in a file named with the filenode
  59. number plus the suffix <code class="literal">_fsm</code>. Tables also have a
  60. <em class="firstterm">visibility map</em>, stored in a fork with the suffix <code class="literal">_vm</code>,
  61. to track which pages are known to have no dead tuples. The visibility map is
  62. described further in <a class="xref" href="storage-vm.html" title="68.4. Visibility Map">Section 68.4</a>. Unlogged tables and indexes
  63. have a third fork, known as the initialization fork, which is stored in a fork
  64. with the suffix <code class="literal">_init</code> (see <a class="xref" href="storage-init.html" title="68.5. The Initialization Fork">Section 68.5</a>).
  65. </p><div class="caution"><h3 class="title">Caution</h3><p>
  66. Note that while a table's filenode often matches its OID, this is
  67. <span class="emphasis"><em>not</em></span> necessarily the case; some operations, like
  68. <code class="command">TRUNCATE</code>, <code class="command">REINDEX</code>, <code class="command">CLUSTER</code> and some forms
  69. of <code class="command">ALTER TABLE</code>, can change the filenode while preserving the OID.
  70. Avoid assuming that filenode and table OID are the same.
  71. Also, for certain system catalogs including <code class="structname">pg_class</code> itself,
  72. <code class="structname">pg_class</code>.<code class="structfield">relfilenode</code> contains zero. The
  73. actual filenode number of these catalogs is stored in a lower-level data
  74. structure, and can be obtained using the <code class="function">pg_relation_filenode()</code>
  75. function.
  76. </p></div><p>
  77. When a table or index exceeds 1 GB, it is divided into gigabyte-sized
  78. <em class="firstterm">segments</em>. The first segment's file name is the same as the
  79. filenode; subsequent segments are named filenode.1, filenode.2, etc.
  80. This arrangement avoids problems on platforms that have file size limitations.
  81. (Actually, 1 GB is just the default segment size. The segment size can be
  82. adjusted using the configuration option <code class="option">--with-segsize</code>
  83. when building <span class="productname">PostgreSQL</span>.)
  84. In principle, free space map and visibility map forks could require multiple
  85. segments as well, though this is unlikely to happen in practice.
  86. </p><p>
  87. A table that has columns with potentially large entries will have an
  88. associated <em class="firstterm">TOAST</em> table, which is used for out-of-line storage of
  89. field values that are too large to keep in the table rows proper.
  90. <code class="structname">pg_class</code>.<code class="structfield">reltoastrelid</code> links from a table to
  91. its <acronym class="acronym">TOAST</acronym> table, if any.
  92. See <a class="xref" href="storage-toast.html" title="68.2. TOAST">Section 68.2</a> for more information.
  93. </p><p>
  94. The contents of tables and indexes are discussed further in
  95. <a class="xref" href="storage-page-layout.html" title="68.6. Database Page Layout">Section 68.6</a>.
  96. </p><p>
  97. Tablespaces make the scenario more complicated. Each user-defined tablespace
  98. has a symbolic link inside the <code class="varname">PGDATA</code><code class="filename">/pg_tblspc</code>
  99. directory, which points to the physical tablespace directory (i.e., the
  100. location specified in the tablespace's <code class="command">CREATE TABLESPACE</code> command).
  101. This symbolic link is named after
  102. the tablespace's OID. Inside the physical tablespace directory there is
  103. a subdirectory with a name that depends on the <span class="productname">PostgreSQL</span>
  104. server version, such as <code class="literal">PG_9.0_201008051</code>. (The reason for using
  105. this subdirectory is so that successive versions of the database can use
  106. the same <code class="command">CREATE TABLESPACE</code> location value without conflicts.)
  107. Within the version-specific subdirectory, there is
  108. a subdirectory for each database that has elements in the tablespace, named
  109. after the database's OID. Tables and indexes are stored within that
  110. directory, using the filenode naming scheme.
  111. The <code class="literal">pg_default</code> tablespace is not accessed through
  112. <code class="filename">pg_tblspc</code>, but corresponds to
  113. <code class="varname">PGDATA</code><code class="filename">/base</code>. Similarly, the <code class="literal">pg_global</code>
  114. tablespace is not accessed through <code class="filename">pg_tblspc</code>, but corresponds to
  115. <code class="varname">PGDATA</code><code class="filename">/global</code>.
  116. </p><p>
  117. The <code class="function">pg_relation_filepath()</code> function shows the entire path
  118. (relative to <code class="varname">PGDATA</code>) of any relation. It is often useful
  119. as a substitute for remembering many of the above rules. But keep in
  120. mind that this function just gives the name of the first segment of the
  121. main fork of the relation — you may need to append a segment number
  122. and/or <code class="literal">_fsm</code>, <code class="literal">_vm</code>, or <code class="literal">_init</code> to find all
  123. the files associated with the relation.
  124. </p><p>
  125. Temporary files (for operations such as sorting more data than can fit in
  126. memory) are created within <code class="varname">PGDATA</code><code class="filename">/base/pgsql_tmp</code>,
  127. or within a <code class="filename">pgsql_tmp</code> subdirectory of a tablespace directory
  128. if a tablespace other than <code class="literal">pg_default</code> is specified for them.
  129. The name of a temporary file has the form
  130. <code class="filename">pgsql_tmp<em class="replaceable"><code>PPP</code></em>.<em class="replaceable"><code>NNN</code></em></code>,
  131. where <em class="replaceable"><code>PPP</code></em> is the PID of the owning backend and
  132. <em class="replaceable"><code>NNN</code></em> distinguishes different temporary files of that backend.
  133. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="storage.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="storage.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="storage-toast.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 68. Database Physical Storage </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 68.2. TOAST</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1