diff options
author | Tom Lane | 2006-07-31 01:16:38 +0000 |
---|---|---|
committer | Tom Lane | 2006-07-31 01:16:38 +0000 |
commit | f122d0d06d0b2d7192865551e53ca37654570350 (patch) | |
tree | b8044c2fd1bf970257f1d085ccc6cb8aef863a72 | |
parent | 4415749c24a14de5f9e28cf1db443b61c5af1cdb (diff) |
Change the bootstrap sequence so that toast tables for system catalogs are
created in the bootstrap phase proper, rather than added after-the-fact
by initdb. This is cleaner than before because it allows us to retire the
undocumented ALTER TABLE ... CREATE TOAST TABLE command, but the real reason
I'm doing it is so that toast tables of shared catalogs will now have
predetermined OIDs. This will allow a reasonably clean solution to the
problem of locking tables before we load their relcache entries, to appear
in a forthcoming patch.
26 files changed, 505 insertions, 382 deletions
diff --git a/doc/src/sgml/bki.sgml b/doc/src/sgml/bki.sgml index 7b75b086bd..da59edb8e9 100644 --- a/doc/src/sgml/bki.sgml +++ b/doc/src/sgml/bki.sgml @@ -210,6 +210,28 @@ </varlistentry> <varlistentry> + <term> + <literal>declare toast</> + <replaceable class="parameter">toasttableoid</replaceable> + <replaceable class="parameter">toastindexoid</replaceable> + <literal>on</> <replaceable class="parameter">tablename</replaceable> + </term> + + <listitem> + <para> + Create a TOAST table for the table named + <replaceable class="parameter">tablename</replaceable>. + The TOAST table is assigned OID + <replaceable class="parameter">toasttableoid</replaceable> + and its index is assigned OID + <replaceable class="parameter">toastindexoid</replaceable>. + As with <literal>declare index</>, filling of the index + is postponed. + </para> + </listitem> + </varlistentry> + + <varlistentry> <term><literal>build indices</></term> <listitem> @@ -236,6 +258,12 @@ </para> <para> + Also, the <literal>declare index</> and <literal>declare toast</> + commands cannot be used until the system catalogs they need have been + created and filled in. + </para> + + <para> Thus, the structure of the <filename>postgres.bki</filename> file has to be: <orderedlist> @@ -286,7 +314,7 @@ </listitem> <listitem> <para> - Define indexes. + Define indexes and toast tables. </para> </listitem> <listitem> diff --git a/doc/src/sgml/keywords.sgml b/doc/src/sgml/keywords.sgml index 744668e6e6..b27ef3065e 100644 --- a/doc/src/sgml/keywords.sgml +++ b/doc/src/sgml/keywords.sgml @@ -4014,13 +4014,6 @@ <entry>reserved</entry> </row> <row> - <entry><token>TOAST</token></entry> - <entry>non-reserved</entry> - <entry></entry> - <entry></entry> - <entry></entry> - </row> - <row> <entry><token>TOP_LEVEL_COUNT</token></entry> <entry></entry> <entry>non-reserved</entry> diff --git a/src/backend/bootstrap/bootparse.y b/src/backend/bootstrap/bootparse.y index e0fcf44f08..4217f2e06f 100644 --- a/src/backend/bootstrap/bootparse.y +++ b/src/backend/bootstrap/bootparse.y @@ -33,6 +33,7 @@ #include "catalog/pg_class.h" #include "catalog/pg_namespace.h" #include "catalog/pg_tablespace.h" +#include "catalog/toasting.h" #include "commands/defrem.h" #include "miscadmin.h" #include "nodes/makefuncs.h" @@ -99,7 +100,7 @@ int num_columns_read = 0; %token <ival> CONST_P ID %token OPEN XCLOSE XCREATE INSERT_TUPLE -%token XDECLARE INDEX ON USING XBUILD INDICES UNIQUE +%token XDECLARE INDEX ON USING XBUILD INDICES UNIQUE XTOAST %token COMMA EQUALS LPAREN RPAREN %token OBJ_ID XBOOTSTRAP XSHARED_RELATION XWITHOUT_OIDS NULLVAL %start TopLevel @@ -126,6 +127,7 @@ Boot_Query : | Boot_InsertStmt | Boot_DeclareIndexStmt | Boot_DeclareUniqueIndexStmt + | Boot_DeclareToastStmt | Boot_BuildIndsStmt ; @@ -278,6 +280,16 @@ Boot_DeclareUniqueIndexStmt: } ; +Boot_DeclareToastStmt: + XDECLARE XTOAST oidspec oidspec ON boot_ident + { + do_start(); + + BootstrapToastTable(LexIDStr($6), $3, $4); + do_end(); + } + ; + Boot_BuildIndsStmt: XBUILD INDICES { diff --git a/src/backend/bootstrap/bootscanner.l b/src/backend/bootstrap/bootscanner.l index 1101050457..467a9e2a83 100644 --- a/src/backend/bootstrap/bootscanner.l +++ b/src/backend/bootstrap/bootscanner.l @@ -99,6 +99,7 @@ insert { return(INSERT_TUPLE); } "index" { return(INDEX); } "on" { return(ON); } "using" { return(USING); } +"toast" { return(XTOAST); } {arrayid} { yylval.ival = EnterString(MapArrayTypeName((char*)yytext)); diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c index e877bc30b0..69fa74dacd 100644 --- a/src/backend/bootstrap/bootstrap.c +++ b/src/backend/bootstrap/bootstrap.c @@ -1242,7 +1242,7 @@ build_indices(void) heap = heap_open(ILHead->il_heap, NoLock); ind = index_open(ILHead->il_ind); - index_build(heap, ind, ILHead->il_info, false, false); + index_build(heap, ind, ILHead->il_info, false); index_close(ind); heap_close(heap, NoLock); diff --git a/src/backend/catalog/Makefile b/src/backend/catalog/Makefile index 6cd535306a..8f8672a816 100644 --- a/src/backend/catalog/Makefile +++ b/src/backend/catalog/Makefile @@ -13,7 +13,7 @@ include $(top_builddir)/src/Makefile.global OBJS = catalog.o dependency.o heap.o index.o indexing.o namespace.o aclchk.o \ pg_aggregate.o pg_constraint.o pg_conversion.o pg_depend.o \ pg_largeobject.o pg_namespace.o pg_operator.o pg_proc.o pg_shdepend.o \ - pg_type.o + pg_type.o toasting.o BKIFILES = postgres.bki postgres.description postgres.shdescription @@ -24,7 +24,7 @@ SUBSYS.o: $(OBJS) # Note: there are some undocumented dependencies on the ordering in which # the catalog header files are assembled into postgres.bki. In particular, -# indexing.h had better be last. +# indexing.h had better be last, and toasting.h just before it. POSTGRES_BKI_SRCS := $(addprefix $(top_srcdir)/src/include/catalog/,\ pg_proc.h pg_type.h pg_attribute.h pg_class.h pg_autovacuum.h \ @@ -35,7 +35,7 @@ POSTGRES_BKI_SRCS := $(addprefix $(top_srcdir)/src/include/catalog/,\ pg_namespace.h pg_conversion.h pg_depend.h \ pg_database.h pg_tablespace.h pg_pltemplate.h \ pg_authid.h pg_auth_members.h pg_shdepend.h pg_shdescription.h \ - indexing.h \ + toasting.h indexing.h \ ) pg_includes := $(sort -I$(top_srcdir)/src/include -I$(top_builddir)/src/include) diff --git a/src/backend/catalog/README b/src/backend/catalog/README index af25a32e69..c84c2d9431 100644 --- a/src/backend/catalog/README +++ b/src/backend/catalog/README @@ -73,8 +73,9 @@ heap_create_with_catalog process, because it needs these tables to exist already. The list of files this currently includes is: pg_proc.h pg_type.h pg_attribute.h pg_class.h Also, indexing.h must be last, since the indexes can't be created until all -the tables are in place. There are reputedly some other order dependencies -in the .bki list, too. +the tables are in place, and toasting.h should probably be next-to-last +(or at least after all the tables that need toast tables). There are +reputedly some other order dependencies in the .bki list, too. ----------------------------------------------------------------- diff --git a/src/backend/catalog/genbki.sh b/src/backend/catalog/genbki.sh index 9011285080..f90e0cb7ac 100644 --- a/src/backend/catalog/genbki.sh +++ b/src/backend/catalog/genbki.sh @@ -276,6 +276,28 @@ comment_level > 0 { next; } print "declare unique index " iname " " oid " " data } +/^DECLARE_TOAST\(/ { +# ---- +# end any prior catalog data insertions before starting a define toast +# ---- + if (reln_open == 1) { + print "close " catalog; + reln_open = 0; + } + + data = substr($0, 15, length($0) - 15); + pos = index(data, ","); + tname = substr(data, 1, pos-1); + data = substr(data, pos+1, length(data)-pos); + pos = index(data, ","); + toastoid = substr(data, 1, pos-1); + data = substr(data, pos+1, length(data)-pos); + # previous commands already removed the trailing ); + indexoid = data; + + print "declare toast " toastoid " " indexoid " on " tname +} + /^BUILD_INDICES/ { print "build indices"; } # ---------------- diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 3f042383c9..6300d2ccba 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -2009,8 +2009,8 @@ RelationTruncateIndexes(Oid heapId) RelationTruncate(currentIndex, 0); /* Initialize the index and rebuild */ - /* Note: we do not need to re-establish pkey or toast settings */ - index_build(heapRelation, currentIndex, indexInfo, false, false); + /* Note: we do not need to re-establish pkey setting */ + index_build(heapRelation, currentIndex, indexInfo, false); /* We're done with this index */ index_close(currentIndex); diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index c8265f971d..a78e76b358 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -424,7 +424,6 @@ UpdateIndexRelation(Oid indexoid, * classObjectId: array of index opclass OIDs, one per index column * reloptions: AM-specific options * isprimary: index is a PRIMARY KEY - * istoast: index is a toast table's index * isconstraint: index is owned by a PRIMARY KEY or UNIQUE constraint * allow_system_table_mods: allow table to be a system catalog * skip_build: true to skip the index_build() step for the moment; caller @@ -442,7 +441,6 @@ index_create(Oid heapRelationId, Oid *classObjectId, Datum reloptions, bool isprimary, - bool istoast, bool isconstraint, bool allow_system_table_mods, bool skip_build) @@ -747,8 +745,7 @@ index_create(Oid heapRelationId, } else { - index_build(heapRelation, indexRelation, indexInfo, - isprimary, istoast); + index_build(heapRelation, indexRelation, indexInfo, isprimary); } /* @@ -1241,8 +1238,8 @@ setNewRelfilenode(Relation relation) * entries of the index and heap relation as needed, using statistics * returned by ambuild as well as data passed by the caller. * - * Note: when reindexing an existing index, isprimary and istoast can be - * false; the index is already properly marked and need not be re-marked. + * Note: when reindexing an existing index, isprimary can be false; + * the index is already properly marked and need not be re-marked. * * Note: before Postgres 8.2, the passed-in heap and index Relations * were automatically closed by this routine. This is no longer the case. @@ -1252,8 +1249,7 @@ void index_build(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, - bool isprimary, - bool istoast) + bool isprimary) { RegProcedure procedure; IndexBuildResult *stats; @@ -1283,7 +1279,8 @@ index_build(Relation heapRelation, index_update_stats(heapRelation, true, isprimary, - istoast ? RelationGetRelid(indexRelation) : InvalidOid, + (heapRelation->rd_rel->relkind == RELKIND_TOASTVALUE) ? + RelationGetRelid(indexRelation) : InvalidOid, stats->heap_tuples); index_update_stats(indexRelation, @@ -1618,8 +1615,8 @@ reindex_index(Oid indexId) } /* Initialize the index and rebuild */ - /* Note: we do not need to re-establish pkey or toast settings */ - index_build(heapRelation, iRel, indexInfo, false, false); + /* Note: we do not need to re-establish pkey setting */ + index_build(heapRelation, iRel, indexInfo, false); } PG_CATCH(); { diff --git a/src/backend/catalog/toasting.c b/src/backend/catalog/toasting.c new file mode 100644 index 0000000000..ab45ce8584 --- /dev/null +++ b/src/backend/catalog/toasting.c @@ -0,0 +1,339 @@ +/*------------------------------------------------------------------------- + * + * toasting.c + * This file contains routines to support creation of toast tables + * + * + * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * IDENTIFICATION + * $PostgreSQL$ + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "access/heapam.h" +#include "access/tuptoaster.h" +#include "access/xact.h" +#include "catalog/dependency.h" +#include "catalog/heap.h" +#include "catalog/index.h" +#include "catalog/indexing.h" +#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" +#include "utils/builtins.h" +#include "utils/syscache.h" + + +static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid); +static bool needs_toast_table(Relation rel); + + +/* + * AlterTableCreateToastTable + * If the table needs a toast table, and doesn't already have one, + * then create a toast table for it. + * + * 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 +AlterTableCreateToastTable(Oid relOid) +{ + Relation rel; + + /* + * Grab an exclusive lock on the target table, which we will NOT release + * until end of transaction. (This is probably redundant in all present + * uses...) + */ + rel = heap_open(relOid, AccessExclusiveLock); + + /* create_toast_table does all the work */ + (void) create_toast_table(rel, InvalidOid, InvalidOid); + + 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) +{ + Relation rel; + + rel = heap_openrv(makeRangeVar(NULL, relName), AccessExclusiveLock); + + /* Note: during bootstrap may see uncataloged relation */ + if (rel->rd_rel->relkind != RELKIND_RELATION && + rel->rd_rel->relkind != RELKIND_UNCATALOGED) + ereport(ERROR, + (errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("\"%s\" is not a table", + relName))); + + /* create_toast_table does all the work */ + if (!create_toast_table(rel, toastOid, toastIndexOid)) + elog(ERROR, "\"%s\" does not require a toast table", + relName); + + heap_close(rel, NoLock); +} + + +/* + * create_toast_table --- internal workhorse + * + * rel is already opened and exclusive-locked + * toastOid and toastIndexOid are normally InvalidOid, but during + * bootstrap they can be nonzero to specify hand-assigned OIDs + */ +static bool +create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid) +{ + Oid relOid = RelationGetRelid(rel); + HeapTuple reltup; + TupleDesc tupdesc; + bool shared_relation; + Relation class_rel; + Oid toast_relid; + Oid toast_idxid; + char toast_relname[NAMEDATALEN]; + char toast_idxname[NAMEDATALEN]; + IndexInfo *indexInfo; + Oid classObjectId[2]; + 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"))); + + /* + * Is it already toasted? + */ + if (rel->rd_rel->reltoastrelid != InvalidOid) + return false; + + /* + * Check to see whether the table actually needs a TOAST table. + */ + if (!needs_toast_table(rel)) + return false; + + /* + * 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'; + + /* + * Note: the toast relation is placed in the regular pg_toast namespace + * even if its master relation is a temp table. There cannot be any + * naming collision, and the toast rel will be destroyed when its master + * is, so there's no need to handle the toast rel as temp. + * + * XXX would it make sense to apply the master's reloptions to the toast + * table? + */ + toast_relid = heap_create_with_catalog(toast_relname, + PG_TOAST_NAMESPACE, + rel->rd_rel->reltablespace, + toastOid, + rel->rd_rel->relowner, + tupdesc, + RELKIND_TOASTVALUE, + shared_relation, + true, + 0, + ONCOMMIT_NOOP, + (Datum) 0, + true); + + /* make the toast relation visible, else index creation will fail */ + CommandCounterIncrement(); + + /* + * 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; + indexInfo->ii_Unique = true; + + classObjectId[0] = OID_BTREE_OPS_OID; + classObjectId[1] = INT4_BTREE_OPS_OID; + + toast_idxid = index_create(toast_relid, toast_idxname, toastIndexOid, + indexInfo, + BTREE_AM_OID, + rel->rd_rel->reltablespace, + classObjectId, (Datum) 0, + true, false, true, false); + + /* + * Store the toast table's OID in the parent relation's pg_class row + */ + class_rel = heap_open(RelationRelationId, RowExclusiveLock); + + reltup = SearchSysCacheCopy(RELOID, + ObjectIdGetDatum(relOid), + 0, 0, 0); + 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; +} + +/* + * Check to see whether the table needs a TOAST table. It does only if + * (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; + data_length = att_align(data_length, att[i]->attalign); + 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? */ + tuple_length = MAXALIGN(offsetof(HeapTupleHeaderData, t_bits) + + BITMAPLEN(tupdesc->natts)) + + MAXALIGN(data_length); + return (tuple_length > TOAST_TUPLE_THRESHOLD); +} diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c index 1870ecffce..323df1de90 100644 --- a/src/backend/commands/cluster.c +++ b/src/backend/commands/cluster.c @@ -26,8 +26,8 @@ #include "catalog/index.h" #include "catalog/indexing.h" #include "catalog/namespace.h" +#include "catalog/toasting.h" #include "commands/cluster.h" -#include "commands/tablecmds.h" #include "miscadmin.h" #include "utils/acl.h" #include "utils/fmgroids.h" @@ -620,7 +620,7 @@ make_new_heap(Oid OIDOldHeap, const char *NewName, Oid NewTableSpace) * AlterTableCreateToastTable ends with CommandCounterIncrement(), so that * the TOAST table will be visible for insertion. */ - AlterTableCreateToastTable(OIDNewHeap, true); + AlterTableCreateToastTable(OIDNewHeap); heap_close(OldHeap, NoLock); diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index df209ec576..02e552af35 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -412,7 +412,7 @@ DefineIndex(RangeVar *heapRelation, index_create(relationId, indexRelationName, indexRelationId, indexInfo, accessMethodId, tablespaceId, classObjectId, - reloptions, primary, false, isconstraint, + reloptions, primary, isconstraint, allowSystemTableMods, skip_build); } diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index b147bd6897..8caa149469 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -17,7 +17,6 @@ #include "access/genam.h" #include "access/heapam.h" #include "access/reloptions.h" -#include "access/tuptoaster.h" #include "access/xact.h" #include "catalog/catalog.h" #include "catalog/dependency.h" @@ -29,9 +28,9 @@ #include "catalog/pg_depend.h" #include "catalog/pg_inherits.h" #include "catalog/pg_namespace.h" -#include "catalog/pg_opclass.h" #include "catalog/pg_trigger.h" #include "catalog/pg_type.h" +#include "catalog/toasting.h" #include "commands/cluster.h" #include "commands/defrem.h" #include "commands/tablecmds.h" @@ -170,7 +169,6 @@ static void StoreCatalogInheritance1(Oid relationId, Oid parentOid, int16 seqNumber, Relation catalogRelation); static int findAttrByName(const char *attributeName, List *schema); static void setRelhassubclassInRelation(Oid relationId, bool relhassubclass); -static bool needs_toast_table(Relation rel); static void AlterIndexNamespaces(Relation classRel, Relation rel, Oid oldNspOid, Oid newNspOid); static void AlterSeqNamespaces(Relation classRel, Relation rel, @@ -2073,12 +2071,6 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd, ATPrepAlterColumnType(wqueue, tab, rel, recurse, recursing, cmd); pass = AT_PASS_ALTER_TYPE; break; - case AT_ToastTable: /* CREATE TOAST TABLE */ - ATSimplePermissions(rel, false); - /* This command never recurses */ - /* No command-specific prep needed */ - pass = AT_PASS_MISC; - break; case AT_ChangeOwner: /* ALTER OWNER */ /* This command never recurses */ /* No command-specific prep needed */ @@ -2196,8 +2188,8 @@ ATRewriteCatalogs(List **wqueue) } /* - * Do an implicit CREATE TOAST TABLE if we executed any subcommands that - * might have added a column or changed column storage. + * Check to see if a toast table must be added, if we executed any + * subcommands that might have added a column or changed column storage. */ foreach(ltab, *wqueue) { @@ -2207,7 +2199,7 @@ ATRewriteCatalogs(List **wqueue) (tab->subcmds[AT_PASS_ADD_COL] || tab->subcmds[AT_PASS_ALTER_TYPE] || tab->subcmds[AT_PASS_COL_ATTRS])) - AlterTableCreateToastTable(tab->relid, true); + AlterTableCreateToastTable(tab->relid); } } @@ -2261,9 +2253,6 @@ ATExecCmd(AlteredTableInfo *tab, Relation rel, AlterTableCmd *cmd) case AT_AlterColumnType: /* ALTER COLUMN TYPE */ ATExecAlterColumnType(tab, rel, cmd->name, (TypeName *) cmd->def); break; - case AT_ToastTable: /* CREATE TOAST TABLE */ - AlterTableCreateToastTable(RelationGetRelid(rel), false); - break; case AT_ChangeOwner: /* ALTER OWNER */ ATExecChangeOwner(RelationGetRelid(rel), get_roleid_checked(cmd->name), @@ -6542,275 +6531,6 @@ ATExecDropInherits(Relation rel, RangeVar *parent) /* - * ALTER TABLE CREATE TOAST TABLE - * - * Note: this is also invoked from outside this module; in such cases we - * expect the caller to have verified that the relation is a table and we - * have all the right permissions. Callers expect this function - * to end with CommandCounterIncrement if it makes any changes. - */ -void -AlterTableCreateToastTable(Oid relOid, bool silent) -{ - Relation rel; - HeapTuple reltup; - TupleDesc tupdesc; - bool shared_relation; - Relation class_rel; - Oid toast_relid; - Oid toast_idxid; - char toast_relname[NAMEDATALEN]; - char toast_idxname[NAMEDATALEN]; - IndexInfo *indexInfo; - Oid classObjectId[2]; - ObjectAddress baseobject, - toastobject; - - /* - * Grab an exclusive lock on the target table, which we will NOT release - * until end of transaction. (This is probably redundant in all present - * uses...) - */ - rel = heap_open(relOid, AccessExclusiveLock); - - /* - * 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). - * Unfortunately we can't distinguish initdb from a manually started - * standalone backend (toasting happens after the bootstrap phase, so - * checking IsBootstrapProcessingMode() won't work). However, we can at - * least prevent this mistake under normal multi-user operation. - */ - shared_relation = rel->rd_rel->relisshared; - if (shared_relation && IsUnderPostmaster) - ereport(ERROR, - (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), - errmsg("shared tables cannot be toasted after initdb"))); - - /* - * Is it already toasted? - */ - if (rel->rd_rel->reltoastrelid != InvalidOid) - { - if (silent) - { - heap_close(rel, NoLock); - return; - } - - ereport(ERROR, - (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), - errmsg("table \"%s\" already has a TOAST table", - RelationGetRelationName(rel)))); - } - - /* - * Check to see whether the table actually needs a TOAST table. - */ - if (!needs_toast_table(rel)) - { - if (silent) - { - heap_close(rel, NoLock); - return; - } - - ereport(ERROR, - (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), - errmsg("table \"%s\" does not need a TOAST table", - RelationGetRelationName(rel)))); - } - - /* - * 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'; - - /* - * Note: the toast relation is placed in the regular pg_toast namespace - * even if its master relation is a temp table. There cannot be any - * naming collision, and the toast rel will be destroyed when its master - * is, so there's no need to handle the toast rel as temp. - * - * XXX would it make sense to apply the master's reloptions to the toast - * table? - */ - toast_relid = heap_create_with_catalog(toast_relname, - PG_TOAST_NAMESPACE, - rel->rd_rel->reltablespace, - InvalidOid, - rel->rd_rel->relowner, - tupdesc, - RELKIND_TOASTVALUE, - shared_relation, - true, - 0, - ONCOMMIT_NOOP, - (Datum) 0, - true); - - /* make the toast relation visible, else index creation will fail */ - CommandCounterIncrement(); - - /* - * 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; - indexInfo->ii_Unique = true; - - classObjectId[0] = OID_BTREE_OPS_OID; - classObjectId[1] = INT4_BTREE_OPS_OID; - - toast_idxid = index_create(toast_relid, toast_idxname, InvalidOid, - indexInfo, - BTREE_AM_OID, - rel->rd_rel->reltablespace, - classObjectId, (Datum) 0, - true, true, false, true, false); - - /* - * Store the toast table's OID in the parent relation's pg_class row - */ - class_rel = heap_open(RelationRelationId, RowExclusiveLock); - - reltup = SearchSysCacheCopy(RELOID, - ObjectIdGetDatum(relOid), - 0, 0, 0); - if (!HeapTupleIsValid(reltup)) - elog(ERROR, "cache lookup failed for relation %u", relOid); - - ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid; - - simple_heap_update(class_rel, &reltup->t_self, reltup); - - /* Keep catalog indexes current */ - CatalogUpdateIndexes(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. - */ - baseobject.classId = RelationRelationId; - baseobject.objectId = relOid; - baseobject.objectSubId = 0; - toastobject.classId = RelationRelationId; - toastobject.objectId = toast_relid; - toastobject.objectSubId = 0; - - recordDependencyOn(&toastobject, &baseobject, DEPENDENCY_INTERNAL); - - /* - * Clean up and make changes visible - */ - heap_close(rel, NoLock); - - CommandCounterIncrement(); -} - -/* - * Check to see whether the table needs a TOAST table. It does only if - * (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; - data_length = att_align(data_length, att[i]->attalign); - 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? */ - tuple_length = MAXALIGN(offsetof(HeapTupleHeaderData, t_bits) + - BITMAPLEN(tupdesc->natts)) + - MAXALIGN(data_length); - return (tuple_length > TOAST_TUPLE_THRESHOLD); -} - - -/* * Execute ALTER TABLE SET SCHEMA * * Note: caller must have checked ownership of the relation already diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c index 0f7c297e0a..af75f5a030 100644 --- a/src/backend/executor/execMain.c +++ b/src/backend/executor/execMain.c @@ -38,7 +38,7 @@ #include "access/xact.h" #include "catalog/heap.h" #include "catalog/namespace.h" -#include "commands/tablecmds.h" +#include "catalog/toasting.h" #include "commands/tablespace.h" #include "commands/trigger.h" #include "executor/execdebug.h" @@ -821,7 +821,7 @@ InitPlan(QueryDesc *queryDesc, int eflags) * AlterTableCreateToastTable ends with CommandCounterIncrement(), so * that the TOAST table will be visible for insertion. */ - AlterTableCreateToastTable(intoRelationId, true); + AlterTableCreateToastTable(intoRelationId); /* * And open the constructed table for writing. diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index dc9715af23..156adcce71 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -422,7 +422,7 @@ static void doNegateFloat(Value *v); SYSID SYSTEM_P TABLE TABLESPACE TEMP TEMPLATE TEMPORARY THEN TIME TIMESTAMP - TO TOAST TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P + TO TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P TRUNCATE TRUSTED TYPE_P UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL @@ -1446,13 +1446,6 @@ alter_table_cmd: n->subtype = AT_DropOids; $$ = (Node *)n; } - /* ALTER TABLE <name> CREATE TOAST TABLE -- ONLY */ - | CREATE TOAST TABLE - { - AlterTableCmd *n = makeNode(AlterTableCmd); - n->subtype = AT_ToastTable; - $$ = (Node *)n; - } /* ALTER TABLE <name> CLUSTER ON <indexname> */ | CLUSTER ON name { @@ -8649,7 +8642,6 @@ unreserved_keyword: | TEMP | TEMPLATE | TEMPORARY - | TOAST | TRANSACTION | TRIGGER | TRUNCATE diff --git a/src/backend/parser/keywords.c b/src/backend/parser/keywords.c index cee5e4d425..10cb5c6484 100644 --- a/src/backend/parser/keywords.c +++ b/src/backend/parser/keywords.c @@ -332,7 +332,6 @@ static const ScanKeyword ScanKeywords[] = { {"time", TIME}, {"timestamp", TIMESTAMP}, {"to", TO}, - {"toast", TOAST}, {"trailing", TRAILING}, {"transaction", TRANSACTION}, {"treat", TREAT}, diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 2a49988e36..4bc2fb0ac1 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -20,6 +20,7 @@ #include "access/xact.h" #include "catalog/catalog.h" #include "catalog/namespace.h" +#include "catalog/toasting.h" #include "commands/alter.h" #include "commands/async.h" #include "commands/cluster.h" @@ -526,7 +527,7 @@ ProcessUtility(Node *parsetree, * secondary relation too. */ CommandCounterIncrement(); - AlterTableCreateToastTable(relOid, true); + AlterTableCreateToastTable(relOid); } break; diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 3d8ccb3ddf..0d6fda835a 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -174,7 +174,6 @@ static void setup_config(void); static void bootstrap_template1(char *short_version); static void setup_auth(void); static void get_set_pwd(void); -static void unlimit_systables(void); static void setup_depend(void); static void setup_sysviews(void); static void setup_description(void); @@ -1560,45 +1559,6 @@ get_set_pwd(void) } /* - * toast sys tables - */ -static void -unlimit_systables(void) -{ - PG_CMD_DECL; - char **line; - static char *systables_setup[] = { - "ALTER TABLE pg_attrdef CREATE TOAST TABLE;\n", - "ALTER TABLE pg_authid CREATE TOAST TABLE;\n", - "ALTER TABLE pg_constraint CREATE TOAST TABLE;\n", - "ALTER TABLE pg_database CREATE TOAST TABLE;\n", - "ALTER TABLE pg_description CREATE TOAST TABLE;\n", - "ALTER TABLE pg_proc CREATE TOAST TABLE;\n", - "ALTER TABLE pg_rewrite CREATE TOAST TABLE;\n", - "ALTER TABLE pg_shdescription CREATE TOAST TABLE;\n", - "ALTER TABLE pg_statistic CREATE TOAST TABLE;\n", - NULL - }; - - fputs(_("enabling unlimited row size for system tables ... "), stdout); - fflush(stdout); - - snprintf(cmd, sizeof(cmd), - "\"%s\" %s template1 >%s", - backend_exec, backend_options, - DEVNULL); - - PG_CMD_OPEN; - - for (line = systables_setup; *line != NULL; line++) - PG_CMD_PUTS(*line); - - PG_CMD_CLOSE; - - check_ok(); -} - -/* * set up pg_depend */ static void @@ -2935,8 +2895,6 @@ main(int argc, char *argv[]) if (pwprompt || pwfilename) get_set_pwd(); - unlimit_systables(); - setup_depend(); setup_sysviews(); diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index 1d977aee65..20a4e8bafb 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -53,6 +53,6 @@ */ /* yyyymmddN */ -#define CATALOG_VERSION_NO 200607281 +#define CATALOG_VERSION_NO 200607301 #endif diff --git a/src/include/catalog/duplicate_oids b/src/include/catalog/duplicate_oids index 0a62347ee4..a75c4d8f09 100755 --- a/src/include/catalog/duplicate_oids +++ b/src/include/catalog/duplicate_oids @@ -12,12 +12,14 @@ # note: we exclude BKI_BOOTSTRAP relations since they are expected to have # matching DATA lines in pg_class.h -cat pg_*.h indexing.h | \ +cat pg_*.h toasting.h indexing.h | \ egrep -v -e '^CATALOG\(.*BKI_BOOTSTRAP' | \ sed -n -e 's/^DATA(insert *OID *= *\([0-9][0-9]*\).*$/\1/p' \ -e 's/^CATALOG([^,]*, *\([0-9][0-9]*\).*$/\1/p' \ -e 's/^DECLARE_INDEX([^,]*, *\([0-9][0-9]*\).*$/\1/p' \ - -e 's/^DECLARE_UNIQUE_INDEX([^,]*, *\([0-9][0-9]*\).*$/\1/p' | \ + -e 's/^DECLARE_UNIQUE_INDEX([^,]*, *\([0-9][0-9]*\).*$/\1/p' \ + -e 's/^DECLARE_TOAST([^,]*, *\([0-9][0-9]*\), *\([0-9][0-9]*\).*$/\1,\2/p' | \ +tr ',' '\n' | \ sort -n | \ uniq -d diff --git a/src/include/catalog/index.h b/src/include/catalog/index.h index c619e74e11..2efcc1de8d 100644 --- a/src/include/catalog/index.h +++ b/src/include/catalog/index.h @@ -37,7 +37,6 @@ extern Oid index_create(Oid heapRelationId, Oid *classObjectId, Datum reloptions, bool isprimary, - bool istoast, bool isconstraint, bool allow_system_table_mods, bool skip_build); @@ -57,8 +56,7 @@ extern void setNewRelfilenode(Relation relation); extern void index_build(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, - bool isprimary, - bool istoast); + bool isprimary); extern double IndexBuildHeapScan(Relation heapRelation, Relation indexRelation, diff --git a/src/include/catalog/toasting.h b/src/include/catalog/toasting.h new file mode 100644 index 0000000000..22501614c7 --- /dev/null +++ b/src/include/catalog/toasting.h @@ -0,0 +1,61 @@ +/*------------------------------------------------------------------------- + * + * toasting.h + * This file provides some definitions to support creation of toast tables + * + * + * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * $PostgreSQL$ + * + *------------------------------------------------------------------------- + */ +#ifndef TOASTING_H +#define TOASTING_H + +/* + * toasting.c prototypes + */ +extern void AlterTableCreateToastTable(Oid relOid); +extern void BootstrapToastTable(char *relName, + Oid toastOid, Oid toastIndexOid); + + +/* + * This macro is just to keep the C compiler from spitting up on the + * upcoming commands for genbki.sh. + */ +#define DECLARE_TOAST(name,toastoid,indexoid) extern int no_such_variable + + +/* + * What follows are lines processed by genbki.sh to create the statements + * the bootstrap parser will turn into BootstrapToastTable commands. + * Each line specifies the system catalog that needs a toast table, + * the OID to assign to the toast table, and the OID to assign to the + * toast table's index. The reason we hard-wire these OIDs is that we + * need stable OIDs for shared relations, and that includes toast tables + * of shared relations. + */ + +/* normal catalogs */ +DECLARE_TOAST(pg_attrdef,2830,2831); +DECLARE_TOAST(pg_constraint,2832,2833); +DECLARE_TOAST(pg_description,2834,2835); +DECLARE_TOAST(pg_proc,2836,2837); +DECLARE_TOAST(pg_rewrite,2838,2839); +DECLARE_TOAST(pg_statistic,2840,2841); + +/* shared catalogs */ +DECLARE_TOAST(pg_authid,2842,2843); +#define PgAuthidToastTable 2842 +#define PgAuthidToastIndex 2843 +DECLARE_TOAST(pg_database,2844,2845); +#define PgDatabaseToastTable 2844 +#define PgDatabaseToastIndex 2845 +DECLARE_TOAST(pg_shdescription,2846,2847); +#define PgShdescriptionToastTable 2846 +#define PgShdescriptionToastIndex 2847 + +#endif /* TOASTING_H */ diff --git a/src/include/catalog/unused_oids b/src/include/catalog/unused_oids index a57c82017e..0a1a64d858 100755 --- a/src/include/catalog/unused_oids +++ b/src/include/catalog/unused_oids @@ -23,16 +23,18 @@ AWK="awk" FIRSTOBJECTID=`grep '#define[ ]*FirstBootstrapObjectId' ../access/transam.h | $AWK '{ print $3 }'` export FIRSTOBJECTID -# this part (down to the uniq step) should match the unused_oids script +# this part (down to the uniq step) should match the duplicate_oids script # note: we exclude BKI_BOOTSTRAP relations since they are expected to have # matching DATA lines in pg_class.h -cat pg_*.h indexing.h | \ +cat pg_*.h toasting.h indexing.h | \ egrep -v -e '^CATALOG\(.*BKI_BOOTSTRAP' | \ sed -n -e 's/^DATA(insert *OID *= *\([0-9][0-9]*\).*$/\1/p' \ -e 's/^CATALOG([^,]*, *\([0-9][0-9]*\).*$/\1/p' \ -e 's/^DECLARE_INDEX([^,]*, *\([0-9][0-9]*\).*$/\1/p' \ - -e 's/^DECLARE_UNIQUE_INDEX([^,]*, *\([0-9][0-9]*\).*$/\1/p' | \ + -e 's/^DECLARE_UNIQUE_INDEX([^,]*, *\([0-9][0-9]*\).*$/\1/p' \ + -e 's/^DECLARE_TOAST([^,]*, *\([0-9][0-9]*\), *\([0-9][0-9]*\).*$/\1,\2/p' | \ +tr ',' '\n' | \ sort -n | \ uniq | \ $AWK ' diff --git a/src/include/commands/tablecmds.h b/src/include/commands/tablecmds.h index 6cdbf27fe7..0e843768b6 100644 --- a/src/include/commands/tablecmds.h +++ b/src/include/commands/tablecmds.h @@ -28,8 +28,6 @@ extern void ATExecChangeOwner(Oid relationOid, Oid newOwnerId, bool recursing); extern void AlterTableInternal(Oid relid, List *cmds, bool recurse); -extern void AlterTableCreateToastTable(Oid relOid, bool silent); - extern void AlterTableNamespace(RangeVar *relation, const char *newschema); extern void AlterRelationNamespaceInternal(Relation classRel, Oid relOid, diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 594e5f2368..d3f18d0738 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -854,7 +854,6 @@ typedef enum AlterTableType AT_DropConstraintQuietly, /* drop constraint, no error/warning (local in * commands/tablecmds.c) */ AT_AlterColumnType, /* alter column type */ - AT_ToastTable, /* create toast table */ AT_ChangeOwner, /* change owner */ AT_ClusterOn, /* CLUSTER ON */ AT_DropCluster, /* SET WITHOUT CLUSTER */ |