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).
|
|
|
|
*
|
|
|
|
*
|
2013-01-01 23:15:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2013, 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
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/storage/large_object/inv_api.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2000-10-24 03:38:44 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2012-10-09 00:24:06 +02:00
|
|
|
#include <limits.h>
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "access/genam.h"
|
|
|
|
#include "access/heapam.h"
|
2009-12-11 04:34:57 +01:00
|
|
|
#include "access/sysattr.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"
|
2009-12-11 04:34:57 +01:00
|
|
|
#include "catalog/dependency.h"
|
2000-10-24 03:38:44 +02:00
|
|
|
#include "catalog/indexing.h"
|
2010-11-25 17:48:49 +01:00
|
|
|
#include "catalog/objectaccess.h"
|
2000-10-24 03:38:44 +02:00
|
|
|
#include "catalog/pg_largeobject.h"
|
2009-12-11 04:34:57 +01:00
|
|
|
#include "catalog/pg_largeobject_metadata.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "libpq/libpq-fs.h"
|
2009-12-11 04:34:57 +01:00
|
|
|
#include "miscadmin.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#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"
|
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)
|
|
|
|
{
|
2009-12-11 04:34:57 +01:00
|
|
|
Relation pg_lo_meta;
|
2010-02-26 03:01:40 +01:00
|
|
|
ScanKeyData skey[1];
|
|
|
|
SysScanDesc sd;
|
2009-12-11 04:34:57 +01:00
|
|
|
HeapTuple tuple;
|
2005-06-13 04:26:53 +02:00
|
|
|
bool retval = false;
|
|
|
|
|
|
|
|
ScanKeyInit(&skey[0],
|
2009-12-11 04:34:57 +01:00
|
|
|
ObjectIdAttributeNumber,
|
2005-06-13 04:26:53 +02:00
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(loid));
|
|
|
|
|
2009-12-11 04:34:57 +01:00
|
|
|
pg_lo_meta = heap_open(LargeObjectMetadataRelationId,
|
|
|
|
AccessShareLock);
|
2005-06-13 04:26:53 +02:00
|
|
|
|
2009-12-11 04:34:57 +01:00
|
|
|
sd = systable_beginscan(pg_lo_meta,
|
|
|
|
LargeObjectMetadataOidIndexId, true,
|
2005-06-13 04:26:53 +02:00
|
|
|
snapshot, 1, skey);
|
|
|
|
|
2009-12-11 04:34:57 +01:00
|
|
|
tuple = systable_getnext(sd);
|
|
|
|
if (HeapTupleIsValid(tuple))
|
2005-06-13 04:26:53 +02:00
|
|
|
retval = true;
|
|
|
|
|
|
|
|
systable_endscan(sd);
|
|
|
|
|
2009-12-11 04:34:57 +01:00
|
|
|
heap_close(pg_lo_meta, AccessShareLock);
|
2005-06-13 04:26:53 +02:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2009-12-11 04:34:57 +01:00
|
|
|
Oid lobjId_new;
|
|
|
|
|
2000-10-22 07:27:23 +02:00
|
|
|
/*
|
2009-12-11 04:34:57 +01:00
|
|
|
* Create a new largeobject with empty data pages
|
2000-10-22 07:27:23 +02:00
|
|
|
*/
|
2009-12-11 04:34:57 +01:00
|
|
|
lobjId_new = LargeObjectCreate(lobjId);
|
2000-10-22 07:27:23 +02:00
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
/*
|
2009-12-11 04:34:57 +01:00
|
|
|
* dependency on the owner of largeobject
|
|
|
|
*
|
|
|
|
* The reason why we use LargeObjectRelationId instead of
|
2010-02-26 03:01:40 +01:00
|
|
|
* LargeObjectMetadataRelationId here is to provide backward compatibility
|
|
|
|
* to the applications which utilize a knowledge about internal layout of
|
|
|
|
* system catalogs. OID of pg_largeobject_metadata and loid of
|
|
|
|
* pg_largeobject are same value, so there are no actual differences here.
|
2000-10-22 07:27:23 +02:00
|
|
|
*/
|
2009-12-11 04:34:57 +01:00
|
|
|
recordDependencyOnOwner(LargeObjectRelationId,
|
|
|
|
lobjId_new, GetUserId());
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2010-11-25 17:48:49 +01:00
|
|
|
/* Post creation hook for new large object */
|
2013-03-07 02:52:06 +01:00
|
|
|
InvokeObjectPostCreateHook(LargeObjectRelationId, lobjId_new, 0);
|
2010-11-25 17:48:49 +01:00
|
|
|
|
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();
|
|
|
|
|
2009-12-11 04:34:57 +01:00
|
|
|
return lobjId_new;
|
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
|
|
|
{
|
Use an MVCC snapshot, rather than SnapshotNow, for catalog scans.
SnapshotNow scans have the undesirable property that, in the face of
concurrent updates, the scan can fail to see either the old or the new
versions of the row. In many cases, we work around this by requiring
DDL operations to hold AccessExclusiveLock on the object being
modified; in some cases, the existing locking is inadequate and random
failures occur as a result. This commit doesn't change anything
related to locking, but will hopefully pave the way to allowing lock
strength reductions in the future.
The major issue has held us back from making this change in the past
is that taking an MVCC snapshot is significantly more expensive than
using a static special snapshot such as SnapshotNow. However, testing
of various worst-case scenarios reveals that this problem is not
severe except under fairly extreme workloads. To mitigate those
problems, we avoid retaking the MVCC snapshot for each new scan;
instead, we take a new snapshot only when invalidation messages have
been processed. The catcache machinery already requires that
invalidation messages be sent before releasing the related heavyweight
lock; else other backends might rely on locally-cached data rather
than scanning the catalog at all. Thus, making snapshot reuse
dependent on the same guarantees shouldn't break anything that wasn't
already subtly broken.
Patch by me. Review by Michael Paquier and Andres Freund.
2013-07-02 15:47:01 +02:00
|
|
|
retval->snapshot = NULL; /* instantaneous MVCC snapshot */
|
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
|
|
|
/*
|
2009-06-11 16:49:15 +02: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.
|
2008-12-04 15:51:02 +01:00
|
|
|
*/
|
|
|
|
retval->snapshot = RegisterSnapshotOnOwner(GetActiveSnapshot(),
|
2009-06-11 16:49:15 +02:00
|
|
|
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
|
2012-10-09 00:24:06 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid flags for opening a large object: %d",
|
|
|
|
flags)));
|
2000-10-24 03:38:44 +02:00
|
|
|
|
Use an MVCC snapshot, rather than SnapshotNow, for catalog scans.
SnapshotNow scans have the undesirable property that, in the face of
concurrent updates, the scan can fail to see either the old or the new
versions of the row. In many cases, we work around this by requiring
DDL operations to hold AccessExclusiveLock on the object being
modified; in some cases, the existing locking is inadequate and random
failures occur as a result. This commit doesn't change anything
related to locking, but will hopefully pave the way to allowing lock
strength reductions in the future.
The major issue has held us back from making this change in the past
is that taking an MVCC snapshot is significantly more expensive than
using a static special snapshot such as SnapshotNow. However, testing
of various worst-case scenarios reveals that this problem is not
severe except under fairly extreme workloads. To mitigate those
problems, we avoid retaking the MVCC snapshot for each new scan;
instead, we take a new snapshot only when invalidation messages have
been processed. The catcache machinery already requires that
invalidation messages be sent before releasing the related heavyweight
lock; else other backends might rely on locally-cached data rather
than scanning the catalog at all. Thus, making snapshot reuse
dependent on the same guarantees shouldn't break anything that wasn't
already subtly broken.
Patch by me. Review by Michael Paquier and Andres Freund.
2013-07-02 15:47:01 +02:00
|
|
|
/* Can't use LargeObjectExists here because we need to specify snapshot */
|
2005-06-13 04:26:53 +02:00
|
|
|
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
|
|
|
|
Use an MVCC snapshot, rather than SnapshotNow, for catalog scans.
SnapshotNow scans have the undesirable property that, in the face of
concurrent updates, the scan can fail to see either the old or the new
versions of the row. In many cases, we work around this by requiring
DDL operations to hold AccessExclusiveLock on the object being
modified; in some cases, the existing locking is inadequate and random
failures occur as a result. This commit doesn't change anything
related to locking, but will hopefully pave the way to allowing lock
strength reductions in the future.
The major issue has held us back from making this change in the past
is that taking an MVCC snapshot is significantly more expensive than
using a static special snapshot such as SnapshotNow. However, testing
of various worst-case scenarios reveals that this problem is not
severe except under fairly extreme workloads. To mitigate those
problems, we avoid retaking the MVCC snapshot for each new scan;
instead, we take a new snapshot only when invalidation messages have
been processed. The catcache machinery already requires that
invalidation messages be sent before releasing the related heavyweight
lock; else other backends might rely on locally-cached data rather
than scanning the catalog at all. Thus, making snapshot reuse
dependent on the same guarantees shouldn't break anything that wasn't
already subtly broken.
Patch by me. Review by Michael Paquier and Andres Freund.
2013-07-02 15:47:01 +02:00
|
|
|
UnregisterSnapshotFromOwner(obj_desc->snapshot,
|
|
|
|
TopTransactionResourceOwner);
|
2008-12-04 15:51:02 +01:00
|
|
|
|
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
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
ObjectAddress object;
|
2000-10-22 07:27:23 +02:00
|
|
|
|
2009-12-11 04:34:57 +01:00
|
|
|
/*
|
|
|
|
* Delete any comments and dependencies on the large object
|
|
|
|
*/
|
|
|
|
object.classId = LargeObjectRelationId;
|
|
|
|
object.objectId = lobjId;
|
|
|
|
object.objectSubId = 0;
|
2012-01-26 15:24:54 +01:00
|
|
|
performDeletion(&object, DROP_CASCADE, 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
|
|
|
*/
|
2012-10-07 01:36:48 +02:00
|
|
|
static uint64
|
2000-10-24 03:38:44 +02:00
|
|
|
inv_getsize(LargeObjectDesc *obj_desc)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2012-10-07 01:36:48 +02:00
|
|
|
uint64 lastbyte = 0;
|
2001-03-22 05:01:46 +01:00
|
|
|
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
|
|
|
*/
|
2009-12-11 04:34:57 +01:00
|
|
|
tuple = systable_getnext_ordered(sd, BackwardScanDirection);
|
|
|
|
if (HeapTupleIsValid(tuple))
|
2000-10-24 03:38:44 +02:00
|
|
|
{
|
2002-05-21 01:51:44 +02:00
|
|
|
Form_pg_largeobject data;
|
|
|
|
bytea *datafield;
|
|
|
|
bool pfreeit;
|
|
|
|
|
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;
|
|
|
|
}
|
2012-10-07 01:36:48 +02:00
|
|
|
lastbyte = (uint64) data->pageno * LOBLKSIZE + getbytealen(datafield);
|
2000-10-24 03:38:44 +02:00
|
|
|
if (pfreeit)
|
|
|
|
pfree(datafield);
|
|
|
|
}
|
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
|
|
|
return lastbyte;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2012-10-07 01:36:48 +02:00
|
|
|
int64
|
|
|
|
inv_seek(LargeObjectDesc *obj_desc, int64 offset, int whence)
|
2000-10-22 07:27:23 +02:00
|
|
|
{
|
2012-10-09 00:24:06 +02:00
|
|
|
int64 newoffset;
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(PointerIsValid(obj_desc));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2012-10-09 00:24:06 +02:00
|
|
|
/*
|
|
|
|
* Note: overflow in the additions is possible, but since we will reject
|
|
|
|
* negative results, we don't need any extra test for that.
|
|
|
|
*/
|
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:
|
2012-10-09 00:24:06 +02:00
|
|
|
newoffset = offset;
|
2000-10-24 03:38:44 +02:00
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
2012-10-09 00:24:06 +02:00
|
|
|
newoffset = obj_desc->offset + offset;
|
2000-10-24 03:38:44 +02:00
|
|
|
break;
|
|
|
|
case SEEK_END:
|
2012-10-09 00:24:06 +02:00
|
|
|
newoffset = inv_getsize(obj_desc) + offset;
|
2000-10-24 03:38:44 +02:00
|
|
|
break;
|
|
|
|
default:
|
2012-10-09 00:24:06 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid whence setting: %d", whence)));
|
|
|
|
newoffset = 0; /* keep compiler quiet */
|
|
|
|
break;
|
2000-10-22 07:27:23 +02:00
|
|
|
}
|
2012-10-09 00:24:06 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* use errmsg_internal here because we don't want to expose INT64_FORMAT
|
|
|
|
* in translatable strings; doing better is not worth the trouble
|
|
|
|
*/
|
|
|
|
if (newoffset < 0 || newoffset > MAX_LARGE_OBJECT_SIZE)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2012-10-09 22:38:00 +02:00
|
|
|
errmsg_internal("invalid large object seek target: " INT64_FORMAT,
|
|
|
|
newoffset)));
|
2012-10-09 00:24:06 +02:00
|
|
|
|
|
|
|
obj_desc->offset = newoffset;
|
|
|
|
return newoffset;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2012-10-07 01:36:48 +02:00
|
|
|
int64
|
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;
|
2012-10-07 01:36:48 +02:00
|
|
|
int64 n;
|
|
|
|
int64 off;
|
2001-03-22 05:01:46 +01:00
|
|
|
int len;
|
|
|
|
int32 pageno = (int32) (obj_desc->offset / LOBLKSIZE);
|
2012-10-07 01:36:48 +02:00
|
|
|
uint64 pageoff;
|
2001-03-22 05:01:46 +01:00
|
|
|
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
|
|
|
*/
|
2012-10-07 01:36:48 +02:00
|
|
|
pageoff = ((uint64) data->pageno) * LOBLKSIZE;
|
2000-10-24 03:38:44 +02:00
|
|
|
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 */
|
2012-10-09 22:38:00 +02:00
|
|
|
Assert(obj_desc->flags & IFS_WRLOCK);
|
2009-12-11 04:34:57 +01:00
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
if (nbytes <= 0)
|
|
|
|
return 0;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2012-10-09 00:24:06 +02:00
|
|
|
/* this addition can't overflow because nbytes is only int32 */
|
2012-10-07 01:36:48 +02:00
|
|
|
if ((nbytes + obj_desc->offset) > MAX_LARGE_OBJECT_SIZE)
|
2012-10-09 00:24:06 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid large object write request size: %d",
|
|
|
|
nbytes)));
|
2012-10-07 01:36:48 +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),
|
2009-06-11 16:49:15 +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
|
2012-10-07 01:36:48 +02:00
|
|
|
inv_truncate(LargeObjectDesc *obj_desc, int64 len)
|
2007-03-03 20:52:47 +01:00
|
|
|
{
|
|
|
|
int32 pageno = (int32) (len / LOBLKSIZE);
|
2012-10-07 01:36:48 +02:00
|
|
|
int32 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 */
|
2012-10-09 22:38:00 +02:00
|
|
|
Assert(obj_desc->flags & IFS_WRLOCK);
|
2012-10-09 00:24:06 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* use errmsg_internal here because we don't want to expose INT64_FORMAT
|
|
|
|
* in translatable strings; doing better is not worth the trouble
|
|
|
|
*/
|
|
|
|
if (len < 0 || len > MAX_LARGE_OBJECT_SIZE)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg_internal("invalid large object truncation target: " INT64_FORMAT,
|
|
|
|
len)));
|
2009-12-11 04:34:57 +01:00
|
|
|
|
2007-03-03 20:52:47 +01:00
|
|
|
open_lo_relation();
|
|
|
|
|
|
|
|
indstate = CatalogOpenIndexes(lo_heap_r);
|
|
|
|
|
2011-01-27 01:33:50 +01:00
|
|
|
/*
|
|
|
|
* Set up to find all pages with desired loid and pageno >= target
|
|
|
|
*/
|
2007-03-03 20:52:47 +01:00
|
|
|
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),
|
2009-06-11 16:49:15 +02:00
|
|
|
values, nulls, replace);
|
2007-03-03 20:52:47 +01:00
|
|
|
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
|
2011-04-10 17:42:00 +02:00
|
|
|
* a hole that we'll fill, but we need to delete the later page
|
|
|
|
* because the loop below won't visit it again.
|
2007-03-03 20:52:47 +01:00
|
|
|
*/
|
2011-01-27 01:33:50 +01:00
|
|
|
if (olddata != NULL)
|
|
|
|
{
|
|
|
|
Assert(olddata->pageno > pageno);
|
2007-03-03 20:52:47 +01:00
|
|
|
simple_heap_delete(lo_heap_r, &oldtuple->t_self);
|
2011-01-27 01:33:50 +01:00
|
|
|
}
|
2007-03-03 20:52:47 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-01-27 01:33:50 +01:00
|
|
|
* Delete any pages after the truncation point. If the initial search
|
|
|
|
* didn't find a page, then of course there's nothing more to do.
|
2007-03-03 20:52:47 +01:00
|
|
|
*/
|
2011-01-27 01:33:50 +01:00
|
|
|
if (olddata != NULL)
|
2007-03-03 20:52:47 +01:00
|
|
|
{
|
2011-01-27 01:33:50 +01:00
|
|
|
while ((oldtuple = systable_getnext_ordered(sd, ForwardScanDirection)) != NULL)
|
|
|
|
{
|
|
|
|
simple_heap_delete(lo_heap_r, &oldtuple->t_self);
|
|
|
|
}
|
2007-03-03 20:52:47 +01:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|