Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 03e5248

Browse files
committed
Replace the usage of heap_addheader to create pg_attribute tuples with regular
heap_form_tuple. Since this removes the last remaining caller of heap_addheader, remove it. Extracted from the column privileges patch from Stephen Frost, with further code cleanups by me.
1 parent c889ebc commit 03e5248

File tree

6 files changed

+113
-157
lines changed

6 files changed

+113
-157
lines changed

src/backend/access/common/heaptuple.c

+1-54
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
*
5151
*
5252
* IDENTIFICATION
53-
* $PostgreSQL: pgsql/src/backend/access/common/heaptuple.c,v 1.123 2008/11/02 01:45:26 tgl Exp $
53+
* $PostgreSQL: pgsql/src/backend/access/common/heaptuple.c,v 1.124 2008/11/14 01:57:41 alvherre Exp $
5454
*
5555
*-------------------------------------------------------------------------
5656
*/
@@ -1576,56 +1576,3 @@ minimal_tuple_from_heap_tuple(HeapTuple htup)
15761576
result->t_len = len;
15771577
return result;
15781578
}
1579-
1580-
1581-
/* ----------------
1582-
* heap_addheader
1583-
*
1584-
* This routine forms a HeapTuple by copying the given structure (tuple
1585-
* data) and adding a generic header. Note that the tuple data is
1586-
* presumed to contain no null fields and no varlena fields.
1587-
*
1588-
* This routine is really only useful for certain system tables that are
1589-
* known to be fixed-width and null-free. Currently it is only used for
1590-
* pg_attribute tuples.
1591-
* ----------------
1592-
*/
1593-
HeapTuple
1594-
heap_addheader(int natts, /* max domain index */
1595-
bool withoid, /* reserve space for oid */
1596-
Size structlen, /* its length */
1597-
void *structure) /* pointer to the struct */
1598-
{
1599-
HeapTuple tuple;
1600-
HeapTupleHeader td;
1601-
Size len;
1602-
int hoff;
1603-
1604-
AssertArg(natts > 0);
1605-
1606-
/* header needs no null bitmap */
1607-
hoff = offsetof(HeapTupleHeaderData, t_bits);
1608-
if (withoid)
1609-
hoff += sizeof(Oid);
1610-
hoff = MAXALIGN(hoff);
1611-
len = hoff + structlen;
1612-
1613-
tuple = (HeapTuple) palloc0(HEAPTUPLESIZE + len);
1614-
tuple->t_data = td = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE);
1615-
1616-
tuple->t_len = len;
1617-
ItemPointerSetInvalid(&(tuple->t_self));
1618-
tuple->t_tableOid = InvalidOid;
1619-
1620-
/* we don't bother to fill the Datum fields */
1621-
1622-
HeapTupleHeaderSetNatts(td, natts);
1623-
td->t_hoff = hoff;
1624-
1625-
if (withoid) /* else leave infomask = 0 */
1626-
td->t_infomask = HEAP_HASOID;
1627-
1628-
memcpy((char *) td + hoff, structure, structlen);
1629-
1630-
return tuple;
1631-
}

src/backend/catalog/heap.c

+79-51
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/catalog/heap.c,v 1.343 2008/11/09 21:24:32 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/catalog/heap.c,v 1.344 2008/11/14 01:57:41 alvherre Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -478,6 +478,60 @@ CheckAttributeType(const char *attname, Oid atttypid)
478478
}
479479
}
480480

481+
/*
482+
* InsertPgAttributeTuple
483+
* Construct and insert a new tuple in pg_attribute.
484+
*
485+
* Caller has already opened and locked pg_attribute. new_attribute is the
486+
* attribute to insert.
487+
*
488+
* indstate is the index state for CatalogIndexInsert. It can be passed as
489+
* NULL, in which case we'll fetch the necessary info. (Don't do this when
490+
* inserting multiple attributes, because it's a tad more expensive.)
491+
*/
492+
void
493+
InsertPgAttributeTuple(Relation pg_attribute_rel,
494+
Form_pg_attribute new_attribute,
495+
CatalogIndexState indstate)
496+
{
497+
Datum values[Natts_pg_attribute];
498+
bool nulls[Natts_pg_attribute];
499+
HeapTuple tup;
500+
501+
/* This is a tad tedious, but way cleaner than what we used to do... */
502+
memset(values, 0, sizeof(values));
503+
memset(nulls, false, sizeof(nulls));
504+
505+
values[Anum_pg_attribute_attrelid - 1] = ObjectIdGetDatum(new_attribute->attrelid);
506+
values[Anum_pg_attribute_attname - 1] = NameGetDatum(&new_attribute->attname);
507+
values[Anum_pg_attribute_atttypid - 1] = ObjectIdGetDatum(new_attribute->atttypid);
508+
values[Anum_pg_attribute_attstattarget - 1] = Int32GetDatum(new_attribute->attstattarget);
509+
values[Anum_pg_attribute_attlen - 1] = Int16GetDatum(new_attribute->attlen);
510+
values[Anum_pg_attribute_attnum - 1] = Int16GetDatum(new_attribute->attnum);
511+
values[Anum_pg_attribute_attndims - 1] = Int32GetDatum(new_attribute->attndims);
512+
values[Anum_pg_attribute_attcacheoff - 1] = Int32GetDatum(new_attribute->attcacheoff);
513+
values[Anum_pg_attribute_atttypmod - 1] = Int32GetDatum(new_attribute->atttypmod);
514+
values[Anum_pg_attribute_attbyval - 1] = BoolGetDatum(new_attribute->attbyval);
515+
values[Anum_pg_attribute_attstorage - 1] = CharGetDatum(new_attribute->attstorage);
516+
values[Anum_pg_attribute_attalign - 1] = CharGetDatum(new_attribute->attalign);
517+
values[Anum_pg_attribute_attnotnull - 1] = BoolGetDatum(new_attribute->attnotnull);
518+
values[Anum_pg_attribute_atthasdef - 1] = BoolGetDatum(new_attribute->atthasdef);
519+
values[Anum_pg_attribute_attisdropped - 1] = BoolGetDatum(new_attribute->attisdropped);
520+
values[Anum_pg_attribute_attislocal - 1] = BoolGetDatum(new_attribute->attislocal);
521+
values[Anum_pg_attribute_attinhcount - 1] = Int32GetDatum(new_attribute->attinhcount);
522+
523+
tup = heap_form_tuple(RelationGetDescr(pg_attribute_rel), values, nulls);
524+
525+
/* finally insert the new tuple, update the indexes, and clean up */
526+
simple_heap_insert(pg_attribute_rel, tup);
527+
528+
if (indstate != NULL)
529+
CatalogIndexInsert(indstate, tup);
530+
else
531+
CatalogUpdateIndexes(pg_attribute_rel, tup);
532+
533+
heap_freetuple(tup);
534+
}
481535
/* --------------------------------
482536
* AddNewAttributeTuples
483537
*
@@ -492,9 +546,8 @@ AddNewAttributeTuples(Oid new_rel_oid,
492546
bool oidislocal,
493547
int oidinhcount)
494548
{
495-
const Form_pg_attribute *dpp;
549+
Form_pg_attribute attr;
496550
int i;
497-
HeapTuple tup;
498551
Relation rel;
499552
CatalogIndexState indstate;
500553
int natts = tupdesc->natts;
@@ -512,35 +565,25 @@ AddNewAttributeTuples(Oid new_rel_oid,
512565
* First we add the user attributes. This is also a convenient place to
513566
* add dependencies on their datatypes.
514567
*/
515-
dpp = tupdesc->attrs;
516568
for (i = 0; i < natts; i++)
517569
{
570+
attr = tupdesc->attrs[i];
518571
/* Fill in the correct relation OID */
519-
(*dpp)->attrelid = new_rel_oid;
572+
attr->attrelid = new_rel_oid;
520573
/* Make sure these are OK, too */
521-
(*dpp)->attstattarget = -1;
522-
(*dpp)->attcacheoff = -1;
523-
524-
tup = heap_addheader(Natts_pg_attribute,
525-
false,
526-
ATTRIBUTE_TUPLE_SIZE,
527-
(void *) *dpp);
528-
529-
simple_heap_insert(rel, tup);
530-
531-
CatalogIndexInsert(indstate, tup);
574+
attr->attstattarget = -1;
575+
attr->attcacheoff = -1;
532576

533-
heap_freetuple(tup);
577+
InsertPgAttributeTuple(rel, attr, indstate);
534578

579+
/* Add dependency info */
535580
myself.classId = RelationRelationId;
536581
myself.objectId = new_rel_oid;
537582
myself.objectSubId = i + 1;
538583
referenced.classId = TypeRelationId;
539-
referenced.objectId = (*dpp)->atttypid;
584+
referenced.objectId = attr->atttypid;
540585
referenced.objectSubId = 0;
541586
recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
542-
543-
dpp++;
544587
}
545588

546589
/*
@@ -550,43 +593,28 @@ AddNewAttributeTuples(Oid new_rel_oid,
550593
*/
551594
if (relkind != RELKIND_VIEW && relkind != RELKIND_COMPOSITE_TYPE)
552595
{
553-
dpp = SysAtt;
554-
for (i = 0; i < (int) lengthof(SysAtt); i++, dpp++)
596+
for (i = 0; i < (int) lengthof(SysAtt); i++)
555597
{
556-
if (tupdesc->tdhasoid ||
557-
(*dpp)->attnum != ObjectIdAttributeNumber)
558-
{
559-
Form_pg_attribute attStruct;
560-
561-
tup = heap_addheader(Natts_pg_attribute,
562-
false,
563-
ATTRIBUTE_TUPLE_SIZE,
564-
(void *) *dpp);
565-
attStruct = (Form_pg_attribute) GETSTRUCT(tup);
566-
567-
/* Fill in the correct relation OID in the copied tuple */
568-
attStruct->attrelid = new_rel_oid;
598+
FormData_pg_attribute attStruct;
569599

570-
/* Fill in correct inheritance info for the OID column */
571-
if (attStruct->attnum == ObjectIdAttributeNumber)
572-
{
573-
attStruct->attislocal = oidislocal;
574-
attStruct->attinhcount = oidinhcount;
575-
}
576-
577-
/*
578-
* Unneeded since they should be OK in the constant data
579-
* anyway
580-
*/
581-
/* attStruct->attstattarget = 0; */
582-
/* attStruct->attcacheoff = -1; */
600+
/* skip OID where appropriate */
601+
if (!tupdesc->tdhasoid &&
602+
SysAtt[i]->attnum == ObjectIdAttributeNumber)
603+
continue;
583604

584-
simple_heap_insert(rel, tup);
605+
memcpy(&attStruct, (char *) SysAtt[i], sizeof(FormData_pg_attribute));
585606

586-
CatalogIndexInsert(indstate, tup);
607+
/* Fill in the correct relation OID in the copied tuple */
608+
attStruct.attrelid = new_rel_oid;
587609

588-
heap_freetuple(tup);
610+
/* Fill in correct inheritance info for the OID column */
611+
if (attStruct.attnum == ObjectIdAttributeNumber)
612+
{
613+
attStruct.attislocal = oidislocal;
614+
attStruct.attinhcount = oidinhcount;
589615
}
616+
617+
InsertPgAttributeTuple(rel, &attStruct, indstate);
590618
}
591619
}
592620

src/backend/catalog/index.c

+2-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.308 2008/11/13 17:42:10 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.309 2008/11/14 01:57:41 alvherre Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -327,7 +327,6 @@ AppendAttributeTuples(Relation indexRelation, int numatts)
327327
Relation pg_attribute;
328328
CatalogIndexState indstate;
329329
TupleDesc indexTupDesc;
330-
HeapTuple new_tuple;
331330
int i;
332331

333332
/*
@@ -351,16 +350,7 @@ AppendAttributeTuples(Relation indexRelation, int numatts)
351350
Assert(indexTupDesc->attrs[i]->attnum == i + 1);
352351
Assert(indexTupDesc->attrs[i]->attcacheoff == -1);
353352

354-
new_tuple = heap_addheader(Natts_pg_attribute,
355-
false,
356-
ATTRIBUTE_TUPLE_SIZE,
357-
(void *) indexTupDesc->attrs[i]);
358-
359-
simple_heap_insert(pg_attribute, new_tuple);
360-
361-
CatalogIndexInsert(indstate, new_tuple);
362-
363-
heap_freetuple(new_tuple);
353+
InsertPgAttributeTuple(pg_attribute, indexTupDesc->attrs[i], indstate);
364354
}
365355

366356
CatalogCloseIndexes(indstate);

src/backend/commands/tablecmds.c

+24-36
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.269 2008/11/02 01:45:27 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.270 2008/11/14 01:57:41 alvherre Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -3450,9 +3450,7 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
34503450
Relation pgclass,
34513451
attrdesc;
34523452
HeapTuple reltup;
3453-
HeapTuple attributeTuple;
3454-
Form_pg_attribute attribute;
3455-
FormData_pg_attribute attributeD;
3453+
FormData_pg_attribute attribute;
34563454
int i;
34573455
int minattnum,
34583456
maxatts;
@@ -3543,37 +3541,27 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
35433541
/* make sure datatype is legal for a column */
35443542
CheckAttributeType(colDef->colname, typeOid);
35453543

3546-
attributeTuple = heap_addheader(Natts_pg_attribute,
3547-
false,
3548-
ATTRIBUTE_TUPLE_SIZE,
3549-
(void *) &attributeD);
3550-
3551-
attribute = (Form_pg_attribute) GETSTRUCT(attributeTuple);
3552-
3553-
attribute->attrelid = myrelid;
3554-
namestrcpy(&(attribute->attname), colDef->colname);
3555-
attribute->atttypid = typeOid;
3556-
attribute->attstattarget = -1;
3557-
attribute->attlen = tform->typlen;
3558-
attribute->attcacheoff = -1;
3559-
attribute->atttypmod = typmod;
3560-
attribute->attnum = i;
3561-
attribute->attbyval = tform->typbyval;
3562-
attribute->attndims = list_length(colDef->typename->arrayBounds);
3563-
attribute->attstorage = tform->typstorage;
3564-
attribute->attalign = tform->typalign;
3565-
attribute->attnotnull = colDef->is_not_null;
3566-
attribute->atthasdef = false;
3567-
attribute->attisdropped = false;
3568-
attribute->attislocal = colDef->is_local;
3569-
attribute->attinhcount = colDef->inhcount;
3544+
attribute.attrelid = myrelid;
3545+
namestrcpy(&(attribute.attname), colDef->colname);
3546+
attribute.atttypid = typeOid;
3547+
attribute.attstattarget = -1;
3548+
attribute.attlen = tform->typlen;
3549+
attribute.attcacheoff = -1;
3550+
attribute.atttypmod = typmod;
3551+
attribute.attnum = i;
3552+
attribute.attbyval = tform->typbyval;
3553+
attribute.attndims = list_length(colDef->typename->arrayBounds);
3554+
attribute.attstorage = tform->typstorage;
3555+
attribute.attalign = tform->typalign;
3556+
attribute.attnotnull = colDef->is_not_null;
3557+
attribute.atthasdef = false;
3558+
attribute.attisdropped = false;
3559+
attribute.attislocal = colDef->is_local;
3560+
attribute.attinhcount = colDef->inhcount;
35703561

35713562
ReleaseSysCache(typeTuple);
35723563

3573-
simple_heap_insert(attrdesc, attributeTuple);
3574-
3575-
/* Update indexes on pg_attribute */
3576-
CatalogUpdateIndexes(attrdesc, attributeTuple);
3564+
InsertPgAttributeTuple(attrdesc, &attribute, NULL);
35773565

35783566
heap_close(attrdesc, RowExclusiveLock);
35793567

@@ -3602,7 +3590,7 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
36023590
RawColumnDefault *rawEnt;
36033591

36043592
rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault));
3605-
rawEnt->attnum = attribute->attnum;
3593+
rawEnt->attnum = attribute.attnum;
36063594
rawEnt->raw_default = copyObject(colDef->raw_default);
36073595

36083596
/*
@@ -3637,7 +3625,7 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
36373625
* returned by AddRelationNewConstraints, so that the right thing happens
36383626
* when a datatype's default applies.
36393627
*/
3640-
defval = (Expr *) build_column_default(rel, attribute->attnum);
3628+
defval = (Expr *) build_column_default(rel, attribute.attnum);
36413629

36423630
if (!defval && GetDomainConstraints(typeOid) != NIL)
36433631
{
@@ -3664,7 +3652,7 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
36643652
NewColumnValue *newval;
36653653

36663654
newval = (NewColumnValue *) palloc0(sizeof(NewColumnValue));
3667-
newval->attnum = attribute->attnum;
3655+
newval->attnum = attribute.attnum;
36683656
newval->expr = defval;
36693657

36703658
tab->newvals = lappend(tab->newvals, newval);
@@ -3678,7 +3666,7 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
36783666
/*
36793667
* Add needed dependency entries for the new column.
36803668
*/
3681-
add_column_datatype_dependency(myrelid, i, attribute->atttypid);
3669+
add_column_datatype_dependency(myrelid, i, attribute.atttypid);
36823670
}
36833671

36843672
/*

0 commit comments

Comments
 (0)