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).
|
|
|
|
*
|
|
|
|
*
|
2017-01-03 19:48:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2017, 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
|
|
|
|
|
|
|
|
2017-11-09 18:56:07 +01:00
|
|
|
/*
|
|
|
|
* GUC: backwards-compatibility flag to suppress LO permission checks
|
|
|
|
*/
|
|
|
|
bool lo_compat_privileges;
|
|
|
|
|
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;
|
2017-10-11 23:43:50 +02:00
|
|
|
CurrentResourceOwner = TopTransactionResourceOwner;
|
|
|
|
|
|
|
|
/* Use RowExclusiveLock since we might either read or write */
|
|
|
|
if (lo_heap_r == NULL)
|
|
|
|
lo_heap_r = heap_open(LargeObjectRelationId, RowExclusiveLock);
|
|
|
|
if (lo_index_r == NULL)
|
|
|
|
lo_index_r = index_open(LargeObjectLOidPNIndexId, RowExclusiveLock);
|
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;
|
2017-10-11 23:43:50 +02:00
|
|
|
CurrentResourceOwner = TopTransactionResourceOwner;
|
|
|
|
|
|
|
|
if (lo_index_r)
|
|
|
|
index_close(lo_index_r, NoLock);
|
|
|
|
if (lo_heap_r)
|
|
|
|
heap_close(lo_heap_r, NoLock);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-05 17:31:06 +02:00
|
|
|
/*
|
|
|
|
* Extract data field from a pg_largeobject tuple, detoasting if needed
|
|
|
|
* and verifying that the length is sane. Returns data pointer (a bytea *),
|
|
|
|
* data length, and an indication of whether to pfree the data pointer.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
getdatafield(Form_pg_largeobject tuple,
|
|
|
|
bytea **pdatafield,
|
|
|
|
int *plen,
|
|
|
|
bool *pfreeit)
|
2000-10-24 03:38:44 +02:00
|
|
|
{
|
2014-06-05 17:31:06 +02:00
|
|
|
bytea *datafield;
|
|
|
|
int len;
|
|
|
|
bool freeit;
|
|
|
|
|
|
|
|
datafield = &(tuple->data); /* see note at top of file */
|
|
|
|
freeit = false;
|
|
|
|
if (VARATT_IS_EXTENDED(datafield))
|
|
|
|
{
|
|
|
|
datafield = (bytea *)
|
|
|
|
heap_tuple_untoast_attr((struct varlena *) datafield);
|
|
|
|
freeit = true;
|
|
|
|
}
|
|
|
|
len = VARSIZE(datafield) - VARHDRSZ;
|
|
|
|
if (len < 0 || len > LOBLKSIZE)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATA_CORRUPTED),
|
|
|
|
errmsg("pg_largeobject entry for OID %u, page %d has invalid data field size %d",
|
|
|
|
tuple->loid, tuple->pageno, len)));
|
|
|
|
*pdatafield = datafield;
|
|
|
|
*plen = len;
|
|
|
|
*pfreeit = freeit;
|
2000-10-24 03:38:44 +02:00
|
|
|
}
|
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;
|
2013-09-30 10:29:09 +02:00
|
|
|
Snapshot snapshot = NULL;
|
|
|
|
int descflags = 0;
|
2000-10-22 07:27:23 +02:00
|
|
|
|
2017-11-09 18:56:07 +01:00
|
|
|
/*
|
|
|
|
* Historically, no difference is made between (INV_WRITE) and (INV_WRITE
|
|
|
|
* | INV_READ), the caller being allowed to read the large object
|
|
|
|
* descriptor in either case.
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
if (flags & INV_WRITE)
|
2017-11-09 18:56:07 +01:00
|
|
|
descflags |= IFS_WRLOCK | IFS_RDLOCK;
|
|
|
|
if (flags & INV_READ)
|
|
|
|
descflags |= IFS_RDLOCK;
|
|
|
|
|
|
|
|
if (descflags == 0)
|
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
|
|
|
|
2017-11-09 18:56:07 +01:00
|
|
|
/* Get snapshot. If write is requested, use an instantaneous snapshot. */
|
|
|
|
if (descflags & IFS_WRLOCK)
|
|
|
|
snapshot = NULL;
|
|
|
|
else
|
|
|
|
snapshot = GetActiveSnapshot();
|
|
|
|
|
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 */
|
2013-09-30 10:29:09 +02:00
|
|
|
if (!myLargeObjectExists(lobjId, snapshot))
|
2005-06-13 04:26:53 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("large object %u does not exist", lobjId)));
|
|
|
|
|
2017-11-09 18:56:07 +01:00
|
|
|
/* Apply permission checks, again specifying snapshot */
|
|
|
|
if ((descflags & IFS_RDLOCK) != 0)
|
|
|
|
{
|
|
|
|
if (!lo_compat_privileges &&
|
|
|
|
pg_largeobject_aclcheck_snapshot(lobjId,
|
|
|
|
GetUserId(),
|
|
|
|
ACL_SELECT,
|
|
|
|
snapshot) != ACLCHECK_OK)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("permission denied for large object %u",
|
|
|
|
lobjId)));
|
|
|
|
}
|
|
|
|
if ((descflags & IFS_WRLOCK) != 0)
|
|
|
|
{
|
|
|
|
if (!lo_compat_privileges &&
|
|
|
|
pg_largeobject_aclcheck_snapshot(lobjId,
|
|
|
|
GetUserId(),
|
|
|
|
ACL_UPDATE,
|
|
|
|
snapshot) != ACLCHECK_OK)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("permission denied for large object %u",
|
|
|
|
lobjId)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OK to create a descriptor */
|
|
|
|
retval = (LargeObjectDesc *) MemoryContextAlloc(mcxt,
|
|
|
|
sizeof(LargeObjectDesc));
|
|
|
|
retval->id = lobjId;
|
|
|
|
retval->subid = GetCurrentSubTransactionId();
|
|
|
|
retval->offset = 0;
|
|
|
|
retval->flags = descflags;
|
|
|
|
|
2013-09-30 10:29:09 +02:00
|
|
|
/*
|
2014-05-06 18:12:18 +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
|
2017-11-09 18:56:07 +01:00
|
|
|
* portal shuts down. Do this last to avoid uselessly leaking the
|
|
|
|
* snapshot if an error is thrown above.
|
2013-09-30 10:29:09 +02:00
|
|
|
*/
|
|
|
|
if (snapshot)
|
|
|
|
snapshot = RegisterSnapshotOnOwner(snapshot,
|
|
|
|
TopTransactionResourceOwner);
|
|
|
|
retval->snapshot = snapshot;
|
|
|
|
|
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
|
|
|
*
|
2017-11-09 18:56:07 +01:00
|
|
|
* Note we expect caller to have done any required permissions check.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
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();
|
|
|
|
|
2017-11-09 18:56:07 +01:00
|
|
|
/* For historical reasons, we always return 1 on success. */
|
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;
|
2014-06-05 17:31:06 +02:00
|
|
|
int len;
|
2002-05-21 01:51:44 +02:00
|
|
|
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);
|
2014-06-05 17:31:06 +02:00
|
|
|
getdatafield(data, &datafield, &len, &pfreeit);
|
|
|
|
lastbyte = (uint64) data->pageno * LOBLKSIZE + len;
|
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
|
|
|
|
2017-11-09 18:56:07 +01:00
|
|
|
/*
|
|
|
|
* We allow seek/tell if you have either read or write permission, so no
|
|
|
|
* need for a permission check here.
|
|
|
|
*/
|
|
|
|
|
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),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +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
|
|
|
|
2017-11-09 18:56:07 +01:00
|
|
|
/*
|
|
|
|
* We allow seek/tell if you have either read or write permission, so no
|
|
|
|
* need for a permission check here.
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
2017-11-09 18:56:07 +01:00
|
|
|
if ((obj_desc->flags & IFS_RDLOCK) == 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("permission denied for large object %u",
|
|
|
|
obj_desc->id)));
|
|
|
|
|
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);
|
|
|
|
|
2014-06-05 17:31:06 +02:00
|
|
|
getdatafield(data, &datafield, &len, &pfreeit);
|
2000-10-24 03:38:44 +02:00
|
|
|
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;
|
2015-02-20 22:51:53 +01:00
|
|
|
union
|
2001-10-25 07:50:21 +02:00
|
|
|
{
|
2002-08-25 19:20:01 +02:00
|
|
|
bytea hdr;
|
2015-02-20 22:51:53 +01:00
|
|
|
/* this is to make the union big enough for a LO data chunk: */
|
|
|
|
char data[LOBLKSIZE + VARHDRSZ];
|
|
|
|
/* ensure union is aligned well enough: */
|
|
|
|
int32 align_it;
|
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 */
|
2017-11-09 18:56:07 +01:00
|
|
|
if ((obj_desc->flags & IFS_WRLOCK) == 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("permission denied for large object %u",
|
|
|
|
obj_desc->id)));
|
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
|
|
|
{
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02: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
|
|
|
*/
|
2014-06-05 17:31:06 +02:00
|
|
|
getdatafield(olddata, &datafield, &len, &pfreeit);
|
2000-10-24 03:38:44 +02:00
|
|
|
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);
|
2017-02-01 23:18:36 +01:00
|
|
|
CatalogTupleUpdateWithInfo(lo_heap_r, &newtup->t_self, newtup,
|
|
|
|
indstate);
|
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);
|
2017-02-01 23:18:36 +01:00
|
|
|
CatalogTupleInsertWithInfo(lo_heap_r, newtup, indstate);
|
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;
|
2015-02-20 22:51:53 +01:00
|
|
|
union
|
2007-03-03 20:52:47 +01:00
|
|
|
{
|
|
|
|
bytea hdr;
|
2015-02-20 22:51:53 +01:00
|
|
|
/* this is to make the union big enough for a LO data chunk: */
|
|
|
|
char data[LOBLKSIZE + VARHDRSZ];
|
|
|
|
/* ensure union is aligned well enough: */
|
|
|
|
int32 align_it;
|
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 */
|
2017-11-09 18:56:07 +01:00
|
|
|
if ((obj_desc->flags & IFS_WRLOCK) == 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("permission denied for large object %u",
|
|
|
|
obj_desc->id)));
|
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
|
|
|
{
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02: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
|
2014-05-06 18:12:18 +02:00
|
|
|
* data in it. Otherwise if we're in a hole, we need to create a page to
|
2007-11-15 22:14:46 +01:00
|
|
|
* 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 */
|
2014-06-05 17:31:06 +02:00
|
|
|
bytea *datafield;
|
2007-11-15 22:14:46 +01:00
|
|
|
int pagelen;
|
2014-06-05 17:31:06 +02:00
|
|
|
bool pfreeit;
|
2007-03-03 20:52:47 +01:00
|
|
|
|
2014-06-05 17:31:06 +02:00
|
|
|
getdatafield(olddata, &datafield, &pagelen, &pfreeit);
|
2007-03-03 20:52:47 +01:00
|
|
|
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);
|
2017-02-01 23:18:36 +01:00
|
|
|
CatalogTupleUpdateWithInfo(lo_heap_r, &newtup->t_self, newtup,
|
|
|
|
indstate);
|
2007-03-03 20:52:47 +01:00
|
|
|
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);
|
2017-02-01 22:13:30 +01:00
|
|
|
CatalogTupleDelete(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);
|
2017-02-01 23:18:36 +01:00
|
|
|
CatalogTupleInsertWithInfo(lo_heap_r, newtup, indstate);
|
2007-03-03 20:52:47 +01:00
|
|
|
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)
|
|
|
|
{
|
2017-02-01 22:13:30 +01:00
|
|
|
CatalogTupleDelete(lo_heap_r, &oldtuple->t_self);
|
2011-01-27 01:33:50 +01:00
|
|
|
}
|
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();
|
|
|
|
}
|