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
59 19599 : AlterTableCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
60 : {
61 19599 : CheckAndCreateToastTable(relOid, reloptions, lockmode, true, InvalidOid);
62 19599 : }
63 :
64 : void
65 553 : NewHeapCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode,
66 : Oid OIDOldToast)
67 : {
68 553 : CheckAndCreateToastTable(relOid, reloptions, lockmode, false, OIDOldToast);
69 553 : }
70 :
71 : void
72 25982 : NewRelationCreateToastTable(Oid relOid, Datum reloptions)
73 : {
74 25982 : CheckAndCreateToastTable(relOid, reloptions, AccessExclusiveLock, false,
75 : InvalidOid);
76 25982 : }
77 :
78 : static void
79 46134 : CheckAndCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode,
80 : bool check, Oid OIDOldToast)
81 : {
82 : Relation rel;
83 :
84 46134 : rel = table_open(relOid, lockmode);
85 :
86 : /* create_toast_table does all the work */
87 46134 : (void) create_toast_table(rel, InvalidOid, InvalidOid, reloptions, lockmode,
88 : check, OIDOldToast);
89 :
90 46134 : table_close(rel, NoLock);
91 46134 : }
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 :
103 2109 : rel = table_openrv(makeRangeVar(NULL, relName, -1), AccessExclusiveLock);
104 :
105 2109 : if (rel->rd_rel->relkind != RELKIND_RELATION &&
106 0 : rel->rd_rel->relkind != RELKIND_MATVIEW)
107 0 : elog(ERROR, "\"%s\" is not a table or materialized view",
108 : relName);
109 :
110 : /* create_toast_table does all the work */
111 2109 : if (!create_toast_table(rel, toastOid, toastIndexOid, (Datum) 0,
112 : AccessExclusiveLock, false, InvalidOid))
113 0 : elog(ERROR, "\"%s\" does not require a toast table",
114 : relName);
115 :
116 2109 : table_close(rel, NoLock);
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
128 48243 : create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
129 : Datum reloptions, LOCKMODE lockmode, bool check,
130 : Oid OIDOldToast)
131 : {
132 48243 : 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 48243 : if (rel->rd_rel->reltoastrelid != InvalidOid)
154 6776 : return false;
155 :
156 : /*
157 : * Check to see whether the table actually needs a TOAST table.
158 : */
159 41467 : if (!IsBinaryUpgrade)
160 : {
161 : /* Normal mode, normal check */
162 39581 : if (!needs_toast_table(rel))
163 28638 : 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 : */
185 1886 : if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid))
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 : */
193 11232 : if (check && lockmode != AccessExclusiveLock)
194 0 : elog(ERROR, "AccessExclusiveLock required to add toast table.");
195 :
196 : /*
197 : * Create the toast table and its index
198 : */
199 11232 : snprintf(toast_relname, sizeof(toast_relname),
200 : "pg_toast_%u", relOid);
201 11232 : snprintf(toast_idxname, sizeof(toast_idxname),
202 : "pg_toast_%u_index", relOid);
203 :
204 : /* this is pretty painful... need a tuple descriptor */
205 11232 : tupdesc = CreateTemplateTupleDesc(3);
206 11232 : TupleDescInitEntry(tupdesc, (AttrNumber) 1,
207 : "chunk_id",
208 : OIDOID,
209 : -1, 0);
210 11232 : TupleDescInitEntry(tupdesc, (AttrNumber) 2,
211 : "chunk_seq",
212 : INT4OID,
213 : -1, 0);
214 11232 : 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 : */
224 11232 : TupleDescAttr(tupdesc, 0)->attstorage = TYPSTORAGE_PLAIN;
225 11232 : TupleDescAttr(tupdesc, 1)->attstorage = TYPSTORAGE_PLAIN;
226 11232 : TupleDescAttr(tupdesc, 2)->attstorage = TYPSTORAGE_PLAIN;
227 :
228 : /* Toast field should not be compressed */
229 11232 : TupleDescAttr(tupdesc, 0)->attcompression = InvalidCompressionMethod;
230 11232 : TupleDescAttr(tupdesc, 1)->attcompression = InvalidCompressionMethod;
231 11232 : TupleDescAttr(tupdesc, 2)->attcompression = InvalidCompressionMethod;
232 :
233 11232 : populate_compact_attribute(tupdesc, 0);
234 11232 : populate_compact_attribute(tupdesc, 1);
235 11232 : populate_compact_attribute(tupdesc, 2);
236 :
237 11232 : 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 : */
243 11232 : if (isTempOrTempToastNamespace(rel->rd_rel->relnamespace))
244 636 : namespaceid = GetTempToastNamespace();
245 : else
246 10596 : namespaceid = PG_TOAST_NAMESPACE;
247 :
248 : /* Toast table is shared if and only if its parent is. */
249 11232 : shared_relation = rel->rd_rel->relisshared;
250 :
251 : /* It's mapped if and only if its parent is, too */
252 11232 : mapped_relation = RelationIsMapped(rel);
253 :
254 22464 : toast_relid = heap_create_with_catalog(toast_relname,
255 : namespaceid,
256 11232 : rel->rd_rel->reltablespace,
257 : toastOid,
258 : InvalidOid,
259 : InvalidOid,
260 11232 : rel->rd_rel->relowner,
261 : table_relation_toast_am(rel),
262 : tupdesc,
263 : NIL,
264 : RELKIND_TOASTVALUE,
265 11232 : 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);
275 : Assert(toast_relid != InvalidOid);
276 :
277 : /* make the toast relation visible, else table_open will fail */
278 11232 : CommandCounterIncrement();
279 :
280 : /* ShareLock is not really needed here, but take it anyway */
281 11232 : 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 :
295 11232 : indexInfo = makeNode(IndexInfo);
296 11232 : indexInfo->ii_NumIndexAttrs = 2;
297 11232 : indexInfo->ii_NumIndexKeyAttrs = 2;
298 11232 : indexInfo->ii_IndexAttrNumbers[0] = 1;
299 11232 : indexInfo->ii_IndexAttrNumbers[1] = 2;
300 11232 : indexInfo->ii_Expressions = NIL;
301 11232 : indexInfo->ii_ExpressionsState = NIL;
302 11232 : indexInfo->ii_Predicate = NIL;
303 11232 : indexInfo->ii_PredicateState = NULL;
304 11232 : indexInfo->ii_ExclusionOps = NULL;
305 11232 : indexInfo->ii_ExclusionProcs = NULL;
306 11232 : indexInfo->ii_ExclusionStrats = NULL;
307 11232 : indexInfo->ii_Unique = true;
308 11232 : indexInfo->ii_NullsNotDistinct = false;
309 11232 : indexInfo->ii_ReadyForInserts = true;
310 11232 : indexInfo->ii_CheckedUnchanged = false;
311 11232 : indexInfo->ii_IndexUnchanged = false;
312 11232 : indexInfo->ii_Concurrent = false;
313 11232 : indexInfo->ii_BrokenHotChain = false;
314 11232 : indexInfo->ii_ParallelWorkers = 0;
315 11232 : indexInfo->ii_Am = BTREE_AM_OID;
316 11232 : indexInfo->ii_AmCache = NULL;
317 11232 : indexInfo->ii_Context = CurrentMemoryContext;
318 :
319 11232 : collationIds[0] = InvalidOid;
320 11232 : collationIds[1] = InvalidOid;
321 :
322 11232 : opclassIds[0] = OID_BTREE_OPS_OID;
323 11232 : opclassIds[1] = INT4_BTREE_OPS_OID;
324 :
325 11232 : coloptions[0] = 0;
326 11232 : coloptions[1] = 0;
327 :
328 11232 : index_create(toast_rel, toast_idxname, toastIndexOid, InvalidOid,
329 : InvalidOid, InvalidOid,
330 : indexInfo,
331 11232 : list_make2("chunk_id", "chunk_seq"),
332 : BTREE_AM_OID,
333 11232 : rel->rd_rel->reltablespace,
334 : collationIds, opclassIds, NULL, coloptions, NULL, (Datum) 0,
335 : INDEX_CREATE_IS_PRIMARY, 0, true, true, NULL);
336 :
337 11232 : table_close(toast_rel, NoLock);
338 :
339 : /*
340 : * Store the toast table's OID in the parent relation's pg_class row
341 : */
342 11232 : class_rel = table_open(RelationRelationId, RowExclusiveLock);
343 :
344 11232 : if (!IsBootstrapProcessingMode())
345 : {
346 : /* normal case, use a transactional update */
347 9123 : reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relOid));
348 9123 : if (!HeapTupleIsValid(reltup))
349 0 : elog(ERROR, "cache lookup failed for relation %u", relOid);
350 :
351 9123 : ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;
352 :
353 9123 : 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 :
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))
369 0 : elog(ERROR, "cache lookup failed for relation %u", relOid);
370 :
371 2109 : ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;
372 :
373 2109 : systable_inplace_update_finish(state, reltup);
374 : }
375 :
376 11232 : heap_freetuple(reltup);
377 :
378 11232 : 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 : */
384 11232 : if (!IsBootstrapProcessingMode())
385 : {
386 9123 : baseobject.classId = RelationRelationId;
387 9123 : baseobject.objectId = relOid;
388 9123 : baseobject.objectSubId = 0;
389 9123 : toastobject.classId = RelationRelationId;
390 9123 : toastobject.objectId = toast_relid;
391 9123 : toastobject.objectSubId = 0;
392 :
393 9123 : recordDependencyOn(&toastobject, &baseobject, DEPENDENCY_INTERNAL);
394 : }
395 :
396 : /*
397 : * Make changes visible
398 : */
399 11232 : CommandCounterIncrement();
400 :
401 11232 : return true;
402 : }
403 :
404 : /*
405 : * Check to see whether the table needs a TOAST table.
406 : */
407 : static bool
408 39581 : needs_toast_table(Relation rel)
409 : {
410 : /*
411 : * No need to create a TOAST table for partitioned tables.
412 : */
413 39581 : if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
414 6077 : 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 : */
420 33504 : 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 33119 : if (IsCatalogRelation(rel) && !IsBootstrapProcessingMode())
430 2805 : return false;
431 :
432 : /* Otherwise, let the AM decide. */
433 30314 : return table_relation_needs_toast_table(rel);
434 : }
|