Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * toasting.c
4 : : * This file contains routines to support creation of toast tables
5 : : *
6 : : *
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/catalog/toasting.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #include "access/genam.h"
18 : : #include "access/heapam.h"
19 : : #include "access/toast_compression.h"
20 : : #include "access/xact.h"
21 : : #include "catalog/binary_upgrade.h"
22 : : #include "catalog/catalog.h"
23 : : #include "catalog/dependency.h"
24 : : #include "catalog/heap.h"
25 : : #include "catalog/index.h"
26 : : #include "catalog/namespace.h"
27 : : #include "catalog/pg_am.h"
28 : : #include "catalog/pg_namespace.h"
29 : : #include "catalog/pg_opclass.h"
30 : : #include "catalog/toasting.h"
31 : : #include "miscadmin.h"
32 : : #include "nodes/makefuncs.h"
33 : : #include "utils/fmgroids.h"
34 : : #include "utils/rel.h"
35 : : #include "utils/syscache.h"
36 : :
37 : : static void CheckAndCreateToastTable(Oid relOid, Datum reloptions,
38 : : LOCKMODE lockmode, bool check,
39 : : Oid OIDOldToast);
40 : : static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
41 : : Datum reloptions, LOCKMODE lockmode, bool check,
42 : : Oid OIDOldToast);
43 : : static bool needs_toast_table(Relation rel);
44 : :
45 : :
46 : : /*
47 : : * CreateToastTable variants
48 : : * If the table needs a toast table, and doesn't already have one,
49 : : * then create a toast table for it.
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
4412 simon@2ndQuadrant.co 59 :CBC 19646 : AlterTableCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
60 : : {
1714 akapila@postgresql.o 61 : 19646 : CheckAndCreateToastTable(relOid, reloptions, lockmode, true, InvalidOid);
4412 simon@2ndQuadrant.co 62 : 19646 : }
63 : :
64 : : void
1714 akapila@postgresql.o 65 : 553 : NewHeapCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode,
66 : : Oid OIDOldToast)
67 : : {
68 : 553 : CheckAndCreateToastTable(relOid, reloptions, lockmode, false, OIDOldToast);
4412 simon@2ndQuadrant.co 69 : 553 : }
70 : :
71 : : void
72 : 26118 : NewRelationCreateToastTable(Oid relOid, Datum reloptions)
73 : : {
1714 akapila@postgresql.o 74 : 26118 : CheckAndCreateToastTable(relOid, reloptions, AccessExclusiveLock, false,
75 : : InvalidOid);
4412 simon@2ndQuadrant.co 76 : 26118 : }
77 : :
78 : : static void
1714 akapila@postgresql.o 79 : 46317 : CheckAndCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode,
80 : : bool check, Oid OIDOldToast)
81 : : {
82 : : Relation rel;
83 : :
2661 andres@anarazel.de 84 : 46317 : rel = table_open(relOid, lockmode);
85 : :
86 : : /* create_toast_table does all the work */
1714 akapila@postgresql.o 87 : 46317 : (void) create_toast_table(rel, InvalidOid, InvalidOid, reloptions, lockmode,
88 : : check, OIDOldToast);
89 : :
2661 andres@anarazel.de 90 : 46317 : table_close(rel, NoLock);
7218 tgl@sss.pgh.pa.us 91 : 46317 : }
92 : :
93 : : /*
94 : : * Create a toast table during bootstrap
95 : : *
96 : : * Here we need to prespecify the OIDs of the toast table and its index
97 : : */
98 : : void
99 : 2109 : BootstrapToastTable(char *relName, Oid toastOid, Oid toastIndexOid)
100 : : {
101 : : Relation rel;
102 : :
2661 andres@anarazel.de 103 : 2109 : rel = table_openrv(makeRangeVar(NULL, relName, -1), AccessExclusiveLock);
104 : :
4811 kgrittn@postgresql.o 105 [ - + ]: 2109 : if (rel->rd_rel->relkind != RELKIND_RELATION &&
4811 kgrittn@postgresql.o 106 [ # # ]:UBC 0 : rel->rd_rel->relkind != RELKIND_MATVIEW)
1762 peter@eisentraut.org 107 [ # # ]: 0 : elog(ERROR, "\"%s\" is not a table or materialized view",
108 : : relName);
109 : :
110 : : /* create_toast_table does all the work */
4412 simon@2ndQuadrant.co 111 [ - + ]:CBC 2109 : if (!create_toast_table(rel, toastOid, toastIndexOid, (Datum) 0,
112 : : AccessExclusiveLock, false, InvalidOid))
7218 tgl@sss.pgh.pa.us 113 [ # # ]:UBC 0 : elog(ERROR, "\"%s\" does not require a toast table",
114 : : relName);
115 : :
2661 andres@anarazel.de 116 :CBC 2109 : table_close(rel, NoLock);
7218 tgl@sss.pgh.pa.us 117 : 2109 : }
118 : :
119 : :
120 : : /*
121 : : * create_toast_table --- internal workhorse
122 : : *
123 : : * rel is already opened and locked
124 : : * toastOid and toastIndexOid are normally InvalidOid, but during
125 : : * bootstrap they can be nonzero to specify hand-assigned OIDs
126 : : */
127 : : static bool
4412 simon@2ndQuadrant.co 128 : 48426 : create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
129 : : Datum reloptions, LOCKMODE lockmode, bool check,
130 : : Oid OIDOldToast)
131 : : {
7218 tgl@sss.pgh.pa.us 132 : 48426 : Oid relOid = RelationGetRelid(rel);
133 : : HeapTuple reltup;
134 : : TupleDesc tupdesc;
135 : : bool shared_relation;
136 : : bool mapped_relation;
137 : : Relation toast_rel;
138 : : Relation class_rel;
139 : : Oid toast_relid;
140 : : Oid namespaceid;
141 : : char toast_relname[NAMEDATALEN];
142 : : char toast_idxname[NAMEDATALEN];
143 : : IndexInfo *indexInfo;
144 : : Oid collationIds[2];
145 : : Oid opclassIds[2];
146 : : int16 coloptions[2];
147 : : ObjectAddress baseobject,
148 : : toastobject;
149 : :
150 : : /*
151 : : * Is it already toasted?
152 : : */
153 [ + + ]: 48426 : if (rel->rd_rel->reltoastrelid != InvalidOid)
154 : 6787 : return false;
155 : :
156 : : /*
157 : : * Check to see whether the table actually needs a TOAST table.
158 : : */
4289 bruce@momjian.us 159 [ + + ]: 41639 : if (!IsBinaryUpgrade)
160 : : {
161 : : /* Normal mode, normal check */
162 [ + + ]: 39753 : if (!needs_toast_table(rel))
163 : 28783 : return false;
164 : : }
165 : : else
166 : : {
167 : : /*
168 : : * In binary-upgrade mode, create a TOAST table if and only if
169 : : * pg_upgrade told us to (ie, a TOAST table OID has been provided).
170 : : *
171 : : * This indicates that the old cluster had a TOAST table for the
172 : : * current table. We must create a TOAST table to receive the old
173 : : * TOAST file, even if the table seems not to need one.
174 : : *
175 : : * Contrariwise, if the old cluster did not have a TOAST table, we
176 : : * should be able to get along without one even if the new version's
177 : : * needs_toast_table rules suggest we should have one. There is a lot
178 : : * of daylight between where we will create a TOAST table and where
179 : : * one is really necessary to avoid failures, so small cross-version
180 : : * differences in the when-to-create heuristic shouldn't be a problem.
181 : : * If we tried to create a TOAST table anyway, we would have the
182 : : * problem that it might take up an OID that will conflict with some
183 : : * old-cluster table we haven't seen yet.
184 : : */
2128 tgl@sss.pgh.pa.us 185 [ + + ]: 1886 : if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid))
4289 bruce@momjian.us 186 : 1597 : return false;
187 : : }
188 : :
189 : : /*
190 : : * If requested check lockmode is sufficient. This is a cross check in
191 : : * case of errors or conflicting decisions in earlier code.
192 : : */
4412 simon@2ndQuadrant.co 193 [ + + - + ]: 11259 : if (check && lockmode != AccessExclusiveLock)
4412 simon@2ndQuadrant.co 194 [ # # ]:UBC 0 : elog(ERROR, "AccessExclusiveLock required to add toast table.");
195 : :
196 : : /*
197 : : * Create the toast table and its index
198 : : */
7218 tgl@sss.pgh.pa.us 199 :CBC 11259 : snprintf(toast_relname, sizeof(toast_relname),
200 : : "pg_toast_%u", relOid);
201 : 11259 : snprintf(toast_idxname, sizeof(toast_idxname),
202 : : "pg_toast_%u_index", relOid);
203 : :
204 : : /* this is pretty painful... need a tuple descriptor */
2723 andres@anarazel.de 205 : 11259 : tupdesc = CreateTemplateTupleDesc(3);
7218 tgl@sss.pgh.pa.us 206 : 11259 : TupleDescInitEntry(tupdesc, (AttrNumber) 1,
207 : : "chunk_id",
208 : : OIDOID,
209 : : -1, 0);
210 : 11259 : TupleDescInitEntry(tupdesc, (AttrNumber) 2,
211 : : "chunk_seq",
212 : : INT4OID,
213 : : -1, 0);
214 : 11259 : TupleDescInitEntry(tupdesc, (AttrNumber) 3,
215 : : "chunk_data",
216 : : BYTEAOID,
217 : : -1, 0);
218 : :
219 : : /*
220 : : * Ensure that the toast table doesn't itself get toasted, or we'll be
221 : : * toast :-(. This is essential for chunk_data because type bytea is
222 : : * toastable; hit the other two just to be sure.
223 : : */
2253 224 : 11259 : TupleDescAttr(tupdesc, 0)->attstorage = TYPSTORAGE_PLAIN;
225 : 11259 : TupleDescAttr(tupdesc, 1)->attstorage = TYPSTORAGE_PLAIN;
226 : 11259 : TupleDescAttr(tupdesc, 2)->attstorage = TYPSTORAGE_PLAIN;
227 : :
228 : : /* Toast field should not be compressed */
1873 rhaas@postgresql.org 229 : 11259 : TupleDescAttr(tupdesc, 0)->attcompression = InvalidCompressionMethod;
230 : 11259 : TupleDescAttr(tupdesc, 1)->attcompression = InvalidCompressionMethod;
231 : 11259 : TupleDescAttr(tupdesc, 2)->attcompression = InvalidCompressionMethod;
232 : :
50 drowley@postgresql.o 233 :GNC 11259 : populate_compact_attribute(tupdesc, 0);
234 : 11259 : populate_compact_attribute(tupdesc, 1);
235 : 11259 : populate_compact_attribute(tupdesc, 2);
236 : :
237 : 11259 : TupleDescFinalize(tupdesc);
238 : :
239 : : /*
240 : : * Toast tables for regular relations go in pg_toast; those for temp
241 : : * relations go into the per-backend temp-toast-table namespace.
242 : : */
4271 bruce@momjian.us 243 [ + + ]:CBC 11259 : if (isTempOrTempToastNamespace(rel->rd_rel->relnamespace))
6859 tgl@sss.pgh.pa.us 244 : 638 : namespaceid = GetTempToastNamespace();
245 : : else
246 : 10621 : namespaceid = PG_TOAST_NAMESPACE;
247 : :
248 : : /* Toast table is shared if and only if its parent is. */
2604 peter@eisentraut.org 249 : 11259 : shared_relation = rel->rd_rel->relisshared;
250 : :
251 : : /* It's mapped if and only if its parent is, too */
252 [ + + + - : 11259 : mapped_relation = RelationIsMapped(rel);
+ - + - +
- + + ]
253 : :
7218 tgl@sss.pgh.pa.us 254 : 22518 : toast_relid = heap_create_with_catalog(toast_relname,
255 : : namespaceid,
256 : 11259 : rel->rd_rel->reltablespace,
257 : : toastOid,
258 : : InvalidOid,
259 : : InvalidOid,
260 : 11259 : rel->rd_rel->relowner,
261 : : table_relation_toast_am(rel),
262 : : tupdesc,
263 : : NIL,
264 : : RELKIND_TOASTVALUE,
5622 rhaas@postgresql.org 265 : 11259 : rel->rd_rel->relpersistence,
266 : : shared_relation,
267 : : mapped_relation,
268 : : ONCOMMIT_NOOP,
269 : : reloptions,
270 : : false,
271 : : true,
272 : : true,
273 : : OIDOldToast,
274 : : NULL);
5763 275 [ - + ]: 11259 : Assert(toast_relid != InvalidOid);
276 : :
277 : : /* make the toast relation visible, else table_open will fail */
7218 tgl@sss.pgh.pa.us 278 : 11259 : CommandCounterIncrement();
279 : :
280 : : /* ShareLock is not really needed here, but take it anyway */
2661 andres@anarazel.de 281 : 11259 : toast_rel = table_open(toast_relid, ShareLock);
282 : :
283 : : /*
284 : : * Create unique index on chunk_id, chunk_seq.
285 : : *
286 : : * NOTE: the normal TOAST access routines could actually function with a
287 : : * single-column index on chunk_id only. However, the slice access
288 : : * routines use both columns for faster access to an individual chunk. In
289 : : * addition, we want it to be unique as a check against the possibility of
290 : : * duplicate TOAST chunk OIDs. The index might also be a little more
291 : : * efficient this way, since btree isn't all that happy with large numbers
292 : : * of equal keys.
293 : : */
294 : :
7218 tgl@sss.pgh.pa.us 295 : 11259 : indexInfo = makeNode(IndexInfo);
296 : 11259 : indexInfo->ii_NumIndexAttrs = 2;
2950 teodor@sigaev.ru 297 : 11259 : indexInfo->ii_NumIndexKeyAttrs = 2;
2945 298 : 11259 : indexInfo->ii_IndexAttrNumbers[0] = 1;
299 : 11259 : indexInfo->ii_IndexAttrNumbers[1] = 2;
7218 tgl@sss.pgh.pa.us 300 : 11259 : indexInfo->ii_Expressions = NIL;
301 : 11259 : indexInfo->ii_ExpressionsState = NIL;
302 : 11259 : indexInfo->ii_Predicate = NIL;
3339 andres@anarazel.de 303 : 11259 : indexInfo->ii_PredicateState = NULL;
5993 tgl@sss.pgh.pa.us 304 : 11259 : indexInfo->ii_ExclusionOps = NULL;
305 : 11259 : indexInfo->ii_ExclusionProcs = NULL;
306 : 11259 : indexInfo->ii_ExclusionStrats = NULL;
7218 307 : 11259 : indexInfo->ii_Unique = true;
1552 peter@eisentraut.org 308 : 11259 : indexInfo->ii_NullsNotDistinct = false;
6802 tgl@sss.pgh.pa.us 309 : 11259 : indexInfo->ii_ReadyForInserts = true;
1574 pg@bowt.ie 310 : 11259 : indexInfo->ii_CheckedUnchanged = false;
311 : 11259 : indexInfo->ii_IndexUnchanged = false;
7193 tgl@sss.pgh.pa.us 312 : 11259 : indexInfo->ii_Concurrent = false;
6802 313 : 11259 : indexInfo->ii_BrokenHotChain = false;
3014 rhaas@postgresql.org 314 : 11259 : indexInfo->ii_ParallelWorkers = 0;
3028 alvherre@alvh.no-ip. 315 : 11259 : indexInfo->ii_Am = BTREE_AM_OID;
3372 tgl@sss.pgh.pa.us 316 : 11259 : indexInfo->ii_AmCache = NULL;
317 : 11259 : indexInfo->ii_Context = CurrentMemoryContext;
318 : :
986 peter@eisentraut.org 319 : 11259 : collationIds[0] = InvalidOid;
320 : 11259 : collationIds[1] = InvalidOid;
321 : :
322 : 11259 : opclassIds[0] = OID_BTREE_OPS_OID;
323 : 11259 : opclassIds[1] = INT4_BTREE_OPS_OID;
324 : :
7056 tgl@sss.pgh.pa.us 325 : 11259 : coloptions[0] = 0;
326 : 11259 : coloptions[1] = 0;
327 : :
5405 rhaas@postgresql.org 328 : 11259 : index_create(toast_rel, toast_idxname, toastIndexOid, InvalidOid,
329 : : InvalidOid, InvalidOid,
330 : : indexInfo,
5444 bruce@momjian.us 331 : 11259 : list_make2("chunk_id", "chunk_seq"),
332 : : BTREE_AM_OID,
333 : 11259 : rel->rd_rel->reltablespace,
334 : : collationIds, opclassIds, NULL, coloptions, NULL, (Datum) 0,
335 : : INDEX_CREATE_IS_PRIMARY, 0, true, true, NULL);
336 : :
2661 andres@anarazel.de 337 : 11259 : table_close(toast_rel, NoLock);
338 : :
339 : : /*
340 : : * Store the toast table's OID in the parent relation's pg_class row
341 : : */
342 : 11259 : class_rel = table_open(RelationRelationId, RowExclusiveLock);
343 : :
7218 tgl@sss.pgh.pa.us 344 [ + + ]: 11259 : if (!IsBootstrapProcessingMode())
345 : : {
346 : : /* normal case, use a transactional update */
588 noah@leadboat.com 347 : 9150 : reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relOid));
348 [ - + ]: 9150 : if (!HeapTupleIsValid(reltup))
588 noah@leadboat.com 349 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for relation %u", relOid);
350 : :
588 noah@leadboat.com 351 :CBC 9150 : ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;
352 : :
3381 alvherre@alvh.no-ip. 353 : 9150 : CatalogTupleUpdate(class_rel, &reltup->t_self, reltup);
354 : : }
355 : : else
356 : : {
357 : : /* While bootstrapping, we cannot UPDATE, so overwrite in-place */
358 : :
359 : : ScanKeyData key[1];
360 : : void *state;
361 : :
588 noah@leadboat.com 362 : 2109 : ScanKeyInit(&key[0],
363 : : Anum_pg_class_oid,
364 : : BTEqualStrategyNumber, F_OIDEQ,
365 : : ObjectIdGetDatum(relOid));
366 : 2109 : systable_inplace_update_begin(class_rel, ClassOidIndexId, true,
367 : : NULL, 1, key, &reltup, &state);
368 [ - + ]: 2109 : if (!HeapTupleIsValid(reltup))
588 noah@leadboat.com 369 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for relation %u", relOid);
370 : :
588 noah@leadboat.com 371 :CBC 2109 : ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;
372 : :
373 : 2109 : systable_inplace_update_finish(state, reltup);
374 : : }
375 : :
7218 tgl@sss.pgh.pa.us 376 : 11259 : heap_freetuple(reltup);
377 : :
2661 andres@anarazel.de 378 : 11259 : table_close(class_rel, RowExclusiveLock);
379 : :
380 : : /*
381 : : * Register dependency from the toast table to the main, so that the toast
382 : : * table will be deleted if the main is. Skip this in bootstrap mode.
383 : : */
7218 tgl@sss.pgh.pa.us 384 [ + + ]: 11259 : if (!IsBootstrapProcessingMode())
385 : : {
386 : 9150 : baseobject.classId = RelationRelationId;
387 : 9150 : baseobject.objectId = relOid;
388 : 9150 : baseobject.objectSubId = 0;
389 : 9150 : toastobject.classId = RelationRelationId;
390 : 9150 : toastobject.objectId = toast_relid;
391 : 9150 : toastobject.objectSubId = 0;
392 : :
393 : 9150 : recordDependencyOn(&toastobject, &baseobject, DEPENDENCY_INTERNAL);
394 : : }
395 : :
396 : : /*
397 : : * Make changes visible
398 : : */
399 : 11259 : CommandCounterIncrement();
400 : :
401 : 11259 : return true;
402 : : }
403 : :
404 : : /*
405 : : * Check to see whether the table needs a TOAST table.
406 : : */
407 : : static bool
408 : 39753 : needs_toast_table(Relation rel)
409 : : {
410 : : /*
411 : : * No need to create a TOAST table for partitioned tables.
412 : : */
2966 rhaas@postgresql.org 413 [ + + ]: 39753 : if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
414 : 6111 : return false;
415 : :
416 : : /*
417 : : * We cannot allow toasting a shared relation after initdb (because
418 : : * there's no way to mark it toasted in other databases' pg_class).
419 : : */
2604 peter@eisentraut.org 420 [ + + + + ]: 33642 : if (rel->rd_rel->relisshared && !IsBootstrapProcessingMode())
421 : 385 : return false;
422 : :
423 : : /*
424 : : * Ignore attempts to create toast tables on catalog tables after initdb.
425 : : * Which catalogs get toast tables is explicitly chosen in catalog/pg_*.h.
426 : : * (We could get here via some ALTER TABLE command if the catalog doesn't
427 : : * have a toast table.)
428 : : */
429 [ + + + + ]: 33257 : if (IsCatalogRelation(rel) && !IsBootstrapProcessingMode())
430 : 2805 : return false;
431 : :
432 : : /* Otherwise, let the AM decide. */
2541 rhaas@postgresql.org 433 : 30452 : return table_relation_needs_toast_table(rel);
434 : : }
|