]> granicus.if.org Git - postgresql/blob - src/backend/catalog/toasting.c
a8c2da66dc3ca764086f71d3f895f76069be0a9d
[postgresql] / src / backend / catalog / toasting.c
1 /*-------------------------------------------------------------------------
2  *
3  * toasting.c
4  *        This file contains routines to support creation of toast tables
5  *
6  *
7  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/catalog/toasting.c,v 1.22 2009/12/23 02:35:18 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include "access/heapam.h"
18 #include "access/tuptoaster.h"
19 #include "access/xact.h"
20 #include "catalog/dependency.h"
21 #include "catalog/heap.h"
22 #include "catalog/index.h"
23 #include "catalog/indexing.h"
24 #include "catalog/namespace.h"
25 #include "catalog/pg_namespace.h"
26 #include "catalog/pg_opclass.h"
27 #include "catalog/pg_type.h"
28 #include "catalog/toasting.h"
29 #include "miscadmin.h"
30 #include "nodes/makefuncs.h"
31 #include "utils/builtins.h"
32 #include "utils/syscache.h"
33
34
35 static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
36                                    Datum reloptions, bool force);
37 static bool needs_toast_table(Relation rel);
38
39
40 /*
41  * AlterTableCreateToastTable
42  *              If the table needs a toast table, and doesn't already have one,
43  *              then create a toast table for it.  (With the force option, make
44  *              a toast table even if it appears unnecessary.)
45  *
46  * The caller can also specify the OID to be used for the toast table.
47  * Usually, toastOid should be InvalidOid to allow a free OID to be assigned.
48  * (This option, as well as the force option, is not used by core Postgres,
49  * but is provided to support pg_migrator.)
50  *
51  * reloptions for the toast table can be passed, too.  Pass (Datum) 0
52  * for default reloptions.
53  *
54  * We expect the caller to have verified that the relation is a table and have
55  * already done any necessary permission checks.  Callers expect this function
56  * to end with CommandCounterIncrement if it makes any changes.
57  */
58 void
59 AlterTableCreateToastTable(Oid relOid, Oid toastOid,
60                                                    Datum reloptions, bool force)
61 {
62         Relation        rel;
63
64         /*
65          * Grab an exclusive lock on the target table, which we will NOT release
66          * until end of transaction.  (This is probably redundant in all present
67          * uses...)
68          */
69         rel = heap_open(relOid, AccessExclusiveLock);
70
71         /* create_toast_table does all the work */
72         (void) create_toast_table(rel, toastOid, InvalidOid, reloptions, force);
73
74         heap_close(rel, NoLock);
75 }
76
77 /*
78  * Create a toast table during bootstrap
79  *
80  * Here we need to prespecify the OIDs of the toast table and its index
81  */
82 void
83 BootstrapToastTable(char *relName, Oid toastOid, Oid toastIndexOid)
84 {
85         Relation        rel;
86
87         rel = heap_openrv(makeRangeVar(NULL, relName, -1), AccessExclusiveLock);
88
89         /* Note: during bootstrap may see uncataloged relation */
90         if (rel->rd_rel->relkind != RELKIND_RELATION &&
91                 rel->rd_rel->relkind != RELKIND_UNCATALOGED)
92                 ereport(ERROR,
93                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
94                                  errmsg("\"%s\" is not a table",
95                                                 relName)));
96
97         /* create_toast_table does all the work */
98         if (!create_toast_table(rel, toastOid, toastIndexOid, (Datum) 0, false))
99                 elog(ERROR, "\"%s\" does not require a toast table",
100                          relName);
101
102         heap_close(rel, NoLock);
103 }
104
105
106 /*
107  * create_toast_table --- internal workhorse
108  *
109  * rel is already opened and exclusive-locked
110  * toastOid and toastIndexOid are normally InvalidOid, but
111  * either or both can be nonzero to specify caller-assigned OIDs
112  */
113 static bool
114 create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
115                                    Datum reloptions, bool force)
116 {
117         Oid                     relOid = RelationGetRelid(rel);
118         HeapTuple       reltup;
119         TupleDesc       tupdesc;
120         bool            shared_relation;
121         Relation        class_rel;
122         Oid                     toast_relid;
123         Oid                     toast_idxid;
124         Oid                     namespaceid;
125         char            toast_relname[NAMEDATALEN];
126         char            toast_idxname[NAMEDATALEN];
127         IndexInfo  *indexInfo;
128         Oid                     classObjectId[2];
129         int16           coloptions[2];
130         ObjectAddress baseobject,
131                                 toastobject;
132
133         /*
134          * Toast table is shared if and only if its parent is.
135          *
136          * We cannot allow toasting a shared relation after initdb (because
137          * there's no way to mark it toasted in other databases' pg_class).
138          */
139         shared_relation = rel->rd_rel->relisshared;
140         if (shared_relation && !IsBootstrapProcessingMode())
141                 ereport(ERROR,
142                                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
143                                  errmsg("shared tables cannot be toasted after initdb")));
144
145         /*
146          * Is it already toasted?
147          */
148         if (rel->rd_rel->reltoastrelid != InvalidOid)
149                 return false;
150
151         /*
152          * Check to see whether the table actually needs a TOAST table.
153          *
154          * Caller can optionally override this check.  (Note: at present no
155          * callers in core Postgres do so, but this option is needed by
156          * pg_migrator.)
157          */
158         if (!force && !needs_toast_table(rel))
159                 return false;
160
161         /*
162          * Create the toast table and its index
163          */
164         snprintf(toast_relname, sizeof(toast_relname),
165                          "pg_toast_%u", relOid);
166         snprintf(toast_idxname, sizeof(toast_idxname),
167                          "pg_toast_%u_index", relOid);
168
169         /* this is pretty painful...  need a tuple descriptor */
170         tupdesc = CreateTemplateTupleDesc(3, false);
171         TupleDescInitEntry(tupdesc, (AttrNumber) 1,
172                                            "chunk_id",
173                                            OIDOID,
174                                            -1, 0);
175         TupleDescInitEntry(tupdesc, (AttrNumber) 2,
176                                            "chunk_seq",
177                                            INT4OID,
178                                            -1, 0);
179         TupleDescInitEntry(tupdesc, (AttrNumber) 3,
180                                            "chunk_data",
181                                            BYTEAOID,
182                                            -1, 0);
183
184         /*
185          * Ensure that the toast table doesn't itself get toasted, or we'll be
186          * toast :-(.  This is essential for chunk_data because type bytea is
187          * toastable; hit the other two just to be sure.
188          */
189         tupdesc->attrs[0]->attstorage = 'p';
190         tupdesc->attrs[1]->attstorage = 'p';
191         tupdesc->attrs[2]->attstorage = 'p';
192
193         /*
194          * Toast tables for regular relations go in pg_toast; those for temp
195          * relations go into the per-backend temp-toast-table namespace.
196          */
197         if (rel->rd_islocaltemp)
198                 namespaceid = GetTempToastNamespace();
199         else
200                 namespaceid = PG_TOAST_NAMESPACE;
201
202         toast_relid = heap_create_with_catalog(toast_relname,
203                                                                                    namespaceid,
204                                                                                    rel->rd_rel->reltablespace,
205                                                                                    toastOid,
206                                                                                    InvalidOid,
207                                                                                    rel->rd_rel->relowner,
208                                                                                    tupdesc,
209                                                                                    NIL,
210                                                                                    RELKIND_TOASTVALUE,
211                                                                                    shared_relation,
212                                                                                    true,
213                                                                                    0,
214                                                                                    ONCOMMIT_NOOP,
215                                                                                    reloptions,
216                                                                                    false,
217                                                                                    true);
218
219         /* make the toast relation visible, else index creation will fail */
220         CommandCounterIncrement();
221
222         /*
223          * Create unique index on chunk_id, chunk_seq.
224          *
225          * NOTE: the normal TOAST access routines could actually function with a
226          * single-column index on chunk_id only. However, the slice access
227          * routines use both columns for faster access to an individual chunk. In
228          * addition, we want it to be unique as a check against the possibility of
229          * duplicate TOAST chunk OIDs. The index might also be a little more
230          * efficient this way, since btree isn't all that happy with large numbers
231          * of equal keys.
232          */
233
234         indexInfo = makeNode(IndexInfo);
235         indexInfo->ii_NumIndexAttrs = 2;
236         indexInfo->ii_KeyAttrNumbers[0] = 1;
237         indexInfo->ii_KeyAttrNumbers[1] = 2;
238         indexInfo->ii_Expressions = NIL;
239         indexInfo->ii_ExpressionsState = NIL;
240         indexInfo->ii_Predicate = NIL;
241         indexInfo->ii_PredicateState = NIL;
242         indexInfo->ii_ExclusionOps = NULL;
243         indexInfo->ii_ExclusionProcs = NULL;
244         indexInfo->ii_ExclusionStrats = NULL;
245         indexInfo->ii_Unique = true;
246         indexInfo->ii_ReadyForInserts = true;
247         indexInfo->ii_Concurrent = false;
248         indexInfo->ii_BrokenHotChain = false;
249
250         classObjectId[0] = OID_BTREE_OPS_OID;
251         classObjectId[1] = INT4_BTREE_OPS_OID;
252
253         coloptions[0] = 0;
254         coloptions[1] = 0;
255
256         toast_idxid = index_create(toast_relid, toast_idxname, toastIndexOid,
257                                                            indexInfo,
258                                                            list_make2("chunk_id", "chunk_seq"),
259                                                            BTREE_AM_OID,
260                                                            rel->rd_rel->reltablespace,
261                                                            classObjectId, coloptions, (Datum) 0,
262                                                            true, false, false, false,
263                                                            true, false, false);
264
265         /*
266          * Store the toast table's OID in the parent relation's pg_class row
267          */
268         class_rel = heap_open(RelationRelationId, RowExclusiveLock);
269
270         reltup = SearchSysCacheCopy(RELOID,
271                                                                 ObjectIdGetDatum(relOid),
272                                                                 0, 0, 0);
273         if (!HeapTupleIsValid(reltup))
274                 elog(ERROR, "cache lookup failed for relation %u", relOid);
275
276         ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;
277
278         if (!IsBootstrapProcessingMode())
279         {
280                 /* normal case, use a transactional update */
281                 simple_heap_update(class_rel, &reltup->t_self, reltup);
282
283                 /* Keep catalog indexes current */
284                 CatalogUpdateIndexes(class_rel, reltup);
285         }
286         else
287         {
288                 /* While bootstrapping, we cannot UPDATE, so overwrite in-place */
289                 heap_inplace_update(class_rel, reltup);
290         }
291
292         heap_freetuple(reltup);
293
294         heap_close(class_rel, RowExclusiveLock);
295
296         /*
297          * Register dependency from the toast table to the master, so that the
298          * toast table will be deleted if the master is.  Skip this in bootstrap
299          * mode.
300          */
301         if (!IsBootstrapProcessingMode())
302         {
303                 baseobject.classId = RelationRelationId;
304                 baseobject.objectId = relOid;
305                 baseobject.objectSubId = 0;
306                 toastobject.classId = RelationRelationId;
307                 toastobject.objectId = toast_relid;
308                 toastobject.objectSubId = 0;
309
310                 recordDependencyOn(&toastobject, &baseobject, DEPENDENCY_INTERNAL);
311         }
312
313         /*
314          * Make changes visible
315          */
316         CommandCounterIncrement();
317
318         return true;
319 }
320
321 /*
322  * Check to see whether the table needs a TOAST table.  It does only if
323  * (1) there are any toastable attributes, and (2) the maximum length
324  * of a tuple could exceed TOAST_TUPLE_THRESHOLD.  (We don't want to
325  * create a toast table for something like "f1 varchar(20)".)
326  */
327 static bool
328 needs_toast_table(Relation rel)
329 {
330         int32           data_length = 0;
331         bool            maxlength_unknown = false;
332         bool            has_toastable_attrs = false;
333         TupleDesc       tupdesc;
334         Form_pg_attribute *att;
335         int32           tuple_length;
336         int                     i;
337
338         tupdesc = rel->rd_att;
339         att = tupdesc->attrs;
340
341         for (i = 0; i < tupdesc->natts; i++)
342         {
343                 if (att[i]->attisdropped)
344                         continue;
345                 data_length = att_align_nominal(data_length, att[i]->attalign);
346                 if (att[i]->attlen > 0)
347                 {
348                         /* Fixed-length types are never toastable */
349                         data_length += att[i]->attlen;
350                 }
351                 else
352                 {
353                         int32           maxlen = type_maximum_size(att[i]->atttypid,
354                                                                                                    att[i]->atttypmod);
355
356                         if (maxlen < 0)
357                                 maxlength_unknown = true;
358                         else
359                                 data_length += maxlen;
360                         if (att[i]->attstorage != 'p')
361                                 has_toastable_attrs = true;
362                 }
363         }
364         if (!has_toastable_attrs)
365                 return false;                   /* nothing to toast? */
366         if (maxlength_unknown)
367                 return true;                    /* any unlimited-length attrs? */
368         tuple_length = MAXALIGN(offsetof(HeapTupleHeaderData, t_bits) +
369                                                         BITMAPLEN(tupdesc->natts)) +
370                 MAXALIGN(data_length);
371         return (tuple_length > TOAST_TUPLE_THRESHOLD);
372 }