1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* inv_api.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* routines for manipulating inversion fs large objects. This file
|
|
|
|
* contains the user-level large object application interface routines.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2006-04-26 02:34:57 +02:00
|
|
|
*
|
2007-06-12 21:46:24 +02:00
|
|
|
* Note: we access pg_largeobject.data using its C struct declaration.
|
|
|
|
* This is safe because it immediately follows pageno which is an int4 field,
|
|
|
|
* and therefore the data field will always be 4-byte aligned, even if it
|
|
|
|
* is in the short 1-byte-header format. We have to detoast it since it's
|
|
|
|
* quite likely to be in compressed or short format. We also need to check
|
|
|
|
* for NULLs, since initdb will mark loid and pageno but not data as NOT NULL.
|
|
|
|
*
|
2006-04-26 02:34:57 +02:00
|
|
|
* Note: many of these routines leak memory in CurrentMemoryContext, as indeed
|
|
|
|
* does most of the backend code. We expect that CurrentMemoryContext will
|
|
|
|
* be a short-lived context. Data that must persist across function calls
|
|
|
|
* is kept either in CacheMemoryContext (the Relation structs) or in the
|
|
|
|
* memory context given to inv_open (for LargeObjectDesc structs).
|
|
|
|
*
|
|
|
|
*
|
2008-01-01 20:46:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2008-12-04 15:51:02 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/storage/large_object/inv_api.c,v 1.136 2008/12/04 14:51:02 alvherre Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2000-10-24 03:38:44 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "access/genam.h"
|
|
|
|
#include "access/heapam.h"
|
2001-02-10 03:31:31 +01:00
|
|
|
#include "access/tuptoaster.h"
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "access/xact.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "catalog/catalog.h"
|
2000-10-24 03:38:44 +02:00
|
|
|
#include "catalog/indexing.h"
|
|
|
|
#include "catalog/pg_largeobject.h"
|
2003-11-21 23:32:49 +01:00
|
|
|
#include "commands/comment.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "libpq/libpq-fs.h"
|
|
|
|
#include "storage/large_object.h"
|
2003-11-21 23:32:49 +01:00
|
|
|
#include "utils/fmgroids.h"
|
2008-06-19 02:46:06 +02:00
|
|
|
#include "utils/rel.h"
|
2004-07-28 16:23:31 +02:00
|
|
|
#include "utils/resowner.h"
|
2008-03-26 19:48:59 +01:00
|
|
|
#include "utils/snapmgr.h"
|
2008-03-26 22:10:39 +01:00
|
|
|
#include "utils/tqual.h"
|
2004-07-28 16:23:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All accesses to pg_largeobject and its index make use of a single Relation
|
|
|
|
* reference, so that we only need to open pg_relation once per transaction.
|
|
|
|
* To avoid problems when the first such reference occurs inside a
|
|
|
|
* subtransaction, we execute a slightly klugy maneuver to assign ownership of
|
|
|
|
* the Relation reference to TopTransactionResourceOwner.
|
|
|
|
*/
|
|
|
|
static Relation lo_heap_r = NULL;
|
|
|
|
static Relation lo_index_r = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open pg_largeobject and its index, if not already done in current xact
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
open_lo_relation(void)
|
|
|
|
{
|
|
|
|
ResourceOwner currentOwner;
|
|
|
|
|
|
|
|
if (lo_heap_r && lo_index_r)
|
|
|
|
return; /* already open in current xact */
|
|
|
|
|
|
|
|
/* Arrange for the top xact to own these relation references */
|
|
|
|
currentOwner = CurrentResourceOwner;
|
2004-07-31 02:45:57 +02:00
|
|
|
PG_TRY();
|
|
|
|
{
|
|
|
|
CurrentResourceOwner = TopTransactionResourceOwner;
|
2004-07-28 16:23:31 +02:00
|
|
|
|
2004-07-31 02:45:57 +02:00
|
|
|
/* Use RowExclusiveLock since we might either read or write */
|
|
|
|
if (lo_heap_r == NULL)
|
2005-04-14 22:03:27 +02:00
|
|
|
lo_heap_r = heap_open(LargeObjectRelationId, RowExclusiveLock);
|
2004-07-31 02:45:57 +02:00
|
|
|
if (lo_index_r == NULL)
|
2006-07-31 22:09:10 +02:00
|
|
|
lo_index_r = index_open(LargeObjectLOidPNIndexId, RowExclusiveLock);
|
2004-07-31 02:45:57 +02:00
|
|
|
}
|
|
|
|
PG_CATCH();
|
|
|
|
{
|
|
|
|
/* Ensure CurrentResourceOwner is restored on error */
|
|
|
|
CurrentResourceOwner = currentOwner;
|
|
|
|
PG_RE_THROW();
|
|
|
|
}
|
|
|
|
PG_END_TRY();
|
2004-07-28 16:23:31 +02:00
|
|
|
CurrentResourceOwner = currentOwner;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean up at main transaction end
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
close_lo_relation(bool isCommit)
|
|
|
|
{
|
|
|
|
if (lo_heap_r || lo_index_r)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Only bother to close if committing; else abort cleanup will handle
|
|
|
|
* it
|
2004-07-28 16:23:31 +02:00
|
|
|
*/
|
|
|
|
if (isCommit)
|
|
|
|
{
|
|
|
|
ResourceOwner currentOwner;
|
|
|
|
|
|
|
|
currentOwner = CurrentResourceOwner;
|
2004-07-31 02:45:57 +02:00
|
|
|
PG_TRY();
|
|
|
|
{
|
|
|
|
CurrentResourceOwner = TopTransactionResourceOwner;
|
|
|
|
|
|
|
|
if (lo_index_r)
|
2006-07-31 22:09:10 +02:00
|
|
|
index_close(lo_index_r, NoLock);
|
2004-07-31 02:45:57 +02:00
|
|
|
if (lo_heap_r)
|
|
|
|
heap_close(lo_heap_r, NoLock);
|
|
|
|
}
|
|
|
|
PG_CATCH();
|
|
|
|
{
|
|
|
|
/* Ensure CurrentResourceOwner is restored on error */
|
|
|
|
CurrentResourceOwner = currentOwner;
|
|
|
|
PG_RE_THROW();
|
|
|
|
}
|
|
|
|
PG_END_TRY();
|
2004-07-28 16:23:31 +02:00
|
|
|
CurrentResourceOwner = currentOwner;
|
|
|
|
}
|
|
|
|
lo_heap_r = NULL;
|
|
|
|
lo_index_r = NULL;
|
|
|
|
}
|
|
|
|
}
|
1998-07-21 06:17:30 +02:00
|
|
|
|
2000-10-21 17:55:29 +02:00
|
|
|
|
2005-06-13 04:26:53 +02:00
|
|
|
/*
|
|
|
|
* Same as pg_largeobject.c's LargeObjectExists(), except snapshot to
|
|
|
|
* read with can be specified.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
myLargeObjectExists(Oid loid, Snapshot snapshot)
|
|
|
|
{
|
|
|
|
bool retval = false;
|
|
|
|
Relation pg_largeobject;
|
|
|
|
ScanKeyData skey[1];
|
|
|
|
SysScanDesc sd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if we can find any tuples belonging to the specified LO
|
|
|
|
*/
|
|
|
|
ScanKeyInit(&skey[0],
|
|
|
|
Anum_pg_largeobject_loid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(loid));
|
|
|
|
|
|
|
|
pg_largeobject = heap_open(LargeObjectRelationId, AccessShareLock);
|
|
|
|
|
|
|
|
sd = systable_beginscan(pg_largeobject, LargeObjectLOidPNIndexId, true,
|
|
|
|
snapshot, 1, skey);
|
|
|
|
|
|
|
|
if (systable_getnext(sd) != NULL)
|
|
|
|
retval = true;
|
|
|
|
|
|
|
|
systable_endscan(sd);
|
|
|
|
|
|
|
|
heap_close(pg_largeobject, AccessShareLock);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
static int32
|
|
|
|
getbytealen(bytea *data)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Assert(!VARATT_IS_EXTENDED(data));
|
2000-10-24 03:38:44 +02:00
|
|
|
if (VARSIZE(data) < VARHDRSZ)
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(ERROR, "invalid VARSIZE(data)");
|
2000-10-24 03:38:44 +02:00
|
|
|
return (VARSIZE(data) - VARHDRSZ);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2004-07-28 16:23:31 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2005-06-13 04:26:53 +02:00
|
|
|
* inv_create -- create a new large object
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2005-06-13 04:26:53 +02:00
|
|
|
* Arguments:
|
|
|
|
* lobjId - OID to use for new large object, or InvalidOid to pick one
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2005-06-13 04:26:53 +02:00
|
|
|
* Returns:
|
|
|
|
* OID of new object
|
|
|
|
*
|
|
|
|
* If lobjId is not InvalidOid, then an error occurs if the OID is already
|
|
|
|
* in use.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2005-06-13 04:26:53 +02:00
|
|
|
Oid
|
|
|
|
inv_create(Oid lobjId)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-10-22 07:27:23 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Allocate an OID to be the LO's identifier, unless we were told what to
|
|
|
|
* use. We can use the index on pg_largeobject for checking OID
|
|
|
|
* uniqueness, even though it has additional columns besides OID.
|
2000-10-22 07:27:23 +02:00
|
|
|
*/
|
2005-06-13 04:26:53 +02:00
|
|
|
if (!OidIsValid(lobjId))
|
|
|
|
{
|
2005-08-12 03:36:05 +02:00
|
|
|
open_lo_relation();
|
|
|
|
|
2008-04-13 01:14:21 +02:00
|
|
|
lobjId = GetNewOidWithIndex(lo_heap_r, LargeObjectLOidPNIndexId,
|
|
|
|
Anum_pg_largeobject_loid);
|
2005-06-13 04:26:53 +02:00
|
|
|
}
|
2000-10-22 07:27:23 +02:00
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Create the LO by writing an empty first page for it in pg_largeobject
|
|
|
|
* (will fail if duplicate)
|
2000-10-22 07:27:23 +02:00
|
|
|
*/
|
2005-06-13 04:26:53 +02:00
|
|
|
LargeObjectCreate(lobjId);
|
2000-10-22 07:27:23 +02:00
|
|
|
|
|
|
|
/*
|
2005-06-13 04:26:53 +02:00
|
|
|
* Advance command counter to make new tuple visible to later operations.
|
2000-10-22 07:27:23 +02:00
|
|
|
*/
|
|
|
|
CommandCounterIncrement();
|
|
|
|
|
2005-06-13 04:26:53 +02:00
|
|
|
return lobjId;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
/*
|
|
|
|
* inv_open -- access an existing large object.
|
|
|
|
*
|
|
|
|
* Returns:
|
2006-04-26 02:34:57 +02:00
|
|
|
* Large object descriptor, appropriately filled in. The descriptor
|
|
|
|
* and subsidiary data are allocated in the specified memory context,
|
|
|
|
* which must be suitably long-lived for the caller's purposes.
|
2000-10-24 03:38:44 +02:00
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
LargeObjectDesc *
|
2006-04-26 02:34:57 +02:00
|
|
|
inv_open(Oid lobjId, int flags, MemoryContext mcxt)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
LargeObjectDesc *retval;
|
2000-10-22 07:27:23 +02:00
|
|
|
|
2006-04-26 02:34:57 +02:00
|
|
|
retval = (LargeObjectDesc *) MemoryContextAlloc(mcxt,
|
|
|
|
sizeof(LargeObjectDesc));
|
2000-10-22 07:27:23 +02:00
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
retval->id = lobjId;
|
2004-09-16 18:58:44 +02:00
|
|
|
retval->subid = GetCurrentSubTransactionId();
|
2000-10-24 03:38:44 +02:00
|
|
|
retval->offset = 0;
|
2000-10-22 07:27:23 +02:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
if (flags & INV_WRITE)
|
2005-06-13 04:26:53 +02:00
|
|
|
{
|
|
|
|
retval->snapshot = SnapshotNow;
|
1997-09-07 07:04:48 +02:00
|
|
|
retval->flags = IFS_WRLOCK | IFS_RDLOCK;
|
2005-06-13 04:26:53 +02:00
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
else if (flags & INV_READ)
|
2005-06-13 04:26:53 +02:00
|
|
|
{
|
2008-12-04 15:51:02 +01:00
|
|
|
/*
|
|
|
|
* We must register the snapshot in TopTransaction's resowner,
|
|
|
|
* because it must stay alive until the LO is closed rather than until
|
|
|
|
* the current portal shuts down.
|
|
|
|
*/
|
|
|
|
retval->snapshot = RegisterSnapshotOnOwner(GetActiveSnapshot(),
|
|
|
|
TopTransactionResourceOwner);
|
1997-09-07 07:04:48 +02:00
|
|
|
retval->flags = IFS_RDLOCK;
|
2005-06-13 04:26:53 +02:00
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
else
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(ERROR, "invalid flags: %d", flags);
|
2000-10-24 03:38:44 +02:00
|
|
|
|
2005-06-13 04:26:53 +02:00
|
|
|
/* Can't use LargeObjectExists here because it always uses SnapshotNow */
|
|
|
|
if (!myLargeObjectExists(lobjId, retval->snapshot))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("large object %u does not exist", lobjId)));
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return retval;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-04-26 02:34:57 +02:00
|
|
|
* Closes a large object descriptor previously made by inv_open(), and
|
|
|
|
* releases the long-term memory used by it.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
1997-09-08 23:56:23 +02:00
|
|
|
inv_close(LargeObjectDesc *obj_desc)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(PointerIsValid(obj_desc));
|
2008-12-04 15:51:02 +01:00
|
|
|
|
2005-06-13 04:26:53 +02:00
|
|
|
if (obj_desc->snapshot != SnapshotNow)
|
2008-12-04 15:51:02 +01:00
|
|
|
UnregisterSnapshotFromOwner(obj_desc->snapshot,
|
|
|
|
TopTransactionResourceOwner);
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
pfree(obj_desc);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-10-24 03:38:44 +02:00
|
|
|
* Destroys an existing large object (not to be confused with a descriptor!)
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* returns -1 if failed
|
|
|
|
*/
|
|
|
|
int
|
1999-12-10 04:56:14 +01:00
|
|
|
inv_drop(Oid lobjId)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-10-24 03:38:44 +02:00
|
|
|
LargeObjectDrop(lobjId);
|
2000-10-22 07:27:23 +02:00
|
|
|
|
2003-11-21 23:32:49 +01:00
|
|
|
/* Delete any comments on the large object */
|
2005-04-14 22:03:27 +02:00
|
|
|
DeleteComments(lobjId, LargeObjectRelationId, 0);
|
2003-11-21 23:32:49 +01:00
|
|
|
|
2000-10-22 07:27:23 +02:00
|
|
|
/*
|
2000-10-24 03:38:44 +02:00
|
|
|
* Advance command counter so that tuple removal will be seen by later
|
|
|
|
* large-object operations in this transaction.
|
2000-10-22 07:27:23 +02:00
|
|
|
*/
|
2000-10-24 03:38:44 +02:00
|
|
|
CommandCounterIncrement();
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-10-24 03:38:44 +02:00
|
|
|
* Determine size of a large object
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-10-24 03:38:44 +02:00
|
|
|
* NOTE: LOs can contain gaps, just like Unix files. We actually return
|
|
|
|
* the offset of the last byte + 1.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-10-24 03:38:44 +02:00
|
|
|
static uint32
|
|
|
|
inv_getsize(LargeObjectDesc *obj_desc)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
bool found = false;
|
|
|
|
uint32 lastbyte = 0;
|
|
|
|
ScanKeyData skey[1];
|
2008-04-13 01:14:21 +02:00
|
|
|
SysScanDesc sd;
|
2002-05-21 01:51:44 +02:00
|
|
|
HeapTuple tuple;
|
2000-10-24 03:38:44 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(PointerIsValid(obj_desc));
|
|
|
|
|
2004-07-28 16:23:31 +02:00
|
|
|
open_lo_relation();
|
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&skey[0],
|
|
|
|
Anum_pg_largeobject_loid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(obj_desc->id));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2008-04-13 01:14:21 +02:00
|
|
|
sd = systable_beginscan_ordered(lo_heap_r, lo_index_r,
|
|
|
|
obj_desc->snapshot, 1, skey);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-11-03 00:52:06 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Because the pg_largeobject index is on both loid and pageno, but we
|
|
|
|
* constrain only loid, a backwards scan should visit all pages of the
|
2005-10-15 04:49:52 +02:00
|
|
|
* large object in reverse pageno order. So, it's sufficient to examine
|
|
|
|
* the first valid tuple (== last valid page).
|
2000-11-03 00:52:06 +01:00
|
|
|
*/
|
2008-04-13 01:14:21 +02:00
|
|
|
while ((tuple = systable_getnext_ordered(sd, BackwardScanDirection)) != NULL)
|
2000-10-24 03:38:44 +02:00
|
|
|
{
|
2002-05-21 01:51:44 +02:00
|
|
|
Form_pg_largeobject data;
|
|
|
|
bytea *datafield;
|
|
|
|
bool pfreeit;
|
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
found = true;
|
2007-11-15 22:14:46 +01:00
|
|
|
if (HeapTupleHasNulls(tuple)) /* paranoia */
|
2007-06-12 21:46:24 +02:00
|
|
|
elog(ERROR, "null field found in pg_largeobject");
|
2002-05-21 01:51:44 +02:00
|
|
|
data = (Form_pg_largeobject) GETSTRUCT(tuple);
|
2007-11-15 22:14:46 +01:00
|
|
|
datafield = &(data->data); /* see note at top of file */
|
2000-10-24 03:38:44 +02:00
|
|
|
pfreeit = false;
|
|
|
|
if (VARATT_IS_EXTENDED(datafield))
|
|
|
|
{
|
|
|
|
datafield = (bytea *)
|
2007-04-06 06:21:44 +02:00
|
|
|
heap_tuple_untoast_attr((struct varlena *) datafield);
|
2000-10-24 03:38:44 +02:00
|
|
|
pfreeit = true;
|
|
|
|
}
|
2000-11-03 00:52:06 +01:00
|
|
|
lastbyte = data->pageno * LOBLKSIZE + getbytealen(datafield);
|
2000-10-24 03:38:44 +02:00
|
|
|
if (pfreeit)
|
|
|
|
pfree(datafield);
|
2000-11-03 00:52:06 +01:00
|
|
|
break;
|
2000-10-24 03:38:44 +02:00
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2008-04-13 01:14:21 +02:00
|
|
|
systable_endscan_ordered(sd);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
if (!found)
|
2003-07-25 00:04:15 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("large object %u does not exist", obj_desc->id)));
|
2000-10-24 03:38:44 +02:00
|
|
|
return lastbyte;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-10-22 07:27:23 +02:00
|
|
|
int
|
|
|
|
inv_seek(LargeObjectDesc *obj_desc, int offset, int whence)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(PointerIsValid(obj_desc));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
switch (whence)
|
2000-10-22 07:27:23 +02:00
|
|
|
{
|
2000-10-24 03:38:44 +02:00
|
|
|
case SEEK_SET:
|
|
|
|
if (offset < 0)
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(ERROR, "invalid seek offset: %d", offset);
|
2000-10-24 03:38:44 +02:00
|
|
|
obj_desc->offset = offset;
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
2001-03-22 05:01:46 +01:00
|
|
|
if (offset < 0 && obj_desc->offset < ((uint32) (-offset)))
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(ERROR, "invalid seek offset: %d", offset);
|
2000-10-24 03:38:44 +02:00
|
|
|
obj_desc->offset += offset;
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
uint32 size = inv_getsize(obj_desc);
|
2000-11-03 00:52:06 +01:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
if (offset < 0 && size < ((uint32) (-offset)))
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(ERROR, "invalid seek offset: %d", offset);
|
2000-11-03 00:52:06 +01:00
|
|
|
obj_desc->offset = size + offset;
|
2000-10-24 03:38:44 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(ERROR, "invalid whence: %d", whence);
|
2000-10-22 07:27:23 +02:00
|
|
|
}
|
2000-10-24 03:38:44 +02:00
|
|
|
return obj_desc->offset;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
inv_tell(LargeObjectDesc *obj_desc)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
Assert(PointerIsValid(obj_desc));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return obj_desc->offset;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
inv_read(LargeObjectDesc *obj_desc, char *buf, int nbytes)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int nread = 0;
|
|
|
|
int n;
|
|
|
|
int off;
|
|
|
|
int len;
|
|
|
|
int32 pageno = (int32) (obj_desc->offset / LOBLKSIZE);
|
|
|
|
uint32 pageoff;
|
|
|
|
ScanKeyData skey[2];
|
2008-04-13 01:14:21 +02:00
|
|
|
SysScanDesc sd;
|
2002-05-21 01:51:44 +02:00
|
|
|
HeapTuple tuple;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
Assert(PointerIsValid(obj_desc));
|
|
|
|
Assert(buf != NULL);
|
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
if (nbytes <= 0)
|
2000-10-22 07:27:23 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2004-07-28 16:23:31 +02:00
|
|
|
open_lo_relation();
|
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&skey[0],
|
|
|
|
Anum_pg_largeobject_loid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(obj_desc->id));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&skey[1],
|
|
|
|
Anum_pg_largeobject_pageno,
|
|
|
|
BTGreaterEqualStrategyNumber, F_INT4GE,
|
|
|
|
Int32GetDatum(pageno));
|
2000-10-22 07:27:23 +02:00
|
|
|
|
2008-04-13 01:14:21 +02:00
|
|
|
sd = systable_beginscan_ordered(lo_heap_r, lo_index_r,
|
|
|
|
obj_desc->snapshot, 2, skey);
|
2000-10-22 07:27:23 +02:00
|
|
|
|
2008-04-13 01:14:21 +02:00
|
|
|
while ((tuple = systable_getnext_ordered(sd, ForwardScanDirection)) != NULL)
|
2000-10-24 03:38:44 +02:00
|
|
|
{
|
2002-05-21 01:51:44 +02:00
|
|
|
Form_pg_largeobject data;
|
|
|
|
bytea *datafield;
|
|
|
|
bool pfreeit;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
if (HeapTupleHasNulls(tuple)) /* paranoia */
|
2007-06-12 21:46:24 +02:00
|
|
|
elog(ERROR, "null field found in pg_largeobject");
|
2002-05-21 01:51:44 +02:00
|
|
|
data = (Form_pg_largeobject) GETSTRUCT(tuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-10-22 07:27:23 +02:00
|
|
|
/*
|
2008-04-13 01:14:21 +02:00
|
|
|
* We expect the indexscan will deliver pages in order. However,
|
2005-10-15 04:49:52 +02:00
|
|
|
* there may be missing pages if the LO contains unwritten "holes". We
|
|
|
|
* want missing sections to read out as zeroes.
|
2000-10-22 07:27:23 +02:00
|
|
|
*/
|
2000-10-24 03:38:44 +02:00
|
|
|
pageoff = ((uint32) data->pageno) * LOBLKSIZE;
|
|
|
|
if (pageoff > obj_desc->offset)
|
2000-10-22 07:27:23 +02:00
|
|
|
{
|
2000-10-24 03:38:44 +02:00
|
|
|
n = pageoff - obj_desc->offset;
|
|
|
|
n = (n <= (nbytes - nread)) ? n : (nbytes - nread);
|
|
|
|
MemSet(buf + nread, 0, n);
|
|
|
|
nread += n;
|
|
|
|
obj_desc->offset += n;
|
2000-10-22 07:27:23 +02:00
|
|
|
}
|
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
if (nread < nbytes)
|
|
|
|
{
|
|
|
|
Assert(obj_desc->offset >= pageoff);
|
|
|
|
off = (int) (obj_desc->offset - pageoff);
|
|
|
|
Assert(off >= 0 && off < LOBLKSIZE);
|
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
datafield = &(data->data); /* see note at top of file */
|
2000-10-24 03:38:44 +02:00
|
|
|
pfreeit = false;
|
|
|
|
if (VARATT_IS_EXTENDED(datafield))
|
|
|
|
{
|
|
|
|
datafield = (bytea *)
|
2007-04-06 06:21:44 +02:00
|
|
|
heap_tuple_untoast_attr((struct varlena *) datafield);
|
2000-10-24 03:38:44 +02:00
|
|
|
pfreeit = true;
|
|
|
|
}
|
|
|
|
len = getbytealen(datafield);
|
|
|
|
if (len > off)
|
|
|
|
{
|
|
|
|
n = len - off;
|
|
|
|
n = (n <= (nbytes - nread)) ? n : (nbytes - nread);
|
|
|
|
memcpy(buf + nread, VARDATA(datafield) + off, n);
|
|
|
|
nread += n;
|
|
|
|
obj_desc->offset += n;
|
|
|
|
}
|
|
|
|
if (pfreeit)
|
|
|
|
pfree(datafield);
|
|
|
|
}
|
2000-10-21 17:55:29 +02:00
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
if (nread >= nbytes)
|
|
|
|
break;
|
2000-10-22 07:27:23 +02:00
|
|
|
}
|
2000-10-21 17:55:29 +02:00
|
|
|
|
2008-04-13 01:14:21 +02:00
|
|
|
systable_endscan_ordered(sd);
|
2000-10-24 03:38:44 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return nread;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2000-10-22 07:27:23 +02:00
|
|
|
int
|
2006-09-07 17:37:25 +02:00
|
|
|
inv_write(LargeObjectDesc *obj_desc, const char *buf, int nbytes)
|
2000-10-22 07:27:23 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int nwritten = 0;
|
|
|
|
int n;
|
|
|
|
int off;
|
|
|
|
int len;
|
|
|
|
int32 pageno = (int32) (obj_desc->offset / LOBLKSIZE);
|
|
|
|
ScanKeyData skey[2];
|
2008-04-13 01:14:21 +02:00
|
|
|
SysScanDesc sd;
|
2002-05-21 01:51:44 +02:00
|
|
|
HeapTuple oldtuple;
|
2001-03-22 05:01:46 +01:00
|
|
|
Form_pg_largeobject olddata;
|
|
|
|
bool neednextpage;
|
|
|
|
bytea *datafield;
|
|
|
|
bool pfreeit;
|
2001-10-25 07:50:21 +02:00
|
|
|
struct
|
|
|
|
{
|
2002-08-25 19:20:01 +02:00
|
|
|
bytea hdr;
|
2008-03-01 20:26:22 +01:00
|
|
|
char data[LOBLKSIZE]; /* make struct big enough */
|
|
|
|
int32 align_it; /* ensure struct is aligned well enough */
|
2001-03-26 01:23:59 +02:00
|
|
|
} workbuf;
|
2007-02-28 00:48:10 +01:00
|
|
|
char *workb = VARDATA(&workbuf.hdr);
|
2001-03-22 05:01:46 +01:00
|
|
|
HeapTuple newtup;
|
|
|
|
Datum values[Natts_pg_largeobject];
|
2008-11-02 02:45:28 +01:00
|
|
|
bool nulls[Natts_pg_largeobject];
|
|
|
|
bool replace[Natts_pg_largeobject];
|
2002-08-05 05:29:17 +02:00
|
|
|
CatalogIndexState indstate;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
Assert(PointerIsValid(obj_desc));
|
|
|
|
Assert(buf != NULL);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2005-06-13 04:26:53 +02:00
|
|
|
/* enforce writability because snapshot is probably wrong otherwise */
|
|
|
|
if ((obj_desc->flags & IFS_WRLOCK) == 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("large object %u was not opened for writing",
|
|
|
|
obj_desc->id)));
|
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
if (nbytes <= 0)
|
|
|
|
return 0;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2004-07-28 16:23:31 +02:00
|
|
|
open_lo_relation();
|
|
|
|
|
|
|
|
indstate = CatalogOpenIndexes(lo_heap_r);
|
2000-10-24 03:38:44 +02:00
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&skey[0],
|
|
|
|
Anum_pg_largeobject_loid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(obj_desc->id));
|
2003-11-09 22:30:38 +01:00
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&skey[1],
|
|
|
|
Anum_pg_largeobject_pageno,
|
|
|
|
BTGreaterEqualStrategyNumber, F_INT4GE,
|
|
|
|
Int32GetDatum(pageno));
|
1998-09-01 06:40:42 +02:00
|
|
|
|
2008-04-13 01:14:21 +02:00
|
|
|
sd = systable_beginscan_ordered(lo_heap_r, lo_index_r,
|
|
|
|
obj_desc->snapshot, 2, skey);
|
2000-10-24 03:38:44 +02:00
|
|
|
|
2002-05-21 01:51:44 +02:00
|
|
|
oldtuple = NULL;
|
2000-10-24 03:38:44 +02:00
|
|
|
olddata = NULL;
|
|
|
|
neednextpage = true;
|
2000-10-22 07:27:23 +02:00
|
|
|
|
|
|
|
while (nwritten < nbytes)
|
|
|
|
{
|
|
|
|
/*
|
2008-04-13 01:14:21 +02:00
|
|
|
* If possible, get next pre-existing page of the LO. We expect the
|
2005-10-15 04:49:52 +02:00
|
|
|
* indexscan will deliver these in order --- but there may be holes.
|
2000-10-22 07:27:23 +02:00
|
|
|
*/
|
2000-10-24 03:38:44 +02:00
|
|
|
if (neednextpage)
|
2000-10-22 07:27:23 +02:00
|
|
|
{
|
2008-04-13 01:14:21 +02:00
|
|
|
if ((oldtuple = systable_getnext_ordered(sd, ForwardScanDirection)) != NULL)
|
2000-10-22 07:27:23 +02:00
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
if (HeapTupleHasNulls(oldtuple)) /* paranoia */
|
2007-06-12 21:46:24 +02:00
|
|
|
elog(ERROR, "null field found in pg_largeobject");
|
2002-05-21 01:51:44 +02:00
|
|
|
olddata = (Form_pg_largeobject) GETSTRUCT(oldtuple);
|
|
|
|
Assert(olddata->pageno >= pageno);
|
2000-10-22 07:27:23 +02:00
|
|
|
}
|
2000-10-24 03:38:44 +02:00
|
|
|
neednextpage = false;
|
2000-10-22 07:27:23 +02:00
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If we have a pre-existing page, see if it is the page we want to
|
|
|
|
* write, or a later one.
|
2000-10-24 03:38:44 +02:00
|
|
|
*/
|
|
|
|
if (olddata != NULL && olddata->pageno == pageno)
|
2000-10-22 07:27:23 +02:00
|
|
|
{
|
|
|
|
/*
|
2000-10-24 03:38:44 +02:00
|
|
|
* Update an existing page with fresh data.
|
|
|
|
*
|
|
|
|
* First, load old data into workbuf
|
2000-10-22 07:27:23 +02:00
|
|
|
*/
|
2007-06-12 21:46:24 +02:00
|
|
|
datafield = &(olddata->data); /* see note at top of file */
|
2000-10-24 03:38:44 +02:00
|
|
|
pfreeit = false;
|
|
|
|
if (VARATT_IS_EXTENDED(datafield))
|
2000-10-22 07:27:23 +02:00
|
|
|
{
|
2000-10-24 03:38:44 +02:00
|
|
|
datafield = (bytea *)
|
2007-04-06 06:21:44 +02:00
|
|
|
heap_tuple_untoast_attr((struct varlena *) datafield);
|
2000-10-24 03:38:44 +02:00
|
|
|
pfreeit = true;
|
2000-10-22 07:27:23 +02:00
|
|
|
}
|
2000-10-24 03:38:44 +02:00
|
|
|
len = getbytealen(datafield);
|
|
|
|
Assert(len <= LOBLKSIZE);
|
|
|
|
memcpy(workb, VARDATA(datafield), len);
|
|
|
|
if (pfreeit)
|
|
|
|
pfree(datafield);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-10-22 07:27:23 +02:00
|
|
|
/*
|
2000-10-24 03:38:44 +02:00
|
|
|
* Fill any hole
|
|
|
|
*/
|
|
|
|
off = (int) (obj_desc->offset % LOBLKSIZE);
|
|
|
|
if (off > len)
|
|
|
|
MemSet(workb + len, 0, off - len);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
/*
|
|
|
|
* Insert appropriate portion of new data
|
|
|
|
*/
|
|
|
|
n = LOBLKSIZE - off;
|
|
|
|
n = (n <= (nbytes - nwritten)) ? n : (nbytes - nwritten);
|
|
|
|
memcpy(workb + off, buf + nwritten, n);
|
|
|
|
nwritten += n;
|
|
|
|
obj_desc->offset += n;
|
|
|
|
off += n;
|
|
|
|
/* compute valid length of new page */
|
|
|
|
len = (len >= off) ? len : off;
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(&workbuf.hdr, len + VARHDRSZ);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
/*
|
|
|
|
* Form and insert updated tuple
|
|
|
|
*/
|
|
|
|
memset(values, 0, sizeof(values));
|
2008-11-02 02:45:28 +01:00
|
|
|
memset(nulls, false, sizeof(nulls));
|
|
|
|
memset(replace, false, sizeof(replace));
|
2001-03-26 01:23:59 +02:00
|
|
|
values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
|
2008-11-02 02:45:28 +01:00
|
|
|
replace[Anum_pg_largeobject_data - 1] = true;
|
|
|
|
newtup = heap_modify_tuple(oldtuple, RelationGetDescr(lo_heap_r),
|
2000-10-24 03:38:44 +02:00
|
|
|
values, nulls, replace);
|
2004-07-28 16:23:31 +02:00
|
|
|
simple_heap_update(lo_heap_r, &newtup->t_self, newtup);
|
2002-08-05 05:29:17 +02:00
|
|
|
CatalogIndexInsert(indstate, newtup);
|
2000-10-24 03:38:44 +02:00
|
|
|
heap_freetuple(newtup);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
/*
|
|
|
|
* We're done with this old page.
|
2000-10-22 07:27:23 +02:00
|
|
|
*/
|
2002-05-21 01:51:44 +02:00
|
|
|
oldtuple = NULL;
|
2000-10-24 03:38:44 +02:00
|
|
|
olddata = NULL;
|
|
|
|
neednextpage = true;
|
2000-10-22 07:27:23 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-10-24 03:38:44 +02:00
|
|
|
/*
|
|
|
|
* Write a brand new page.
|
|
|
|
*
|
|
|
|
* First, fill any hole
|
|
|
|
*/
|
|
|
|
off = (int) (obj_desc->offset % LOBLKSIZE);
|
|
|
|
if (off > 0)
|
|
|
|
MemSet(workb, 0, off);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
/*
|
|
|
|
* Insert appropriate portion of new data
|
|
|
|
*/
|
|
|
|
n = LOBLKSIZE - off;
|
|
|
|
n = (n <= (nbytes - nwritten)) ? n : (nbytes - nwritten);
|
|
|
|
memcpy(workb + off, buf + nwritten, n);
|
|
|
|
nwritten += n;
|
|
|
|
obj_desc->offset += n;
|
|
|
|
/* compute valid length of new page */
|
|
|
|
len = off + n;
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(&workbuf.hdr, len + VARHDRSZ);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
/*
|
|
|
|
* Form and insert updated tuple
|
|
|
|
*/
|
|
|
|
memset(values, 0, sizeof(values));
|
2008-11-02 02:45:28 +01:00
|
|
|
memset(nulls, false, sizeof(nulls));
|
2000-10-24 03:38:44 +02:00
|
|
|
values[Anum_pg_largeobject_loid - 1] = ObjectIdGetDatum(obj_desc->id);
|
|
|
|
values[Anum_pg_largeobject_pageno - 1] = Int32GetDatum(pageno);
|
2001-03-26 01:23:59 +02:00
|
|
|
values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
|
2008-11-02 02:45:28 +01:00
|
|
|
newtup = heap_form_tuple(lo_heap_r->rd_att, values, nulls);
|
2004-07-28 16:23:31 +02:00
|
|
|
simple_heap_insert(lo_heap_r, newtup);
|
2002-08-05 05:29:17 +02:00
|
|
|
CatalogIndexInsert(indstate, newtup);
|
2000-10-24 03:38:44 +02:00
|
|
|
heap_freetuple(newtup);
|
2000-10-22 07:27:23 +02:00
|
|
|
}
|
2000-10-24 03:38:44 +02:00
|
|
|
pageno++;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2000-10-22 07:27:23 +02:00
|
|
|
|
2008-04-13 01:14:21 +02:00
|
|
|
systable_endscan_ordered(sd);
|
2000-10-22 07:27:23 +02:00
|
|
|
|
2002-08-05 05:29:17 +02:00
|
|
|
CatalogCloseIndexes(indstate);
|
2000-10-22 07:27:23 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Advance command counter so that my tuple updates will be seen by later
|
|
|
|
* large-object operations in this transaction.
|
2000-10-22 07:27:23 +02:00
|
|
|
*/
|
2000-10-24 03:38:44 +02:00
|
|
|
CommandCounterIncrement();
|
2000-10-22 07:27:23 +02:00
|
|
|
|
|
|
|
return nwritten;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2007-03-03 20:52:47 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
inv_truncate(LargeObjectDesc *obj_desc, int len)
|
|
|
|
{
|
|
|
|
int32 pageno = (int32) (len / LOBLKSIZE);
|
|
|
|
int off;
|
2007-11-15 22:14:46 +01:00
|
|
|
ScanKeyData skey[2];
|
2008-04-13 01:14:21 +02:00
|
|
|
SysScanDesc sd;
|
2007-03-03 20:52:47 +01:00
|
|
|
HeapTuple oldtuple;
|
2007-11-15 22:14:46 +01:00
|
|
|
Form_pg_largeobject olddata;
|
2007-03-03 20:52:47 +01:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
bytea hdr;
|
2008-03-01 20:26:22 +01:00
|
|
|
char data[LOBLKSIZE]; /* make struct big enough */
|
|
|
|
int32 align_it; /* ensure struct is aligned well enough */
|
2007-03-03 20:52:47 +01:00
|
|
|
} workbuf;
|
2007-11-15 22:14:46 +01:00
|
|
|
char *workb = VARDATA(&workbuf.hdr);
|
2007-03-03 20:52:47 +01:00
|
|
|
HeapTuple newtup;
|
|
|
|
Datum values[Natts_pg_largeobject];
|
2008-11-02 02:45:28 +01:00
|
|
|
bool nulls[Natts_pg_largeobject];
|
|
|
|
bool replace[Natts_pg_largeobject];
|
2007-03-03 20:52:47 +01:00
|
|
|
CatalogIndexState indstate;
|
|
|
|
|
|
|
|
Assert(PointerIsValid(obj_desc));
|
|
|
|
|
|
|
|
/* enforce writability because snapshot is probably wrong otherwise */
|
|
|
|
if ((obj_desc->flags & IFS_WRLOCK) == 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("large object %u was not opened for writing",
|
|
|
|
obj_desc->id)));
|
|
|
|
|
|
|
|
open_lo_relation();
|
|
|
|
|
|
|
|
indstate = CatalogOpenIndexes(lo_heap_r);
|
|
|
|
|
|
|
|
ScanKeyInit(&skey[0],
|
|
|
|
Anum_pg_largeobject_loid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(obj_desc->id));
|
|
|
|
|
|
|
|
ScanKeyInit(&skey[1],
|
|
|
|
Anum_pg_largeobject_pageno,
|
|
|
|
BTGreaterEqualStrategyNumber, F_INT4GE,
|
|
|
|
Int32GetDatum(pageno));
|
|
|
|
|
2008-04-13 01:14:21 +02:00
|
|
|
sd = systable_beginscan_ordered(lo_heap_r, lo_index_r,
|
|
|
|
obj_desc->snapshot, 2, skey);
|
2007-03-03 20:52:47 +01:00
|
|
|
|
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* If possible, get the page the truncation point is in. The truncation
|
|
|
|
* point may be beyond the end of the LO or in a hole.
|
2007-03-03 20:52:47 +01:00
|
|
|
*/
|
|
|
|
olddata = NULL;
|
2008-04-13 01:14:21 +02:00
|
|
|
if ((oldtuple = systable_getnext_ordered(sd, ForwardScanDirection)) != NULL)
|
2007-03-03 20:52:47 +01:00
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
if (HeapTupleHasNulls(oldtuple)) /* paranoia */
|
2007-06-12 21:46:24 +02:00
|
|
|
elog(ERROR, "null field found in pg_largeobject");
|
2007-03-03 20:52:47 +01:00
|
|
|
olddata = (Form_pg_largeobject) GETSTRUCT(oldtuple);
|
|
|
|
Assert(olddata->pageno >= pageno);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* If we found the page of the truncation point we need to truncate the
|
|
|
|
* data in it. Otherwise if we're in a hole, we need to create a page to
|
|
|
|
* mark the end of data.
|
2007-03-03 20:52:47 +01:00
|
|
|
*/
|
|
|
|
if (olddata != NULL && olddata->pageno == pageno)
|
|
|
|
{
|
|
|
|
/* First, load old data into workbuf */
|
2007-11-15 22:14:46 +01:00
|
|
|
bytea *datafield = &(olddata->data); /* see note at top of
|
|
|
|
* file */
|
|
|
|
bool pfreeit = false;
|
|
|
|
int pagelen;
|
2007-03-03 20:52:47 +01:00
|
|
|
|
|
|
|
if (VARATT_IS_EXTENDED(datafield))
|
|
|
|
{
|
|
|
|
datafield = (bytea *)
|
2007-04-06 06:21:44 +02:00
|
|
|
heap_tuple_untoast_attr((struct varlena *) datafield);
|
2007-03-03 20:52:47 +01:00
|
|
|
pfreeit = true;
|
|
|
|
}
|
|
|
|
pagelen = getbytealen(datafield);
|
|
|
|
Assert(pagelen <= LOBLKSIZE);
|
|
|
|
memcpy(workb, VARDATA(datafield), pagelen);
|
|
|
|
if (pfreeit)
|
2007-11-15 22:14:46 +01:00
|
|
|
pfree(datafield);
|
2007-03-03 20:52:47 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill any hole
|
|
|
|
*/
|
|
|
|
off = len % LOBLKSIZE;
|
|
|
|
if (off > pagelen)
|
2007-11-15 22:14:46 +01:00
|
|
|
MemSet(workb + pagelen, 0, off - pagelen);
|
2007-03-03 20:52:47 +01:00
|
|
|
|
|
|
|
/* compute length of new page */
|
|
|
|
SET_VARSIZE(&workbuf.hdr, off + VARHDRSZ);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Form and insert updated tuple
|
|
|
|
*/
|
|
|
|
memset(values, 0, sizeof(values));
|
2008-11-02 02:45:28 +01:00
|
|
|
memset(nulls, false, sizeof(nulls));
|
|
|
|
memset(replace, false, sizeof(replace));
|
2007-03-03 20:52:47 +01:00
|
|
|
values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
|
2008-11-02 02:45:28 +01:00
|
|
|
replace[Anum_pg_largeobject_data - 1] = true;
|
|
|
|
newtup = heap_modify_tuple(oldtuple, RelationGetDescr(lo_heap_r),
|
2007-03-03 20:52:47 +01:00
|
|
|
values, nulls, replace);
|
|
|
|
simple_heap_update(lo_heap_r, &newtup->t_self, newtup);
|
|
|
|
CatalogIndexInsert(indstate, newtup);
|
|
|
|
heap_freetuple(newtup);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* If the first page we found was after the truncation point, we're in
|
|
|
|
* a hole that we'll fill, but we need to delete the later page.
|
2007-03-03 20:52:47 +01:00
|
|
|
*/
|
|
|
|
if (olddata != NULL && olddata->pageno > pageno)
|
|
|
|
simple_heap_delete(lo_heap_r, &oldtuple->t_self);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a brand new page.
|
2007-11-15 22:14:46 +01:00
|
|
|
*
|
2007-03-03 20:52:47 +01:00
|
|
|
* Fill the hole up to the truncation point
|
|
|
|
*/
|
|
|
|
off = len % LOBLKSIZE;
|
|
|
|
if (off > 0)
|
|
|
|
MemSet(workb, 0, off);
|
|
|
|
|
|
|
|
/* compute length of new page */
|
|
|
|
SET_VARSIZE(&workbuf.hdr, off + VARHDRSZ);
|
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
/*
|
2007-03-03 20:52:47 +01:00
|
|
|
* Form and insert new tuple
|
|
|
|
*/
|
|
|
|
memset(values, 0, sizeof(values));
|
2008-11-02 02:45:28 +01:00
|
|
|
memset(nulls, false, sizeof(nulls));
|
2007-03-03 20:52:47 +01:00
|
|
|
values[Anum_pg_largeobject_loid - 1] = ObjectIdGetDatum(obj_desc->id);
|
|
|
|
values[Anum_pg_largeobject_pageno - 1] = Int32GetDatum(pageno);
|
|
|
|
values[Anum_pg_largeobject_data - 1] = PointerGetDatum(&workbuf);
|
2008-11-02 02:45:28 +01:00
|
|
|
newtup = heap_form_tuple(lo_heap_r->rd_att, values, nulls);
|
2007-03-03 20:52:47 +01:00
|
|
|
simple_heap_insert(lo_heap_r, newtup);
|
|
|
|
CatalogIndexInsert(indstate, newtup);
|
|
|
|
heap_freetuple(newtup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete any pages after the truncation point
|
|
|
|
*/
|
2008-04-13 01:14:21 +02:00
|
|
|
while ((oldtuple = systable_getnext_ordered(sd, ForwardScanDirection)) != NULL)
|
2007-03-03 20:52:47 +01:00
|
|
|
{
|
|
|
|
simple_heap_delete(lo_heap_r, &oldtuple->t_self);
|
|
|
|
}
|
|
|
|
|
2008-04-13 01:14:21 +02:00
|
|
|
systable_endscan_ordered(sd);
|
2007-03-03 20:52:47 +01:00
|
|
|
|
|
|
|
CatalogCloseIndexes(indstate);
|
2007-11-15 22:14:46 +01:00
|
|
|
|
2007-03-03 20:52:47 +01:00
|
|
|
/*
|
|
|
|
* Advance command counter so that tuple updates will be seen by later
|
|
|
|
* large-object operations in this transaction.
|
|
|
|
*/
|
|
|
|
CommandCounterIncrement();
|
|
|
|
}
|