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