4 Tablespaces in PostgreSQL allow database administrators to define
5 locations in the file system where the files representing database
6 objects can be stored. Once created, a tablespace can be referred to by
7 name when creating database objects.
9 By using tablespaces, an administrator can control the disk layout of a
10 PostgreSQL installation. This is useful in at least two ways. First, if
11 the partition or volume on which the cluster was initialized runs out
12 of space and cannot be extended, a tablespace can be created on a
13 different partition and used until the system can be reconfigured.
15 Second, tablespaces allow an administrator to use knowledge of the
16 usage pattern of database objects to optimize performance. For example,
17 an index which is very heavily used can be placed on a very fast,
18 highly available disk, such as an expensive solid state device. At the
19 same time a table storing archived data which is rarely used or not
20 performance critical could be stored on a less expensive, slower disk
25 Even though located outside the main PostgreSQL data directory,
26 tablespaces are an integral part of the database cluster and cannot be
27 treated as an autonomous collection of data files. They are dependent
28 on metadata contained in the main data directory, and therefore cannot
29 be attached to a different database cluster or backed up individually.
30 Similarly, if you lose a tablespace (file deletion, disk failure,
31 etc.), the database cluster might become unreadable or unable to start.
32 Placing a tablespace on a temporary file system like a RAM disk risks
33 the reliability of the entire cluster.
35 To define a tablespace, use the CREATE TABLESPACE command, for
37 CREATE TABLESPACE fastspace LOCATION '/ssd1/postgresql/data';
39 The location must be an existing, empty directory that is owned by the
40 PostgreSQL operating system user. All objects subsequently created
41 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 or
48 There is usually not much point in making more than one tablespace per
49 logical file system, since you cannot control the location of
50 individual files within a logical file system. However, PostgreSQL does
51 not enforce any such limitation, and indeed it is not directly aware of
52 the file system boundaries on your system. It just stores files in the
53 directories you tell it to use.
55 Creation of the tablespace itself must be done as a database superuser,
56 but after that you can allow ordinary database users to use it. To do
57 that, grant them the CREATE privilege on it.
59 Tables, indexes, and entire databases can be assigned to particular
60 tablespaces. To do so, a user with the CREATE privilege on a given
61 tablespace must pass the tablespace name as a parameter to the relevant
62 command. For example, the following creates a table in the tablespace
64 CREATE TABLE foo(i int) TABLESPACE space1;
66 Alternatively, use the default_tablespace parameter:
67 SET default_tablespace = space1;
68 CREATE TABLE foo(i int);
70 When default_tablespace is set to anything but an empty string, it
71 supplies an implicit TABLESPACE clause for CREATE TABLE and CREATE
72 INDEX commands that do not have an explicit one.
74 There is also a temp_tablespaces parameter, which determines the
75 placement of temporary tables and indexes, as well as temporary files
76 that are used for purposes such as sorting large data sets. This can be
77 a list of tablespace names, rather than only one, so that the load
78 associated with temporary objects can be spread over multiple
79 tablespaces. A random member of the list is picked each time a
80 temporary object is to be created.
82 The tablespace associated with a database is used to store the system
83 catalogs of that database. Furthermore, it is the default tablespace
84 used for tables, indexes, and temporary files created within the
85 database, if no TABLESPACE clause is given and no other selection is
86 specified by default_tablespace or temp_tablespaces (as appropriate).
87 If a database is created without specifying a tablespace for it, it
88 uses the same tablespace as the template database it is copied from.
90 Two tablespaces are automatically created when the database cluster is
91 initialized. The pg_global tablespace is used only for shared system
92 catalogs. The pg_default tablespace is the default tablespace of the
93 template1 and template0 databases (and, therefore, will be the default
94 tablespace for other databases as well, unless overridden by a
95 TABLESPACE clause in CREATE DATABASE).
97 Once created, a tablespace can be used from any database, provided the
98 requesting user has sufficient privilege. This means that a tablespace
99 cannot be dropped until all objects in all databases using the
100 tablespace have been removed.
102 To remove an empty tablespace, use the DROP TABLESPACE command.
104 To determine the set of existing tablespaces, examine the pg_tablespace
105 system catalog, for example
106 SELECT spcname, spcowner::regrole, pg_tablespace_location(oid) FROM pg_tablespac
109 It is possible to find which databases use which tablespaces; see
110 Table 9.76. The psql program's \db meta-command is also useful for
111 listing the existing tablespaces.
113 The directory $PGDATA/pg_tblspc contains symbolic links that point to
114 each of the non-built-in tablespaces defined in the cluster. Although
115 not recommended, it is possible to adjust the tablespace layout by hand
116 by redefining these links. Under no circumstances perform this
117 operation while the server is running.