1
by Martin Pitt
Import upstream version 9.3~beta1 |
1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
2 |
<HTML |
|
3 |
><HEAD |
|
4 |
><TITLE |
|
5 |
>Tablespaces</TITLE |
|
6 |
><META |
|
7 |
NAME="GENERATOR" |
|
8 |
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK |
|
9 |
REV="MADE" |
|
10 |
HREF="mailto:pgsql-docs@postgresql.org"><LINK |
|
11 |
REL="HOME" |
|
1.3.4
by Martin Pitt
Import upstream version 9.3.13 |
12 |
TITLE="PostgreSQL 9.3.13 Documentation" |
1
by Martin Pitt
Import upstream version 9.3~beta1 |
13 |
HREF="index.html"><LINK |
14 |
REL="UP" |
|
15 |
TITLE="Managing Databases" |
|
16 |
HREF="managing-databases.html"><LINK |
|
17 |
REL="PREVIOUS" |
|
18 |
TITLE="Destroying a Database" |
|
19 |
HREF="manage-ag-dropdb.html"><LINK |
|
20 |
REL="NEXT" |
|
21 |
TITLE="Localization" |
|
22 |
HREF="charset.html"><LINK |
|
23 |
REL="STYLESHEET" |
|
24 |
TYPE="text/css" |
|
25 |
HREF="stylesheet.css"><META |
|
26 |
HTTP-EQUIV="Content-Type" |
|
27 |
CONTENT="text/html; charset=ISO-8859-1"><META |
|
28 |
NAME="creation" |
|
1.3.4
by Martin Pitt
Import upstream version 9.3.13 |
29 |
CONTENT="2016-05-09T21:13:26"></HEAD |
1
by Martin Pitt
Import upstream version 9.3~beta1 |
30 |
><BODY |
31 |
CLASS="SECT1" |
|
32 |
><DIV |
|
33 |
CLASS="NAVHEADER" |
|
34 |
><TABLE |
|
35 |
SUMMARY="Header navigation table" |
|
36 |
WIDTH="100%" |
|
37 |
BORDER="0" |
|
38 |
CELLPADDING="0" |
|
39 |
CELLSPACING="0" |
|
40 |
><TR |
|
41 |
><TH |
|
42 |
COLSPAN="5" |
|
43 |
ALIGN="center" |
|
44 |
VALIGN="bottom" |
|
45 |
><A |
|
46 |
HREF="index.html" |
|
1.3.4
by Martin Pitt
Import upstream version 9.3.13 |
47 |
>PostgreSQL 9.3.13 Documentation</A |
1
by Martin Pitt
Import upstream version 9.3~beta1 |
48 |
></TH |
49 |
></TR |
|
50 |
><TR |
|
51 |
><TD |
|
52 |
WIDTH="10%" |
|
53 |
ALIGN="left" |
|
54 |
VALIGN="top" |
|
55 |
><A |
|
56 |
TITLE="Destroying a Database" |
|
57 |
HREF="manage-ag-dropdb.html" |
|
58 |
ACCESSKEY="P" |
|
59 |
>Prev</A |
|
60 |
></TD |
|
61 |
><TD |
|
62 |
WIDTH="10%" |
|
63 |
ALIGN="left" |
|
64 |
VALIGN="top" |
|
65 |
><A |
|
66 |
HREF="managing-databases.html" |
|
67 |
ACCESSKEY="U" |
|
68 |
>Up</A |
|
69 |
></TD |
|
70 |
><TD |
|
71 |
WIDTH="60%" |
|
72 |
ALIGN="center" |
|
73 |
VALIGN="bottom" |
|
74 |
>Chapter 21. Managing Databases</TD |
|
75 |
><TD |
|
76 |
WIDTH="20%" |
|
77 |
ALIGN="right" |
|
78 |
VALIGN="top" |
|
79 |
><A |
|
80 |
TITLE="Localization" |
|
81 |
HREF="charset.html" |
|
82 |
ACCESSKEY="N" |
|
83 |
>Next</A |
|
84 |
></TD |
|
85 |
></TR |
|
86 |
></TABLE |
|
87 |
><HR |
|
88 |
ALIGN="LEFT" |
|
89 |
WIDTH="100%"></DIV |
|
90 |
><DIV |
|
91 |
CLASS="SECT1" |
|
92 |
><H1 |
|
93 |
CLASS="SECT1" |
|
94 |
><A |
|
95 |
NAME="MANAGE-AG-TABLESPACES" |
|
96 |
>21.6. Tablespaces</A |
|
97 |
></H1 |
|
98 |
><P |
|
99 |
> Tablespaces in <SPAN |
|
100 |
CLASS="PRODUCTNAME" |
|
101 |
>PostgreSQL</SPAN |
|
102 |
> allow database administrators to
|
|
103 |
define locations in the file system where the files representing |
|
104 |
database objects can be stored. Once created, a tablespace can be referred |
|
105 |
to by name when creating database objects. |
|
106 |
</P |
|
107 |
><P |
|
108 |
> By using tablespaces, an administrator can control the disk layout
|
|
109 |
of a <SPAN |
|
110 |
CLASS="PRODUCTNAME" |
|
111 |
>PostgreSQL</SPAN |
|
112 |
> installation. This is useful in at
|
|
113 |
least two ways. First, if the partition or volume on which the |
|
114 |
cluster was initialized runs out of space and cannot be extended, |
|
115 |
a tablespace can be created on a different partition and used |
|
116 |
until the system can be reconfigured. |
|
117 |
</P |
|
118 |
><P |
|
119 |
> Second, tablespaces allow an administrator to use knowledge of the
|
|
120 |
usage pattern of database objects to optimize performance. For |
|
121 |
example, an index which is very heavily used can be placed on a |
|
122 |
very fast, highly available disk, such as an expensive solid state |
|
123 |
device. At the same time a table storing archived data which is |
|
124 |
rarely used or not performance critical could be stored on a less |
|
125 |
expensive, slower disk system. |
|
126 |
</P |
|
127 |
><P |
|
128 |
> To define a tablespace, use the <A |
|
129 |
HREF="sql-createtablespace.html" |
|
130 |
>CREATE TABLESPACE</A |
|
131 |
>
|
|
132 |
command, for example:: |
|
133 |
</P><PRE |
|
134 |
CLASS="PROGRAMLISTING" |
|
135 |
>CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';</PRE |
|
136 |
><P> |
|
137 |
The location must be an existing, empty directory that is owned by |
|
138 |
the <SPAN |
|
139 |
CLASS="PRODUCTNAME" |
|
140 |
>PostgreSQL</SPAN |
|
141 |
> operating system user. All objects subsequently
|
|
142 |
created within the tablespace will be stored in files underneath this |
|
143 |
directory. |
|
144 |
</P |
|
145 |
><DIV |
|
146 |
CLASS="NOTE" |
|
147 |
><BLOCKQUOTE |
|
148 |
CLASS="NOTE" |
|
149 |
><P |
|
150 |
><B |
|
151 |
>Note: </B |
|
152 |
> There is usually not much point in making more than one
|
|
153 |
tablespace per logical file system, since you cannot control the location |
|
154 |
of individual files within a logical file system. However, |
|
155 |
<SPAN |
|
156 |
CLASS="PRODUCTNAME" |
|
157 |
>PostgreSQL</SPAN |
|
158 |
> does not enforce any such limitation, and
|
|
159 |
indeed it is not directly aware of the file system boundaries on your |
|
160 |
system. It just stores files in the directories you tell it to use. |
|
161 |
</P |
|
162 |
></BLOCKQUOTE |
|
163 |
></DIV |
|
164 |
><P |
|
165 |
> Creation of the tablespace itself must be done as a database superuser,
|
|
166 |
but after that you can allow ordinary database users to use it. |
|
167 |
To do that, grant them the <TT |
|
168 |
CLASS="LITERAL" |
|
169 |
>CREATE</TT |
|
170 |
> privilege on it.
|
|
171 |
</P |
|
172 |
><P |
|
173 |
> Tables, indexes, and entire databases can be assigned to
|
|
174 |
particular tablespaces. To do so, a user with the <TT |
|
175 |
CLASS="LITERAL" |
|
176 |
>CREATE</TT |
|
177 |
>
|
|
178 |
privilege on a given tablespace must pass the tablespace name as a |
|
179 |
parameter to the relevant command. For example, the following creates |
|
180 |
a table in the tablespace <TT |
|
181 |
CLASS="LITERAL" |
|
182 |
>space1</TT |
|
183 |
>:
|
|
184 |
</P><PRE |
|
185 |
CLASS="PROGRAMLISTING" |
|
186 |
>CREATE TABLE foo(i int) TABLESPACE space1;</PRE |
|
187 |
><P> |
|
188 |
</P |
|
189 |
><P |
|
190 |
> Alternatively, use the <A |
|
191 |
HREF="runtime-config-client.html#GUC-DEFAULT-TABLESPACE" |
|
192 |
>default_tablespace</A |
|
193 |
> parameter:
|
|
194 |
</P><PRE |
|
195 |
CLASS="PROGRAMLISTING" |
|
196 |
>SET default_tablespace = space1;
|
|
197 |
CREATE TABLE foo(i int);</PRE |
|
198 |
><P> |
|
199 |
When <TT |
|
200 |
CLASS="VARNAME" |
|
201 |
>default_tablespace</TT |
|
202 |
> is set to anything but an empty
|
|
203 |
string, it supplies an implicit <TT |
|
204 |
CLASS="LITERAL" |
|
205 |
>TABLESPACE</TT |
|
206 |
> clause for
|
|
207 |
<TT |
|
208 |
CLASS="COMMAND" |
|
209 |
>CREATE TABLE</TT |
|
210 |
> and <TT |
|
211 |
CLASS="COMMAND" |
|
212 |
>CREATE INDEX</TT |
|
213 |
> commands that
|
|
214 |
do not have an explicit one. |
|
215 |
</P |
|
216 |
><P |
|
217 |
> There is also a <A |
|
218 |
HREF="runtime-config-client.html#GUC-TEMP-TABLESPACES" |
|
219 |
>temp_tablespaces</A |
|
220 |
> parameter, which
|
|
221 |
determines the placement of temporary tables and indexes, as well as |
|
222 |
temporary files that are used for purposes such as sorting large data |
|
223 |
sets. This can be a list of tablespace names, rather than only one, |
|
224 |
so that the load associated with temporary objects can be spread over |
|
225 |
multiple tablespaces. A random member of the list is picked each time |
|
226 |
a temporary object is to be created. |
|
227 |
</P |
|
228 |
><P |
|
229 |
> The tablespace associated with a database is used to store the system
|
|
230 |
catalogs of that database. Furthermore, it is the default tablespace |
|
231 |
used for tables, indexes, and temporary files created within the database, |
|
232 |
if no <TT |
|
233 |
CLASS="LITERAL" |
|
234 |
>TABLESPACE</TT |
|
235 |
> clause is given and no other selection is
|
|
236 |
specified by <TT |
|
237 |
CLASS="VARNAME" |
|
238 |
>default_tablespace</TT |
|
239 |
> or
|
|
240 |
<TT |
|
241 |
CLASS="VARNAME" |
|
242 |
>temp_tablespaces</TT |
|
243 |
> (as appropriate).
|
|
244 |
If a database is created without specifying a tablespace for it, |
|
245 |
it uses the same tablespace as the template database it is copied from. |
|
246 |
</P |
|
247 |
><P |
|
248 |
> Two tablespaces are automatically created when the database cluster
|
|
249 |
is initialized. The |
|
250 |
<TT |
|
251 |
CLASS="LITERAL" |
|
252 |
>pg_global</TT |
|
253 |
> tablespace is used for shared system catalogs. The
|
|
254 |
<TT |
|
255 |
CLASS="LITERAL" |
|
256 |
>pg_default</TT |
|
257 |
> tablespace is the default tablespace of the
|
|
258 |
<TT |
|
259 |
CLASS="LITERAL" |
|
260 |
>template1</TT |
|
261 |
> and <TT |
|
262 |
CLASS="LITERAL" |
|
263 |
>template0</TT |
|
264 |
> databases (and, therefore,
|
|
265 |
will be the default tablespace for other databases as well, unless |
|
266 |
overridden by a <TT |
|
267 |
CLASS="LITERAL" |
|
268 |
>TABLESPACE</TT |
|
269 |
> clause in <TT |
|
270 |
CLASS="COMMAND" |
|
271 |
>CREATE
|
|
272 |
DATABASE</TT |
|
273 |
>).
|
|
274 |
</P |
|
275 |
><P |
|
276 |
> Once created, a tablespace can be used from any database, provided
|
|
277 |
the requesting user has sufficient privilege. This means that a tablespace |
|
278 |
cannot be dropped until all objects in all databases using the tablespace |
|
279 |
have been removed. |
|
280 |
</P |
|
281 |
><P |
|
282 |
> To remove an empty tablespace, use the <A |
|
283 |
HREF="sql-droptablespace.html" |
|
284 |
>DROP TABLESPACE</A |
|
285 |
>
|
|
286 |
command. |
|
287 |
</P |
|
288 |
><P |
|
289 |
> To determine the set of existing tablespaces, examine the
|
|
1.1.2
by Martin Pitt
Import upstream version 9.3~rc1 |
290 |
<A |
291 |
HREF="catalog-pg-tablespace.html" |
|
292 |
><TT |
|
1
by Martin Pitt
Import upstream version 9.3~beta1 |
293 |
CLASS="STRUCTNAME" |
294 |
>pg_tablespace</TT |
|
1.1.2
by Martin Pitt
Import upstream version 9.3~rc1 |
295 |
>
|
296 |
</A |
|
1
by Martin Pitt
Import upstream version 9.3~beta1 |
297 |
> system catalog, for example
|
298 |
</P><PRE |
|
299 |
CLASS="SYNOPSIS" |
|
300 |
>SELECT spcname FROM pg_tablespace;</PRE |
|
301 |
><P> |
|
302 |
The <A |
|
303 |
HREF="app-psql.html" |
|
304 |
><SPAN |
|
305 |
CLASS="APPLICATION" |
|
306 |
>psql</SPAN |
|
307 |
></A |
|
308 |
> program's <TT |
|
309 |
CLASS="LITERAL" |
|
310 |
>\db</TT |
|
311 |
> meta-command
|
|
312 |
is also useful for listing the existing tablespaces. |
|
313 |
</P |
|
314 |
><P |
|
315 |
> <SPAN |
|
316 |
CLASS="PRODUCTNAME" |
|
317 |
>PostgreSQL</SPAN |
|
318 |
> makes use of symbolic links
|
|
319 |
to simplify the implementation of tablespaces. This |
|
320 |
means that tablespaces can be used <SPAN |
|
321 |
CLASS="emphasis" |
|
322 |
><I |
|
323 |
CLASS="EMPHASIS" |
|
324 |
>only</I |
|
325 |
></SPAN |
|
326 |
> on systems
|
|
327 |
that support symbolic links. |
|
328 |
</P |
|
329 |
><P |
|
330 |
> The directory <TT |
|
331 |
CLASS="FILENAME" |
|
332 |
>$PGDATA/pg_tblspc</TT |
|
333 |
> contains symbolic links that
|
|
334 |
point to each of the non-built-in tablespaces defined in the cluster. |
|
335 |
Although not recommended, it is possible to adjust the tablespace |
|
1.1.2
by Martin Pitt
Import upstream version 9.3~rc1 |
336 |
layout by hand by redefining these links. Under no circumstances perform |
337 |
this operation while the server is running. Note that in PostgreSQL 9.1 |
|
338 |
and earlier you will also need to update the <TT |
|
1
by Martin Pitt
Import upstream version 9.3~beta1 |
339 |
CLASS="STRUCTNAME" |
340 |
>pg_tablespace</TT |
|
1.1.2
by Martin Pitt
Import upstream version 9.3~rc1 |
341 |
>
|
342 |
catalog with the new locations. (If you do not, <TT |
|
1
by Martin Pitt
Import upstream version 9.3~beta1 |
343 |
CLASS="LITERAL" |
344 |
>pg_dump</TT |
|
1.1.2
by Martin Pitt
Import upstream version 9.3~rc1 |
345 |
> will
|
346 |
continue to output the old tablespace locations.) |
|
1
by Martin Pitt
Import upstream version 9.3~beta1 |
347 |
</P |
348 |
></DIV |
|
349 |
><DIV |
|
350 |
CLASS="NAVFOOTER" |
|
351 |
><HR |
|
352 |
ALIGN="LEFT" |
|
353 |
WIDTH="100%"><TABLE |
|
354 |
SUMMARY="Footer navigation table" |
|
355 |
WIDTH="100%" |
|
356 |
BORDER="0" |
|
357 |
CELLPADDING="0" |
|
358 |
CELLSPACING="0" |
|
359 |
><TR |
|
360 |
><TD |
|
361 |
WIDTH="33%" |
|
362 |
ALIGN="left" |
|
363 |
VALIGN="top" |
|
364 |
><A |
|
365 |
HREF="manage-ag-dropdb.html" |
|
366 |
ACCESSKEY="P" |
|
367 |
>Prev</A |
|
368 |
></TD |
|
369 |
><TD |
|
370 |
WIDTH="34%" |
|
371 |
ALIGN="center" |
|
372 |
VALIGN="top" |
|
373 |
><A |
|
374 |
HREF="index.html" |
|
375 |
ACCESSKEY="H" |
|
376 |
>Home</A |
|
377 |
></TD |
|
378 |
><TD |
|
379 |
WIDTH="33%" |
|
380 |
ALIGN="right" |
|
381 |
VALIGN="top" |
|
382 |
><A |
|
383 |
HREF="charset.html" |
|
384 |
ACCESSKEY="N" |
|
385 |
>Next</A |
|
386 |
></TD |
|
387 |
></TR |
|
388 |
><TR |
|
389 |
><TD |
|
390 |
WIDTH="33%" |
|
391 |
ALIGN="left" |
|
392 |
VALIGN="top" |
|
393 |
>Destroying a Database</TD |
|
394 |
><TD |
|
395 |
WIDTH="34%" |
|
396 |
ALIGN="center" |
|
397 |
VALIGN="top" |
|
398 |
><A |
|
399 |
HREF="managing-databases.html" |
|
400 |
ACCESSKEY="U" |
|
401 |
>Up</A |
|
402 |
></TD |
|
403 |
><TD |
|
404 |
WIDTH="33%" |
|
405 |
ALIGN="right" |
|
406 |
VALIGN="top" |
|
407 |
>Localization</TD |
|
408 |
></TR |
|
409 |
></TABLE |
|
410 |
></DIV |
|
411 |
></BODY |
|
412 |
></HTML |
|
413 |
>
|