Home Explore Blog CI



postgresql

8th chunk of `doc/src/sgml/manage-ag.sgml`
e4267e97af42a52ec200a2fcbf0ef10eaad95680e990eaf10000000100000f46
 <literal>CREATE</literal> privilege on it.
  </para>

  <para>
   Tables, indexes, and entire databases can be assigned to
   particular tablespaces. To do so, a user with the <literal>CREATE</literal>
   privilege on a given tablespace must pass the tablespace name as a
   parameter to the relevant command. For example, the following creates
   a table in the tablespace <literal>space1</literal>:
<programlisting>
CREATE TABLE foo(i int) TABLESPACE space1;
</programlisting>
  </para>

  <para>
   Alternatively, use the <xref linkend="guc-default-tablespace"/> parameter:
<programlisting>
SET default_tablespace = space1;
CREATE TABLE foo(i int);
</programlisting>
   When <varname>default_tablespace</varname> is set to anything but an empty
   string, it supplies an implicit <literal>TABLESPACE</literal> clause for
   <command>CREATE TABLE</command> and <command>CREATE INDEX</command> commands that
   do not have an explicit one.
  </para>

  <para>
   There is also a <xref linkend="guc-temp-tablespaces"/> parameter, which
   determines the placement of temporary tables and indexes, as well as
   temporary files that are used for purposes such as sorting large data
   sets.  This can be a list of tablespace names, rather than only one,
   so that the load associated with temporary objects can be spread over
   multiple tablespaces.  A random member of the list is picked each time
   a temporary object is to be created.
  </para>

  <para>
   The tablespace associated with a database is used to store the system
   catalogs of that database.  Furthermore, it is the default tablespace
   used for tables, indexes, and temporary files created within the database,
   if no <literal>TABLESPACE</literal> clause is given and no other selection is
   specified by <varname>default_tablespace</varname> or
   <varname>temp_tablespaces</varname> (as appropriate).
   If a database is created without specifying a tablespace for it,
   it uses the same tablespace as the template database it is copied from.
  </para>

  <para>
   Two tablespaces are automatically created when the database cluster
   is initialized.  The
   <literal>pg_global</literal> tablespace is used only for shared system catalogs. The
   <literal>pg_default</literal> tablespace is the default tablespace of the
   <literal>template1</literal> and <literal>template0</literal> databases (and, therefore,
   will be the default tablespace for other databases as well, unless
   overridden by a <literal>TABLESPACE</literal> clause in <command>CREATE
   DATABASE</command>).
  </para>

  <para>
   Once created, a tablespace can be used from any database, provided
   the requesting user has sufficient privilege. This means that a tablespace
   cannot be dropped until all objects in all databases using the tablespace
   have been removed.
  </para>

  <para>
   To remove an empty tablespace, use the <xref
   linkend="sql-droptablespace"/>
   command.
  </para>

  <para>
   To determine the set of existing tablespaces, examine the
   <link linkend="catalog-pg-tablespace"><structname>pg_tablespace</structname>
   </link> system catalog, for example
<synopsis>
SELECT spcname, spcowner::regrole, pg_tablespace_location(oid) FROM pg_tablespace;
</synopsis>
   It is possible to find which databases use which tablespaces;
   see <xref linkend="functions-info-catalog-table"/>.  The <xref
   linkend="app-psql"/> program's <literal>\db</literal> meta-command
   is also useful for listing the existing tablespaces.
  </para>

  <para>
   The directory <filename>$PGDATA/pg_tblspc</filename> contains symbolic links that
   point to each of the non-built-in tablespaces defined in the cluster.
   Although not recommended, it is possible to adjust the tablespace
   layout by hand by redefining these links. Under no circumstances perform
   this operation while the server is running.
  </para>

 </sect1>
</chapter>

Title: Tablespace Usage, Management, and System Catalogs in PostgreSQL
Summary
Tablespaces can be assigned to tables, indexes, and databases using the TABLESPACE parameter or the default_tablespace setting. temp_tablespaces manages temporary object placement across multiple tablespaces. Each database has an associated tablespace for system catalogs and default object creation. pg_global and pg_default tablespaces are created during cluster initialization. Tablespaces can be used from any database with sufficient privileges and cannot be dropped until all objects using them are removed. Existing tablespaces can be examined using the pg_tablespace system catalog, the \db meta-command in psql, or by inspecting symbolic links in $PGDATA/pg_tblspc.