PostgreSQL Source Code  git master
toast_internals.c File Reference
#include "postgres.h"
#include "access/detoast.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/heaptoast.h"
#include "access/table.h"
#include "access/toast_internals.h"
#include "access/xact.h"
#include "catalog/catalog.h"
#include "miscadmin.h"
#include "utils/fmgroids.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
Include dependency graph for toast_internals.c:

Go to the source code of this file.

Functions

static bool toastrel_valueid_exists (Relation toastrel, Oid valueid)
 
static bool toastid_valueid_exists (Oid toastrelid, Oid valueid)
 
Datum toast_compress_datum (Datum value, char cmethod)
 
Datum toast_save_datum (Relation rel, Datum value, struct varlena *oldexternal, int options)
 
void toast_delete_datum (Relation rel, Datum value, bool is_speculative)
 
Oid toast_get_valid_index (Oid toastoid, LOCKMODE lock)
 
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)
 

Function Documentation

◆ init_toast_snapshot()

void init_toast_snapshot ( Snapshot  toast_snapshot)

Definition at line 641 of file toast_internals.c.

642 {
643  Snapshot snapshot = GetOldestSnapshot();
644 
645  /*
646  * GetOldestSnapshot returns NULL if the session has no active snapshots.
647  * We can get that if, for example, a procedure fetches a toasted value
648  * into a local variable, commits, and then tries to detoast the value.
649  * Such coding is unsafe, because once we commit there is nothing to
650  * prevent the toast data from being deleted. Detoasting *must* happen in
651  * the same transaction that originally fetched the toast pointer. Hence,
652  * rather than trying to band-aid over the problem, throw an error. (This
653  * is not very much protection, because in many scenarios the procedure
654  * would have already created a new transaction snapshot, preventing us
655  * from detecting the problem. But it's better than nothing, and for sure
656  * we shouldn't expend code on masking the problem more.)
657  */
658  if (snapshot == NULL)
659  elog(ERROR, "cannot fetch toast data without an active snapshot");
660 
661  /*
662  * Catalog snapshots can be returned by GetOldestSnapshot() even if not
663  * registered or active. That easily hides bugs around not having a
664  * snapshot set up - most of the time there is a valid catalog snapshot.
665  * So additionally insist that the current snapshot is registered or
666  * active.
667  */
669 
670  InitToastSnapshot(*toast_snapshot, snapshot->lsn, snapshot->whenTaken);
671 }
#define Assert(condition)
Definition: c.h:858
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
bool HaveRegisteredOrActiveSnapshot(void)
Definition: snapmgr.c:1624
Snapshot GetOldestSnapshot(void)
Definition: snapmgr.c:323
#define InitToastSnapshot(snapshotdata, l, w)
Definition: snapmgr.h:56
TimestampTz whenTaken
Definition: snapshot.h:208
XLogRecPtr lsn
Definition: snapshot.h:209

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

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

◆ toast_close_indexes()

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

Definition at line 623 of file toast_internals.c.

624 {
625  int i;
626 
627  /* Close relations and clean up things */
628  for (i = 0; i < num_indexes; i++)
629  index_close(toastidxs[i], lock);
630  pfree(toastidxs);
631 }
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
int i
Definition: isn.c:73
void pfree(void *pointer)
Definition: mcxt.c:1520

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().

◆ toast_compress_datum()

Datum toast_compress_datum ( Datum  value,
char  cmethod 
)

Definition at line 46 of file toast_internals.c.

47 {
48  struct varlena *tmp = NULL;
49  int32 valsize;
51 
54 
56 
57  /* If the compression method is not valid, use the current default */
58  if (!CompressionMethodIsValid(cmethod))
59  cmethod = default_toast_compression;
60 
61  /*
62  * Call appropriate compression routine for the compression method.
63  */
64  switch (cmethod)
65  {
67  tmp = pglz_compress_datum((const struct varlena *) value);
69  break;
71  tmp = lz4_compress_datum((const struct varlena *) value);
73  break;
74  default:
75  elog(ERROR, "invalid compression method %c", cmethod);
76  }
77 
78  if (tmp == NULL)
79  return PointerGetDatum(NULL);
80 
81  /*
82  * We recheck the actual size even if compression reports success, because
83  * it might be satisfied with having saved as little as one byte in the
84  * compressed data --- which could turn into a net loss once you consider
85  * header and alignment padding. Worst case, the compressed format might
86  * require three padding bytes (plus header, which is included in
87  * VARSIZE(tmp)), whereas the uncompressed format would take only one
88  * header byte and no padding if the value is short enough. So we insist
89  * on a savings of more than 2 bytes to ensure we have a gain.
90  */
91  if (VARSIZE(tmp) < valsize - 2)
92  {
93  /* successful compression */
96  return PointerGetDatum(tmp);
97  }
98  else
99  {
100  /* incompressible data */
101  pfree(tmp);
102  return PointerGetDatum(NULL);
103  }
104 }
signed int int32
Definition: c.h:494
static struct @155 value
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
Definition: c.h:687
int default_toast_compression
struct varlena * lz4_compress_datum(const struct varlena *value)
struct varlena * pglz_compress_datum(const struct varlena *value)
#define CompressionMethodIsValid(cm)
ToastCompressionId
@ TOAST_INVALID_COMPRESSION_ID
@ TOAST_LZ4_COMPRESSION_ID
@ TOAST_PGLZ_COMPRESSION_ID
#define TOAST_PGLZ_COMPRESSION
#define TOAST_LZ4_COMPRESSION
#define TOAST_COMPRESS_SET_SIZE_AND_COMPRESS_METHOD(ptr, len, cm_method)
#define VARATT_IS_COMPRESSED(PTR)
Definition: varatt.h:288
#define VARSIZE(PTR)
Definition: varatt.h:279
#define VARATT_IS_EXTERNAL(PTR)
Definition: varatt.h:289
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

References Assert, CompressionMethodIsValid, DatumGetPointer(), default_toast_compression, 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, value, VARATT_IS_COMPRESSED, VARATT_IS_EXTERNAL, VARSIZE, and VARSIZE_ANY_EXHDR.

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

◆ toast_delete_datum()

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

Definition at line 385 of file toast_internals.c.

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

References BTEqualStrategyNumber, DatumGetPointer(), ForwardScanDirection, heap_abort_speculative(), init_toast_snapshot(), NoLock, 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, value, VARATT_EXTERNAL_GET_POINTER, and VARATT_IS_EXTERNAL_ONDISK.

Referenced by toast_delete_external(), and toast_tuple_cleanup().

◆ toast_get_valid_index()

Oid toast_get_valid_index ( Oid  toastoid,
LOCKMODE  lock 
)

Definition at line 530 of file toast_internals.c.

531 {
532  int num_indexes;
533  int validIndex;
534  Oid validIndexOid;
535  Relation *toastidxs;
536  Relation toastrel;
537 
538  /* Open the toast relation */
539  toastrel = table_open(toastoid, lock);
540 
541  /* Look for the valid index of the toast relation */
542  validIndex = toast_open_indexes(toastrel,
543  lock,
544  &toastidxs,
545  &num_indexes);
546  validIndexOid = RelationGetRelid(toastidxs[validIndex]);
547 
548  /* Close the toast relation and all its indexes */
549  toast_close_indexes(toastidxs, num_indexes, NoLock);
550  table_close(toastrel, NoLock);
551 
552  return validIndexOid;
553 }
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetRelid(relation)
Definition: rel.h:505

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

Referenced by finish_heap_swap(), and swap_relation_files().

◆ toast_open_indexes()

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

Definition at line 564 of file toast_internals.c.

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

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

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

◆ toast_save_datum()

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

Definition at line 119 of file toast_internals.c.

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

References Assert, CHECK_FOR_INTERRUPTS, data, DatumGetPointer(), GetCurrentCommandId(), GetNewOidWithIndex(), heap_form_tuple(), heap_freetuple(), heap_insert(), i, index_insert(), Int32GetDatum(), InvalidOid, Min, NoLock, 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, value, 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().

◆ toastid_valueid_exists()

static bool toastid_valueid_exists ( Oid  toastrelid,
Oid  valueid 
)
static

Definition at line 509 of file toast_internals.c.

510 {
511  bool result;
512  Relation toastrel;
513 
514  toastrel = table_open(toastrelid, AccessShareLock);
515 
516  result = toastrel_valueid_exists(toastrel, valueid);
517 
518  table_close(toastrel, AccessShareLock);
519 
520  return result;
521 }
#define AccessShareLock
Definition: lockdefs.h:36

References AccessShareLock, table_close(), table_open(), and toastrel_valueid_exists().

Referenced by toast_save_datum().

◆ toastrel_valueid_exists()

static bool toastrel_valueid_exists ( Relation  toastrel,
Oid  valueid 
)
static

Definition at line 461 of file toast_internals.c.

462 {
463  bool result = false;
464  ScanKeyData toastkey;
465  SysScanDesc toastscan;
466  int num_indexes;
467  int validIndex;
468  Relation *toastidxs;
469 
470  /* Fetch a valid index relation */
471  validIndex = toast_open_indexes(toastrel,
473  &toastidxs,
474  &num_indexes);
475 
476  /*
477  * Setup a scan key to find chunks with matching va_valueid
478  */
479  ScanKeyInit(&toastkey,
480  (AttrNumber) 1,
481  BTEqualStrategyNumber, F_OIDEQ,
482  ObjectIdGetDatum(valueid));
483 
484  /*
485  * Is there any such chunk?
486  */
487  toastscan = systable_beginscan(toastrel,
488  RelationGetRelid(toastidxs[validIndex]),
489  true, SnapshotAny, 1, &toastkey);
490 
491  if (systable_getnext(toastscan) != NULL)
492  result = true;
493 
494  systable_endscan(toastscan);
495 
496  /* Clean up */
497  toast_close_indexes(toastidxs, num_indexes, RowExclusiveLock);
498 
499  return result;
500 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:596
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:503
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:384
#define SnapshotAny
Definition: snapmgr.h:33

References BTEqualStrategyNumber, ObjectIdGetDatum(), RelationGetRelid, RowExclusiveLock, ScanKeyInit(), SnapshotAny, systable_beginscan(), systable_endscan(), systable_getnext(), toast_close_indexes(), and toast_open_indexes().

Referenced by toast_save_datum(), and toastid_valueid_exists().