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