PostgreSQL Source Code  git master
toast_internals.h File Reference
#include "storage/lockdefs.h"
#include "utils/relcache.h"
#include "utils/snapshot.h"
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_HDRSZ   ((int32) sizeof(toast_compress_header))
 
#define TOAST_COMPRESS_RAWSIZE(ptr)   (((toast_compress_header *) (ptr))->rawsize)
 
#define TOAST_COMPRESS_SIZE(ptr)   ((int32) VARSIZE_ANY(ptr) - TOAST_COMPRESS_HDRSZ)
 
#define TOAST_COMPRESS_RAWDATA(ptr)   (((char *) (ptr)) + TOAST_COMPRESS_HDRSZ)
 
#define TOAST_COMPRESS_SET_RAWSIZE(ptr, len)   (((toast_compress_header *) (ptr))->rawsize = (len))
 

Typedefs

typedef struct toast_compress_header toast_compress_header
 

Functions

Datum toast_compress_datum (Datum value)
 
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_HDRSZ

#define TOAST_COMPRESS_HDRSZ   ((int32) sizeof(toast_compress_header))

Definition at line 32 of file toast_internals.h.

Referenced by toast_compress_datum(), and toast_decompress_datum_slice().

◆ TOAST_COMPRESS_RAWDATA

#define TOAST_COMPRESS_RAWDATA (   ptr)    (((char *) (ptr)) + TOAST_COMPRESS_HDRSZ)

◆ TOAST_COMPRESS_RAWSIZE

#define TOAST_COMPRESS_RAWSIZE (   ptr)    (((toast_compress_header *) (ptr))->rawsize)

Definition at line 33 of file toast_internals.h.

Referenced by toast_decompress_datum().

◆ TOAST_COMPRESS_SET_RAWSIZE

#define TOAST_COMPRESS_SET_RAWSIZE (   ptr,
  len 
)    (((toast_compress_header *) (ptr))->rawsize = (len))

Definition at line 37 of file toast_internals.h.

Referenced by toast_compress_datum().

◆ TOAST_COMPRESS_SIZE

#define TOAST_COMPRESS_SIZE (   ptr)    ((int32) VARSIZE_ANY(ptr) - TOAST_COMPRESS_HDRSZ)

Definition at line 34 of file toast_internals.h.

Referenced by toast_decompress_datum().

Typedef Documentation

◆ toast_compress_header

Function Documentation

◆ init_toast_snapshot()

void init_toast_snapshot ( Snapshot  toast_snapshot)

Definition at line 624 of file toast_internals.c.

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

Referenced by toast_delete_datum(), toast_fetch_datum(), and toast_fetch_datum_slice().

625 {
626  Snapshot snapshot = GetOldestSnapshot();
627 
628  if (snapshot == NULL)
629  elog(ERROR, "no known snapshots");
630 
631  InitToastSnapshot(*toast_snapshot, snapshot->lsn, snapshot->whenTaken);
632 }
XLogRecPtr lsn
Definition: snapshot.h:203
#define ERROR
Definition: elog.h:43
Snapshot GetOldestSnapshot(void)
Definition: snapmgr.c:413
#define elog(elevel,...)
Definition: elog.h:228
TimestampTz whenTaken
Definition: snapshot.h:202
#define InitToastSnapshot(snapshotdata, l, w)
Definition: snapmgr.h:91

◆ toast_close_indexes()

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

Definition at line 605 of file toast_internals.c.

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

Referenced by toast_delete_datum(), toast_fetch_datum(), toast_fetch_datum_slice(), toast_get_valid_index(), toast_save_datum(), and toastrel_valueid_exists().

606 {
607  int i;
608 
609  /* Close relations and clean up things */
610  for (i = 0; i < num_indexes; i++)
611  index_close(toastidxs[i], lock);
612  pfree(toastidxs);
613 }
void pfree(void *pointer)
Definition: mcxt.c:1056
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:152
int i

◆ toast_compress_datum()

Datum toast_compress_datum ( Datum  value)

Definition at line 47 of file toast_internals.c.

References Assert, DatumGetPointer, PGLZ_Strategy::max_input_size, palloc(), pfree(), pglz_compress(), PGLZ_MAX_OUTPUT, PGLZ_strategy_default, PointerGetDatum, SET_VARSIZE_COMPRESSED, TOAST_COMPRESS_HDRSZ, TOAST_COMPRESS_RAWDATA, TOAST_COMPRESS_SET_RAWSIZE, VARATT_IS_COMPRESSED, VARATT_IS_EXTERNAL, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by index_form_tuple(), and toast_tuple_try_compression().

48 {
49  struct varlena *tmp;
51  int32 len;
52 
55 
56  /*
57  * No point in wasting a palloc cycle if value size is out of the allowed
58  * range for compression
59  */
60  if (valsize < PGLZ_strategy_default->min_input_size ||
62  return PointerGetDatum(NULL);
63 
64  tmp = (struct varlena *) palloc(PGLZ_MAX_OUTPUT(valsize) +
66 
67  /*
68  * We recheck the actual size even if pglz_compress() reports success,
69  * because it might be satisfied with having saved as little as one byte
70  * in the compressed data --- which could turn into a net loss once you
71  * consider header and alignment padding. Worst case, the compressed
72  * format might require three padding bytes (plus header, which is
73  * included in VARSIZE(tmp)), whereas the uncompressed format would take
74  * only one header byte and no padding if the value is short enough. So
75  * we insist on a savings of more than 2 bytes to ensure we have a gain.
76  */
78  valsize,
81  if (len >= 0 &&
82  len + TOAST_COMPRESS_HDRSZ < valsize - 2)
83  {
84  TOAST_COMPRESS_SET_RAWSIZE(tmp, valsize);
86  /* successful compression */
87  return PointerGetDatum(tmp);
88  }
89  else
90  {
91  /* incompressible data */
92  pfree(tmp);
93  return PointerGetDatum(NULL);
94  }
95 }
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:312
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PointerGetDatum(X)
Definition: postgres.h:556
static struct @145 value
signed int int32
Definition: c.h:347
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:313
void pfree(void *pointer)
Definition: mcxt.c:1056
#define TOAST_COMPRESS_RAWDATA(ptr)
int32 pglz_compress(const char *source, int32 slen, char *dest, const PGLZ_Strategy *strategy)
#define PGLZ_MAX_OUTPUT(_dlen)
Definition: pg_lzcompress.h:21
#define Assert(condition)
Definition: c.h:739
int32 max_input_size
Definition: pg_lzcompress.h:60
#define TOAST_COMPRESS_HDRSZ
#define DatumGetPointer(X)
Definition: postgres.h:549
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
#define TOAST_COMPRESS_SET_RAWSIZE(ptr, len)
#define SET_VARSIZE_COMPRESSED(PTR, len)
Definition: postgres.h:331
Definition: c.h:556
const PGLZ_Strategy *const PGLZ_strategy_default

◆ toast_delete_datum()

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

Definition at line 369 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().

370 {
371  struct varlena *attr = (struct varlena *) DatumGetPointer(value);
372  struct varatt_external toast_pointer;
373  Relation toastrel;
374  Relation *toastidxs;
375  ScanKeyData toastkey;
376  SysScanDesc toastscan;
377  HeapTuple toasttup;
378  int num_indexes;
379  int validIndex;
380  SnapshotData SnapshotToast;
381 
382  if (!VARATT_IS_EXTERNAL_ONDISK(attr))
383  return;
384 
385  /* Must copy to access aligned fields */
386  VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
387 
388  /*
389  * Open the toast relation and its indexes
390  */
391  toastrel = table_open(toast_pointer.va_toastrelid, RowExclusiveLock);
392 
393  /* Fetch valid relation used for process */
394  validIndex = toast_open_indexes(toastrel,
396  &toastidxs,
397  &num_indexes);
398 
399  /*
400  * Setup a scan key to find chunks with matching va_valueid
401  */
402  ScanKeyInit(&toastkey,
403  (AttrNumber) 1,
404  BTEqualStrategyNumber, F_OIDEQ,
405  ObjectIdGetDatum(toast_pointer.va_valueid));
406 
407  /*
408  * Find all the chunks. (We don't actually care whether we see them in
409  * sequence or not, but since we've already locked the index we might as
410  * well use systable_beginscan_ordered.)
411  */
412  init_toast_snapshot(&SnapshotToast);
413  toastscan = systable_beginscan_ordered(toastrel, toastidxs[validIndex],
414  &SnapshotToast, 1, &toastkey);
415  while ((toasttup = systable_getnext_ordered(toastscan, ForwardScanDirection)) != NULL)
416  {
417  /*
418  * Have a chunk, delete it
419  */
420  if (is_speculative)
421  heap_abort_speculative(toastrel, &toasttup->t_self);
422  else
423  simple_heap_delete(toastrel, &toasttup->t_self);
424  }
425 
426  /*
427  * End scan and close relations
428  */
429  systable_endscan_ordered(toastscan);
430  toast_close_indexes(toastidxs, num_indexes, RowExclusiveLock);
431  table_close(toastrel, RowExclusiveLock);
432 }
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:314
void heap_abort_speculative(Relation relation, ItemPointer tid)
Definition: heapam.c:5567
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr)
Definition: detoast.h:32
void init_toast_snapshot(Snapshot toast_snapshot)
static struct @145 value
HeapTuple systable_getnext_ordered(SysScanDesc sysscan, ScanDirection direction)
Definition: genam.c:630
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
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:649
void simple_heap_delete(Relation relation, ItemPointer tid)
Definition: heapam.c:2847
#define DatumGetPointer(X)
Definition: postgres.h:549
SysScanDesc systable_beginscan_ordered(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:565
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Definition: c.h:556
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 512 of file toast_internals.c.

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

Referenced by finish_heap_swap(), and swap_relation_files().

513 {
514  int num_indexes;
515  int validIndex;
516  Oid validIndexOid;
517  Relation *toastidxs;
518  Relation toastrel;
519 
520  /* Open the toast relation */
521  toastrel = table_open(toastoid, lock);
522 
523  /* Look for the valid index of the toast relation */
524  validIndex = toast_open_indexes(toastrel,
525  lock,
526  &toastidxs,
527  &num_indexes);
528  validIndexOid = RelationGetRelid(toastidxs[validIndex]);
529 
530  /* Close the toast relation and all its indexes */
531  toast_close_indexes(toastidxs, num_indexes, lock);
532  table_close(toastrel, lock);
533 
534  return validIndexOid;
535 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
unsigned int Oid
Definition: postgres_ext.h:31
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:422

◆ toast_open_indexes()

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

Definition at line 546 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 toast_delete_datum(), toast_fetch_datum(), toast_fetch_datum_slice(), toast_get_valid_index(), toast_save_datum(), and toastrel_valueid_exists().

550 {
551  int i = 0;
552  int res = 0;
553  bool found = false;
554  List *indexlist;
555  ListCell *lc;
556 
557  /* Get index list of the toast relation */
558  indexlist = RelationGetIndexList(toastrel);
559  Assert(indexlist != NIL);
560 
561  *num_indexes = list_length(indexlist);
562 
563  /* Open all the index relations */
564  *toastidxs = (Relation *) palloc(*num_indexes * sizeof(Relation));
565  foreach(lc, indexlist)
566  (*toastidxs)[i++] = index_open(lfirst_oid(lc), lock);
567 
568  /* Fetch the first valid index in list */
569  for (i = 0; i < *num_indexes; i++)
570  {
571  Relation toastidx = (*toastidxs)[i];
572 
573  if (toastidx->rd_index->indisvalid)
574  {
575  res = i;
576  found = true;
577  break;
578  }
579  }
580 
581  /*
582  * Free index list, not necessary anymore as relations are opened and a
583  * valid index has been found.
584  */
585  list_free(indexlist);
586 
587  /*
588  * The toast relation should have one valid index, so something is going
589  * wrong if there is nothing.
590  */
591  if (!found)
592  elog(ERROR, "no valid index found for toast relation with Oid %u",
593  RelationGetRelid(toastrel));
594 
595  return res;
596 }
#define NIL
Definition: pg_list.h:65
Form_pg_index rd_index
Definition: rel.h:143
#define ERROR
Definition: elog.h:43
#define Assert(condition)
Definition: c.h:739
static int list_length(const List *l)
Definition: pg_list.h:169
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4347
void * palloc(Size size)
Definition: mcxt.c:949
void list_free(List *list)
Definition: list.c:1377
#define elog(elevel,...)
Definition: elog.h:228
int i
Definition: pg_list.h:50
#define RelationGetRelid(relation)
Definition: rel.h:422
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:126
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ toast_save_datum()

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

Definition at line 110 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_extsize, varatt_external::va_rawsize, varatt_external::va_toastrelid, varatt_external::va_valueid, VARATT_EXTERNAL_GET_POINTER, VARATT_EXTERNAL_IS_COMPRESSED, VARATT_IS_COMPRESSED, VARATT_IS_EXTERNAL, VARATT_IS_EXTERNAL_ONDISK, VARATT_IS_SHORT, VARDATA, VARDATA_EXTERNAL, VARDATA_SHORT, VARHDRSZ, VARHDRSZ_SHORT, VARRAWSIZE_4B_C, VARSIZE, VARSIZE_SHORT, and VARTAG_ONDISK.

Referenced by toast_tuple_externalize().

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