Correct constness of system attributes in heap.c & prerequisites.

This allows the compiler / linker to mark affected pages as read-only.

There's a fair number of pre-requisite changes, to allow the const
properly be propagated. Most of consts were already required for
correctness anyway, just not represented on the type-level.  Arguably
we could be more aggressive in using consts in related code, but..

This requires using a few of the types underlying typedefs that
removes pointers (e.g. const NameData *) as declaring the typedefed
type constant doesn't have the same meaning (it makes the variable
const, not what it points to).

Discussion: https://postgr.es/m/20181015200754.7y7zfuzsoux2c4ya@alap3.anarazel.de
This commit is contained in:
Andres Freund 2018-10-16 09:44:43 -07:00
parent c015ccb306
commit 02a30a09f9
11 changed files with 32 additions and 31 deletions

View File

@ -144,7 +144,7 @@ static List *insert_ordered_unique_oid(List *list, Oid datum);
* fixed-size portion of the structure anyway. * fixed-size portion of the structure anyway.
*/ */
static FormData_pg_attribute a1 = { static const FormData_pg_attribute a1 = {
.attname = {"ctid"}, .attname = {"ctid"},
.atttypid = TIDOID, .atttypid = TIDOID,
.attlen = sizeof(ItemPointerData), .attlen = sizeof(ItemPointerData),
@ -158,7 +158,7 @@ static FormData_pg_attribute a1 = {
.attislocal = true, .attislocal = true,
}; };
static FormData_pg_attribute a2 = { static const FormData_pg_attribute a2 = {
.attname = {"oid"}, .attname = {"oid"},
.atttypid = OIDOID, .atttypid = OIDOID,
.attlen = sizeof(Oid), .attlen = sizeof(Oid),
@ -172,7 +172,7 @@ static FormData_pg_attribute a2 = {
.attislocal = true, .attislocal = true,
}; };
static FormData_pg_attribute a3 = { static const FormData_pg_attribute a3 = {
.attname = {"xmin"}, .attname = {"xmin"},
.atttypid = XIDOID, .atttypid = XIDOID,
.attlen = sizeof(TransactionId), .attlen = sizeof(TransactionId),
@ -186,7 +186,7 @@ static FormData_pg_attribute a3 = {
.attislocal = true, .attislocal = true,
}; };
static FormData_pg_attribute a4 = { static const FormData_pg_attribute a4 = {
.attname = {"cmin"}, .attname = {"cmin"},
.atttypid = CIDOID, .atttypid = CIDOID,
.attlen = sizeof(CommandId), .attlen = sizeof(CommandId),
@ -200,7 +200,7 @@ static FormData_pg_attribute a4 = {
.attislocal = true, .attislocal = true,
}; };
static FormData_pg_attribute a5 = { static const FormData_pg_attribute a5 = {
.attname = {"xmax"}, .attname = {"xmax"},
.atttypid = XIDOID, .atttypid = XIDOID,
.attlen = sizeof(TransactionId), .attlen = sizeof(TransactionId),
@ -214,7 +214,7 @@ static FormData_pg_attribute a5 = {
.attislocal = true, .attislocal = true,
}; };
static FormData_pg_attribute a6 = { static const FormData_pg_attribute a6 = {
.attname = {"cmax"}, .attname = {"cmax"},
.atttypid = CIDOID, .atttypid = CIDOID,
.attlen = sizeof(CommandId), .attlen = sizeof(CommandId),
@ -234,7 +234,7 @@ static FormData_pg_attribute a6 = {
* table of a particular class/type. In any case table is still the word * table of a particular class/type. In any case table is still the word
* used in SQL. * used in SQL.
*/ */
static FormData_pg_attribute a7 = { static const FormData_pg_attribute a7 = {
.attname = {"tableoid"}, .attname = {"tableoid"},
.atttypid = OIDOID, .atttypid = OIDOID,
.attlen = sizeof(Oid), .attlen = sizeof(Oid),
@ -248,14 +248,14 @@ static FormData_pg_attribute a7 = {
.attislocal = true, .attislocal = true,
}; };
static const Form_pg_attribute SysAtt[] = {&a1, &a2, &a3, &a4, &a5, &a6, &a7}; static const FormData_pg_attribute *SysAtt[] = {&a1, &a2, &a3, &a4, &a5, &a6, &a7};
/* /*
* This function returns a Form_pg_attribute pointer for a system attribute. * This function returns a Form_pg_attribute pointer for a system attribute.
* Note that we elog if the presented attno is invalid, which would only * Note that we elog if the presented attno is invalid, which would only
* happen if there's a problem upstream. * happen if there's a problem upstream.
*/ */
Form_pg_attribute const FormData_pg_attribute *
SystemAttributeDefinition(AttrNumber attno, bool relhasoids) SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
{ {
if (attno >= 0 || attno < -(int) lengthof(SysAtt)) if (attno >= 0 || attno < -(int) lengthof(SysAtt))
@ -269,14 +269,14 @@ SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
* If the given name is a system attribute name, return a Form_pg_attribute * If the given name is a system attribute name, return a Form_pg_attribute
* pointer for a prototype definition. If not, return NULL. * pointer for a prototype definition. If not, return NULL.
*/ */
Form_pg_attribute const FormData_pg_attribute *
SystemAttributeByName(const char *attname, bool relhasoids) SystemAttributeByName(const char *attname, bool relhasoids)
{ {
int j; int j;
for (j = 0; j < (int) lengthof(SysAtt); j++) for (j = 0; j < (int) lengthof(SysAtt); j++)
{ {
Form_pg_attribute att = SysAtt[j]; const FormData_pg_attribute *att = SysAtt[j];
if (relhasoids || att->attnum != ObjectIdAttributeNumber) if (relhasoids || att->attnum != ObjectIdAttributeNumber)
{ {

View File

@ -352,7 +352,7 @@ ConstructTupleDescriptor(Relation heapRelation,
if (atnum != 0) if (atnum != 0)
{ {
/* Simple index column */ /* Simple index column */
Form_pg_attribute from; const FormData_pg_attribute *from;
if (atnum < 0) if (atnum < 0)
{ {

View File

@ -899,7 +899,7 @@ int
SPI_fnumber(TupleDesc tupdesc, const char *fname) SPI_fnumber(TupleDesc tupdesc, const char *fname)
{ {
int res; int res;
Form_pg_attribute sysatt; const FormData_pg_attribute *sysatt;
for (res = 0; res < tupdesc->natts; res++) for (res = 0; res < tupdesc->natts; res++)
{ {
@ -921,7 +921,7 @@ SPI_fnumber(TupleDesc tupdesc, const char *fname)
char * char *
SPI_fname(TupleDesc tupdesc, int fnumber) SPI_fname(TupleDesc tupdesc, int fnumber)
{ {
Form_pg_attribute att; const FormData_pg_attribute *att;
SPI_result = 0; SPI_result = 0;

View File

@ -1692,7 +1692,7 @@ build_index_tlist(PlannerInfo *root, IndexOptInfo *index,
if (indexkey != 0) if (indexkey != 0)
{ {
/* simple column */ /* simple column */
Form_pg_attribute att_tup; const FormData_pg_attribute *att_tup;
if (indexkey < 0) if (indexkey < 0)
att_tup = SystemAttributeDefinition(indexkey, att_tup = SystemAttributeDefinition(indexkey,

View File

@ -3135,7 +3135,7 @@ attnameAttNum(Relation rd, const char *attname, bool sysColOK)
static int static int
specialAttNum(const char *attname) specialAttNum(const char *attname)
{ {
Form_pg_attribute sysatt; const FormData_pg_attribute *sysatt;
sysatt = SystemAttributeByName(attname, sysatt = SystemAttributeByName(attname,
true /* "oid" will be accepted */ ); true /* "oid" will be accepted */ );
@ -3152,12 +3152,12 @@ specialAttNum(const char *attname)
* heap_open()'ed. Use the cache version get_atttype() * heap_open()'ed. Use the cache version get_atttype()
* for access to non-opened relations. * for access to non-opened relations.
*/ */
Name const NameData *
attnumAttName(Relation rd, int attid) attnumAttName(Relation rd, int attid)
{ {
if (attid <= 0) if (attid <= 0)
{ {
Form_pg_attribute sysatt; const FormData_pg_attribute *sysatt;
sysatt = SystemAttributeDefinition(attid, rd->rd_rel->relhasoids); sysatt = SystemAttributeDefinition(attid, rd->rd_rel->relhasoids);
return &sysatt->attname; return &sysatt->attname;
@ -3179,7 +3179,7 @@ attnumTypeId(Relation rd, int attid)
{ {
if (attid <= 0) if (attid <= 0)
{ {
Form_pg_attribute sysatt; const FormData_pg_attribute *sysatt;
sysatt = SystemAttributeDefinition(attid, rd->rd_rel->relhasoids); sysatt = SystemAttributeDefinition(attid, rd->rd_rel->relhasoids);
return sysatt->atttypid; return sysatt->atttypid;

View File

@ -2065,7 +2065,7 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
for (i = 0; i < index_form->indnatts; i++) for (i = 0; i < index_form->indnatts; i++)
{ {
int16 attnum = index_form->indkey.values[i]; int16 attnum = index_form->indkey.values[i];
Form_pg_attribute attform; const FormData_pg_attribute *attform;
char *attname; char *attname;
Oid defopclass; Oid defopclass;

View File

@ -1025,6 +1025,7 @@ expanded_record_lookup_field(ExpandedRecordHeader *erh, const char *fieldname,
TupleDesc tupdesc; TupleDesc tupdesc;
int fno; int fno;
Form_pg_attribute attr; Form_pg_attribute attr;
const FormData_pg_attribute *sysattr;
tupdesc = expanded_record_get_tupdesc(erh); tupdesc = expanded_record_get_tupdesc(erh);
@ -1044,13 +1045,13 @@ expanded_record_lookup_field(ExpandedRecordHeader *erh, const char *fieldname,
} }
/* How about system attributes? */ /* How about system attributes? */
attr = SystemAttributeByName(fieldname, tupdesc->tdhasoid); sysattr = SystemAttributeByName(fieldname, tupdesc->tdhasoid);
if (attr != NULL) if (sysattr != NULL)
{ {
finfo->fnumber = attr->attnum; finfo->fnumber = sysattr->attnum;
finfo->ftypeid = attr->atttypid; finfo->ftypeid = sysattr->atttypid;
finfo->ftypmod = attr->atttypmod; finfo->ftypmod = sysattr->atttypmod;
finfo->fcollation = attr->attcollation; finfo->fcollation = sysattr->attcollation;
return true; return true;
} }

View File

@ -188,7 +188,7 @@ namege(PG_FUNCTION_ARGS)
/* (see char.c for comparison/operation routines) */ /* (see char.c for comparison/operation routines) */
int int
namecpy(Name n1, Name n2) namecpy(Name n1, const NameData *n2)
{ {
if (!n1 || !n2) if (!n1 || !n2)
return -1; return -1;

View File

@ -127,10 +127,10 @@ extern void RemoveAttrDefault(Oid relid, AttrNumber attnum,
extern void RemoveAttrDefaultById(Oid attrdefId); extern void RemoveAttrDefaultById(Oid attrdefId);
extern void RemoveStatistics(Oid relid, AttrNumber attnum); extern void RemoveStatistics(Oid relid, AttrNumber attnum);
extern Form_pg_attribute SystemAttributeDefinition(AttrNumber attno, extern const FormData_pg_attribute *SystemAttributeDefinition(AttrNumber attno,
bool relhasoids); bool relhasoids);
extern Form_pg_attribute SystemAttributeByName(const char *attname, extern const FormData_pg_attribute *SystemAttributeByName(const char *attname,
bool relhasoids); bool relhasoids);
extern void CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind, extern void CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind,

View File

@ -125,7 +125,7 @@ extern void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
extern List *expandRelAttrs(ParseState *pstate, RangeTblEntry *rte, extern List *expandRelAttrs(ParseState *pstate, RangeTblEntry *rte,
int rtindex, int sublevels_up, int location); int rtindex, int sublevels_up, int location);
extern int attnameAttNum(Relation rd, const char *attname, bool sysColOK); extern int attnameAttNum(Relation rd, const char *attname, bool sysColOK);
extern Name attnumAttName(Relation rd, int attid); extern const NameData *attnumAttName(Relation rd, int attid);
extern Oid attnumTypeId(Relation rd, int attid); extern Oid attnumTypeId(Relation rd, int attid);
extern Oid attnumCollationId(Relation rd, int attid); extern Oid attnumCollationId(Relation rd, int attid);
extern bool isQueryUsingTempRelation(Query *query); extern bool isQueryUsingTempRelation(Query *query);

View File

@ -37,7 +37,7 @@ extern unsigned hex_decode(const char *src, unsigned len, char *dst);
extern int2vector *buildint2vector(const int16 *int2s, int n); extern int2vector *buildint2vector(const int16 *int2s, int n);
/* name.c */ /* name.c */
extern int namecpy(Name n1, Name n2); extern int namecpy(Name n1, const NameData *n2);
extern int namestrcpy(Name name, const char *str); extern int namestrcpy(Name name, const char *str);
extern int namestrcmp(Name name, const char *str); extern int namestrcmp(Name name, const char *str);