PostgreSQL Source Code  git master
toasting.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "access/toast_compression.h"
#include "access/xact.h"
#include "catalog/binary_upgrade.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/heap.h"
#include "catalog/index.h"
#include "catalog/namespace.h"
#include "catalog/pg_am.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_type.h"
#include "catalog/toasting.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "storage/lock.h"
#include "utils/builtins.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for toasting.c:

Go to the source code of this file.

Functions

static void CheckAndCreateToastTable (Oid relOid, Datum reloptions, LOCKMODE lockmode, bool check)
 
static bool create_toast_table (Relation rel, Oid toastOid, Oid toastIndexOid, Datum reloptions, LOCKMODE lockmode, bool check)
 
static bool needs_toast_table (Relation rel)
 
void AlterTableCreateToastTable (Oid relOid, Datum reloptions, LOCKMODE lockmode)
 
void NewHeapCreateToastTable (Oid relOid, Datum reloptions, LOCKMODE lockmode)
 
void NewRelationCreateToastTable (Oid relOid, Datum reloptions)
 
void BootstrapToastTable (char *relName, Oid toastOid, Oid toastIndexOid)
 

Function Documentation

◆ AlterTableCreateToastTable()

void AlterTableCreateToastTable ( Oid  relOid,
Datum  reloptions,
LOCKMODE  lockmode 
)

Definition at line 58 of file toasting.c.

References CheckAndCreateToastTable().

Referenced by ATRewriteCatalogs().

59 {
60  CheckAndCreateToastTable(relOid, reloptions, lockmode, true);
61 }
static void CheckAndCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode, bool check)
Definition: toasting.c:76

◆ BootstrapToastTable()

void BootstrapToastTable ( char *  relName,
Oid  toastOid,
Oid  toastIndexOid 
)

Definition at line 94 of file toasting.c.

References AccessExclusiveLock, create_toast_table(), elog, ERROR, makeRangeVar(), NoLock, RelationData::rd_rel, table_close(), and table_openrv().

95 {
96  Relation rel;
97 
98  rel = table_openrv(makeRangeVar(NULL, relName, -1), AccessExclusiveLock);
99 
100  if (rel->rd_rel->relkind != RELKIND_RELATION &&
101  rel->rd_rel->relkind != RELKIND_MATVIEW)
102  elog(ERROR, "\"%s\" is not a table or materialized view",
103  relName);
104 
105  /* create_toast_table does all the work */
106  if (!create_toast_table(rel, toastOid, toastIndexOid, (Datum) 0,
107  AccessExclusiveLock, false))
108  elog(ERROR, "\"%s\" does not require a toast table",
109  relName);
110 
111  table_close(rel, NoLock);
112 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Form_pg_class rd_rel
Definition: rel.h:109
static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, Datum reloptions, LOCKMODE lockmode, bool check)
Definition: toasting.c:123
#define ERROR
Definition: elog.h:46
#define NoLock
Definition: lockdefs.h:34
uintptr_t Datum
Definition: postgres.h:411
Relation table_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: table.c:102
#define AccessExclusiveLock
Definition: lockdefs.h:45
#define elog(elevel,...)
Definition: elog.h:232
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:422

◆ CheckAndCreateToastTable()

static void CheckAndCreateToastTable ( Oid  relOid,
Datum  reloptions,
LOCKMODE  lockmode,
bool  check 
)
static

Definition at line 76 of file toasting.c.

References create_toast_table(), InvalidOid, NoLock, table_close(), and table_open().

Referenced by AlterTableCreateToastTable(), NewHeapCreateToastTable(), and NewRelationCreateToastTable().

77 {
78  Relation rel;
79 
80  rel = table_open(relOid, lockmode);
81 
82  /* create_toast_table does all the work */
83  (void) create_toast_table(rel, InvalidOid, InvalidOid, reloptions, lockmode, check);
84 
85  table_close(rel, NoLock);
86 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, Datum reloptions, LOCKMODE lockmode, bool check)
Definition: toasting.c:123
#define NoLock
Definition: lockdefs.h:34
#define InvalidOid
Definition: postgres_ext.h:36
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ create_toast_table()

static bool create_toast_table ( Relation  rel,
Oid  toastOid,
Oid  toastIndexOid,
Datum  reloptions,
LOCKMODE  lockmode,
bool  check 
)
static

Definition at line 123 of file toasting.c.

References AccessExclusiveLock, Assert, binary_upgrade_next_toast_pg_class_oid, CatalogTupleUpdate(), ObjectAddress::classId, CommandCounterIncrement(), CreateTemplateTupleDesc(), CurrentMemoryContext, DEPENDENCY_INTERNAL, elog, ERROR, GETSTRUCT, GetTempToastNamespace(), heap_create_with_catalog(), heap_freetuple(), heap_inplace_update(), HeapTupleIsValid, IndexInfo::ii_Am, IndexInfo::ii_AmCache, IndexInfo::ii_BrokenHotChain, IndexInfo::ii_Concurrent, IndexInfo::ii_Context, IndexInfo::ii_ExclusionOps, IndexInfo::ii_ExclusionProcs, IndexInfo::ii_ExclusionStrats, IndexInfo::ii_Expressions, IndexInfo::ii_ExpressionsState, IndexInfo::ii_IndexAttrNumbers, IndexInfo::ii_NumIndexAttrs, IndexInfo::ii_NumIndexKeyAttrs, IndexInfo::ii_OpclassOptions, IndexInfo::ii_ParallelWorkers, IndexInfo::ii_Predicate, IndexInfo::ii_PredicateState, IndexInfo::ii_ReadyForInserts, IndexInfo::ii_Unique, index_create(), INDEX_CREATE_IS_PRIMARY, InvalidCompressionMethod, InvalidOid, IsBinaryUpgrade, IsBootstrapProcessingMode, isTempOrTempToastNamespace(), list_make2, makeNode, NAMEDATALEN, needs_toast_table(), NIL, NoLock, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidIsValid, ONCOMMIT_NOOP, RelationData::rd_rel, recordDependencyOn(), RelationGetRelid, RelationIsMapped, RELOID, RowExclusiveLock, SearchSysCacheCopy1, ShareLock, snprintf, HeapTupleData::t_self, table_close(), table_open(), table_relation_toast_am(), TupleDescAttr, and TupleDescInitEntry().

Referenced by BootstrapToastTable(), and CheckAndCreateToastTable().

125 {
126  Oid relOid = RelationGetRelid(rel);
127  HeapTuple reltup;
128  TupleDesc tupdesc;
129  bool shared_relation;
130  bool mapped_relation;
131  Relation toast_rel;
132  Relation class_rel;
133  Oid toast_relid;
134  Oid namespaceid;
135  char toast_relname[NAMEDATALEN];
136  char toast_idxname[NAMEDATALEN];
137  IndexInfo *indexInfo;
138  Oid collationObjectId[2];
139  Oid classObjectId[2];
140  int16 coloptions[2];
141  ObjectAddress baseobject,
142  toastobject;
143 
144  /*
145  * Is it already toasted?
146  */
147  if (rel->rd_rel->reltoastrelid != InvalidOid)
148  return false;
149 
150  /*
151  * Check to see whether the table actually needs a TOAST table.
152  */
153  if (!IsBinaryUpgrade)
154  {
155  /* Normal mode, normal check */
156  if (!needs_toast_table(rel))
157  return false;
158  }
159  else
160  {
161  /*
162  * In binary-upgrade mode, create a TOAST table if and only if
163  * pg_upgrade told us to (ie, a TOAST table OID has been provided).
164  *
165  * This indicates that the old cluster had a TOAST table for the
166  * current table. We must create a TOAST table to receive the old
167  * TOAST file, even if the table seems not to need one.
168  *
169  * Contrariwise, if the old cluster did not have a TOAST table, we
170  * should be able to get along without one even if the new version's
171  * needs_toast_table rules suggest we should have one. There is a lot
172  * of daylight between where we will create a TOAST table and where
173  * one is really necessary to avoid failures, so small cross-version
174  * differences in the when-to-create heuristic shouldn't be a problem.
175  * If we tried to create a TOAST table anyway, we would have the
176  * problem that it might take up an OID that will conflict with some
177  * old-cluster table we haven't seen yet.
178  */
180  return false;
181  }
182 
183  /*
184  * If requested check lockmode is sufficient. This is a cross check in
185  * case of errors or conflicting decisions in earlier code.
186  */
187  if (check && lockmode != AccessExclusiveLock)
188  elog(ERROR, "AccessExclusiveLock required to add toast table.");
189 
190  /*
191  * Create the toast table and its index
192  */
193  snprintf(toast_relname, sizeof(toast_relname),
194  "pg_toast_%u", relOid);
195  snprintf(toast_idxname, sizeof(toast_idxname),
196  "pg_toast_%u_index", relOid);
197 
198  /* this is pretty painful... need a tuple descriptor */
199  tupdesc = CreateTemplateTupleDesc(3);
200  TupleDescInitEntry(tupdesc, (AttrNumber) 1,
201  "chunk_id",
202  OIDOID,
203  -1, 0);
204  TupleDescInitEntry(tupdesc, (AttrNumber) 2,
205  "chunk_seq",
206  INT4OID,
207  -1, 0);
208  TupleDescInitEntry(tupdesc, (AttrNumber) 3,
209  "chunk_data",
210  BYTEAOID,
211  -1, 0);
212 
213  /*
214  * Ensure that the toast table doesn't itself get toasted, or we'll be
215  * toast :-(. This is essential for chunk_data because type bytea is
216  * toastable; hit the other two just to be sure.
217  */
218  TupleDescAttr(tupdesc, 0)->attstorage = TYPSTORAGE_PLAIN;
219  TupleDescAttr(tupdesc, 1)->attstorage = TYPSTORAGE_PLAIN;
220  TupleDescAttr(tupdesc, 2)->attstorage = TYPSTORAGE_PLAIN;
221 
222  /* Toast field should not be compressed */
223  TupleDescAttr(tupdesc, 0)->attcompression = InvalidCompressionMethod;
224  TupleDescAttr(tupdesc, 1)->attcompression = InvalidCompressionMethod;
225  TupleDescAttr(tupdesc, 2)->attcompression = InvalidCompressionMethod;
226 
227  /*
228  * Toast tables for regular relations go in pg_toast; those for temp
229  * relations go into the per-backend temp-toast-table namespace.
230  */
231  if (isTempOrTempToastNamespace(rel->rd_rel->relnamespace))
232  namespaceid = GetTempToastNamespace();
233  else
234  namespaceid = PG_TOAST_NAMESPACE;
235 
236  /* Toast table is shared if and only if its parent is. */
237  shared_relation = rel->rd_rel->relisshared;
238 
239  /* It's mapped if and only if its parent is, too */
240  mapped_relation = RelationIsMapped(rel);
241 
242  toast_relid = heap_create_with_catalog(toast_relname,
243  namespaceid,
244  rel->rd_rel->reltablespace,
245  toastOid,
246  InvalidOid,
247  InvalidOid,
248  rel->rd_rel->relowner,
250  tupdesc,
251  NIL,
252  RELKIND_TOASTVALUE,
253  rel->rd_rel->relpersistence,
254  shared_relation,
255  mapped_relation,
257  reloptions,
258  false,
259  true,
260  true,
261  InvalidOid,
262  NULL);
263  Assert(toast_relid != InvalidOid);
264 
265  /* make the toast relation visible, else table_open will fail */
267 
268  /* ShareLock is not really needed here, but take it anyway */
269  toast_rel = table_open(toast_relid, ShareLock);
270 
271  /*
272  * Create unique index on chunk_id, chunk_seq.
273  *
274  * NOTE: the normal TOAST access routines could actually function with a
275  * single-column index on chunk_id only. However, the slice access
276  * routines use both columns for faster access to an individual chunk. In
277  * addition, we want it to be unique as a check against the possibility of
278  * duplicate TOAST chunk OIDs. The index might also be a little more
279  * efficient this way, since btree isn't all that happy with large numbers
280  * of equal keys.
281  */
282 
283  indexInfo = makeNode(IndexInfo);
284  indexInfo->ii_NumIndexAttrs = 2;
285  indexInfo->ii_NumIndexKeyAttrs = 2;
286  indexInfo->ii_IndexAttrNumbers[0] = 1;
287  indexInfo->ii_IndexAttrNumbers[1] = 2;
288  indexInfo->ii_Expressions = NIL;
289  indexInfo->ii_ExpressionsState = NIL;
290  indexInfo->ii_Predicate = NIL;
291  indexInfo->ii_PredicateState = NULL;
292  indexInfo->ii_ExclusionOps = NULL;
293  indexInfo->ii_ExclusionProcs = NULL;
294  indexInfo->ii_ExclusionStrats = NULL;
295  indexInfo->ii_OpclassOptions = NULL;
296  indexInfo->ii_Unique = true;
297  indexInfo->ii_ReadyForInserts = true;
298  indexInfo->ii_Concurrent = false;
299  indexInfo->ii_BrokenHotChain = false;
300  indexInfo->ii_ParallelWorkers = 0;
301  indexInfo->ii_Am = BTREE_AM_OID;
302  indexInfo->ii_AmCache = NULL;
303  indexInfo->ii_Context = CurrentMemoryContext;
304 
305  collationObjectId[0] = InvalidOid;
306  collationObjectId[1] = InvalidOid;
307 
308  classObjectId[0] = OID_BTREE_OPS_OID;
309  classObjectId[1] = INT4_BTREE_OPS_OID;
310 
311  coloptions[0] = 0;
312  coloptions[1] = 0;
313 
314  index_create(toast_rel, toast_idxname, toastIndexOid, InvalidOid,
316  indexInfo,
317  list_make2("chunk_id", "chunk_seq"),
318  BTREE_AM_OID,
319  rel->rd_rel->reltablespace,
320  collationObjectId, classObjectId, coloptions, (Datum) 0,
321  INDEX_CREATE_IS_PRIMARY, 0, true, true, NULL);
322 
323  table_close(toast_rel, NoLock);
324 
325  /*
326  * Store the toast table's OID in the parent relation's pg_class row
327  */
328  class_rel = table_open(RelationRelationId, RowExclusiveLock);
329 
330  reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relOid));
331  if (!HeapTupleIsValid(reltup))
332  elog(ERROR, "cache lookup failed for relation %u", relOid);
333 
334  ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;
335 
337  {
338  /* normal case, use a transactional update */
339  CatalogTupleUpdate(class_rel, &reltup->t_self, reltup);
340  }
341  else
342  {
343  /* While bootstrapping, we cannot UPDATE, so overwrite in-place */
344  heap_inplace_update(class_rel, reltup);
345  }
346 
347  heap_freetuple(reltup);
348 
349  table_close(class_rel, RowExclusiveLock);
350 
351  /*
352  * Register dependency from the toast table to the main, so that the toast
353  * table will be deleted if the main is. Skip this in bootstrap mode.
354  */
356  {
357  baseobject.classId = RelationRelationId;
358  baseobject.objectId = relOid;
359  baseobject.objectSubId = 0;
360  toastobject.classId = RelationRelationId;
361  toastobject.objectId = toast_relid;
362  toastobject.objectSubId = 0;
363 
364  recordDependencyOn(&toastobject, &baseobject, DEPENDENCY_INTERNAL);
365  }
366 
367  /*
368  * Make changes visible
369  */
371 
372  return true;
373 }
#define list_make2(x1, x2)
Definition: pg_list.h:208
signed short int16
Definition: c.h:428
#define NIL
Definition: pg_list.h:65
MemoryContext ii_Context
Definition: execnodes.h:179
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
List * ii_Predicate
Definition: execnodes.h:163
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
Oid binary_upgrade_next_toast_pg_class_oid
Definition: heap.c:94
bool isTempOrTempToastNamespace(Oid namespaceId)
Definition: namespace.c:3226
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
ExprState * ii_PredicateState
Definition: execnodes.h:164
Oid * ii_ExclusionProcs
Definition: execnodes.h:166
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
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:698
#define OidIsValid(objectId)
Definition: c.h:710
bool IsBinaryUpgrade
Definition: globals.c:113
#define NAMEDATALEN
List * ii_ExpressionsState
Definition: execnodes.h:162
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
int ii_NumIndexKeyAttrs
Definition: execnodes.h:159
ItemPointerData t_self
Definition: htup.h:65
Oid GetTempToastNamespace(void)
Definition: namespace.c:3344
#define NoLock
Definition: lockdefs.h:34
static bool needs_toast_table(Relation rel)
Definition: toasting.c:379
#define RowExclusiveLock
Definition: lockdefs.h:38
bool ii_BrokenHotChain
Definition: execnodes.h:175
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583
bool ii_ReadyForInserts
Definition: execnodes.h:173
void * ii_AmCache
Definition: execnodes.h:178
#define RelationIsMapped(relation)
Definition: rel.h:526
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
int ii_NumIndexAttrs
Definition: execnodes.h:158
#define INDEX_CREATE_IS_PRIMARY
Definition: index.h:61
#define InvalidOid
Definition: postgres_ext.h:36
bool ii_Unique
Definition: execnodes.h:172
#define makeNode(_type_)
Definition: nodes.h:587
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
List * ii_Expressions
Definition: execnodes.h:161
#define Assert(condition)
Definition: c.h:804
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:1143
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:406
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:175
#define AccessExclusiveLock
Definition: lockdefs.h:45
#define InvalidCompressionMethod
Oid * ii_ExclusionOps
Definition: execnodes.h:165
void heap_inplace_update(Relation relation, HeapTuple tuple)
Definition: heapam.c:5991
#define elog(elevel,...)
Definition: elog.h:232
#define ShareLock
Definition: lockdefs.h:41
static Oid table_relation_toast_am(Relation rel)
Definition: tableam.h:1857
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:216
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:477
Datum * ii_OpclassOptions
Definition: execnodes.h:171
Oid ii_Am
Definition: execnodes.h:177

◆ needs_toast_table()

static bool needs_toast_table ( Relation  rel)
static

Definition at line 379 of file toasting.c.

References IsBootstrapProcessingMode, IsCatalogRelation(), RelationData::rd_rel, and table_relation_needs_toast_table().

Referenced by create_toast_table().

380 {
381  /*
382  * No need to create a TOAST table for partitioned tables.
383  */
384  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
385  return false;
386 
387  /*
388  * We cannot allow toasting a shared relation after initdb (because
389  * there's no way to mark it toasted in other databases' pg_class).
390  */
391  if (rel->rd_rel->relisshared && !IsBootstrapProcessingMode())
392  return false;
393 
394  /*
395  * Ignore attempts to create toast tables on catalog tables after initdb.
396  * Which catalogs get toast tables is explicitly chosen in catalog/pg_*.h.
397  * (We could get here via some ALTER TABLE command if the catalog doesn't
398  * have a toast table.)
399  */
401  return false;
402 
403  /* Otherwise, let the AM decide. */
405 }
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:104
Form_pg_class rd_rel
Definition: rel.h:109
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:406
static bool table_relation_needs_toast_table(Relation rel)
Definition: tableam.h:1847

◆ NewHeapCreateToastTable()

void NewHeapCreateToastTable ( Oid  relOid,
Datum  reloptions,
LOCKMODE  lockmode 
)

Definition at line 64 of file toasting.c.

References CheckAndCreateToastTable().

Referenced by make_new_heap().

65 {
66  CheckAndCreateToastTable(relOid, reloptions, lockmode, false);
67 }
static void CheckAndCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode, bool check)
Definition: toasting.c:76

◆ NewRelationCreateToastTable()

void NewRelationCreateToastTable ( Oid  relOid,
Datum  reloptions 
)

Definition at line 70 of file toasting.c.

References AccessExclusiveLock, and CheckAndCreateToastTable().

Referenced by create_ctas_internal(), and ProcessUtilitySlow().

71 {
72  CheckAndCreateToastTable(relOid, reloptions, AccessExclusiveLock, false);
73 }
static void CheckAndCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode, bool check)
Definition: toasting.c:76
#define AccessExclusiveLock
Definition: lockdefs.h:45