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