2006-07-31 03:16:38 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* toasting.c
|
|
|
|
* This file contains routines to support creation of toast tables
|
|
|
|
*
|
|
|
|
*
|
2015-01-06 17:43:47 +01:00
|
|
|
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
|
2006-07-31 03:16:38 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/catalog/toasting.c
|
2006-07-31 03:16:38 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/tuptoaster.h"
|
|
|
|
#include "access/xact.h"
|
2013-12-19 22:10:01 +01:00
|
|
|
#include "catalog/binary_upgrade.h"
|
2006-07-31 03:16:38 +02:00
|
|
|
#include "catalog/dependency.h"
|
|
|
|
#include "catalog/heap.h"
|
|
|
|
#include "catalog/index.h"
|
2007-07-26 00:16:18 +02:00
|
|
|
#include "catalog/namespace.h"
|
2006-07-31 03:16:38 +02:00
|
|
|
#include "catalog/pg_namespace.h"
|
|
|
|
#include "catalog/pg_opclass.h"
|
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "catalog/toasting.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "nodes/makefuncs.h"
|
2014-04-06 17:13:43 +02:00
|
|
|
#include "storage/lock.h"
|
2006-07-31 03:16:38 +02:00
|
|
|
#include "utils/builtins.h"
|
2011-02-23 18:18:09 +01:00
|
|
|
#include "utils/rel.h"
|
2006-07-31 03:16:38 +02:00
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
2015-03-11 03:33:25 +01:00
|
|
|
/* Potentially set by pg_upgrade_support functions */
|
2011-01-08 03:25:34 +01:00
|
|
|
Oid binary_upgrade_next_toast_pg_type_oid = InvalidOid;
|
2010-02-03 02:14:17 +01:00
|
|
|
|
2014-04-06 17:13:43 +02:00
|
|
|
static void CheckAndCreateToastTable(Oid relOid, Datum reloptions,
|
2014-05-06 18:12:18 +02:00
|
|
|
LOCKMODE lockmode, bool check);
|
2009-02-02 20:31:40 +01:00
|
|
|
static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
|
2014-05-06 18:12:18 +02:00
|
|
|
Datum reloptions, LOCKMODE lockmode, bool check);
|
2006-07-31 03:16:38 +02:00
|
|
|
static bool needs_toast_table(Relation rel);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2014-04-06 17:13:43 +02:00
|
|
|
* CreateToastTable variants
|
2006-07-31 03:16:38 +02:00
|
|
|
* If the table needs a toast table, and doesn't already have one,
|
2010-01-06 04:04:03 +01:00
|
|
|
* then create a toast table for it.
|
2009-06-11 22:46:11 +02:00
|
|
|
*
|
2009-05-08 00:58:28 +02:00
|
|
|
* reloptions for the toast table can be passed, too. Pass (Datum) 0
|
|
|
|
* for default reloptions.
|
2006-07-31 03:16:38 +02:00
|
|
|
*
|
|
|
|
* We expect the caller to have verified that the relation is a table and have
|
|
|
|
* already done any necessary permission checks. Callers expect this function
|
|
|
|
* to end with CommandCounterIncrement if it makes any changes.
|
|
|
|
*/
|
|
|
|
void
|
2014-04-06 17:13:43 +02:00
|
|
|
AlterTableCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
|
|
|
|
{
|
|
|
|
CheckAndCreateToastTable(relOid, reloptions, lockmode, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NewHeapCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode)
|
|
|
|
{
|
|
|
|
CheckAndCreateToastTable(relOid, reloptions, lockmode, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NewRelationCreateToastTable(Oid relOid, Datum reloptions)
|
|
|
|
{
|
|
|
|
CheckAndCreateToastTable(relOid, reloptions, AccessExclusiveLock, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
CheckAndCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode, bool check)
|
2006-07-31 03:16:38 +02:00
|
|
|
{
|
|
|
|
Relation rel;
|
|
|
|
|
2014-04-06 17:13:43 +02:00
|
|
|
rel = heap_open(relOid, lockmode);
|
2006-07-31 03:16:38 +02:00
|
|
|
|
|
|
|
/* create_toast_table does all the work */
|
2014-04-06 17:13:43 +02:00
|
|
|
(void) create_toast_table(rel, InvalidOid, InvalidOid, reloptions, lockmode, check);
|
2006-07-31 03:16:38 +02:00
|
|
|
|
|
|
|
heap_close(rel, NoLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a toast table during bootstrap
|
|
|
|
*
|
|
|
|
* Here we need to prespecify the OIDs of the toast table and its index
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
BootstrapToastTable(char *relName, Oid toastOid, Oid toastIndexOid)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
Relation rel;
|
2006-07-31 03:16:38 +02:00
|
|
|
|
2008-09-01 22:42:46 +02:00
|
|
|
rel = heap_openrv(makeRangeVar(NULL, relName, -1), AccessExclusiveLock);
|
2006-07-31 03:16:38 +02:00
|
|
|
|
2013-03-04 01:23:31 +01:00
|
|
|
if (rel->rd_rel->relkind != RELKIND_RELATION &&
|
|
|
|
rel->rd_rel->relkind != RELKIND_MATVIEW)
|
2006-07-31 03:16:38 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
2013-03-04 01:23:31 +01:00
|
|
|
errmsg("\"%s\" is not a table or materialized view",
|
2006-07-31 03:16:38 +02:00
|
|
|
relName)));
|
|
|
|
|
|
|
|
/* create_toast_table does all the work */
|
2014-04-06 17:13:43 +02:00
|
|
|
if (!create_toast_table(rel, toastOid, toastIndexOid, (Datum) 0,
|
2014-05-06 18:12:18 +02:00
|
|
|
AccessExclusiveLock, false))
|
2006-07-31 03:16:38 +02:00
|
|
|
elog(ERROR, "\"%s\" does not require a toast table",
|
|
|
|
relName);
|
|
|
|
|
|
|
|
heap_close(rel, NoLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create_toast_table --- internal workhorse
|
|
|
|
*
|
2011-04-14 03:07:14 +02:00
|
|
|
* rel is already opened and locked
|
2010-01-06 04:04:03 +01:00
|
|
|
* toastOid and toastIndexOid are normally InvalidOid, but during
|
|
|
|
* bootstrap they can be nonzero to specify hand-assigned OIDs
|
2006-07-31 03:16:38 +02:00
|
|
|
*/
|
|
|
|
static bool
|
2014-04-06 17:13:43 +02:00
|
|
|
create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
|
|
|
|
Datum reloptions, LOCKMODE lockmode, bool check)
|
2006-07-31 03:16:38 +02:00
|
|
|
{
|
|
|
|
Oid relOid = RelationGetRelid(rel);
|
|
|
|
HeapTuple reltup;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
bool shared_relation;
|
2010-02-07 21:48:13 +01:00
|
|
|
bool mapped_relation;
|
2011-01-25 21:42:03 +01:00
|
|
|
Relation toast_rel;
|
2006-07-31 03:16:38 +02:00
|
|
|
Relation class_rel;
|
|
|
|
Oid toast_relid;
|
2009-12-24 23:09:24 +01:00
|
|
|
Oid toast_typid = InvalidOid;
|
2007-07-26 00:16:18 +02:00
|
|
|
Oid namespaceid;
|
2006-07-31 03:16:38 +02:00
|
|
|
char toast_relname[NAMEDATALEN];
|
|
|
|
char toast_idxname[NAMEDATALEN];
|
|
|
|
IndexInfo *indexInfo;
|
2011-02-08 22:04:18 +01:00
|
|
|
Oid collationObjectId[2];
|
2006-07-31 03:16:38 +02:00
|
|
|
Oid classObjectId[2];
|
2007-01-09 03:14:16 +01:00
|
|
|
int16 coloptions[2];
|
2006-07-31 03:16:38 +02:00
|
|
|
ObjectAddress baseobject,
|
|
|
|
toastobject;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Toast table is shared if and only if its parent is.
|
|
|
|
*
|
|
|
|
* We cannot allow toasting a shared relation after initdb (because
|
|
|
|
* there's no way to mark it toasted in other databases' pg_class).
|
|
|
|
*/
|
|
|
|
shared_relation = rel->rd_rel->relisshared;
|
|
|
|
if (shared_relation && !IsBootstrapProcessingMode())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("shared tables cannot be toasted after initdb")));
|
|
|
|
|
2010-02-07 21:48:13 +01:00
|
|
|
/* It's mapped if and only if its parent is, too */
|
|
|
|
mapped_relation = RelationIsMapped(rel);
|
|
|
|
|
2006-07-31 03:16:38 +02:00
|
|
|
/*
|
|
|
|
* Is it already toasted?
|
|
|
|
*/
|
|
|
|
if (rel->rd_rel->reltoastrelid != InvalidOid)
|
|
|
|
return false;
|
|
|
|
|
2014-08-07 20:56:13 +02:00
|
|
|
if (!IsBinaryUpgrade)
|
|
|
|
{
|
|
|
|
if (!needs_toast_table(rel))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Check to see whether the table needs a TOAST table.
|
|
|
|
*
|
|
|
|
* If an update-in-place TOAST relfilenode is specified, force TOAST file
|
|
|
|
* creation even if it seems not to need one. This handles the case
|
|
|
|
* where the old cluster needed a TOAST table but the new cluster
|
|
|
|
* would not normally create one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a TOAST oid is not specified, skip TOAST creation as we will do
|
|
|
|
* it later so we don't create a TOAST table whose OID later conflicts
|
|
|
|
* with a user-supplied OID. This handles cases where the old cluster
|
|
|
|
* didn't need a TOAST table, but the new cluster does.
|
|
|
|
*/
|
|
|
|
if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a special TOAST value has been passed in, it means we are in
|
|
|
|
* cleanup mode --- we are creating needed TOAST tables after all user
|
|
|
|
* tables with specified OIDs have been created. We let the system
|
|
|
|
* assign a TOAST oid for us. The tables are empty so the missing
|
|
|
|
* TOAST tables were not a problem.
|
|
|
|
*/
|
|
|
|
if (binary_upgrade_next_toast_pg_class_oid == OPTIONALLY_CREATE_TOAST_OID)
|
|
|
|
{
|
|
|
|
/* clear as it is not to be used; it is just a flag */
|
|
|
|
binary_upgrade_next_toast_pg_class_oid = InvalidOid;
|
|
|
|
|
|
|
|
if (!needs_toast_table(rel))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* both should be set, or not set */
|
|
|
|
Assert(OidIsValid(binary_upgrade_next_toast_pg_class_oid) ==
|
|
|
|
OidIsValid(binary_upgrade_next_toast_pg_type_oid));
|
|
|
|
}
|
2006-07-31 03:16:38 +02:00
|
|
|
|
2014-04-06 17:13:43 +02:00
|
|
|
/*
|
2014-05-06 18:12:18 +02:00
|
|
|
* If requested check lockmode is sufficient. This is a cross check in
|
|
|
|
* case of errors or conflicting decisions in earlier code.
|
2014-04-06 17:13:43 +02:00
|
|
|
*/
|
|
|
|
if (check && lockmode != AccessExclusiveLock)
|
|
|
|
elog(ERROR, "AccessExclusiveLock required to add toast table.");
|
|
|
|
|
2006-07-31 03:16:38 +02:00
|
|
|
/*
|
|
|
|
* Create the toast table and its index
|
|
|
|
*/
|
|
|
|
snprintf(toast_relname, sizeof(toast_relname),
|
|
|
|
"pg_toast_%u", relOid);
|
|
|
|
snprintf(toast_idxname, sizeof(toast_idxname),
|
|
|
|
"pg_toast_%u_index", relOid);
|
|
|
|
|
|
|
|
/* this is pretty painful... need a tuple descriptor */
|
|
|
|
tupdesc = CreateTemplateTupleDesc(3, false);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 1,
|
|
|
|
"chunk_id",
|
|
|
|
OIDOID,
|
|
|
|
-1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 2,
|
|
|
|
"chunk_seq",
|
|
|
|
INT4OID,
|
|
|
|
-1, 0);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 3,
|
|
|
|
"chunk_data",
|
|
|
|
BYTEAOID,
|
|
|
|
-1, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that the toast table doesn't itself get toasted, or we'll be
|
|
|
|
* toast :-(. This is essential for chunk_data because type bytea is
|
|
|
|
* toastable; hit the other two just to be sure.
|
|
|
|
*/
|
|
|
|
tupdesc->attrs[0]->attstorage = 'p';
|
|
|
|
tupdesc->attrs[1]->attstorage = 'p';
|
|
|
|
tupdesc->attrs[2]->attstorage = 'p';
|
|
|
|
|
|
|
|
/*
|
2007-07-26 00:16:18 +02:00
|
|
|
* Toast tables for regular relations go in pg_toast; those for temp
|
|
|
|
* relations go into the per-backend temp-toast-table namespace.
|
|
|
|
*/
|
2014-08-26 03:28:19 +02:00
|
|
|
if (isTempOrTempToastNamespace(rel->rd_rel->relnamespace))
|
2007-07-26 00:16:18 +02:00
|
|
|
namespaceid = GetTempToastNamespace();
|
|
|
|
else
|
|
|
|
namespaceid = PG_TOAST_NAMESPACE;
|
|
|
|
|
2014-08-26 04:19:05 +02:00
|
|
|
/*
|
|
|
|
* Use binary-upgrade override for pg_type.oid, if supplied. We might
|
|
|
|
* be in the post-schema-restore phase where we are doing ALTER TABLE
|
|
|
|
* to create TOAST tables that didn't exist in the old cluster.
|
|
|
|
*/
|
2011-04-25 18:00:21 +02:00
|
|
|
if (IsBinaryUpgrade && OidIsValid(binary_upgrade_next_toast_pg_type_oid))
|
2009-12-24 23:09:24 +01:00
|
|
|
{
|
2011-01-08 03:25:34 +01:00
|
|
|
toast_typid = binary_upgrade_next_toast_pg_type_oid;
|
|
|
|
binary_upgrade_next_toast_pg_type_oid = InvalidOid;
|
2009-12-24 23:09:24 +01:00
|
|
|
}
|
|
|
|
|
2006-07-31 03:16:38 +02:00
|
|
|
toast_relid = heap_create_with_catalog(toast_relname,
|
2007-07-26 00:16:18 +02:00
|
|
|
namespaceid,
|
2006-07-31 03:16:38 +02:00
|
|
|
rel->rd_rel->reltablespace,
|
|
|
|
toastOid,
|
2009-12-24 23:09:24 +01:00
|
|
|
toast_typid,
|
2010-01-29 00:21:13 +01:00
|
|
|
InvalidOid,
|
2006-07-31 03:16:38 +02:00
|
|
|
rel->rd_rel->relowner,
|
|
|
|
tupdesc,
|
2008-05-10 01:32:05 +02:00
|
|
|
NIL,
|
2006-07-31 03:16:38 +02:00
|
|
|
RELKIND_TOASTVALUE,
|
2010-12-13 18:34:26 +01:00
|
|
|
rel->rd_rel->relpersistence,
|
2006-07-31 03:16:38 +02:00
|
|
|
shared_relation,
|
2010-02-07 21:48:13 +01:00
|
|
|
mapped_relation,
|
2006-07-31 03:16:38 +02:00
|
|
|
true,
|
|
|
|
0,
|
|
|
|
ONCOMMIT_NOOP,
|
2009-02-02 20:31:40 +01:00
|
|
|
reloptions,
|
2009-10-05 21:24:49 +02:00
|
|
|
false,
|
2012-10-23 23:07:26 +02:00
|
|
|
true,
|
Change many routines to return ObjectAddress rather than OID
The changed routines are mostly those that can be directly called by
ProcessUtilitySlow; the intention is to make the affected object
information more precise, in support for future event trigger changes.
Originally it was envisioned that the OID of the affected object would
be enough, and in most cases that is correct, but upon actually
implementing the event trigger changes it turned out that ObjectAddress
is more widely useful.
Additionally, some command execution routines grew an output argument
that's an object address which provides further info about the executed
command. To wit:
* for ALTER DOMAIN / ADD CONSTRAINT, it corresponds to the address of
the new constraint
* for ALTER OBJECT / SET SCHEMA, it corresponds to the address of the
schema that originally contained the object.
* for ALTER EXTENSION {ADD, DROP} OBJECT, it corresponds to the address
of the object added to or dropped from the extension.
There's no user-visible change in this commit, and no functional change
either.
Discussion: 20150218213255.GC6717@tamriel.snowman.net
Reviewed-By: Stephen Frost, Andres Freund
2015-03-03 18:10:50 +01:00
|
|
|
true,
|
|
|
|
NULL);
|
2010-07-26 01:21:22 +02:00
|
|
|
Assert(toast_relid != InvalidOid);
|
2006-07-31 03:16:38 +02:00
|
|
|
|
2011-01-25 21:42:03 +01:00
|
|
|
/* make the toast relation visible, else heap_open will fail */
|
2006-07-31 03:16:38 +02:00
|
|
|
CommandCounterIncrement();
|
|
|
|
|
2011-01-25 21:42:03 +01:00
|
|
|
/* ShareLock is not really needed here, but take it anyway */
|
|
|
|
toast_rel = heap_open(toast_relid, ShareLock);
|
|
|
|
|
2006-07-31 03:16:38 +02:00
|
|
|
/*
|
|
|
|
* Create unique index on chunk_id, chunk_seq.
|
|
|
|
*
|
|
|
|
* NOTE: the normal TOAST access routines could actually function with a
|
|
|
|
* single-column index on chunk_id only. However, the slice access
|
|
|
|
* routines use both columns for faster access to an individual chunk. In
|
|
|
|
* addition, we want it to be unique as a check against the possibility of
|
|
|
|
* duplicate TOAST chunk OIDs. The index might also be a little more
|
|
|
|
* efficient this way, since btree isn't all that happy with large numbers
|
|
|
|
* of equal keys.
|
|
|
|
*/
|
|
|
|
|
|
|
|
indexInfo = makeNode(IndexInfo);
|
|
|
|
indexInfo->ii_NumIndexAttrs = 2;
|
|
|
|
indexInfo->ii_KeyAttrNumbers[0] = 1;
|
|
|
|
indexInfo->ii_KeyAttrNumbers[1] = 2;
|
|
|
|
indexInfo->ii_Expressions = NIL;
|
|
|
|
indexInfo->ii_ExpressionsState = NIL;
|
|
|
|
indexInfo->ii_Predicate = NIL;
|
|
|
|
indexInfo->ii_PredicateState = NIL;
|
2009-12-07 06:22:23 +01:00
|
|
|
indexInfo->ii_ExclusionOps = NULL;
|
|
|
|
indexInfo->ii_ExclusionProcs = NULL;
|
|
|
|
indexInfo->ii_ExclusionStrats = NULL;
|
2006-07-31 03:16:38 +02:00
|
|
|
indexInfo->ii_Unique = true;
|
2007-09-20 19:56:33 +02:00
|
|
|
indexInfo->ii_ReadyForInserts = true;
|
2006-08-25 06:06:58 +02:00
|
|
|
indexInfo->ii_Concurrent = false;
|
2007-09-20 19:56:33 +02:00
|
|
|
indexInfo->ii_BrokenHotChain = false;
|
2006-07-31 03:16:38 +02:00
|
|
|
|
2011-02-08 22:04:18 +01:00
|
|
|
collationObjectId[0] = InvalidOid;
|
|
|
|
collationObjectId[1] = InvalidOid;
|
|
|
|
|
2006-07-31 03:16:38 +02:00
|
|
|
classObjectId[0] = OID_BTREE_OPS_OID;
|
|
|
|
classObjectId[1] = INT4_BTREE_OPS_OID;
|
|
|
|
|
2007-01-09 03:14:16 +01:00
|
|
|
coloptions[0] = 0;
|
|
|
|
coloptions[1] = 0;
|
|
|
|
|
2011-07-18 17:02:48 +02:00
|
|
|
index_create(toast_rel, toast_idxname, toastIndexOid, InvalidOid,
|
2011-06-09 20:32:50 +02:00
|
|
|
indexInfo,
|
|
|
|
list_make2("chunk_id", "chunk_seq"),
|
|
|
|
BTREE_AM_OID,
|
|
|
|
rel->rd_rel->reltablespace,
|
|
|
|
collationObjectId, classObjectId, coloptions, (Datum) 0,
|
|
|
|
true, false, false, false,
|
2014-11-06 10:48:33 +01:00
|
|
|
true, false, false, true, false);
|
2006-07-31 03:16:38 +02:00
|
|
|
|
2011-01-25 21:42:03 +01:00
|
|
|
heap_close(toast_rel, NoLock);
|
|
|
|
|
2006-07-31 03:16:38 +02:00
|
|
|
/*
|
|
|
|
* Store the toast table's OID in the parent relation's pg_class row
|
|
|
|
*/
|
|
|
|
class_rel = heap_open(RelationRelationId, RowExclusiveLock);
|
|
|
|
|
2010-02-14 19:42:19 +01:00
|
|
|
reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relOid));
|
2006-07-31 03:16:38 +02:00
|
|
|
if (!HeapTupleIsValid(reltup))
|
|
|
|
elog(ERROR, "cache lookup failed for relation %u", relOid);
|
|
|
|
|
|
|
|
((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid;
|
|
|
|
|
|
|
|
if (!IsBootstrapProcessingMode())
|
|
|
|
{
|
|
|
|
/* normal case, use a transactional update */
|
|
|
|
simple_heap_update(class_rel, &reltup->t_self, reltup);
|
|
|
|
|
|
|
|
/* Keep catalog indexes current */
|
|
|
|
CatalogUpdateIndexes(class_rel, reltup);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* While bootstrapping, we cannot UPDATE, so overwrite in-place */
|
|
|
|
heap_inplace_update(class_rel, reltup);
|
|
|
|
}
|
|
|
|
|
|
|
|
heap_freetuple(reltup);
|
|
|
|
|
|
|
|
heap_close(class_rel, RowExclusiveLock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register dependency from the toast table to the master, so that the
|
|
|
|
* toast table will be deleted if the master is. Skip this in bootstrap
|
|
|
|
* mode.
|
|
|
|
*/
|
|
|
|
if (!IsBootstrapProcessingMode())
|
|
|
|
{
|
|
|
|
baseobject.classId = RelationRelationId;
|
|
|
|
baseobject.objectId = relOid;
|
|
|
|
baseobject.objectSubId = 0;
|
|
|
|
toastobject.classId = RelationRelationId;
|
|
|
|
toastobject.objectId = toast_relid;
|
|
|
|
toastobject.objectSubId = 0;
|
|
|
|
|
|
|
|
recordDependencyOn(&toastobject, &baseobject, DEPENDENCY_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make changes visible
|
|
|
|
*/
|
|
|
|
CommandCounterIncrement();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-05-06 18:12:18 +02:00
|
|
|
* Check to see whether the table needs a TOAST table. It does only if
|
2006-07-31 03:16:38 +02:00
|
|
|
* (1) there are any toastable attributes, and (2) the maximum length
|
|
|
|
* of a tuple could exceed TOAST_TUPLE_THRESHOLD. (We don't want to
|
|
|
|
* create a toast table for something like "f1 varchar(20)".)
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
needs_toast_table(Relation rel)
|
|
|
|
{
|
|
|
|
int32 data_length = 0;
|
|
|
|
bool maxlength_unknown = false;
|
|
|
|
bool has_toastable_attrs = false;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
Form_pg_attribute *att;
|
|
|
|
int32 tuple_length;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
tupdesc = rel->rd_att;
|
|
|
|
att = tupdesc->attrs;
|
|
|
|
|
|
|
|
for (i = 0; i < tupdesc->natts; i++)
|
|
|
|
{
|
|
|
|
if (att[i]->attisdropped)
|
|
|
|
continue;
|
2007-04-06 06:21:44 +02:00
|
|
|
data_length = att_align_nominal(data_length, att[i]->attalign);
|
2006-07-31 03:16:38 +02:00
|
|
|
if (att[i]->attlen > 0)
|
|
|
|
{
|
|
|
|
/* Fixed-length types are never toastable */
|
|
|
|
data_length += att[i]->attlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int32 maxlen = type_maximum_size(att[i]->atttypid,
|
|
|
|
att[i]->atttypmod);
|
|
|
|
|
|
|
|
if (maxlen < 0)
|
|
|
|
maxlength_unknown = true;
|
|
|
|
else
|
|
|
|
data_length += maxlen;
|
|
|
|
if (att[i]->attstorage != 'p')
|
|
|
|
has_toastable_attrs = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!has_toastable_attrs)
|
|
|
|
return false; /* nothing to toast? */
|
|
|
|
if (maxlength_unknown)
|
|
|
|
return true; /* any unlimited-length attrs? */
|
2015-02-21 21:13:06 +01:00
|
|
|
tuple_length = MAXALIGN(SizeofHeapTupleHeader +
|
2006-07-31 03:16:38 +02:00
|
|
|
BITMAPLEN(tupdesc->natts)) +
|
|
|
|
MAXALIGN(data_length);
|
|
|
|
return (tuple_length > TOAST_TUPLE_THRESHOLD);
|
|
|
|
}
|