PostgreSQL Source Code  git master
toast_internals.h File Reference
Include dependency graph for toast_internals.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  toast_compress_header
 

Macros

#define TOAST_COMPRESS_EXTSIZE(ptr)   (((toast_compress_header *) (ptr))->tcinfo & VARLENA_EXTSIZE_MASK)
 
#define TOAST_COMPRESS_METHOD(ptr)   (((toast_compress_header *) (ptr))->tcinfo >> VARLENA_EXTSIZE_BITS)
 
#define TOAST_COMPRESS_SET_SIZE_AND_COMPRESS_METHOD(ptr, len, cm_method)
 

Typedefs

typedef struct toast_compress_header toast_compress_header
 

Functions

Datum toast_compress_datum (Datum value, char cmethod)
 
Oid toast_get_valid_index (Oid toastoid, LOCKMODE lock)
 
void toast_delete_datum (Relation rel, Datum value, bool is_speculative)
 
Datum toast_save_datum (Relation rel, Datum value, struct varlena *oldexternal, int options)
 
int toast_open_indexes (Relation toastrel, LOCKMODE lock, Relation **toastidxs, int *num_indexes)
 
void toast_close_indexes (Relation *toastidxs, int num_indexes, LOCKMODE lock)
 
void init_toast_snapshot (Snapshot toast_snapshot)
 

Macro Definition Documentation

◆ TOAST_COMPRESS_EXTSIZE

#define TOAST_COMPRESS_EXTSIZE (   ptr)    (((toast_compress_header *) (ptr))->tcinfo & VARLENA_EXTSIZE_MASK)

Definition at line 34 of file toast_internals.h.

Referenced by toast_decompress_datum_slice().

◆ TOAST_COMPRESS_METHOD

#define TOAST_COMPRESS_METHOD (   ptr)    (((toast_compress_header *) (ptr))->tcinfo >> VARLENA_EXTSIZE_BITS)

Definition at line 36 of file toast_internals.h.

Referenced by toast_decompress_datum(), and toast_decompress_datum_slice().

◆ TOAST_COMPRESS_SET_SIZE_AND_COMPRESS_METHOD

#define TOAST_COMPRESS_SET_SIZE_AND_COMPRESS_METHOD (   ptr,
  len,
  cm_method 
)
Value:
do { \
Assert((len) > 0 && (len) <= VARLENA_EXTSIZE_MASK); \
Assert((cm_method) == TOAST_PGLZ_COMPRESSION_ID || \
(cm_method) == TOAST_LZ4_COMPRESSION_ID); \
((toast_compress_header *) (ptr))->tcinfo = \
(len) | ((uint32) (cm_method) << VARLENA_EXTSIZE_BITS); \
} while (0)
#define VARLENA_EXTSIZE_BITS
Definition: postgres.h:82
#define VARLENA_EXTSIZE_MASK
Definition: postgres.h:83
unsigned int uint32
Definition: c.h:441

Definition at line 39 of file toast_internals.h.

Referenced by toast_compress_datum().

Typedef Documentation

◆ toast_compress_header

Function Documentation

◆ init_toast_snapshot()

void init_toast_snapshot ( Snapshot  toast_snapshot)

Definition at line 637 of file toast_internals.c.

References elog, ERROR, GetOldestSnapshot(), InitToastSnapshot, SnapshotData::lsn, and SnapshotData::whenTaken.

Referenced by check_toasted_attribute(), heap_fetch_toast_slice(), and toast_delete_datum().

638 {
639  Snapshot snapshot = GetOldestSnapshot();
640 
641  if (snapshot == NULL)
642  elog(ERROR, "no known snapshots");
643 
644  InitToastSnapshot(*toast_snapshot, snapshot->lsn, snapshot->whenTaken);
645 }
XLogRecPtr lsn
Definition: snapshot.h:209
#define ERROR
Definition: elog.h:46
Snapshot GetOldestSnapshot(void)
Definition: snapmgr.c:357
#define elog(elevel,...)
Definition: elog.h:232
TimestampTz whenTaken
Definition: snapshot.h:208
#define InitToastSnapshot(snapshotdata, l, w)
Definition: snapmgr.h:90

◆ toast_close_indexes()

void toast_close_indexes ( Relation toastidxs,
int  num_indexes,
LOCKMODE  lock 
)

Definition at line 618 of file toast_internals.c.

References i, index_close(), and pfree().

Referenced by heap_fetch_toast_slice(), toast_delete_datum(), toast_get_valid_index(), toast_save_datum(), toastrel_valueid_exists(), and verify_heapam().

619 {
620  int i;
621 
622  /* Close relations and clean up things */
623  for (i = 0; i < num_indexes; i++)
624  index_close(toastidxs[i], lock);
625  pfree(toastidxs);
626 }
void pfree(void *pointer)
Definition: mcxt.c:1169
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
int i

◆ toast_compress_datum()

Datum toast_compress_datum ( Datum  value,
char  cmethod 
)

Definition at line 47 of file toast_internals.c.

References Assert, CompressionMethodIsValid, DatumGetPointer, elog, ERROR, lz4_compress_datum(), pfree(), pglz_compress_datum(), PointerGetDatum, TOAST_COMPRESS_SET_SIZE_AND_COMPRESS_METHOD, TOAST_INVALID_COMPRESSION_ID, TOAST_LZ4_COMPRESSION, TOAST_LZ4_COMPRESSION_ID, TOAST_PGLZ_COMPRESSION, TOAST_PGLZ_COMPRESSION_ID, VARATT_IS_COMPRESSED, VARATT_IS_EXTERNAL, VARSIZE, and VARSIZE_ANY_EXHDR.

Referenced by brin_form_tuple(), index_form_tuple(), and toast_tuple_try_compression().

48 {
49  struct varlena *tmp = NULL;
50  int32 valsize;
52 
55 
57 
59 
60  /*
61  * Call appropriate compression routine for the compression method.
62  */
63  switch (cmethod)
64  {
66  tmp = pglz_compress_datum((const struct varlena *) value);
68  break;
70  tmp = lz4_compress_datum((const struct varlena *) value);
72  break;
73  default:
74  elog(ERROR, "invalid compression method %c", cmethod);
75  }
76 
77  if (tmp == NULL)
78  return PointerGetDatum(NULL);
79 
80  /*
81  * We recheck the actual size even if compression reports success, because
82  * it might be satisfied with having saved as little as one byte in the
83  * compressed data --- which could turn into a net loss once you consider
84  * header and alignment padding. Worst case, the compressed format might
85  * require three padding bytes (plus header, which is included in
86  * VARSIZE(tmp)), whereas the uncompressed format would take only one
87  * header byte and no padding if the value is short enough. So we insist
88  * on a savings of more than 2 bytes to ensure we have a gain.
89  */
90  if (VARSIZE(tmp) < valsize - 2)
91  {
92  /* successful compression */
95  return PointerGetDatum(tmp);
96  }
97  else
98  {
99  /* incompressible data */
100  pfree(tmp);
101  return PointerGetDatum(NULL);
102  }
103 }
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:325
static struct @142 value
#define CompressionMethodIsValid(cm)
#define VARSIZE(PTR)
Definition: postgres.h:316
#define PointerGetDatum(X)
Definition: postgres.h:600
ToastCompressionId
signed int int32
Definition: c.h:429
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:326
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
#define TOAST_LZ4_COMPRESSION
struct varlena * pglz_compress_datum(const struct varlena *value)
struct varlena * lz4_compress_datum(const struct varlena *value)
#define TOAST_COMPRESS_SET_SIZE_AND_COMPRESS_METHOD(ptr, len, cm_method)
#define Assert(condition)
Definition: c.h:804
#define TOAST_PGLZ_COMPRESSION
#define DatumGetPointer(X)
Definition: postgres.h:593
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
#define elog(elevel,...)
Definition: elog.h:232
Definition: c.h:621

◆ toast_delete_datum()

void toast_delete_datum ( Relation  rel,
Datum  value,
bool  is_speculative 
)

Definition at line 382 of file toast_internals.c.

References BTEqualStrategyNumber, DatumGetPointer, ForwardScanDirection, heap_abort_speculative(), init_toast_snapshot(), ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), simple_heap_delete(), systable_beginscan_ordered(), systable_endscan_ordered(), systable_getnext_ordered(), HeapTupleData::t_self, table_close(), table_open(), toast_close_indexes(), toast_open_indexes(), varatt_external::va_toastrelid, varatt_external::va_valueid, VARATT_EXTERNAL_GET_POINTER, and VARATT_IS_EXTERNAL_ONDISK.

Referenced by toast_delete_external(), and toast_tuple_cleanup().

383 {
384  struct varlena *attr = (struct varlena *) DatumGetPointer(value);
385  struct varatt_external toast_pointer;
386  Relation toastrel;
387  Relation *toastidxs;
388  ScanKeyData toastkey;
389  SysScanDesc toastscan;
390  HeapTuple toasttup;
391  int num_indexes;
392  int validIndex;
393  SnapshotData SnapshotToast;
394 
395  if (!VARATT_IS_EXTERNAL_ONDISK(attr))
396  return;
397 
398  /* Must copy to access aligned fields */
399  VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
400 
401  /*
402  * Open the toast relation and its indexes
403  */
404  toastrel = table_open(toast_pointer.va_toastrelid, RowExclusiveLock);
405 
406  /* Fetch valid relation used for process */
407  validIndex = toast_open_indexes(toastrel,
409  &toastidxs,
410  &num_indexes);
411 
412  /*
413  * Setup a scan key to find chunks with matching va_valueid
414  */
415  ScanKeyInit(&toastkey,
416  (AttrNumber) 1,
417  BTEqualStrategyNumber, F_OIDEQ,
418  ObjectIdGetDatum(toast_pointer.va_valueid));
419 
420  /*
421  * Find all the chunks. (We don't actually care whether we see them in
422  * sequence or not, but since we've already locked the index we might as
423  * well use systable_beginscan_ordered.)
424  */
425  init_toast_snapshot(&SnapshotToast);
426  toastscan = systable_beginscan_ordered(toastrel, toastidxs[validIndex],
427  &SnapshotToast, 1, &toastkey);
428  while ((toasttup = systable_getnext_ordered(toastscan, ForwardScanDirection)) != NULL)
429  {
430  /*
431  * Have a chunk, delete it
432  */
433  if (is_speculative)
434  heap_abort_speculative(toastrel, &toasttup->t_self);
435  else
436  simple_heap_delete(toastrel, &toasttup->t_self);
437  }
438 
439  /*
440  * End scan and close relations
441  */
442  systable_endscan_ordered(toastscan);
443  toast_close_indexes(toastidxs, num_indexes, RowExclusiveLock);
444  table_close(toastrel, RowExclusiveLock);
445 }
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:327
void heap_abort_speculative(Relation relation, ItemPointer tid)
Definition: heapam.c:5895
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static struct @142 value
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr)
Definition: detoast.h:22
void init_toast_snapshot(Snapshot toast_snapshot)
HeapTuple systable_getnext_ordered(SysScanDesc sysscan, ScanDirection direction)
Definition: genam.c:707
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
void toast_close_indexes(Relation *toastidxs, int num_indexes, LOCKMODE lock)
int toast_open_indexes(Relation toastrel, LOCKMODE lock, Relation **toastidxs, int *num_indexes)
void systable_endscan_ordered(SysScanDesc sysscan)
Definition: genam.c:732
void simple_heap_delete(Relation relation, ItemPointer tid)
Definition: heapam.c:3168
#define DatumGetPointer(X)
Definition: postgres.h:593
SysScanDesc systable_beginscan_ordered(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:642
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Definition: c.h:621
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
int16 AttrNumber
Definition: attnum.h:21
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ toast_get_valid_index()

Oid toast_get_valid_index ( Oid  toastoid,
LOCKMODE  lock 
)

Definition at line 525 of file toast_internals.c.

References NoLock, RelationGetRelid, table_close(), table_open(), toast_close_indexes(), and toast_open_indexes().

Referenced by finish_heap_swap(), and swap_relation_files().

526 {
527  int num_indexes;
528  int validIndex;
529  Oid validIndexOid;
530  Relation *toastidxs;
531  Relation toastrel;
532 
533  /* Open the toast relation */
534  toastrel = table_open(toastoid, lock);
535 
536  /* Look for the valid index of the toast relation */
537  validIndex = toast_open_indexes(toastrel,
538  lock,
539  &toastidxs,
540  &num_indexes);
541  validIndexOid = RelationGetRelid(toastidxs[validIndex]);
542 
543  /* Close the toast relation and all its indexes */
544  toast_close_indexes(toastidxs, num_indexes, NoLock);
545  table_close(toastrel, NoLock);
546 
547  return validIndexOid;
548 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
unsigned int Oid
Definition: postgres_ext.h:31
#define NoLock
Definition: lockdefs.h:34
void toast_close_indexes(Relation *toastidxs, int num_indexes, LOCKMODE lock)
int toast_open_indexes(Relation toastrel, LOCKMODE lock, Relation **toastidxs, int *num_indexes)
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define RelationGetRelid(relation)
Definition: rel.h:457

◆ toast_open_indexes()

int toast_open_indexes ( Relation  toastrel,
LOCKMODE  lock,
Relation **  toastidxs,
int *  num_indexes 
)

Definition at line 559 of file toast_internals.c.

References Assert, elog, ERROR, i, index_open(), lfirst_oid, list_free(), list_length(), NIL, palloc(), RelationData::rd_index, RelationGetIndexList(), and RelationGetRelid.

Referenced by heap_fetch_toast_slice(), toast_delete_datum(), toast_get_valid_index(), toast_save_datum(), toastrel_valueid_exists(), and verify_heapam().

563 {
564  int i = 0;
565  int res = 0;
566  bool found = false;
567  List *indexlist;
568  ListCell *lc;
569 
570  /* Get index list of the toast relation */
571  indexlist = RelationGetIndexList(toastrel);
572  Assert(indexlist != NIL);
573 
574  *num_indexes = list_length(indexlist);
575 
576  /* Open all the index relations */
577  *toastidxs = (Relation *) palloc(*num_indexes * sizeof(Relation));
578  foreach(lc, indexlist)
579  (*toastidxs)[i++] = index_open(lfirst_oid(lc), lock);
580 
581  /* Fetch the first valid index in list */
582  for (i = 0; i < *num_indexes; i++)
583  {
584  Relation toastidx = (*toastidxs)[i];
585 
586  if (toastidx->rd_index->indisvalid)
587  {
588  res = i;
589  found = true;
590  break;
591  }
592  }
593 
594  /*
595  * Free index list, not necessary anymore as relations are opened and a
596  * valid index has been found.
597  */
598  list_free(indexlist);
599 
600  /*
601  * The toast relation should have one valid index, so something is going
602  * wrong if there is nothing.
603  */
604  if (!found)
605  elog(ERROR, "no valid index found for toast relation with Oid %u",
606  RelationGetRelid(toastrel));
607 
608  return res;
609 }
#define NIL
Definition: pg_list.h:65
Form_pg_index rd_index
Definition: rel.h:175
#define ERROR
Definition: elog.h:46
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4551
void * palloc(Size size)
Definition: mcxt.c:1062
void list_free(List *list)
Definition: list.c:1391
#define elog(elevel,...)
Definition: elog.h:232
int i
Definition: pg_list.h:50
#define RelationGetRelid(relation)
Definition: rel.h:457
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ toast_save_datum()

Datum toast_save_datum ( Relation  rel,
Datum  value,
struct varlena oldexternal,
int  options 
)

Definition at line 118 of file toast_internals.c.

References Assert, CHECK_FOR_INTERRUPTS, DatumGetPointer, GetCurrentCommandId(), GetNewOidWithIndex(), heap_form_tuple(), heap_freetuple(), heap_insert(), i, index_insert(), Int32GetDatum, InvalidOid, Min, ObjectIdGetDatum, OidIsValid, palloc(), PointerGetDatum, RelationData::rd_att, RelationData::rd_index, RelationData::rd_rel, RelationData::rd_toastoid, RelationGetRelid, RowExclusiveLock, SET_VARSIZE, SET_VARTAG_EXTERNAL, HeapTupleData::t_self, table_close(), table_open(), toast_close_indexes(), TOAST_MAX_CHUNK_SIZE, toast_open_indexes(), TOAST_POINTER_SIZE, toastid_valueid_exists(), toastrel_valueid_exists(), UNIQUE_CHECK_NO, UNIQUE_CHECK_YES, varatt_external::va_extinfo, varatt_external::va_rawsize, varatt_external::va_toastrelid, varatt_external::va_valueid, VARATT_EXTERNAL_GET_POINTER, VARATT_EXTERNAL_IS_COMPRESSED, VARATT_EXTERNAL_SET_SIZE_AND_COMPRESS_METHOD, VARATT_IS_COMPRESSED, VARATT_IS_EXTERNAL, VARATT_IS_EXTERNAL_ONDISK, VARATT_IS_SHORT, VARDATA, VARDATA_COMPRESSED_GET_COMPRESS_METHOD, VARDATA_COMPRESSED_GET_EXTSIZE, VARDATA_EXTERNAL, VARDATA_SHORT, VARHDRSZ, VARHDRSZ_SHORT, VARSIZE, VARSIZE_SHORT, and VARTAG_ONDISK.

Referenced by toast_tuple_externalize().

120 {
121  Relation toastrel;
122  Relation *toastidxs;
123  HeapTuple toasttup;
124  TupleDesc toasttupDesc;
125  Datum t_values[3];
126  bool t_isnull[3];
127  CommandId mycid = GetCurrentCommandId(true);
128  struct varlena *result;
129  struct varatt_external toast_pointer;
130  union
131  {
132  struct varlena hdr;
133  /* this is to make the union big enough for a chunk: */
134  char data[TOAST_MAX_CHUNK_SIZE + VARHDRSZ];
135  /* ensure union is aligned well enough: */
136  int32 align_it;
137  } chunk_data;
138  int32 chunk_size;
139  int32 chunk_seq = 0;
140  char *data_p;
141  int32 data_todo;
142  Pointer dval = DatumGetPointer(value);
143  int num_indexes;
144  int validIndex;
145 
147 
148  /*
149  * Open the toast relation and its indexes. We can use the index to check
150  * uniqueness of the OID we assign to the toasted item, even though it has
151  * additional columns besides OID.
152  */
153  toastrel = table_open(rel->rd_rel->reltoastrelid, RowExclusiveLock);
154  toasttupDesc = toastrel->rd_att;
155 
156  /* Open all the toast indexes and look for the valid one */
157  validIndex = toast_open_indexes(toastrel,
159  &toastidxs,
160  &num_indexes);
161 
162  /*
163  * Get the data pointer and length, and compute va_rawsize and va_extinfo.
164  *
165  * va_rawsize is the size of the equivalent fully uncompressed datum, so
166  * we have to adjust for short headers.
167  *
168  * va_extinfo stored the actual size of the data payload in the toast
169  * records and the compression method in first 2 bits if data is
170  * compressed.
171  */
172  if (VARATT_IS_SHORT(dval))
173  {
174  data_p = VARDATA_SHORT(dval);
175  data_todo = VARSIZE_SHORT(dval) - VARHDRSZ_SHORT;
176  toast_pointer.va_rawsize = data_todo + VARHDRSZ; /* as if not short */
177  toast_pointer.va_extinfo = data_todo;
178  }
179  else if (VARATT_IS_COMPRESSED(dval))
180  {
181  data_p = VARDATA(dval);
182  data_todo = VARSIZE(dval) - VARHDRSZ;
183  /* rawsize in a compressed datum is just the size of the payload */
184  toast_pointer.va_rawsize = VARDATA_COMPRESSED_GET_EXTSIZE(dval) + VARHDRSZ;
185 
186  /* set external size and compression method */
187  VARATT_EXTERNAL_SET_SIZE_AND_COMPRESS_METHOD(toast_pointer, data_todo,
189  /* Assert that the numbers look like it's compressed */
190  Assert(VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer));
191  }
192  else
193  {
194  data_p = VARDATA(dval);
195  data_todo = VARSIZE(dval) - VARHDRSZ;
196  toast_pointer.va_rawsize = VARSIZE(dval);
197  toast_pointer.va_extinfo = data_todo;
198  }
199 
200  /*
201  * Insert the correct table OID into the result TOAST pointer.
202  *
203  * Normally this is the actual OID of the target toast table, but during
204  * table-rewriting operations such as CLUSTER, we have to insert the OID
205  * of the table's real permanent toast table instead. rd_toastoid is set
206  * if we have to substitute such an OID.
207  */
208  if (OidIsValid(rel->rd_toastoid))
209  toast_pointer.va_toastrelid = rel->rd_toastoid;
210  else
211  toast_pointer.va_toastrelid = RelationGetRelid(toastrel);
212 
213  /*
214  * Choose an OID to use as the value ID for this toast value.
215  *
216  * Normally we just choose an unused OID within the toast table. But
217  * during table-rewriting operations where we are preserving an existing
218  * toast table OID, we want to preserve toast value OIDs too. So, if
219  * rd_toastoid is set and we had a prior external value from that same
220  * toast table, re-use its value ID. If we didn't have a prior external
221  * value (which is a corner case, but possible if the table's attstorage
222  * options have been changed), we have to pick a value ID that doesn't
223  * conflict with either new or existing toast value OIDs.
224  */
225  if (!OidIsValid(rel->rd_toastoid))
226  {
227  /* normal case: just choose an unused OID */
228  toast_pointer.va_valueid =
229  GetNewOidWithIndex(toastrel,
230  RelationGetRelid(toastidxs[validIndex]),
231  (AttrNumber) 1);
232  }
233  else
234  {
235  /* rewrite case: check to see if value was in old toast table */
236  toast_pointer.va_valueid = InvalidOid;
237  if (oldexternal != NULL)
238  {
239  struct varatt_external old_toast_pointer;
240 
241  Assert(VARATT_IS_EXTERNAL_ONDISK(oldexternal));
242  /* Must copy to access aligned fields */
243  VARATT_EXTERNAL_GET_POINTER(old_toast_pointer, oldexternal);
244  if (old_toast_pointer.va_toastrelid == rel->rd_toastoid)
245  {
246  /* This value came from the old toast table; reuse its OID */
247  toast_pointer.va_valueid = old_toast_pointer.va_valueid;
248 
249  /*
250  * There is a corner case here: the table rewrite might have
251  * to copy both live and recently-dead versions of a row, and
252  * those versions could easily reference the same toast value.
253  * When we copy the second or later version of such a row,
254  * reusing the OID will mean we select an OID that's already
255  * in the new toast table. Check for that, and if so, just
256  * fall through without writing the data again.
257  *
258  * While annoying and ugly-looking, this is a good thing
259  * because it ensures that we wind up with only one copy of
260  * the toast value when there is only one copy in the old
261  * toast table. Before we detected this case, we'd have made
262  * multiple copies, wasting space; and what's worse, the
263  * copies belonging to already-deleted heap tuples would not
264  * be reclaimed by VACUUM.
265  */
266  if (toastrel_valueid_exists(toastrel,
267  toast_pointer.va_valueid))
268  {
269  /* Match, so short-circuit the data storage loop below */
270  data_todo = 0;
271  }
272  }
273  }
274  if (toast_pointer.va_valueid == InvalidOid)
275  {
276  /*
277  * new value; must choose an OID that doesn't conflict in either
278  * old or new toast table
279  */
280  do
281  {
282  toast_pointer.va_valueid =
283  GetNewOidWithIndex(toastrel,
284  RelationGetRelid(toastidxs[validIndex]),
285  (AttrNumber) 1);
286  } while (toastid_valueid_exists(rel->rd_toastoid,
287  toast_pointer.va_valueid));
288  }
289  }
290 
291  /*
292  * Initialize constant parts of the tuple data
293  */
294  t_values[0] = ObjectIdGetDatum(toast_pointer.va_valueid);
295  t_values[2] = PointerGetDatum(&chunk_data);
296  t_isnull[0] = false;
297  t_isnull[1] = false;
298  t_isnull[2] = false;
299 
300  /*
301  * Split up the item into chunks
302  */
303  while (data_todo > 0)
304  {
305  int i;
306 
308 
309  /*
310  * Calculate the size of this chunk
311  */
312  chunk_size = Min(TOAST_MAX_CHUNK_SIZE, data_todo);
313 
314  /*
315  * Build a tuple and store it
316  */
317  t_values[1] = Int32GetDatum(chunk_seq++);
318  SET_VARSIZE(&chunk_data, chunk_size + VARHDRSZ);
319  memcpy(VARDATA(&chunk_data), data_p, chunk_size);
320  toasttup = heap_form_tuple(toasttupDesc, t_values, t_isnull);
321 
322  heap_insert(toastrel, toasttup, mycid, options, NULL);
323 
324  /*
325  * Create the index entry. We cheat a little here by not using
326  * FormIndexDatum: this relies on the knowledge that the index columns
327  * are the same as the initial columns of the table for all the
328  * indexes. We also cheat by not providing an IndexInfo: this is okay
329  * for now because btree doesn't need one, but we might have to be
330  * more honest someday.
331  *
332  * Note also that there had better not be any user-created index on
333  * the TOAST table, since we don't bother to update anything else.
334  */
335  for (i = 0; i < num_indexes; i++)
336  {
337  /* Only index relations marked as ready can be updated */
338  if (toastidxs[i]->rd_index->indisready)
339  index_insert(toastidxs[i], t_values, t_isnull,
340  &(toasttup->t_self),
341  toastrel,
342  toastidxs[i]->rd_index->indisunique ?
344  false, NULL);
345  }
346 
347  /*
348  * Free memory
349  */
350  heap_freetuple(toasttup);
351 
352  /*
353  * Move on to next chunk
354  */
355  data_todo -= chunk_size;
356  data_p += chunk_size;
357  }
358 
359  /*
360  * Done - close toast relation and its indexes
361  */
362  toast_close_indexes(toastidxs, num_indexes, RowExclusiveLock);
363  table_close(toastrel, RowExclusiveLock);
364 
365  /*
366  * Create the TOAST pointer value that we'll return
367  */
368  result = (struct varlena *) palloc(TOAST_POINTER_SIZE);
370  memcpy(VARDATA_EXTERNAL(result), &toast_pointer, sizeof(toast_pointer));
371 
372  return PointerGetDatum(result);
373 }
void heap_insert(Relation relation, HeapTuple tup, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:2060
uint32 CommandId
Definition: c.h:601
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:322
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:327
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:325
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define VARDATA(PTR)
Definition: postgres.h:315
#define TOAST_MAX_CHUNK_SIZE
Definition: heaptoast.h:84
#define VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer)
Definition: postgres.h:391
static struct @142 value
#define VARHDRSZ_SHORT
Definition: postgres.h:292
#define VARSIZE(PTR)
Definition: postgres.h:316
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr)
Definition: detoast.h:22
#define PointerGetDatum(X)
Definition: postgres.h:600
#define VARHDRSZ
Definition: c.h:627
#define Min(x, y)
Definition: c.h:986
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define VARDATA_COMPRESSED_GET_EXTSIZE(PTR)
Definition: postgres.h:365
Form_pg_class rd_rel
Definition: rel.h:110
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
#define OidIsValid(objectId)
Definition: c.h:710
#define VARDATA_EXTERNAL(PTR)
Definition: postgres.h:323
signed int int32
Definition: c.h:429
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:326
Form_pg_index rd_index
Definition: rel.h:175
char * Pointer
Definition: c.h:418
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define VARATT_IS_SHORT(PTR)
Definition: postgres.h:339
static bool toastid_valueid_exists(Oid toastrelid, Oid valueid)
ItemPointerData t_self
Definition: htup.h:65
#define SET_VARTAG_EXTERNAL(PTR, tag)
Definition: postgres.h:346
Oid rd_toastoid
Definition: rel.h:234
#define RowExclusiveLock
Definition: lockdefs.h:38
void toast_close_indexes(Relation *toastidxs, int num_indexes, LOCKMODE lock)
#define TOAST_POINTER_SIZE
Definition: detoast.h:31
#define VARSIZE_SHORT(PTR)
Definition: postgres.h:318
int toast_open_indexes(Relation toastrel, LOCKMODE lock, Relation **toastidxs, int *num_indexes)
uintptr_t Datum
Definition: postgres.h:411
TupleDesc rd_att
Definition: rel.h:111
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:804
#define VARDATA_COMPRESSED_GET_COMPRESS_METHOD(PTR)
Definition: postgres.h:367
#define DatumGetPointer(X)
Definition: postgres.h:593
#define VARDATA_SHORT(PTR)
Definition: postgres.h:319
#define Int32GetDatum(X)
Definition: postgres.h:523
void * palloc(Size size)
Definition: mcxt.c:1062
int i
#define VARATT_EXTERNAL_SET_SIZE_AND_COMPRESS_METHOD(toast_pointer, len, cm)
Definition: postgres.h:376
Definition: c.h:621
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:102
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:761
bool index_insert(Relation indexRelation, Datum *values, bool *isnull, ItemPointer heap_t_ctid, Relation heapRelation, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition: indexam.c:176
static bool toastrel_valueid_exists(Relation toastrel, Oid valueid)
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
int16 AttrNumber
Definition: attnum.h:21
#define RelationGetRelid(relation)
Definition: rel.h:457