gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

132 líneas
10KB

  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>22.6. Tablespaces</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="manage-ag-dropdb.html" title="22.5. Destroying a Database" /><link rel="next" href="charset.html" title="Chapter 23. Localization" /></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">22.6. Tablespaces</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="manage-ag-dropdb.html" title="22.5. Destroying a Database">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="managing-databases.html" title="Chapter 22. Managing Databases">Up</a></td><th width="60%" align="center">Chapter 22. Managing Databases</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="charset.html" title="Chapter 23. Localization">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="MANAGE-AG-TABLESPACES"><div class="titlepage"><div><div><h2 class="title" style="clear: both">22.6. Tablespaces</h2></div></div></div><a id="id-1.6.9.9.2" class="indexterm"></a><p>
  3. Tablespaces in <span class="productname">PostgreSQL</span> allow database administrators to
  4. define locations in the file system where the files representing
  5. database objects can be stored. Once created, a tablespace can be referred
  6. to by name when creating database objects.
  7. </p><p>
  8. By using tablespaces, an administrator can control the disk layout
  9. of a <span class="productname">PostgreSQL</span> installation. This is useful in at
  10. least two ways. First, if the partition or volume on which the
  11. cluster was initialized runs out of space and cannot be extended,
  12. a tablespace can be created on a different partition and used
  13. until the system can be reconfigured.
  14. </p><p>
  15. Second, tablespaces allow an administrator to use knowledge of the
  16. usage pattern of database objects to optimize performance. For
  17. example, an index which is very heavily used can be placed on a
  18. very fast, highly available disk, such as an expensive solid state
  19. device. At the same time a table storing archived data which is
  20. rarely used or not performance critical could be stored on a less
  21. expensive, slower disk system.
  22. </p><div class="warning"><h3 class="title">Warning</h3><p>
  23. Even though located outside the main PostgreSQL data directory,
  24. tablespaces are an integral part of the database cluster and
  25. <span class="emphasis"><em>cannot</em></span> be treated as an autonomous collection
  26. of data files. They are dependent on metadata contained in the main
  27. data directory, and therefore cannot be attached to a different
  28. database cluster or backed up individually. Similarly, if you lose
  29. a tablespace (file deletion, disk failure, etc), the database cluster
  30. might become unreadable or unable to start. Placing a tablespace
  31. on a temporary file system like a RAM disk risks the reliability of
  32. the entire cluster.
  33. </p></div><p>
  34. To define a tablespace, use the <a class="xref" href="sql-createtablespace.html" title="CREATE TABLESPACE"><span class="refentrytitle">CREATE TABLESPACE</span></a>
  35. command, for example:<a id="id-1.6.9.9.7.2" class="indexterm"></a>:
  36. </p><pre class="programlisting">
  37. CREATE TABLESPACE fastspace LOCATION '/ssd1/postgresql/data';
  38. </pre><p>
  39. The location must be an existing, empty directory that is owned by
  40. the <span class="productname">PostgreSQL</span> operating system user. All objects subsequently
  41. created within the tablespace will be stored in files underneath this
  42. directory. The location must not be on removable or transient storage,
  43. as the cluster might fail to function if the tablespace is missing
  44. or lost.
  45. </p><div class="note"><h3 class="title">Note</h3><p>
  46. There is usually not much point in making more than one
  47. tablespace per logical file system, since you cannot control the location
  48. of individual files within a logical file system. However,
  49. <span class="productname">PostgreSQL</span> does not enforce any such limitation, and
  50. indeed it is not directly aware of the file system boundaries on your
  51. system. It just stores files in the directories you tell it to use.
  52. </p></div><p>
  53. Creation of the tablespace itself must be done as a database superuser,
  54. but after that you can allow ordinary database users to use it.
  55. To do that, grant them the <code class="literal">CREATE</code> privilege on it.
  56. </p><p>
  57. Tables, indexes, and entire databases can be assigned to
  58. particular tablespaces. To do so, a user with the <code class="literal">CREATE</code>
  59. privilege on a given tablespace must pass the tablespace name as a
  60. parameter to the relevant command. For example, the following creates
  61. a table in the tablespace <code class="literal">space1</code>:
  62. </p><pre class="programlisting">
  63. CREATE TABLE foo(i int) TABLESPACE space1;
  64. </pre><p>
  65. </p><p>
  66. Alternatively, use the <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TABLESPACE">default_tablespace</a> parameter:
  67. </p><pre class="programlisting">
  68. SET default_tablespace = space1;
  69. CREATE TABLE foo(i int);
  70. </pre><p>
  71. When <code class="varname">default_tablespace</code> is set to anything but an empty
  72. string, it supplies an implicit <code class="literal">TABLESPACE</code> clause for
  73. <code class="command">CREATE TABLE</code> and <code class="command">CREATE INDEX</code> commands that
  74. do not have an explicit one.
  75. </p><p>
  76. There is also a <a class="xref" href="runtime-config-client.html#GUC-TEMP-TABLESPACES">temp_tablespaces</a> parameter, which
  77. determines the placement of temporary tables and indexes, as well as
  78. temporary files that are used for purposes such as sorting large data
  79. sets. This can be a list of tablespace names, rather than only one,
  80. so that the load associated with temporary objects can be spread over
  81. multiple tablespaces. A random member of the list is picked each time
  82. a temporary object is to be created.
  83. </p><p>
  84. The tablespace associated with a database is used to store the system
  85. catalogs of that database. Furthermore, it is the default tablespace
  86. used for tables, indexes, and temporary files created within the database,
  87. if no <code class="literal">TABLESPACE</code> clause is given and no other selection is
  88. specified by <code class="varname">default_tablespace</code> or
  89. <code class="varname">temp_tablespaces</code> (as appropriate).
  90. If a database is created without specifying a tablespace for it,
  91. it uses the same tablespace as the template database it is copied from.
  92. </p><p>
  93. Two tablespaces are automatically created when the database cluster
  94. is initialized. The
  95. <code class="literal">pg_global</code> tablespace is used for shared system catalogs. The
  96. <code class="literal">pg_default</code> tablespace is the default tablespace of the
  97. <code class="literal">template1</code> and <code class="literal">template0</code> databases (and, therefore,
  98. will be the default tablespace for other databases as well, unless
  99. overridden by a <code class="literal">TABLESPACE</code> clause in <code class="command">CREATE
  100. DATABASE</code>).
  101. </p><p>
  102. Once created, a tablespace can be used from any database, provided
  103. the requesting user has sufficient privilege. This means that a tablespace
  104. cannot be dropped until all objects in all databases using the tablespace
  105. have been removed.
  106. </p><p>
  107. To remove an empty tablespace, use the <a class="xref" href="sql-droptablespace.html" title="DROP TABLESPACE"><span class="refentrytitle">DROP TABLESPACE</span></a>
  108. command.
  109. </p><p>
  110. To determine the set of existing tablespaces, examine the
  111. <a class="link" href="catalog-pg-tablespace.html" title="51.55. pg_tablespace"><code class="structname">pg_tablespace</code>
  112. </a> system catalog, for example
  113. </p><pre class="synopsis">
  114. SELECT spcname FROM pg_tablespace;
  115. </pre><p>
  116. The <a class="xref" href="app-psql.html" title="psql"><span class="refentrytitle"><span class="application">psql</span></span></a> program's <code class="literal">\db</code> meta-command
  117. is also useful for listing the existing tablespaces.
  118. </p><p>
  119. <span class="productname">PostgreSQL</span> makes use of symbolic links
  120. to simplify the implementation of tablespaces. This
  121. means that tablespaces can be used <span class="emphasis"><em>only</em></span> on systems
  122. that support symbolic links.
  123. </p><p>
  124. The directory <code class="filename">$PGDATA/pg_tblspc</code> contains symbolic links that
  125. point to each of the non-built-in tablespaces defined in the cluster.
  126. Although not recommended, it is possible to adjust the tablespace
  127. layout by hand by redefining these links. Under no circumstances perform
  128. this operation while the server is running. Note that in PostgreSQL 9.1
  129. and earlier you will also need to update the <code class="structname">pg_tablespace</code>
  130. catalog with the new locations. (If you do not, <code class="literal">pg_dump</code> will
  131. continue to output the old tablespace locations.)
  132. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="manage-ag-dropdb.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="managing-databases.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="charset.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">22.5. Destroying a Database </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 23. Localization</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1