PostgreSQL Source Code  git master
large_object.h File Reference
#include "utils/snapshot.h"
Include dependency graph for large_object.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  LargeObjectDesc
 

Macros

#define IFS_RDLOCK   (1 << 0) /* LO was opened for reading */
 
#define IFS_WRLOCK   (1 << 1) /* LO was opened for writing */
 
#define LOBLKSIZE   (BLCKSZ / 4)
 
#define MAX_LARGE_OBJECT_SIZE   ((int64) INT_MAX * LOBLKSIZE)
 

Typedefs

typedef struct LargeObjectDesc LargeObjectDesc
 

Functions

void close_lo_relation (bool isCommit)
 
Oid inv_create (Oid lobjId)
 
LargeObjectDescinv_open (Oid lobjId, int flags, MemoryContext mcxt)
 
void inv_close (LargeObjectDesc *obj_desc)
 
int inv_drop (Oid lobjId)
 
int64 inv_seek (LargeObjectDesc *obj_desc, int64 offset, int whence)
 
int64 inv_tell (LargeObjectDesc *obj_desc)
 
int inv_read (LargeObjectDesc *obj_desc, char *buf, int nbytes)
 
int inv_write (LargeObjectDesc *obj_desc, const char *buf, int nbytes)
 
void inv_truncate (LargeObjectDesc *obj_desc, int64 len)
 

Variables

PGDLLIMPORT bool lo_compat_privileges
 

Macro Definition Documentation

◆ IFS_RDLOCK

#define IFS_RDLOCK   (1 << 0) /* LO was opened for reading */

Definition at line 48 of file large_object.h.

◆ IFS_WRLOCK

#define IFS_WRLOCK   (1 << 1) /* LO was opened for writing */

Definition at line 49 of file large_object.h.

◆ LOBLKSIZE

#define LOBLKSIZE   (BLCKSZ / 4)

Definition at line 70 of file large_object.h.

◆ MAX_LARGE_OBJECT_SIZE

#define MAX_LARGE_OBJECT_SIZE   ((int64) INT_MAX * LOBLKSIZE)

Definition at line 76 of file large_object.h.

Typedef Documentation

◆ LargeObjectDesc

Function Documentation

◆ close_lo_relation()

void close_lo_relation ( bool  isCommit)

Definition at line 99 of file inv_api.c.

100 {
101  if (lo_heap_r || lo_index_r)
102  {
103  /*
104  * Only bother to close if committing; else abort cleanup will handle
105  * it
106  */
107  if (isCommit)
108  {
109  ResourceOwner currentOwner;
110 
111  currentOwner = CurrentResourceOwner;
113 
114  if (lo_index_r)
116  if (lo_heap_r)
118 
119  CurrentResourceOwner = currentOwner;
120  }
121  lo_heap_r = NULL;
122  lo_index_r = NULL;
123  }
124 }
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
static Relation lo_index_r
Definition: inv_api.c:68
static Relation lo_heap_r
Definition: inv_api.c:67
#define NoLock
Definition: lockdefs.h:34
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:149
ResourceOwner CurrentResourceOwner
Definition: resowner.c:147
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126

References CurrentResourceOwner, index_close(), lo_heap_r, lo_index_r, NoLock, table_close(), and TopTransactionResourceOwner.

Referenced by AtEOXact_LargeObject().

◆ inv_close()

void inv_close ( LargeObjectDesc obj_desc)

Definition at line 339 of file inv_api.c.

340 {
341  Assert(PointerIsValid(obj_desc));
342  pfree(obj_desc);
343 }
#define PointerIsValid(pointer)
Definition: c.h:752
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
Definition: mcxt.c:1456

References Assert(), pfree(), and PointerIsValid.

Referenced by be_lo_export(), be_lo_from_bytea(), be_lo_put(), closeLOfd(), lo_get_fragment_internal(), and lo_import_internal().

◆ inv_create()

Oid inv_create ( Oid  lobjId)

Definition at line 212 of file inv_api.c.

213 {
214  Oid lobjId_new;
215 
216  /*
217  * Create a new largeobject with empty data pages
218  */
219  lobjId_new = LargeObjectCreate(lobjId);
220 
221  /*
222  * dependency on the owner of largeobject
223  *
224  * The reason why we use LargeObjectRelationId instead of
225  * LargeObjectMetadataRelationId here is to provide backward compatibility
226  * to the applications which utilize a knowledge about internal layout of
227  * system catalogs. OID of pg_largeobject_metadata and loid of
228  * pg_largeobject are same value, so there are no actual differences here.
229  */
230  recordDependencyOnOwner(LargeObjectRelationId,
231  lobjId_new, GetUserId());
232 
233  /* Post creation hook for new large object */
234  InvokeObjectPostCreateHook(LargeObjectRelationId, lobjId_new, 0);
235 
236  /*
237  * Advance command counter to make new tuple visible to later operations.
238  */
240 
241  return lobjId_new;
242 }
Oid GetUserId(void)
Definition: miscinit.c:509
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:173
Oid LargeObjectCreate(Oid loid)
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:165
unsigned int Oid
Definition: postgres_ext.h:31
void CommandCounterIncrement(void)
Definition: xact.c:1078

References CommandCounterIncrement(), GetUserId(), InvokeObjectPostCreateHook, LargeObjectCreate(), and recordDependencyOnOwner().

Referenced by be_lo_creat(), be_lo_create(), be_lo_from_bytea(), and lo_import_internal().

◆ inv_drop()

int inv_drop ( Oid  lobjId)

Definition at line 351 of file inv_api.c.

352 {
353  ObjectAddress object;
354 
355  /*
356  * Delete any comments and dependencies on the large object
357  */
358  object.classId = LargeObjectRelationId;
359  object.objectId = lobjId;
360  object.objectSubId = 0;
361  performDeletion(&object, DROP_CASCADE, 0);
362 
363  /*
364  * Advance command counter so that tuple removal will be seen by later
365  * large-object operations in this transaction.
366  */
368 
369  /* For historical reasons, we always return 1 on success. */
370  return 1;
371 }
void performDeletion(const ObjectAddress *object, DropBehavior behavior, int flags)
Definition: dependency.c:328
@ DROP_CASCADE
Definition: parsenodes.h:2194

References ObjectAddress::classId, CommandCounterIncrement(), DROP_CASCADE, and performDeletion().

Referenced by be_lo_unlink().

◆ inv_open()

LargeObjectDesc* inv_open ( Oid  lobjId,
int  flags,
MemoryContext  mcxt 
)

Definition at line 255 of file inv_api.c.

256 {
257  LargeObjectDesc *retval;
258  Snapshot snapshot = NULL;
259  int descflags = 0;
260 
261  /*
262  * Historically, no difference is made between (INV_WRITE) and (INV_WRITE
263  * | INV_READ), the caller being allowed to read the large object
264  * descriptor in either case.
265  */
266  if (flags & INV_WRITE)
267  descflags |= IFS_WRLOCK | IFS_RDLOCK;
268  if (flags & INV_READ)
269  descflags |= IFS_RDLOCK;
270 
271  if (descflags == 0)
272  ereport(ERROR,
273  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
274  errmsg("invalid flags for opening a large object: %d",
275  flags)));
276 
277  /* Get snapshot. If write is requested, use an instantaneous snapshot. */
278  if (descflags & IFS_WRLOCK)
279  snapshot = NULL;
280  else
281  snapshot = GetActiveSnapshot();
282 
283  /* Can't use LargeObjectExists here because we need to specify snapshot */
284  if (!myLargeObjectExists(lobjId, snapshot))
285  ereport(ERROR,
286  (errcode(ERRCODE_UNDEFINED_OBJECT),
287  errmsg("large object %u does not exist", lobjId)));
288 
289  /* Apply permission checks, again specifying snapshot */
290  if ((descflags & IFS_RDLOCK) != 0)
291  {
292  if (!lo_compat_privileges &&
294  GetUserId(),
295  ACL_SELECT,
296  snapshot) != ACLCHECK_OK)
297  ereport(ERROR,
298  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
299  errmsg("permission denied for large object %u",
300  lobjId)));
301  }
302  if ((descflags & IFS_WRLOCK) != 0)
303  {
304  if (!lo_compat_privileges &&
306  GetUserId(),
307  ACL_UPDATE,
308  snapshot) != ACLCHECK_OK)
309  ereport(ERROR,
310  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
311  errmsg("permission denied for large object %u",
312  lobjId)));
313  }
314 
315  /* OK to create a descriptor */
316  retval = (LargeObjectDesc *) MemoryContextAlloc(mcxt,
317  sizeof(LargeObjectDesc));
318  retval->id = lobjId;
319  retval->offset = 0;
320  retval->flags = descflags;
321 
322  /* caller sets if needed, not used by the functions in this file */
323  retval->subid = InvalidSubTransactionId;
324 
325  /*
326  * The snapshot (if any) is just the currently active snapshot. The
327  * caller will replace it with a longer-lived copy if needed.
328  */
329  retval->snapshot = snapshot;
330 
331  return retval;
332 }
@ ACLCHECK_OK
Definition: acl.h:182
AclResult pg_largeobject_aclcheck_snapshot(Oid lobj_oid, Oid roleid, AclMode mode, Snapshot snapshot)
Definition: aclchk.c:3947
#define InvalidSubTransactionId
Definition: c.h:647
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
static bool myLargeObjectExists(Oid loid, Snapshot snapshot)
Definition: inv_api.c:132
bool lo_compat_privileges
Definition: inv_api.c:58
#define IFS_RDLOCK
Definition: large_object.h:48
#define IFS_WRLOCK
Definition: large_object.h:49
#define INV_READ
Definition: libpq-fs.h:22
#define INV_WRITE
Definition: libpq-fs.h:21
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1021
#define ACL_UPDATE
Definition: parsenodes.h:85
#define ACL_SELECT
Definition: parsenodes.h:84
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:751
Snapshot snapshot
Definition: large_object.h:42
SubTransactionId subid
Definition: large_object.h:43

References ACL_SELECT, ACL_UPDATE, ACLCHECK_OK, ereport, errcode(), errmsg(), ERROR, LargeObjectDesc::flags, GetActiveSnapshot(), GetUserId(), LargeObjectDesc::id, IFS_RDLOCK, IFS_WRLOCK, INV_READ, INV_WRITE, InvalidSubTransactionId, lo_compat_privileges, MemoryContextAlloc(), myLargeObjectExists(), LargeObjectDesc::offset, pg_largeobject_aclcheck_snapshot(), LargeObjectDesc::snapshot, and LargeObjectDesc::subid.

Referenced by be_lo_export(), be_lo_from_bytea(), be_lo_open(), be_lo_put(), lo_get_fragment_internal(), and lo_import_internal().

◆ inv_read()

int inv_read ( LargeObjectDesc obj_desc,
char *  buf,
int  nbytes 
)

Definition at line 490 of file inv_api.c.

491 {
492  int nread = 0;
493  int64 n;
494  int64 off;
495  int len;
496  int32 pageno = (int32) (obj_desc->offset / LOBLKSIZE);
497  uint64 pageoff;
498  ScanKeyData skey[2];
499  SysScanDesc sd;
500  HeapTuple tuple;
501 
502  Assert(PointerIsValid(obj_desc));
503  Assert(buf != NULL);
504 
505  if ((obj_desc->flags & IFS_RDLOCK) == 0)
506  ereport(ERROR,
507  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
508  errmsg("permission denied for large object %u",
509  obj_desc->id)));
510 
511  if (nbytes <= 0)
512  return 0;
513 
515 
516  ScanKeyInit(&skey[0],
517  Anum_pg_largeobject_loid,
518  BTEqualStrategyNumber, F_OIDEQ,
519  ObjectIdGetDatum(obj_desc->id));
520 
521  ScanKeyInit(&skey[1],
522  Anum_pg_largeobject_pageno,
524  Int32GetDatum(pageno));
525 
527  obj_desc->snapshot, 2, skey);
528 
529  while ((tuple = systable_getnext_ordered(sd, ForwardScanDirection)) != NULL)
530  {
532  bytea *datafield;
533  bool pfreeit;
534 
535  if (HeapTupleHasNulls(tuple)) /* paranoia */
536  elog(ERROR, "null field found in pg_largeobject");
538 
539  /*
540  * We expect the indexscan will deliver pages in order. However,
541  * there may be missing pages if the LO contains unwritten "holes". We
542  * want missing sections to read out as zeroes.
543  */
544  pageoff = ((uint64) data->pageno) * LOBLKSIZE;
545  if (pageoff > obj_desc->offset)
546  {
547  n = pageoff - obj_desc->offset;
548  n = (n <= (nbytes - nread)) ? n : (nbytes - nread);
549  MemSet(buf + nread, 0, n);
550  nread += n;
551  obj_desc->offset += n;
552  }
553 
554  if (nread < nbytes)
555  {
556  Assert(obj_desc->offset >= pageoff);
557  off = (int) (obj_desc->offset - pageoff);
558  Assert(off >= 0 && off < LOBLKSIZE);
559 
560  getdatafield(data, &datafield, &len, &pfreeit);
561  if (len > off)
562  {
563  n = len - off;
564  n = (n <= (nbytes - nread)) ? n : (nbytes - nread);
565  memcpy(buf + nread, VARDATA(datafield) + off, n);
566  nread += n;
567  obj_desc->offset += n;
568  }
569  if (pfreeit)
570  pfree(datafield);
571  }
572 
573  if (nread >= nbytes)
574  break;
575  }
576 
578 
579  return nread;
580 }
signed int int32
Definition: c.h:483
#define MemSet(start, val, len)
Definition: c.h:1009
SysScanDesc systable_beginscan_ordered(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:646
void systable_endscan_ordered(SysScanDesc sysscan)
Definition: genam.c:736
HeapTuple systable_getnext_ordered(SysScanDesc sysscan, ScanDirection direction)
Definition: genam.c:711
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
#define HeapTupleHasNulls(tuple)
Definition: htup_details.h:659
static void getdatafield(Form_pg_largeobject tuple, bytea **pdatafield, int *plen, bool *pfreeit)
Definition: inv_api.c:170
static void open_lo_relation(void)
Definition: inv_api.c:75
#define LOBLKSIZE
Definition: large_object.h:70
const void size_t len
const void * data
FormData_pg_largeobject * Form_pg_largeobject
static char * buf
Definition: pg_test_fsync.c:67
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
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
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
Definition: c.h:676
#define VARDATA(PTR)
Definition: varatt.h:278

References Assert(), BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, buf, data, elog(), ereport, errcode(), errmsg(), ERROR, LargeObjectDesc::flags, ForwardScanDirection, getdatafield(), GETSTRUCT, HeapTupleHasNulls, LargeObjectDesc::id, IFS_RDLOCK, Int32GetDatum(), len, lo_heap_r, lo_index_r, LOBLKSIZE, MemSet, ObjectIdGetDatum(), LargeObjectDesc::offset, open_lo_relation(), pfree(), PointerIsValid, ScanKeyInit(), LargeObjectDesc::snapshot, systable_beginscan_ordered(), systable_endscan_ordered(), systable_getnext_ordered(), and VARDATA.

Referenced by be_lo_export(), lo_get_fragment_internal(), and lo_read().

◆ inv_seek()

int64 inv_seek ( LargeObjectDesc obj_desc,
int64  offset,
int  whence 
)

Definition at line 428 of file inv_api.c.

429 {
430  int64 newoffset;
431 
432  Assert(PointerIsValid(obj_desc));
433 
434  /*
435  * We allow seek/tell if you have either read or write permission, so no
436  * need for a permission check here.
437  */
438 
439  /*
440  * Note: overflow in the additions is possible, but since we will reject
441  * negative results, we don't need any extra test for that.
442  */
443  switch (whence)
444  {
445  case SEEK_SET:
446  newoffset = offset;
447  break;
448  case SEEK_CUR:
449  newoffset = obj_desc->offset + offset;
450  break;
451  case SEEK_END:
452  newoffset = inv_getsize(obj_desc) + offset;
453  break;
454  default:
455  ereport(ERROR,
456  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
457  errmsg("invalid whence setting: %d", whence)));
458  newoffset = 0; /* keep compiler quiet */
459  break;
460  }
461 
462  /*
463  * use errmsg_internal here because we don't want to expose INT64_FORMAT
464  * in translatable strings; doing better is not worth the trouble
465  */
466  if (newoffset < 0 || newoffset > MAX_LARGE_OBJECT_SIZE)
467  ereport(ERROR,
468  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
469  errmsg_internal("invalid large object seek target: " INT64_FORMAT,
470  newoffset)));
471 
472  obj_desc->offset = newoffset;
473  return newoffset;
474 }
#define INT64_FORMAT
Definition: c.h:537
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1156
static uint64 inv_getsize(LargeObjectDesc *obj_desc)
Definition: inv_api.c:380
#define MAX_LARGE_OBJECT_SIZE
Definition: large_object.h:76

References Assert(), ereport, errcode(), errmsg(), errmsg_internal(), ERROR, INT64_FORMAT, inv_getsize(), MAX_LARGE_OBJECT_SIZE, LargeObjectDesc::offset, and PointerIsValid.

Referenced by be_lo_lseek(), be_lo_lseek64(), be_lo_put(), and lo_get_fragment_internal().

◆ inv_tell()

int64 inv_tell ( LargeObjectDesc obj_desc)

Definition at line 477 of file inv_api.c.

478 {
479  Assert(PointerIsValid(obj_desc));
480 
481  /*
482  * We allow seek/tell if you have either read or write permission, so no
483  * need for a permission check here.
484  */
485 
486  return obj_desc->offset;
487 }

References Assert(), LargeObjectDesc::offset, and PointerIsValid.

Referenced by be_lo_tell(), and be_lo_tell64().

◆ inv_truncate()

void inv_truncate ( LargeObjectDesc obj_desc,
int64  len 
)

Definition at line 780 of file inv_api.c.

781 {
782  int32 pageno = (int32) (len / LOBLKSIZE);
783  int32 off;
784  ScanKeyData skey[2];
785  SysScanDesc sd;
786  HeapTuple oldtuple;
787  Form_pg_largeobject olddata;
788  union
789  {
790  bytea hdr;
791  /* this is to make the union big enough for a LO data chunk: */
792  char data[LOBLKSIZE + VARHDRSZ];
793  /* ensure union is aligned well enough: */
794  int32 align_it;
795  } workbuf;
796  char *workb = VARDATA(&workbuf.hdr);
797  HeapTuple newtup;
798  Datum values[Natts_pg_largeobject];
799  bool nulls[Natts_pg_largeobject];
800  bool replace[Natts_pg_largeobject];
801  CatalogIndexState indstate;
802 
803  Assert(PointerIsValid(obj_desc));
804 
805  /* enforce writability because snapshot is probably wrong otherwise */
806  if ((obj_desc->flags & IFS_WRLOCK) == 0)
807  ereport(ERROR,
808  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
809  errmsg("permission denied for large object %u",
810  obj_desc->id)));
811 
812  /*
813  * use errmsg_internal here because we don't want to expose INT64_FORMAT
814  * in translatable strings; doing better is not worth the trouble
815  */
816  if (len < 0 || len > MAX_LARGE_OBJECT_SIZE)
817  ereport(ERROR,
818  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
819  errmsg_internal("invalid large object truncation target: " INT64_FORMAT,
820  len)));
821 
823 
824  indstate = CatalogOpenIndexes(lo_heap_r);
825 
826  /*
827  * Set up to find all pages with desired loid and pageno >= target
828  */
829  ScanKeyInit(&skey[0],
830  Anum_pg_largeobject_loid,
831  BTEqualStrategyNumber, F_OIDEQ,
832  ObjectIdGetDatum(obj_desc->id));
833 
834  ScanKeyInit(&skey[1],
835  Anum_pg_largeobject_pageno,
837  Int32GetDatum(pageno));
838 
840  obj_desc->snapshot, 2, skey);
841 
842  /*
843  * If possible, get the page the truncation point is in. The truncation
844  * point may be beyond the end of the LO or in a hole.
845  */
846  olddata = NULL;
847  if ((oldtuple = systable_getnext_ordered(sd, ForwardScanDirection)) != NULL)
848  {
849  if (HeapTupleHasNulls(oldtuple)) /* paranoia */
850  elog(ERROR, "null field found in pg_largeobject");
851  olddata = (Form_pg_largeobject) GETSTRUCT(oldtuple);
852  Assert(olddata->pageno >= pageno);
853  }
854 
855  /*
856  * If we found the page of the truncation point we need to truncate the
857  * data in it. Otherwise if we're in a hole, we need to create a page to
858  * mark the end of data.
859  */
860  if (olddata != NULL && olddata->pageno == pageno)
861  {
862  /* First, load old data into workbuf */
863  bytea *datafield;
864  int pagelen;
865  bool pfreeit;
866 
867  getdatafield(olddata, &datafield, &pagelen, &pfreeit);
868  memcpy(workb, VARDATA(datafield), pagelen);
869  if (pfreeit)
870  pfree(datafield);
871 
872  /*
873  * Fill any hole
874  */
875  off = len % LOBLKSIZE;
876  if (off > pagelen)
877  MemSet(workb + pagelen, 0, off - pagelen);
878 
879  /* compute length of new page */
880  SET_VARSIZE(&workbuf.hdr, off + VARHDRSZ);
881 
882  /*
883  * Form and insert updated tuple
884  */
885  memset(values, 0, sizeof(values));
886  memset(nulls, false, sizeof(nulls));
887  memset(replace, false, sizeof(replace));
888  values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
889  replace[Anum_pg_largeobject_data - 1] = true;
890  newtup = heap_modify_tuple(oldtuple, RelationGetDescr(lo_heap_r),
891  values, nulls, replace);
892  CatalogTupleUpdateWithInfo(lo_heap_r, &newtup->t_self, newtup,
893  indstate);
894  heap_freetuple(newtup);
895  }
896  else
897  {
898  /*
899  * If the first page we found was after the truncation point, we're in
900  * a hole that we'll fill, but we need to delete the later page
901  * because the loop below won't visit it again.
902  */
903  if (olddata != NULL)
904  {
905  Assert(olddata->pageno > pageno);
906  CatalogTupleDelete(lo_heap_r, &oldtuple->t_self);
907  }
908 
909  /*
910  * Write a brand new page.
911  *
912  * Fill the hole up to the truncation point
913  */
914  off = len % LOBLKSIZE;
915  if (off > 0)
916  MemSet(workb, 0, off);
917 
918  /* compute length of new page */
919  SET_VARSIZE(&workbuf.hdr, off + VARHDRSZ);
920 
921  /*
922  * Form and insert new tuple
923  */
924  memset(values, 0, sizeof(values));
925  memset(nulls, false, sizeof(nulls));
926  values[Anum_pg_largeobject_loid - 1] = ObjectIdGetDatum(obj_desc->id);
927  values[Anum_pg_largeobject_pageno - 1] = Int32GetDatum(pageno);
928  values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
929  newtup = heap_form_tuple(lo_heap_r->rd_att, values, nulls);
930  CatalogTupleInsertWithInfo(lo_heap_r, newtup, indstate);
931  heap_freetuple(newtup);
932  }
933 
934  /*
935  * Delete any pages after the truncation point. If the initial search
936  * didn't find a page, then of course there's nothing more to do.
937  */
938  if (olddata != NULL)
939  {
940  while ((oldtuple = systable_getnext_ordered(sd, ForwardScanDirection)) != NULL)
941  {
942  CatalogTupleDelete(lo_heap_r, &oldtuple->t_self);
943  }
944  }
945 
947 
948  CatalogCloseIndexes(indstate);
949 
950  /*
951  * Advance command counter so that tuple updates will be seen by later
952  * large-object operations in this transaction.
953  */
955 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define VARHDRSZ
Definition: c.h:681
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1108
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1201
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1426
void CatalogTupleInsertWithInfo(Relation heapRel, HeapTuple tup, CatalogIndexState indstate)
Definition: indexing.c:256
void CatalogCloseIndexes(CatalogIndexState indstate)
Definition: indexing.c:61
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition: indexing.c:43
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:365
void CatalogTupleUpdateWithInfo(Relation heapRel, ItemPointer otid, HeapTuple tup, CatalogIndexState indstate)
Definition: indexing.c:337
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
#define RelationGetDescr(relation)
Definition: rel.h:530
ItemPointerData t_self
Definition: htup.h:65
TupleDesc rd_att
Definition: rel.h:112
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References Assert(), BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, CatalogCloseIndexes(), CatalogOpenIndexes(), CatalogTupleDelete(), CatalogTupleInsertWithInfo(), CatalogTupleUpdateWithInfo(), CommandCounterIncrement(), data, elog(), ereport, errcode(), errmsg(), errmsg_internal(), ERROR, LargeObjectDesc::flags, ForwardScanDirection, getdatafield(), GETSTRUCT, heap_form_tuple(), heap_freetuple(), heap_modify_tuple(), HeapTupleHasNulls, LargeObjectDesc::id, IFS_WRLOCK, Int32GetDatum(), INT64_FORMAT, len, lo_heap_r, lo_index_r, LOBLKSIZE, MAX_LARGE_OBJECT_SIZE, MemSet, ObjectIdGetDatum(), open_lo_relation(), pfree(), PointerGetDatum(), PointerIsValid, RelationData::rd_att, RelationGetDescr, ScanKeyInit(), SET_VARSIZE, LargeObjectDesc::snapshot, systable_beginscan_ordered(), systable_endscan_ordered(), systable_getnext_ordered(), HeapTupleData::t_self, values, VARDATA, and VARHDRSZ.

Referenced by lo_truncate_internal().

◆ inv_write()

int inv_write ( LargeObjectDesc obj_desc,
const char *  buf,
int  nbytes 
)

Definition at line 583 of file inv_api.c.

584 {
585  int nwritten = 0;
586  int n;
587  int off;
588  int len;
589  int32 pageno = (int32) (obj_desc->offset / LOBLKSIZE);
590  ScanKeyData skey[2];
591  SysScanDesc sd;
592  HeapTuple oldtuple;
593  Form_pg_largeobject olddata;
594  bool neednextpage;
595  bytea *datafield;
596  bool pfreeit;
597  union
598  {
599  bytea hdr;
600  /* this is to make the union big enough for a LO data chunk: */
601  char data[LOBLKSIZE + VARHDRSZ];
602  /* ensure union is aligned well enough: */
603  int32 align_it;
604  } workbuf;
605  char *workb = VARDATA(&workbuf.hdr);
606  HeapTuple newtup;
607  Datum values[Natts_pg_largeobject];
608  bool nulls[Natts_pg_largeobject];
609  bool replace[Natts_pg_largeobject];
610  CatalogIndexState indstate;
611 
612  Assert(PointerIsValid(obj_desc));
613  Assert(buf != NULL);
614 
615  /* enforce writability because snapshot is probably wrong otherwise */
616  if ((obj_desc->flags & IFS_WRLOCK) == 0)
617  ereport(ERROR,
618  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
619  errmsg("permission denied for large object %u",
620  obj_desc->id)));
621 
622  if (nbytes <= 0)
623  return 0;
624 
625  /* this addition can't overflow because nbytes is only int32 */
626  if ((nbytes + obj_desc->offset) > MAX_LARGE_OBJECT_SIZE)
627  ereport(ERROR,
628  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
629  errmsg("invalid large object write request size: %d",
630  nbytes)));
631 
633 
634  indstate = CatalogOpenIndexes(lo_heap_r);
635 
636  ScanKeyInit(&skey[0],
637  Anum_pg_largeobject_loid,
638  BTEqualStrategyNumber, F_OIDEQ,
639  ObjectIdGetDatum(obj_desc->id));
640 
641  ScanKeyInit(&skey[1],
642  Anum_pg_largeobject_pageno,
644  Int32GetDatum(pageno));
645 
647  obj_desc->snapshot, 2, skey);
648 
649  oldtuple = NULL;
650  olddata = NULL;
651  neednextpage = true;
652 
653  while (nwritten < nbytes)
654  {
655  /*
656  * If possible, get next pre-existing page of the LO. We expect the
657  * indexscan will deliver these in order --- but there may be holes.
658  */
659  if (neednextpage)
660  {
661  if ((oldtuple = systable_getnext_ordered(sd, ForwardScanDirection)) != NULL)
662  {
663  if (HeapTupleHasNulls(oldtuple)) /* paranoia */
664  elog(ERROR, "null field found in pg_largeobject");
665  olddata = (Form_pg_largeobject) GETSTRUCT(oldtuple);
666  Assert(olddata->pageno >= pageno);
667  }
668  neednextpage = false;
669  }
670 
671  /*
672  * If we have a pre-existing page, see if it is the page we want to
673  * write, or a later one.
674  */
675  if (olddata != NULL && olddata->pageno == pageno)
676  {
677  /*
678  * Update an existing page with fresh data.
679  *
680  * First, load old data into workbuf
681  */
682  getdatafield(olddata, &datafield, &len, &pfreeit);
683  memcpy(workb, VARDATA(datafield), len);
684  if (pfreeit)
685  pfree(datafield);
686 
687  /*
688  * Fill any hole
689  */
690  off = (int) (obj_desc->offset % LOBLKSIZE);
691  if (off > len)
692  MemSet(workb + len, 0, off - len);
693 
694  /*
695  * Insert appropriate portion of new data
696  */
697  n = LOBLKSIZE - off;
698  n = (n <= (nbytes - nwritten)) ? n : (nbytes - nwritten);
699  memcpy(workb + off, buf + nwritten, n);
700  nwritten += n;
701  obj_desc->offset += n;
702  off += n;
703  /* compute valid length of new page */
704  len = (len >= off) ? len : off;
705  SET_VARSIZE(&workbuf.hdr, len + VARHDRSZ);
706 
707  /*
708  * Form and insert updated tuple
709  */
710  memset(values, 0, sizeof(values));
711  memset(nulls, false, sizeof(nulls));
712  memset(replace, false, sizeof(replace));
713  values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
714  replace[Anum_pg_largeobject_data - 1] = true;
715  newtup = heap_modify_tuple(oldtuple, RelationGetDescr(lo_heap_r),
716  values, nulls, replace);
717  CatalogTupleUpdateWithInfo(lo_heap_r, &newtup->t_self, newtup,
718  indstate);
719  heap_freetuple(newtup);
720 
721  /*
722  * We're done with this old page.
723  */
724  oldtuple = NULL;
725  olddata = NULL;
726  neednextpage = true;
727  }
728  else
729  {
730  /*
731  * Write a brand new page.
732  *
733  * First, fill any hole
734  */
735  off = (int) (obj_desc->offset % LOBLKSIZE);
736  if (off > 0)
737  MemSet(workb, 0, off);
738 
739  /*
740  * Insert appropriate portion of new data
741  */
742  n = LOBLKSIZE - off;
743  n = (n <= (nbytes - nwritten)) ? n : (nbytes - nwritten);
744  memcpy(workb + off, buf + nwritten, n);
745  nwritten += n;
746  obj_desc->offset += n;
747  /* compute valid length of new page */
748  len = off + n;
749  SET_VARSIZE(&workbuf.hdr, len + VARHDRSZ);
750 
751  /*
752  * Form and insert updated tuple
753  */
754  memset(values, 0, sizeof(values));
755  memset(nulls, false, sizeof(nulls));
756  values[Anum_pg_largeobject_loid - 1] = ObjectIdGetDatum(obj_desc->id);
757  values[Anum_pg_largeobject_pageno - 1] = Int32GetDatum(pageno);
758  values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
759  newtup = heap_form_tuple(lo_heap_r->rd_att, values, nulls);
760  CatalogTupleInsertWithInfo(lo_heap_r, newtup, indstate);
761  heap_freetuple(newtup);
762  }
763  pageno++;
764  }
765 
767 
768  CatalogCloseIndexes(indstate);
769 
770  /*
771  * Advance command counter so that my tuple updates will be seen by later
772  * large-object operations in this transaction.
773  */
775 
776  return nwritten;
777 }

References Assert(), BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, buf, CatalogCloseIndexes(), CatalogOpenIndexes(), CatalogTupleInsertWithInfo(), CatalogTupleUpdateWithInfo(), CommandCounterIncrement(), data, elog(), ereport, errcode(), errmsg(), ERROR, LargeObjectDesc::flags, ForwardScanDirection, getdatafield(), GETSTRUCT, heap_form_tuple(), heap_freetuple(), heap_modify_tuple(), HeapTupleHasNulls, LargeObjectDesc::id, IFS_WRLOCK, Int32GetDatum(), len, lo_heap_r, lo_index_r, LOBLKSIZE, MAX_LARGE_OBJECT_SIZE, MemSet, ObjectIdGetDatum(), LargeObjectDesc::offset, open_lo_relation(), pfree(), PointerGetDatum(), PointerIsValid, RelationData::rd_att, RelationGetDescr, ScanKeyInit(), SET_VARSIZE, LargeObjectDesc::snapshot, systable_beginscan_ordered(), systable_endscan_ordered(), systable_getnext_ordered(), HeapTupleData::t_self, values, VARDATA, and VARHDRSZ.

Referenced by be_lo_from_bytea(), be_lo_put(), lo_import_internal(), and lo_write().

Variable Documentation

◆ lo_compat_privileges

PGDLLIMPORT bool lo_compat_privileges
extern

Definition at line 58 of file inv_api.c.

Referenced by be_lo_put(), be_lo_unlink(), check_object_ownership(), and inv_open().