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 97 of file inv_api.c.

98{
99 if (lo_heap_r || lo_index_r)
100 {
101 /*
102 * Only bother to close if committing; else abort cleanup will handle
103 * it
104 */
105 if (isCommit)
106 {
107 ResourceOwner currentOwner;
108
109 currentOwner = CurrentResourceOwner;
111
112 if (lo_index_r)
114 if (lo_heap_r)
116
117 CurrentResourceOwner = currentOwner;
118 }
119 lo_heap_r = NULL;
120 lo_index_r = NULL;
121 }
122}
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
static Relation lo_index_r
Definition: inv_api.c:66
static Relation lo_heap_r
Definition: inv_api.c:65
#define NoLock
Definition: lockdefs.h:34
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:175
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
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 299 of file inv_api.c.

300{
301 Assert(obj_desc);
302 pfree(obj_desc);
303}
Assert(PointerIsAligned(start, uint64))
void pfree(void *pointer)
Definition: mcxt.c:1594

References Assert(), and pfree().

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 173 of file inv_api.c.

174{
175 Oid lobjId_new;
176
177 /*
178 * Create a new largeobject with empty data pages
179 */
180 lobjId_new = LargeObjectCreate(lobjId);
181
182 /*
183 * dependency on the owner of largeobject
184 *
185 * Note that LO dependencies are recorded using classId
186 * LargeObjectRelationId for backwards-compatibility reasons. Using
187 * LargeObjectMetadataRelationId instead would simplify matters for the
188 * backend, but it'd complicate pg_dump and possibly break other clients.
189 */
190 recordDependencyOnOwner(LargeObjectRelationId,
191 lobjId_new, GetUserId());
192
193 /* Post creation hook for new large object */
194 InvokeObjectPostCreateHook(LargeObjectRelationId, lobjId_new, 0);
195
196 /*
197 * Advance command counter to make new tuple visible to later operations.
198 */
200
201 return lobjId_new;
202}
Oid GetUserId(void)
Definition: miscinit.c:469
#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:168
unsigned int Oid
Definition: postgres_ext.h:32
void CommandCounterIncrement(void)
Definition: xact.c:1101

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 311 of file inv_api.c.

312{
313 ObjectAddress object;
314
315 /*
316 * Delete any comments and dependencies on the large object
317 */
318 object.classId = LargeObjectRelationId;
319 object.objectId = lobjId;
320 object.objectSubId = 0;
321 performDeletion(&object, DROP_CASCADE, 0);
322
323 /*
324 * Advance command counter so that tuple removal will be seen by later
325 * large-object operations in this transaction.
326 */
328
329 /* For historical reasons, we always return 1 on success. */
330 return 1;
331}
void performDeletion(const ObjectAddress *object, DropBehavior behavior, int flags)
Definition: dependency.c:274
@ DROP_CASCADE
Definition: parsenodes.h:2399

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 215 of file inv_api.c.

216{
217 LargeObjectDesc *retval;
218 Snapshot snapshot = NULL;
219 int descflags = 0;
220
221 /*
222 * Historically, no difference is made between (INV_WRITE) and (INV_WRITE
223 * | INV_READ), the caller being allowed to read the large object
224 * descriptor in either case.
225 */
226 if (flags & INV_WRITE)
227 descflags |= IFS_WRLOCK | IFS_RDLOCK;
228 if (flags & INV_READ)
229 descflags |= IFS_RDLOCK;
230
231 if (descflags == 0)
233 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
234 errmsg("invalid flags for opening a large object: %d",
235 flags)));
236
237 /* Get snapshot. If write is requested, use an instantaneous snapshot. */
238 if (descflags & IFS_WRLOCK)
239 snapshot = NULL;
240 else
241 snapshot = GetActiveSnapshot();
242
243 /* Can't use LargeObjectExists here because we need to specify snapshot */
244 if (!LargeObjectExistsWithSnapshot(lobjId, snapshot))
246 (errcode(ERRCODE_UNDEFINED_OBJECT),
247 errmsg("large object %u does not exist", lobjId)));
248
249 /* Apply permission checks, again specifying snapshot */
250 if ((descflags & IFS_RDLOCK) != 0)
251 {
254 GetUserId(),
256 snapshot) != ACLCHECK_OK)
258 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
259 errmsg("permission denied for large object %u",
260 lobjId)));
261 }
262 if ((descflags & IFS_WRLOCK) != 0)
263 {
266 GetUserId(),
268 snapshot) != ACLCHECK_OK)
270 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
271 errmsg("permission denied for large object %u",
272 lobjId)));
273 }
274
275 /* OK to create a descriptor */
276 retval = (LargeObjectDesc *) MemoryContextAlloc(mcxt,
277 sizeof(LargeObjectDesc));
278 retval->id = lobjId;
279 retval->offset = 0;
280 retval->flags = descflags;
281
282 /* caller sets if needed, not used by the functions in this file */
284
285 /*
286 * The snapshot (if any) is just the currently active snapshot. The
287 * caller will replace it with a longer-lived copy if needed.
288 */
289 retval->snapshot = snapshot;
290
291 return retval;
292}
@ ACLCHECK_OK
Definition: acl.h:183
AclResult pg_largeobject_aclcheck_snapshot(Oid lobj_oid, Oid roleid, AclMode mode, Snapshot snapshot)
Definition: aclchk.c:4074
#define InvalidSubTransactionId
Definition: c.h:666
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
bool lo_compat_privileges
Definition: inv_api.c:56
#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:1229
#define ACL_UPDATE
Definition: parsenodes.h:78
#define ACL_SELECT
Definition: parsenodes.h:77
bool LargeObjectExistsWithSnapshot(Oid loid, Snapshot snapshot)
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:800
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, LargeObjectExistsWithSnapshot(), lo_compat_privileges, MemoryContextAlloc(), 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 450 of file inv_api.c.

451{
452 int nread = 0;
453 int64 n;
454 int64 off;
455 int len;
456 int32 pageno = (int32) (obj_desc->offset / LOBLKSIZE);
457 uint64 pageoff;
458 ScanKeyData skey[2];
459 SysScanDesc sd;
460 HeapTuple tuple;
461
462 Assert(obj_desc);
463 Assert(buf != NULL);
464
465 if ((obj_desc->flags & IFS_RDLOCK) == 0)
467 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
468 errmsg("permission denied for large object %u",
469 obj_desc->id)));
470
471 if (nbytes <= 0)
472 return 0;
473
475
476 ScanKeyInit(&skey[0],
477 Anum_pg_largeobject_loid,
478 BTEqualStrategyNumber, F_OIDEQ,
479 ObjectIdGetDatum(obj_desc->id));
480
481 ScanKeyInit(&skey[1],
482 Anum_pg_largeobject_pageno,
484 Int32GetDatum(pageno));
485
487 obj_desc->snapshot, 2, skey);
488
489 while ((tuple = systable_getnext_ordered(sd, ForwardScanDirection)) != NULL)
490 {
492 bytea *datafield;
493 bool pfreeit;
494
495 if (HeapTupleHasNulls(tuple)) /* paranoia */
496 elog(ERROR, "null field found in pg_largeobject");
498
499 /*
500 * We expect the indexscan will deliver pages in order. However,
501 * there may be missing pages if the LO contains unwritten "holes". We
502 * want missing sections to read out as zeroes.
503 */
504 pageoff = ((uint64) data->pageno) * LOBLKSIZE;
505 if (pageoff > obj_desc->offset)
506 {
507 n = pageoff - obj_desc->offset;
508 n = (n <= (nbytes - nread)) ? n : (nbytes - nread);
509 MemSet(buf + nread, 0, n);
510 nread += n;
511 obj_desc->offset += n;
512 }
513
514 if (nread < nbytes)
515 {
516 Assert(obj_desc->offset >= pageoff);
517 off = (int) (obj_desc->offset - pageoff);
518 Assert(off >= 0 && off < LOBLKSIZE);
519
520 getdatafield(data, &datafield, &len, &pfreeit);
521 if (len > off)
522 {
523 n = len - off;
524 n = (n <= (nbytes - nread)) ? n : (nbytes - nread);
525 memcpy(buf + nread, VARDATA(datafield) + off, n);
526 nread += n;
527 obj_desc->offset += n;
528 }
529 if (pfreeit)
530 pfree(datafield);
531 }
532
533 if (nread >= nbytes)
534 break;
535 }
536
538
539 return nread;
540}
int64_t int64
Definition: c.h:538
int32_t int32
Definition: c.h:537
uint64_t uint64
Definition: c.h:542
#define MemSet(start, val, len)
Definition: c.h:1022
#define elog(elevel,...)
Definition: elog.h:226
SysScanDesc systable_beginscan_ordered(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:650
void systable_endscan_ordered(SysScanDesc sysscan)
Definition: genam.c:757
HeapTuple systable_getnext_ordered(SysScanDesc sysscan, ScanDirection direction)
Definition: genam.c:732
static bool HeapTupleHasNulls(const HeapTupleData *tuple)
Definition: htup_details.h:738
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
static void getdatafield(Form_pg_largeobject tuple, bytea **pdatafield, int *plen, bool *pfreeit)
Definition: inv_api.c:131
static void open_lo_relation(void)
Definition: inv_api.c:73
#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:72
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:222
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:695
static char * VARDATA(const void *PTR)
Definition: varatt.h:305

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(), 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 388 of file inv_api.c.

389{
390 int64 newoffset;
391
392 Assert(obj_desc);
393
394 /*
395 * We allow seek/tell if you have either read or write permission, so no
396 * need for a permission check here.
397 */
398
399 /*
400 * Note: overflow in the additions is possible, but since we will reject
401 * negative results, we don't need any extra test for that.
402 */
403 switch (whence)
404 {
405 case SEEK_SET:
406 newoffset = offset;
407 break;
408 case SEEK_CUR:
409 newoffset = obj_desc->offset + offset;
410 break;
411 case SEEK_END:
412 newoffset = inv_getsize(obj_desc) + offset;
413 break;
414 default:
416 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
417 errmsg("invalid whence setting: %d", whence)));
418 newoffset = 0; /* keep compiler quiet */
419 break;
420 }
421
422 /*
423 * use errmsg_internal here because we don't want to expose INT64_FORMAT
424 * in translatable strings; doing better is not worth the trouble
425 */
426 if (newoffset < 0 || newoffset > MAX_LARGE_OBJECT_SIZE)
428 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
429 errmsg_internal("invalid large object seek target: " INT64_FORMAT,
430 newoffset)));
431
432 obj_desc->offset = newoffset;
433 return newoffset;
434}
#define INT64_FORMAT
Definition: c.h:559
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1170
static uint64 inv_getsize(LargeObjectDesc *obj_desc)
Definition: inv_api.c:340
#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, and LargeObjectDesc::offset.

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 437 of file inv_api.c.

438{
439 Assert(obj_desc);
440
441 /*
442 * We allow seek/tell if you have either read or write permission, so no
443 * need for a permission check here.
444 */
445
446 return obj_desc->offset;
447}

References Assert(), and LargeObjectDesc::offset.

Referenced by be_lo_tell(), and be_lo_tell64().

◆ inv_truncate()

void inv_truncate ( LargeObjectDesc obj_desc,
int64  len 
)

Definition at line 738 of file inv_api.c.

739{
740 int32 pageno = (int32) (len / LOBLKSIZE);
741 int32 off;
742 ScanKeyData skey[2];
743 SysScanDesc sd;
744 HeapTuple oldtuple;
745 Form_pg_largeobject olddata;
746 union
747 {
748 alignas(int32) bytea hdr;
749 /* this is to make the union big enough for a LO data chunk: */
750 char data[LOBLKSIZE + VARHDRSZ];
751 } workbuf = {0};
752 char *workb = VARDATA(&workbuf.hdr);
753 HeapTuple newtup;
754 Datum values[Natts_pg_largeobject];
755 bool nulls[Natts_pg_largeobject];
756 bool replace[Natts_pg_largeobject];
757 CatalogIndexState indstate;
758
759 Assert(obj_desc);
760
761 /* enforce writability because snapshot is probably wrong otherwise */
762 if ((obj_desc->flags & IFS_WRLOCK) == 0)
764 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
765 errmsg("permission denied for large object %u",
766 obj_desc->id)));
767
768 /*
769 * use errmsg_internal here because we don't want to expose INT64_FORMAT
770 * in translatable strings; doing better is not worth the trouble
771 */
772 if (len < 0 || len > MAX_LARGE_OBJECT_SIZE)
774 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
775 errmsg_internal("invalid large object truncation target: " INT64_FORMAT,
776 len)));
777
779
780 indstate = CatalogOpenIndexes(lo_heap_r);
781
782 /*
783 * Set up to find all pages with desired loid and pageno >= target
784 */
785 ScanKeyInit(&skey[0],
786 Anum_pg_largeobject_loid,
787 BTEqualStrategyNumber, F_OIDEQ,
788 ObjectIdGetDatum(obj_desc->id));
789
790 ScanKeyInit(&skey[1],
791 Anum_pg_largeobject_pageno,
793 Int32GetDatum(pageno));
794
796 obj_desc->snapshot, 2, skey);
797
798 /*
799 * If possible, get the page the truncation point is in. The truncation
800 * point may be beyond the end of the LO or in a hole.
801 */
802 olddata = NULL;
803 if ((oldtuple = systable_getnext_ordered(sd, ForwardScanDirection)) != NULL)
804 {
805 if (HeapTupleHasNulls(oldtuple)) /* paranoia */
806 elog(ERROR, "null field found in pg_largeobject");
807 olddata = (Form_pg_largeobject) GETSTRUCT(oldtuple);
808 Assert(olddata->pageno >= pageno);
809 }
810
811 /*
812 * If we found the page of the truncation point we need to truncate the
813 * data in it. Otherwise if we're in a hole, we need to create a page to
814 * mark the end of data.
815 */
816 if (olddata != NULL && olddata->pageno == pageno)
817 {
818 /* First, load old data into workbuf */
819 bytea *datafield;
820 int pagelen;
821 bool pfreeit;
822
823 getdatafield(olddata, &datafield, &pagelen, &pfreeit);
824 memcpy(workb, VARDATA(datafield), pagelen);
825 if (pfreeit)
826 pfree(datafield);
827
828 /*
829 * Fill any hole
830 */
831 off = len % LOBLKSIZE;
832 if (off > pagelen)
833 MemSet(workb + pagelen, 0, off - pagelen);
834
835 /* compute length of new page */
836 SET_VARSIZE(&workbuf.hdr, off + VARHDRSZ);
837
838 /*
839 * Form and insert updated tuple
840 */
841 memset(values, 0, sizeof(values));
842 memset(nulls, false, sizeof(nulls));
843 memset(replace, false, sizeof(replace));
844 values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
845 replace[Anum_pg_largeobject_data - 1] = true;
846 newtup = heap_modify_tuple(oldtuple, RelationGetDescr(lo_heap_r),
847 values, nulls, replace);
849 indstate);
850 heap_freetuple(newtup);
851 }
852 else
853 {
854 /*
855 * If the first page we found was after the truncation point, we're in
856 * a hole that we'll fill, but we need to delete the later page
857 * because the loop below won't visit it again.
858 */
859 if (olddata != NULL)
860 {
861 Assert(olddata->pageno > pageno);
863 }
864
865 /*
866 * Write a brand new page.
867 *
868 * Fill the hole up to the truncation point
869 */
870 off = len % LOBLKSIZE;
871 if (off > 0)
872 MemSet(workb, 0, off);
873
874 /* compute length of new page */
875 SET_VARSIZE(&workbuf.hdr, off + VARHDRSZ);
876
877 /*
878 * Form and insert new tuple
879 */
880 memset(values, 0, sizeof(values));
881 memset(nulls, false, sizeof(nulls));
882 values[Anum_pg_largeobject_loid - 1] = ObjectIdGetDatum(obj_desc->id);
883 values[Anum_pg_largeobject_pageno - 1] = Int32GetDatum(pageno);
884 values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
885 newtup = heap_form_tuple(lo_heap_r->rd_att, values, nulls);
886 CatalogTupleInsertWithInfo(lo_heap_r, newtup, indstate);
887 heap_freetuple(newtup);
888 }
889
890 /*
891 * Delete any pages after the truncation point. If the initial search
892 * didn't find a page, then of course there's nothing more to do.
893 */
894 if (olddata != NULL)
895 {
896 while ((oldtuple = systable_getnext_ordered(sd, ForwardScanDirection)) != NULL)
897 {
899 }
900 }
901
903
904 CatalogCloseIndexes(indstate);
905
906 /*
907 * Advance command counter so that tuple updates will be seen by later
908 * large-object operations in this transaction.
909 */
911}
static Datum values[MAXATTR]
Definition: bootstrap.c:153
#define VARHDRSZ
Definition: c.h:700
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition: heaptuple.c:1210
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
void CatalogTupleUpdateWithInfo(Relation heapRel, const ItemPointerData *otid, HeapTuple tup, CatalogIndexState indstate)
Definition: indexing.c:337
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, const ItemPointerData *tid)
Definition: indexing.c:365
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
uint64_t Datum
Definition: postgres.h:70
#define RelationGetDescr(relation)
Definition: rel.h:541
ItemPointerData t_self
Definition: htup.h:65
TupleDesc rd_att
Definition: rel.h:112
static void SET_VARSIZE(void *PTR, Size len)
Definition: varatt.h:432

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(), 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 543 of file inv_api.c.

544{
545 int nwritten = 0;
546 int n;
547 int off;
548 int len;
549 int32 pageno = (int32) (obj_desc->offset / LOBLKSIZE);
550 ScanKeyData skey[2];
551 SysScanDesc sd;
552 HeapTuple oldtuple;
553 Form_pg_largeobject olddata;
554 bool neednextpage;
555 bytea *datafield;
556 bool pfreeit;
557 union
558 {
559 alignas(int32) bytea hdr;
560 /* this is to make the union big enough for a LO data chunk: */
561 char data[LOBLKSIZE + VARHDRSZ];
562 } workbuf = {0};
563 char *workb = VARDATA(&workbuf.hdr);
564 HeapTuple newtup;
565 Datum values[Natts_pg_largeobject];
566 bool nulls[Natts_pg_largeobject];
567 bool replace[Natts_pg_largeobject];
568 CatalogIndexState indstate;
569
570 Assert(obj_desc);
571 Assert(buf != NULL);
572
573 /* enforce writability because snapshot is probably wrong otherwise */
574 if ((obj_desc->flags & IFS_WRLOCK) == 0)
576 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
577 errmsg("permission denied for large object %u",
578 obj_desc->id)));
579
580 if (nbytes <= 0)
581 return 0;
582
583 /* this addition can't overflow because nbytes is only int32 */
584 if ((nbytes + obj_desc->offset) > MAX_LARGE_OBJECT_SIZE)
586 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
587 errmsg("invalid large object write request size: %d",
588 nbytes)));
589
591
592 indstate = CatalogOpenIndexes(lo_heap_r);
593
594 ScanKeyInit(&skey[0],
595 Anum_pg_largeobject_loid,
596 BTEqualStrategyNumber, F_OIDEQ,
597 ObjectIdGetDatum(obj_desc->id));
598
599 ScanKeyInit(&skey[1],
600 Anum_pg_largeobject_pageno,
602 Int32GetDatum(pageno));
603
605 obj_desc->snapshot, 2, skey);
606
607 oldtuple = NULL;
608 olddata = NULL;
609 neednextpage = true;
610
611 while (nwritten < nbytes)
612 {
613 /*
614 * If possible, get next pre-existing page of the LO. We expect the
615 * indexscan will deliver these in order --- but there may be holes.
616 */
617 if (neednextpage)
618 {
619 if ((oldtuple = systable_getnext_ordered(sd, ForwardScanDirection)) != NULL)
620 {
621 if (HeapTupleHasNulls(oldtuple)) /* paranoia */
622 elog(ERROR, "null field found in pg_largeobject");
623 olddata = (Form_pg_largeobject) GETSTRUCT(oldtuple);
624 Assert(olddata->pageno >= pageno);
625 }
626 neednextpage = false;
627 }
628
629 /*
630 * If we have a pre-existing page, see if it is the page we want to
631 * write, or a later one.
632 */
633 if (olddata != NULL && olddata->pageno == pageno)
634 {
635 /*
636 * Update an existing page with fresh data.
637 *
638 * First, load old data into workbuf
639 */
640 getdatafield(olddata, &datafield, &len, &pfreeit);
641 memcpy(workb, VARDATA(datafield), len);
642 if (pfreeit)
643 pfree(datafield);
644
645 /*
646 * Fill any hole
647 */
648 off = (int) (obj_desc->offset % LOBLKSIZE);
649 if (off > len)
650 MemSet(workb + len, 0, off - len);
651
652 /*
653 * Insert appropriate portion of new data
654 */
655 n = LOBLKSIZE - off;
656 n = (n <= (nbytes - nwritten)) ? n : (nbytes - nwritten);
657 memcpy(workb + off, buf + nwritten, n);
658 nwritten += n;
659 obj_desc->offset += n;
660 off += n;
661 /* compute valid length of new page */
662 len = (len >= off) ? len : off;
663 SET_VARSIZE(&workbuf.hdr, len + VARHDRSZ);
664
665 /*
666 * Form and insert updated tuple
667 */
668 memset(values, 0, sizeof(values));
669 memset(nulls, false, sizeof(nulls));
670 memset(replace, false, sizeof(replace));
671 values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
672 replace[Anum_pg_largeobject_data - 1] = true;
673 newtup = heap_modify_tuple(oldtuple, RelationGetDescr(lo_heap_r),
674 values, nulls, replace);
676 indstate);
677 heap_freetuple(newtup);
678
679 /*
680 * We're done with this old page.
681 */
682 oldtuple = NULL;
683 olddata = NULL;
684 neednextpage = true;
685 }
686 else
687 {
688 /*
689 * Write a brand new page.
690 *
691 * First, fill any hole
692 */
693 off = (int) (obj_desc->offset % LOBLKSIZE);
694 if (off > 0)
695 MemSet(workb, 0, off);
696
697 /*
698 * Insert appropriate portion of new data
699 */
700 n = LOBLKSIZE - off;
701 n = (n <= (nbytes - nwritten)) ? n : (nbytes - nwritten);
702 memcpy(workb + off, buf + nwritten, n);
703 nwritten += n;
704 obj_desc->offset += n;
705 /* compute valid length of new page */
706 len = off + n;
707 SET_VARSIZE(&workbuf.hdr, len + VARHDRSZ);
708
709 /*
710 * Form and insert updated tuple
711 */
712 memset(values, 0, sizeof(values));
713 memset(nulls, false, sizeof(nulls));
714 values[Anum_pg_largeobject_loid - 1] = ObjectIdGetDatum(obj_desc->id);
715 values[Anum_pg_largeobject_pageno - 1] = Int32GetDatum(pageno);
716 values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
717 newtup = heap_form_tuple(lo_heap_r->rd_att, values, nulls);
718 CatalogTupleInsertWithInfo(lo_heap_r, newtup, indstate);
719 heap_freetuple(newtup);
720 }
721 pageno++;
722 }
723
725
726 CatalogCloseIndexes(indstate);
727
728 /*
729 * Advance command counter so that my tuple updates will be seen by later
730 * large-object operations in this transaction.
731 */
733
734 return nwritten;
735}

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(), 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 56 of file inv_api.c.

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