diff options
| author | Andres Freund <andres@anarazel.de> | 2018-11-20 15:36:57 -0800 |
|---|---|---|
| committer | Andres Freund <andres@anarazel.de> | 2018-11-20 16:00:17 -0800 |
| commit | 578b229718e8f15fa779e20f086c4b6bb3776106 (patch) | |
| tree | 701869752158d27daa080d292befeb2e52f19037 /src/backend/catalog/heap.c | |
| parent | 0999ac479292c12a7c373e612b15e1ff47077990 (diff) | |
| download | postgresql-578b229718e8f15fa779e20f086c4b6bb3776106.tar.gz | |
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
Diffstat (limited to 'src/backend/catalog/heap.c')
| -rw-r--r-- | src/backend/catalog/heap.c | 87 |
1 files changed, 23 insertions, 64 deletions
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index bd4c439ef3..11debaa780 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -159,20 +159,6 @@ static const FormData_pg_attribute a1 = { }; static const FormData_pg_attribute a2 = { - .attname = {"oid"}, - .atttypid = OIDOID, - .attlen = sizeof(Oid), - .attnum = ObjectIdAttributeNumber, - .attcacheoff = -1, - .atttypmod = -1, - .attbyval = true, - .attstorage = 'p', - .attalign = 'i', - .attnotnull = true, - .attislocal = true, -}; - -static const FormData_pg_attribute a3 = { .attname = {"xmin"}, .atttypid = XIDOID, .attlen = sizeof(TransactionId), @@ -186,7 +172,7 @@ static const FormData_pg_attribute a3 = { .attislocal = true, }; -static const FormData_pg_attribute a4 = { +static const FormData_pg_attribute a3 = { .attname = {"cmin"}, .atttypid = CIDOID, .attlen = sizeof(CommandId), @@ -200,7 +186,7 @@ static const FormData_pg_attribute a4 = { .attislocal = true, }; -static const FormData_pg_attribute a5 = { +static const FormData_pg_attribute a4 = { .attname = {"xmax"}, .atttypid = XIDOID, .attlen = sizeof(TransactionId), @@ -214,7 +200,7 @@ static const FormData_pg_attribute a5 = { .attislocal = true, }; -static const FormData_pg_attribute a6 = { +static const FormData_pg_attribute a5 = { .attname = {"cmax"}, .atttypid = CIDOID, .attlen = sizeof(CommandId), @@ -234,7 +220,7 @@ static const FormData_pg_attribute a6 = { * table of a particular class/type. In any case table is still the word * used in SQL. */ -static const FormData_pg_attribute a7 = { +static const FormData_pg_attribute a6 = { .attname = {"tableoid"}, .atttypid = OIDOID, .attlen = sizeof(Oid), @@ -248,7 +234,7 @@ static const FormData_pg_attribute a7 = { .attislocal = true, }; -static const FormData_pg_attribute *SysAtt[] = {&a1, &a2, &a3, &a4, &a5, &a6, &a7}; +static const FormData_pg_attribute *SysAtt[] = {&a1, &a2, &a3, &a4, &a5, &a6}; /* * This function returns a Form_pg_attribute pointer for a system attribute. @@ -256,12 +242,10 @@ static const FormData_pg_attribute *SysAtt[] = {&a1, &a2, &a3, &a4, &a5, &a6, &a * happen if there's a problem upstream. */ const FormData_pg_attribute * -SystemAttributeDefinition(AttrNumber attno, bool relhasoids) +SystemAttributeDefinition(AttrNumber attno) { if (attno >= 0 || attno < -(int) lengthof(SysAtt)) elog(ERROR, "invalid system attribute number %d", attno); - if (attno == ObjectIdAttributeNumber && !relhasoids) - elog(ERROR, "invalid system attribute number %d", attno); return SysAtt[-attno - 1]; } @@ -270,7 +254,7 @@ SystemAttributeDefinition(AttrNumber attno, bool relhasoids) * pointer for a prototype definition. If not, return NULL. */ const FormData_pg_attribute * -SystemAttributeByName(const char *attname, bool relhasoids) +SystemAttributeByName(const char *attname) { int j; @@ -278,11 +262,8 @@ SystemAttributeByName(const char *attname, bool relhasoids) { const FormData_pg_attribute *att = SysAtt[j]; - if (relhasoids || att->attnum != ObjectIdAttributeNumber) - { - if (strcmp(NameStr(att->attname), attname) == 0) - return att; - } + if (strcmp(NameStr(att->attname), attname) == 0) + return att; } return NULL; @@ -501,8 +482,7 @@ CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind, { Form_pg_attribute attr = TupleDescAttr(tupdesc, i); - if (SystemAttributeByName(NameStr(attr->attname), - tupdesc->tdhasoid) != NULL) + if (SystemAttributeByName(NameStr(attr->attname)) != NULL) ereport(ERROR, (errcode(ERRCODE_DUPLICATE_COLUMN), errmsg("column name \"%s\" conflicts with a system column name", @@ -725,9 +705,7 @@ InsertPgAttributeTuple(Relation pg_attribute_rel, static void AddNewAttributeTuples(Oid new_rel_oid, TupleDesc tupdesc, - char relkind, - bool oidislocal, - int oidinhcount) + char relkind) { Form_pg_attribute attr; int i; @@ -789,23 +767,11 @@ AddNewAttributeTuples(Oid new_rel_oid, { FormData_pg_attribute attStruct; - /* skip OID where appropriate */ - if (!tupdesc->tdhasoid && - SysAtt[i]->attnum == ObjectIdAttributeNumber) - continue; - memcpy(&attStruct, (char *) SysAtt[i], sizeof(FormData_pg_attribute)); /* Fill in the correct relation OID in the copied tuple */ attStruct.attrelid = new_rel_oid; - /* Fill in correct inheritance info for the OID column */ - if (attStruct.attnum == ObjectIdAttributeNumber) - { - attStruct.attislocal = oidislocal; - attStruct.attinhcount = oidinhcount; - } - InsertPgAttributeTuple(rel, &attStruct, indstate); } } @@ -847,6 +813,7 @@ InsertPgClassTuple(Relation pg_class_desc, memset(values, 0, sizeof(values)); memset(nulls, false, sizeof(nulls)); + values[Anum_pg_class_oid - 1] = ObjectIdGetDatum(new_rel_oid); values[Anum_pg_class_relname - 1] = NameGetDatum(&rd_rel->relname); values[Anum_pg_class_relnamespace - 1] = ObjectIdGetDatum(rd_rel->relnamespace); values[Anum_pg_class_reltype - 1] = ObjectIdGetDatum(rd_rel->reltype); @@ -865,7 +832,6 @@ InsertPgClassTuple(Relation pg_class_desc, values[Anum_pg_class_relkind - 1] = CharGetDatum(rd_rel->relkind); values[Anum_pg_class_relnatts - 1] = Int16GetDatum(rd_rel->relnatts); values[Anum_pg_class_relchecks - 1] = Int16GetDatum(rd_rel->relchecks); - values[Anum_pg_class_relhasoids - 1] = BoolGetDatum(rd_rel->relhasoids); values[Anum_pg_class_relhasrules - 1] = BoolGetDatum(rd_rel->relhasrules); values[Anum_pg_class_relhastriggers - 1] = BoolGetDatum(rd_rel->relhastriggers); values[Anum_pg_class_relrowsecurity - 1] = BoolGetDatum(rd_rel->relrowsecurity); @@ -891,12 +857,6 @@ InsertPgClassTuple(Relation pg_class_desc, tup = heap_form_tuple(RelationGetDescr(pg_class_desc), values, nulls); - /* - * The new tuple must have the oid already chosen for the rel. Sure would - * be embarrassing to do this sort of thing in polite company. - */ - HeapTupleSetOid(tup, new_rel_oid); - /* finally insert the new tuple, update the indexes, and clean up */ CatalogTupleInsert(pg_class_desc, tup); @@ -1071,8 +1031,6 @@ AddNewRelationType(const char *typeName, * relpersistence: rel's persistence status (permanent, temp, or unlogged) * shared_relation: true if it's to be a shared relation * mapped_relation: true if the relation will use the relfilenode map - * oidislocal: true if oid column (if any) should be marked attislocal - * oidinhcount: attinhcount to assign to oid column (if any) * oncommit: ON COMMIT marking (only relevant if it's a temp table) * reloptions: reloptions in Datum form, or (Datum) 0 if none * use_user_acl: true if should look for user-defined default permissions; @@ -1100,8 +1058,6 @@ heap_create_with_catalog(const char *relname, char relpersistence, bool shared_relation, bool mapped_relation, - bool oidislocal, - int oidinhcount, OnCommitAction oncommit, Datum reloptions, bool use_user_acl, @@ -1144,7 +1100,7 @@ heap_create_with_catalog(const char *relname, * autogenerated array, we can rename it out of the way; otherwise we can * at least give a good error message. */ - old_type_oid = GetSysCacheOid2(TYPENAMENSP, + old_type_oid = GetSysCacheOid2(TYPENAMENSP, Anum_pg_type_oid, CStringGetDatum(relname), ObjectIdGetDatum(relnamespace)); if (OidIsValid(old_type_oid)) @@ -1347,8 +1303,7 @@ heap_create_with_catalog(const char *relname, /* * now add tuples to pg_attribute for the attributes in our new relation. */ - AddNewAttributeTuples(relid, new_rel_desc->rd_att, relkind, - oidislocal, oidinhcount); + AddNewAttributeTuples(relid, new_rel_desc->rd_att, relkind); /* * Make a dependency link to force the relation to be deleted if its @@ -1741,9 +1696,10 @@ RemoveAttrDefault(Oid relid, AttrNumber attnum, while (HeapTupleIsValid(tuple = systable_getnext(scan))) { ObjectAddress object; + Form_pg_attrdef attrtuple = (Form_pg_attrdef) GETSTRUCT(tuple); object.classId = AttrDefaultRelationId; - object.objectId = HeapTupleGetOid(tuple); + object.objectId = attrtuple->oid; object.objectSubId = 0; performDeletion(&object, behavior, @@ -1784,7 +1740,7 @@ RemoveAttrDefaultById(Oid attrdefId) /* Find the pg_attrdef tuple */ ScanKeyInit(&scankeys[0], - ObjectIdAttributeNumber, + Anum_pg_attrdef_oid, BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(attrdefId)); @@ -2162,6 +2118,8 @@ StoreAttrDefault(Relation rel, AttrNumber attnum, ObjectAddress colobject, defobject; + adrel = heap_open(AttrDefaultRelationId, RowExclusiveLock); + /* * Flatten expression to string form for storage. */ @@ -2170,14 +2128,15 @@ StoreAttrDefault(Relation rel, AttrNumber attnum, /* * Make the pg_attrdef entry. */ + attrdefOid = GetNewOidWithIndex(adrel, AttrDefaultOidIndexId, + Anum_pg_attrdef_oid); + values[Anum_pg_attrdef_oid - 1] = ObjectIdGetDatum(attrdefOid); values[Anum_pg_attrdef_adrelid - 1] = RelationGetRelid(rel); values[Anum_pg_attrdef_adnum - 1] = attnum; values[Anum_pg_attrdef_adbin - 1] = CStringGetTextDatum(adbin); - adrel = heap_open(AttrDefaultRelationId, RowExclusiveLock); - tuple = heap_form_tuple(adrel->rd_att, values, nulls); - attrdefOid = CatalogTupleInsert(adrel, tuple); + CatalogTupleInsert(adrel, tuple); defobject.classId = AttrDefaultRelationId; defobject.objectId = attrdefOid; |
