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.
*/
static FormData_pg_attribute a1 = {
static const FormData_pg_attribute a1 = {
.attname = {"ctid"},
.atttypid = TIDOID,
.attlen = sizeof(ItemPointerData),
@ -158,7 +158,7 @@ static FormData_pg_attribute a1 = {
.attislocal = true,
};
static FormData_pg_attribute a2 = {
static const FormData_pg_attribute a2 = {
.attname = {"oid"},
.atttypid = OIDOID,
.attlen = sizeof(Oid),
@ -172,7 +172,7 @@ static FormData_pg_attribute a2 = {
.attislocal = true,
};
static FormData_pg_attribute a3 = {
static const FormData_pg_attribute a3 = {
.attname = {"xmin"},
.atttypid = XIDOID,
.attlen = sizeof(TransactionId),
@ -186,7 +186,7 @@ static FormData_pg_attribute a3 = {
.attislocal = true,
};
static FormData_pg_attribute a4 = {
static const FormData_pg_attribute a4 = {
.attname = {"cmin"},
.atttypid = CIDOID,
.attlen = sizeof(CommandId),
@ -200,7 +200,7 @@ static FormData_pg_attribute a4 = {
.attislocal = true,
};
static FormData_pg_attribute a5 = {
static const FormData_pg_attribute a5 = {
.attname = {"xmax"},
.atttypid = XIDOID,
.attlen = sizeof(TransactionId),
@ -214,7 +214,7 @@ static FormData_pg_attribute a5 = {
.attislocal = true,
};
static FormData_pg_attribute a6 = {
static const FormData_pg_attribute a6 = {
.attname = {"cmax"},
.atttypid = CIDOID,
.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
* used in SQL.
*/
static FormData_pg_attribute a7 = {
static const FormData_pg_attribute a7 = {
.attname = {"tableoid"},
.atttypid = OIDOID,
.attlen = sizeof(Oid),
@ -248,14 +248,14 @@ static FormData_pg_attribute a7 = {
.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.
* Note that we elog if the presented attno is invalid, which would only
* happen if there's a problem upstream.
*/
Form_pg_attribute
const FormData_pg_attribute *
SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
{
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
* pointer for a prototype definition. If not, return NULL.
*/
Form_pg_attribute
const FormData_pg_attribute *
SystemAttributeByName(const char *attname, bool relhasoids)
{
int 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)
{

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -127,10 +127,10 @@ extern void RemoveAttrDefault(Oid relid, AttrNumber attnum,
extern void RemoveAttrDefaultById(Oid attrdefId);
extern void RemoveStatistics(Oid relid, AttrNumber attnum);
extern Form_pg_attribute SystemAttributeDefinition(AttrNumber attno,
extern const FormData_pg_attribute *SystemAttributeDefinition(AttrNumber attno,
bool relhasoids);
extern Form_pg_attribute SystemAttributeByName(const char *attname,
extern const FormData_pg_attribute *SystemAttributeByName(const char *attname,
bool relhasoids);
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,
int rtindex, int sublevels_up, int location);
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 attnumCollationId(Relation rd, int attid);
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);
/* 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 namestrcmp(Name name, const char *str);