]> granicus.if.org Git - postgresql/commitdiff
Correct constness of system attributes in heap.c & prerequisites.
authorAndres Freund <andres@anarazel.de>
Tue, 16 Oct 2018 16:44:43 +0000 (09:44 -0700)
committerAndres Freund <andres@anarazel.de>
Tue, 16 Oct 2018 16:44:43 +0000 (09:44 -0700)
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

src/backend/catalog/heap.c
src/backend/catalog/index.c
src/backend/executor/spi.c
src/backend/optimizer/util/plancat.c
src/backend/parser/parse_relation.c
src/backend/parser/parse_utilcmd.c
src/backend/utils/adt/expandedrecord.c
src/backend/utils/adt/name.c
src/include/catalog/heap.h
src/include/parser/parse_relation.h
src/include/utils/builtins.h

index 4ad58689fc32e82e8ad941f35303c247f227ec4d..3c9c03c99717461dae178db0b00d4c2147210239 100644 (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)
                {
index 339bb35f9bcd6eaa347d23f2e774fb211ab68061..f1ef4c319a037079b3d4b4447c69c5f1292ba861 100644 (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)
                        {
index fb36e762f28e9b4109204021fd239a649fcca2d6..19212738568a9381783b0a31a509aa319ade492b 100644 (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;
 
index 8369e3ad62dde0bd012964ae3894e6e9a1956575..46de00460d9f1a96dee7ac385d6aa242e0c15b6c 100644 (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,
index f115ed863af0d13edaf33ac65d9a9c75131d2561..66a7105b099ab34778419114d4bf46e08f31ad06 100644 (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;
index 42bf9bfec628b82f3fd11c7bd1d554d80263f9e2..d8387d4356990ac117d04f61c4b822ee34c25af1 100644 (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;
 
index b1b6883c19f3fb72e3f54a926507ccf76062bff0..9aa6f3aac5149981f9fb3df0891ab9dd8a309c22 100644 (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;
        }
 
index fe20448ac57594ed2fd44b2a7443b8b6ce383911..c266da2de1ce1b015eafe382d2e6dbb234a351f4 100644 (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;
index b3e8fdd9c604cece887c10a4545cc6ca9ae61043..39f04b06eef39cde97bd0fe4d167c4f6bd56b91d 100644 (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,
index 687a7d7b1b9686d5f131fa1eb6020b2d6ee574a8..bf0c3b057ba6af23004ff33478ab0f6fddc17d95 100644 (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);
index 6f55699912c90e4b8db6651ad50ea6cfd2e95551..61785a243354f9b70e07e3a0011e49deeb6df4ad 100644 (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);