PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, 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/tuptoaster.h"
18 #include "access/xact.h"
19 #include "catalog/binary_upgrade.h"
20 #include "catalog/dependency.h"
21 #include "catalog/heap.h"
22 #include "catalog/index.h"
23 #include "catalog/namespace.h"
24 #include "catalog/pg_am.h"
25 #include "catalog/pg_namespace.h"
26 #include "catalog/pg_opclass.h"
27 #include "catalog/pg_type.h"
28 #include "catalog/toasting.h"
29 #include "miscadmin.h"
30 #include "nodes/makefuncs.h"
31 #include "storage/lock.h"
32 #include "utils/builtins.h"
33 #include "utils/rel.h"
34 #include "utils/syscache.h"
35 
36 /* Potentially set by pg_upgrade_support functions */
38 
39 static void CheckAndCreateToastTable(Oid relOid, Datum reloptions,
40  LOCKMODE lockmode, bool check);
41 static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
42  Datum reloptions, LOCKMODE lockmode, bool check);
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 AlterTableCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
60 {
61  CheckAndCreateToastTable(relOid, reloptions, lockmode, true);
62 }
63 
64 void
65 NewHeapCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
66 {
67  CheckAndCreateToastTable(relOid, reloptions, lockmode, false);
68 }
69 
70 void
72 {
73  CheckAndCreateToastTable(relOid, reloptions, AccessExclusiveLock, false);
74 }
75 
76 static void
77 CheckAndCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode, bool check)
78 {
79  Relation rel;
80 
81  rel = heap_open(relOid, lockmode);
82 
83  /* create_toast_table does all the work */
84  (void) create_toast_table(rel, InvalidOid, InvalidOid, reloptions, lockmode, check);
85 
86  heap_close(rel, NoLock);
87 }
88 
89 /*
90  * Create a toast table during bootstrap
91  *
92  * Here we need to prespecify the OIDs of the toast table and its index
93  */
94 void
95 BootstrapToastTable(char *relName, Oid toastOid, Oid toastIndexOid)
96 {
97  Relation rel;
98 
99  rel = heap_openrv(makeRangeVar(NULL, relName, -1), AccessExclusiveLock);
100 
101  if (rel->rd_rel->relkind != RELKIND_RELATION &&
102  rel->rd_rel->relkind != RELKIND_MATVIEW)
103  ereport(ERROR,
104  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
105  errmsg("\"%s\" is not a table or materialized view",
106  relName)));
107 
108  /* create_toast_table does all the work */
109  if (!create_toast_table(rel, toastOid, toastIndexOid, (Datum) 0,
110  AccessExclusiveLock, false))
111  elog(ERROR, "\"%s\" does not require a toast table",
112  relName);
113 
114  heap_close(rel, NoLock);
115 }
116 
117 
118 /*
119  * create_toast_table --- internal workhorse
120  *
121  * rel is already opened and locked
122  * toastOid and toastIndexOid are normally InvalidOid, but during
123  * bootstrap they can be nonzero to specify hand-assigned OIDs
124  */
125 static bool
126 create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
127  Datum reloptions, LOCKMODE lockmode, bool check)
128 {
129  Oid relOid = RelationGetRelid(rel);
130  HeapTuple reltup;
131  TupleDesc tupdesc;
132  bool shared_relation;
133  bool mapped_relation;
134  Relation toast_rel;
135  Relation class_rel;
136  Oid toast_relid;
137  Oid toast_typid = InvalidOid;
138  Oid namespaceid;
139  char toast_relname[NAMEDATALEN];
140  char toast_idxname[NAMEDATALEN];
141  IndexInfo *indexInfo;
142  Oid collationObjectId[2];
143  Oid classObjectId[2];
144  int16 coloptions[2];
145  ObjectAddress baseobject,
146  toastobject;
147 
148  /*
149  * Toast table is shared if and only if its parent is.
150  *
151  * We cannot allow toasting a shared relation after initdb (because
152  * there's no way to mark it toasted in other databases' pg_class).
153  */
154  shared_relation = rel->rd_rel->relisshared;
155  if (shared_relation && !IsBootstrapProcessingMode())
156  ereport(ERROR,
157  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
158  errmsg("shared tables cannot be toasted after initdb")));
159 
160  /* It's mapped if and only if its parent is, too */
161  mapped_relation = RelationIsMapped(rel);
162 
163  /*
164  * Is it already toasted?
165  */
166  if (rel->rd_rel->reltoastrelid != InvalidOid)
167  return false;
168 
169  /*
170  * Check to see whether the table actually needs a TOAST table.
171  */
172  if (!IsBinaryUpgrade)
173  {
174  /* Normal mode, normal check */
175  if (!needs_toast_table(rel))
176  return false;
177  }
178  else
179  {
180  /*
181  * In binary-upgrade mode, create a TOAST table if and only if
182  * pg_upgrade told us to (ie, a TOAST table OID has been provided).
183  *
184  * This indicates that the old cluster had a TOAST table for the
185  * current table. We must create a TOAST table to receive the old
186  * TOAST file, even if the table seems not to need one.
187  *
188  * Contrariwise, if the old cluster did not have a TOAST table, we
189  * should be able to get along without one even if the new version's
190  * needs_toast_table rules suggest we should have one. There is a lot
191  * of daylight between where we will create a TOAST table and where
192  * one is really necessary to avoid failures, so small cross-version
193  * differences in the when-to-create heuristic shouldn't be a problem.
194  * If we tried to create a TOAST table anyway, we would have the
195  * problem that it might take up an OID that will conflict with some
196  * old-cluster table we haven't seen yet.
197  */
200  return false;
201  }
202 
203  /*
204  * If requested check lockmode is sufficient. This is a cross check in
205  * case of errors or conflicting decisions in earlier code.
206  */
207  if (check && lockmode != AccessExclusiveLock)
208  elog(ERROR, "AccessExclusiveLock required to add toast table.");
209 
210  /*
211  * Create the toast table and its index
212  */
213  snprintf(toast_relname, sizeof(toast_relname),
214  "pg_toast_%u", relOid);
215  snprintf(toast_idxname, sizeof(toast_idxname),
216  "pg_toast_%u_index", relOid);
217 
218  /* this is pretty painful... need a tuple descriptor */
219  tupdesc = CreateTemplateTupleDesc(3, false);
220  TupleDescInitEntry(tupdesc, (AttrNumber) 1,
221  "chunk_id",
222  OIDOID,
223  -1, 0);
224  TupleDescInitEntry(tupdesc, (AttrNumber) 2,
225  "chunk_seq",
226  INT4OID,
227  -1, 0);
228  TupleDescInitEntry(tupdesc, (AttrNumber) 3,
229  "chunk_data",
230  BYTEAOID,
231  -1, 0);
232 
233  /*
234  * Ensure that the toast table doesn't itself get toasted, or we'll be
235  * toast :-(. This is essential for chunk_data because type bytea is
236  * toastable; hit the other two just to be sure.
237  */
238  tupdesc->attrs[0]->attstorage = 'p';
239  tupdesc->attrs[1]->attstorage = 'p';
240  tupdesc->attrs[2]->attstorage = 'p';
241 
242  /*
243  * Toast tables for regular relations go in pg_toast; those for temp
244  * relations go into the per-backend temp-toast-table namespace.
245  */
246  if (isTempOrTempToastNamespace(rel->rd_rel->relnamespace))
247  namespaceid = GetTempToastNamespace();
248  else
249  namespaceid = PG_TOAST_NAMESPACE;
250 
251  /*
252  * Use binary-upgrade override for pg_type.oid, if supplied. We might be
253  * in the post-schema-restore phase where we are doing ALTER TABLE to
254  * create TOAST tables that didn't exist in the old cluster.
255  */
257  {
260  }
261 
262  toast_relid = heap_create_with_catalog(toast_relname,
263  namespaceid,
264  rel->rd_rel->reltablespace,
265  toastOid,
266  toast_typid,
267  InvalidOid,
268  rel->rd_rel->relowner,
269  tupdesc,
270  NIL,
272  rel->rd_rel->relpersistence,
273  shared_relation,
274  mapped_relation,
275  true,
276  0,
278  reloptions,
279  false,
280  true,
281  true,
282  NULL);
283  Assert(toast_relid != InvalidOid);
284 
285  /* make the toast relation visible, else heap_open will fail */
287 
288  /* ShareLock is not really needed here, but take it anyway */
289  toast_rel = heap_open(toast_relid, ShareLock);
290 
291  /*
292  * Create unique index on chunk_id, chunk_seq.
293  *
294  * NOTE: the normal TOAST access routines could actually function with a
295  * single-column index on chunk_id only. However, the slice access
296  * routines use both columns for faster access to an individual chunk. In
297  * addition, we want it to be unique as a check against the possibility of
298  * duplicate TOAST chunk OIDs. The index might also be a little more
299  * efficient this way, since btree isn't all that happy with large numbers
300  * of equal keys.
301  */
302 
303  indexInfo = makeNode(IndexInfo);
304  indexInfo->ii_NumIndexAttrs = 2;
305  indexInfo->ii_KeyAttrNumbers[0] = 1;
306  indexInfo->ii_KeyAttrNumbers[1] = 2;
307  indexInfo->ii_Expressions = NIL;
308  indexInfo->ii_ExpressionsState = NIL;
309  indexInfo->ii_Predicate = NIL;
310  indexInfo->ii_PredicateState = NIL;
311  indexInfo->ii_ExclusionOps = NULL;
312  indexInfo->ii_ExclusionProcs = NULL;
313  indexInfo->ii_ExclusionStrats = NULL;
314  indexInfo->ii_Unique = true;
315  indexInfo->ii_ReadyForInserts = true;
316  indexInfo->ii_Concurrent = false;
317  indexInfo->ii_BrokenHotChain = false;
318  indexInfo->ii_AmCache = NULL;
319  indexInfo->ii_Context = CurrentMemoryContext;
320 
321  collationObjectId[0] = InvalidOid;
322  collationObjectId[1] = InvalidOid;
323 
324  classObjectId[0] = OID_BTREE_OPS_OID;
325  classObjectId[1] = INT4_BTREE_OPS_OID;
326 
327  coloptions[0] = 0;
328  coloptions[1] = 0;
329 
330  index_create(toast_rel, toast_idxname, toastIndexOid, InvalidOid,
331  indexInfo,
332  list_make2("chunk_id", "chunk_seq"),
333  BTREE_AM_OID,
334  rel->rd_rel->reltablespace,
335  collationObjectId, classObjectId, coloptions, (Datum) 0,
336  true, false, false, false,
337  true, false, false, true, false);
338 
339  heap_close(toast_rel, NoLock);
340 
341  /*
342  * Store the toast table's OID in the parent relation's pg_class row
343  */
345 
346  reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relOid));
347  if (!HeapTupleIsValid(reltup))
348  elog(ERROR, "cache lookup failed for relation %u", relOid);
349 
350  ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;
351 
353  {
354  /* normal case, use a transactional update */
355  CatalogTupleUpdate(class_rel, &reltup->t_self, reltup);
356  }
357  else
358  {
359  /* While bootstrapping, we cannot UPDATE, so overwrite in-place */
360  heap_inplace_update(class_rel, reltup);
361  }
362 
363  heap_freetuple(reltup);
364 
365  heap_close(class_rel, RowExclusiveLock);
366 
367  /*
368  * Register dependency from the toast table to the master, so that the
369  * toast table will be deleted if the master is. Skip this in bootstrap
370  * mode.
371  */
373  {
374  baseobject.classId = RelationRelationId;
375  baseobject.objectId = relOid;
376  baseobject.objectSubId = 0;
377  toastobject.classId = RelationRelationId;
378  toastobject.objectId = toast_relid;
379  toastobject.objectSubId = 0;
380 
381  recordDependencyOn(&toastobject, &baseobject, DEPENDENCY_INTERNAL);
382  }
383 
384  /*
385  * Make changes visible
386  */
388 
389  return true;
390 }
391 
392 /*
393  * Check to see whether the table needs a TOAST table. It does only if
394  * (1) there are any toastable attributes, and (2) the maximum length
395  * of a tuple could exceed TOAST_TUPLE_THRESHOLD. (We don't want to
396  * create a toast table for something like "f1 varchar(20)".)
397  */
398 static bool
400 {
401  int32 data_length = 0;
402  bool maxlength_unknown = false;
403  bool has_toastable_attrs = false;
404  TupleDesc tupdesc;
405  Form_pg_attribute *att;
406  int32 tuple_length;
407  int i;
408 
409  tupdesc = rel->rd_att;
410  att = tupdesc->attrs;
411 
412  for (i = 0; i < tupdesc->natts; i++)
413  {
414  if (att[i]->attisdropped)
415  continue;
416  data_length = att_align_nominal(data_length, att[i]->attalign);
417  if (att[i]->attlen > 0)
418  {
419  /* Fixed-length types are never toastable */
420  data_length += att[i]->attlen;
421  }
422  else
423  {
424  int32 maxlen = type_maximum_size(att[i]->atttypid,
425  att[i]->atttypmod);
426 
427  if (maxlen < 0)
428  maxlength_unknown = true;
429  else
430  data_length += maxlen;
431  if (att[i]->attstorage != 'p')
432  has_toastable_attrs = true;
433  }
434  }
435  if (!has_toastable_attrs)
436  return false; /* nothing to toast? */
437  if (maxlength_unknown)
438  return true; /* any unlimited-length attrs? */
439  tuple_length = MAXALIGN(SizeofHeapTupleHeader +
440  BITMAPLEN(tupdesc->natts)) +
441  MAXALIGN(data_length);
442  return (tuple_length > TOAST_TUPLE_THRESHOLD);
443 }
#define list_make2(x1, x2)
Definition: pg_list.h:134
signed short int16
Definition: c.h:255
#define TOAST_TUPLE_THRESHOLD
Definition: tuptoaster.h:55
#define NIL
Definition: pg_list.h:69
MemoryContext ii_Context
Definition: execnodes.h:84
#define SizeofHeapTupleHeader
Definition: htup_details.h:170
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
List * ii_Predicate
Definition: execnodes.h:71
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:144
void AlterTableCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
Definition: toasting.c:59
int LOCKMODE
Definition: lockdefs.h:26
Oid binary_upgrade_next_toast_pg_class_oid
Definition: heap.c:87
#define OIDOID
Definition: pg_type.h:328
bool isTempOrTempToastNamespace(Oid namespaceId)
Definition: namespace.c:3032
#define RelationRelationId
Definition: pg_class.h:29
#define BTREE_AM_OID
Definition: pg_am.h:70
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define RELKIND_MATVIEW
Definition: pg_class.h:165
#define INT4OID
Definition: pg_type.h:316
Oid * ii_ExclusionProcs
Definition: execnodes.h:74
Oid index_create(Relation heapRelation, const char *indexRelationName, Oid indexRelationId, Oid relFileNode, IndexInfo *indexInfo, List *indexColNames, Oid accessMethodObjectId, Oid tableSpaceId, Oid *collationObjectId, Oid *classObjectId, int16 *coloptions, Datum reloptions, bool isprimary, bool isconstraint, bool deferrable, bool initdeferred, bool allow_system_table_mods, bool skip_build, bool concurrent, bool is_internal, bool if_not_exists)
Definition: index.c:697
int errcode(int sqlerrcode)
Definition: elog.c:575
#define OID_BTREE_OPS_OID
Definition: pg_opclass.h:145
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define BITMAPLEN(NATTS)
Definition: htup_details.h:548
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
#define heap_close(r, l)
Definition: heapam.h:97
Form_pg_class rd_rel
Definition: rel.h:114
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_TOAST_NAMESPACE
Definition: pg_namespace.h:74
void NewRelationCreateToastTable(Oid relOid, Datum reloptions)
Definition: toasting.c:71
#define OidIsValid(objectId)
Definition: c.h:538
int natts
Definition: tupdesc.h:73
bool IsBinaryUpgrade
Definition: globals.c:101
static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, Datum reloptions, LOCKMODE lockmode, bool check)
Definition: toasting.c:126
void BootstrapToastTable(char *relName, Oid toastOid, Oid toastIndexOid)
Definition: toasting.c:95
signed int int32
Definition: c.h:256
#define NAMEDATALEN
List * ii_ExpressionsState
Definition: execnodes.h:70
static void CheckAndCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode, bool check)
Definition: toasting.c:77
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
int32 type_maximum_size(Oid type_oid, int32 typemod)
Definition: format_type.c:389
ItemPointerData t_self
Definition: htup.h:65
List * ii_PredicateState
Definition: execnodes.h:72
Oid GetTempToastNamespace(void)
Definition: namespace.c:3110
#define NoLock
Definition: lockdefs.h:34
static bool needs_toast_table(Relation rel)
Definition: toasting.c:399
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
bool ii_BrokenHotChain
Definition: execnodes.h:82
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:493
bool ii_ReadyForInserts
Definition: execnodes.h:80
#define ereport(elevel, rest)
Definition: elog.h:122
void * ii_AmCache
Definition: execnodes.h:83
#define RelationIsMapped(relation)
Definition: rel.h:454
#define RELKIND_TOASTVALUE
Definition: pg_class.h:163
uintptr_t Datum
Definition: postgres.h:372
void CommandCounterIncrement(void)
Definition: xact.c:922
int ii_NumIndexAttrs
Definition: execnodes.h:67
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
TupleDesc rd_att
Definition: rel.h:115
Relation heap_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1315
#define InvalidOid
Definition: postgres_ext.h:36
bool ii_Unique
Definition: execnodes.h:79
#define makeNode(_type_)
Definition: nodes.h:570
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
List * ii_Expressions
Definition: execnodes.h:69
#define Assert(condition)
Definition: c.h:675
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:210
#define MAXALIGN(LEN)
Definition: c.h:588
AttrNumber ii_KeyAttrNumbers[INDEX_MAX_KEYS]
Definition: execnodes.h:68
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
#define INT4_BTREE_OPS_OID
Definition: pg_opclass.h:121
bool ii_Concurrent
Definition: execnodes.h:81
#define BYTEAOID
Definition: pg_type.h:292
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:365
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:161
#define AccessExclusiveLock
Definition: lockdefs.h:46
Oid * ii_ExclusionOps
Definition: execnodes.h:73
int errmsg(const char *fmt,...)
Definition: elog.c:797
void heap_inplace_update(Relation relation, HeapTuple tuple)
Definition: heapam.c:6249
#define ShareLock
Definition: lockdefs.h:41
int i
Oid heap_create_with_catalog(const char *relname, Oid relnamespace, Oid reltablespace, Oid relid, Oid reltypeid, Oid reloftypeid, Oid ownerid, TupleDesc tupdesc, List *cooked_constraints, char relkind, char relpersistence, bool shared_relation, bool mapped_relation, bool oidislocal, int oidinhcount, OnCommitAction oncommit, Datum reloptions, bool use_user_acl, bool allow_system_table_mods, bool is_internal, ObjectAddress *typaddress)
Definition: heap.c:1017
Oid binary_upgrade_next_toast_pg_type_oid
Definition: toasting.c:37
#define elog
Definition: elog.h:219
uint16 * ii_ExclusionStrats
Definition: execnodes.h:75
#define RELKIND_RELATION
Definition: pg_class.h:160
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:417
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:419
void NewHeapCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
Definition: toasting.c:65