1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-30 21:42:05 +03:00

Introduce macros for typalign and typstorage constants.

Our usual practice for "poor man's enum" catalog columns is to define
macros for the possible values and use those, not literal constants,
in C code.  But for some reason lost in the mists of time, this was
never done for typalign/attalign or typstorage/attstorage.  It's never
too late to make it better though, so let's do that.

The reason I got interested in this right now is the need to duplicate
some uses of the TYPSTORAGE constants in an upcoming ALTER TYPE patch.
But in general, this sort of change aids greppability and readability,
so it's a good idea even without any specific motivation.

I may have missed a few places that could be converted, and it's even
more likely that pending patches will re-introduce some hard-coded
references.  But that's not fatal --- there's no expectation that
we'd actually change any of these values.  We can clean up stragglers
over time.

Discussion: https://postgr.es/m/16457.1583189537@sss.pgh.pa.us
This commit is contained in:
Tom Lane
2020-03-04 10:34:25 -05:00
parent 0ad6f848ee
commit 3ed2005ff5
76 changed files with 341 additions and 309 deletions

View File

@ -1527,7 +1527,7 @@ update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats)
/* XXX knows more than it should about type float4: */
arry = construct_array(numdatums, nnum,
FLOAT4OID,
sizeof(float4), true, 'i');
sizeof(float4), true, TYPALIGN_INT);
values[i++] = PointerGetDatum(arry); /* stanumbersN */
}
else

View File

@ -357,7 +357,8 @@ filter_list_to_array(List *filterlist)
pfree(result);
}
return PointerGetDatum(construct_array(data, l, TEXTOID, -1, false, 'i'));
return PointerGetDatum(construct_array(data, l, TEXTOID,
-1, false, TYPALIGN_INT));
}
/*

View File

@ -2293,7 +2293,7 @@ convert_requires_to_datum(List *requires)
}
a = construct_array(datums, ndatums,
NAMEOID,
NAMEDATALEN, false, 'c');
NAMEDATALEN, false, TYPALIGN_CHAR);
return PointerGetDatum(a);
}
@ -2503,7 +2503,7 @@ pg_extension_config_dump(PG_FUNCTION_ARGS)
a = construct_array(&elementDatum, 1,
OIDOID,
sizeof(Oid), true, 'i');
sizeof(Oid), true, TYPALIGN_INT);
}
else
{
@ -2539,7 +2539,7 @@ pg_extension_config_dump(PG_FUNCTION_ARGS)
-1 /* varlena array */ ,
sizeof(Oid) /* OID's typlen */ ,
true /* OID's typbyval */ ,
'i' /* OID's typalign */ );
TYPALIGN_INT /* OID's typalign */ );
}
repl_val[Anum_pg_extension_extconfig - 1] = PointerGetDatum(a);
repl_repl[Anum_pg_extension_extconfig - 1] = true;
@ -2556,7 +2556,7 @@ pg_extension_config_dump(PG_FUNCTION_ARGS)
a = construct_array(&elementDatum, 1,
TEXTOID,
-1, false, 'i');
-1, false, TYPALIGN_INT);
}
else
{
@ -2577,7 +2577,7 @@ pg_extension_config_dump(PG_FUNCTION_ARGS)
-1 /* varlena array */ ,
-1 /* TEXT's typlen */ ,
false /* TEXT's typbyval */ ,
'i' /* TEXT's typalign */ );
TYPALIGN_INT /* TEXT's typalign */ );
}
repl_val[Anum_pg_extension_extcondition - 1] = PointerGetDatum(a);
repl_repl[Anum_pg_extension_extcondition - 1] = true;
@ -2698,14 +2698,14 @@ extension_config_remove(Oid extensionoid, Oid tableoid)
int i;
/* We already checked there are no nulls */
deconstruct_array(a, OIDOID, sizeof(Oid), true, 'i',
deconstruct_array(a, OIDOID, sizeof(Oid), true, TYPALIGN_INT,
&dvalues, NULL, &nelems);
for (i = arrayIndex; i < arrayLength - 1; i++)
dvalues[i] = dvalues[i + 1];
a = construct_array(dvalues, arrayLength - 1,
OIDOID, sizeof(Oid), true, 'i');
OIDOID, sizeof(Oid), true, TYPALIGN_INT);
repl_val[Anum_pg_extension_extconfig - 1] = PointerGetDatum(a);
}
@ -2744,14 +2744,14 @@ extension_config_remove(Oid extensionoid, Oid tableoid)
int i;
/* We already checked there are no nulls */
deconstruct_array(a, TEXTOID, -1, false, 'i',
deconstruct_array(a, TEXTOID, -1, false, TYPALIGN_INT,
&dvalues, NULL, &nelems);
for (i = arrayIndex; i < arrayLength - 1; i++)
dvalues[i] = dvalues[i + 1];
a = construct_array(dvalues, arrayLength - 1,
TEXTOID, -1, false, 'i');
TEXTOID, -1, false, TYPALIGN_INT);
repl_val[Anum_pg_extension_extcondition - 1] = PointerGetDatum(a);
}

View File

@ -433,9 +433,9 @@ interpret_function_parameter_list(ParseState *pstate,
if (outCount > 0 || varCount > 0)
{
*allParameterTypes = construct_array(allTypes, parameterCount, OIDOID,
sizeof(Oid), true, 'i');
sizeof(Oid), true, TYPALIGN_INT);
*parameterModes = construct_array(paramModes, parameterCount, CHAROID,
1, true, 'c');
1, true, TYPALIGN_CHAR);
if (outCount > 1)
*requiredResultType = RECORDOID;
/* otherwise we set requiredResultType correctly above */
@ -454,7 +454,7 @@ interpret_function_parameter_list(ParseState *pstate,
paramNames[i] = CStringGetTextDatum("");
}
*parameterNames = construct_array(paramNames, parameterCount, TEXTOID,
-1, false, 'i');
-1, false, TYPALIGN_INT);
}
else
*parameterNames = NULL;
@ -1107,7 +1107,7 @@ CreateFunction(ParseState *pstate, CreateFunctionStmt *stmt)
foreach(lc, trftypes_list)
arr[i++] = ObjectIdGetDatum(lfirst_oid(lc));
trftypes = construct_array(arr, list_length(trftypes_list),
OIDOID, sizeof(Oid), true, 'i');
OIDOID, sizeof(Oid), true, TYPALIGN_INT);
}
else
{

View File

@ -614,7 +614,7 @@ RemoveRoleFromObjectPolicy(Oid roleid, Oid classid, Oid policy_id)
/* This is the array for the new tuple */
role_ids = construct_array(role_oids, num_roles, OIDOID,
sizeof(Oid), true, 'i');
sizeof(Oid), true, TYPALIGN_INT);
replaces[Anum_pg_policy_polroles - 1] = true;
values[Anum_pg_policy_polroles - 1] = PointerGetDatum(role_ids);
@ -735,7 +735,7 @@ CreatePolicy(CreatePolicyStmt *stmt)
/* Collect role ids */
role_oids = policy_role_list_to_array(stmt->roles, &nitems);
role_ids = construct_array(role_oids, nitems, OIDOID,
sizeof(Oid), true, 'i');
sizeof(Oid), true, TYPALIGN_INT);
/* Parse the supplied clause */
qual_pstate = make_parsestate(NULL);
@ -919,7 +919,7 @@ AlterPolicy(AlterPolicyStmt *stmt)
{
role_oids = policy_role_list_to_array(stmt->roles, &nitems);
role_ids = construct_array(role_oids, nitems, OIDOID,
sizeof(Oid), true, 'i');
sizeof(Oid), true, TYPALIGN_INT);
}
/* Get id of table. Also handles permissions checks. */

View File

@ -788,6 +788,7 @@ build_regtype_array(Oid *param_types, int num_params)
tmp_ary[i] = ObjectIdGetDatum(param_types[i]);
/* XXX: this hardcodes assumptions about the regtype type */
result = construct_array(tmp_ary, num_params, REGTYPEOID, 4, true, 'i');
result = construct_array(tmp_ary, num_params, REGTYPEOID,
4, true, TYPALIGN_INT);
return PointerGetDatum(result);
}

View File

@ -323,7 +323,7 @@ CreateStatistics(CreateStatsStmt *stmt)
if (build_mcv)
types[ntypes++] = CharGetDatum(STATS_EXT_MCV);
Assert(ntypes > 0 && ntypes <= lengthof(types));
stxkind = construct_array(types, ntypes, CHAROID, 1, true, 'c');
stxkind = construct_array(types, ntypes, CHAROID, 1, true, TYPALIGN_CHAR);
statrel = table_open(StatisticExtRelationId, RowExclusiveLock);

View File

@ -293,7 +293,7 @@ publicationListToArray(List *publist)
MemoryContextSwitchTo(oldcxt);
arr = construct_array(datums, list_length(publist),
TEXTOID, -1, false, 'i');
TEXTOID, -1, false, TYPALIGN_INT);
MemoryContextDelete(memcxt);

View File

@ -2030,14 +2030,14 @@ storage_name(char c)
{
switch (c)
{
case 'p':
case TYPSTORAGE_PLAIN:
return "PLAIN";
case 'm':
return "MAIN";
case 'x':
return "EXTENDED";
case 'e':
case TYPSTORAGE_EXTERNAL:
return "EXTERNAL";
case TYPSTORAGE_EXTENDED:
return "EXTENDED";
case TYPSTORAGE_MAIN:
return "MAIN";
default:
return "???";
}
@ -7388,13 +7388,13 @@ ATExecSetStorage(Relation rel, const char *colName, Node *newValue, LOCKMODE loc
storagemode = strVal(newValue);
if (pg_strcasecmp(storagemode, "plain") == 0)
newstorage = 'p';
newstorage = TYPSTORAGE_PLAIN;
else if (pg_strcasecmp(storagemode, "external") == 0)
newstorage = 'e';
newstorage = TYPSTORAGE_EXTERNAL;
else if (pg_strcasecmp(storagemode, "extended") == 0)
newstorage = 'x';
newstorage = TYPSTORAGE_EXTENDED;
else if (pg_strcasecmp(storagemode, "main") == 0)
newstorage = 'm';
newstorage = TYPSTORAGE_MAIN;
else
{
ereport(ERROR,
@ -7426,7 +7426,7 @@ ATExecSetStorage(Relation rel, const char *colName, Node *newValue, LOCKMODE loc
* safety check: do not allow toasted storage modes unless column datatype
* is TOAST-aware.
*/
if (newstorage == 'p' || TypeIsToastable(attrtuple->atttypid))
if (newstorage == TYPSTORAGE_PLAIN || TypeIsToastable(attrtuple->atttypid))
attrtuple->attstorage = newstorage;
else
ereport(ERROR,

View File

@ -132,8 +132,8 @@ DefineType(ParseState *pstate, List *names, List *parameters)
Oid elemType = InvalidOid;
char *defaultValue = NULL;
bool byValue = false;
char alignment = 'i'; /* default alignment */
char storage = 'p'; /* default TOAST storage method */
char alignment = TYPALIGN_INT; /* default alignment */
char storage = TYPSTORAGE_PLAIN; /* default TOAST storage method */
Oid collation = InvalidOid;
DefElem *likeTypeEl = NULL;
DefElem *internalLengthEl = NULL;
@ -382,16 +382,16 @@ DefineType(ParseState *pstate, List *names, List *parameters)
if (pg_strcasecmp(a, "double") == 0 ||
pg_strcasecmp(a, "float8") == 0 ||
pg_strcasecmp(a, "pg_catalog.float8") == 0)
alignment = 'd';
alignment = TYPALIGN_DOUBLE;
else if (pg_strcasecmp(a, "int4") == 0 ||
pg_strcasecmp(a, "pg_catalog.int4") == 0)
alignment = 'i';
alignment = TYPALIGN_INT;
else if (pg_strcasecmp(a, "int2") == 0 ||
pg_strcasecmp(a, "pg_catalog.int2") == 0)
alignment = 's';
alignment = TYPALIGN_SHORT;
else if (pg_strcasecmp(a, "char") == 0 ||
pg_strcasecmp(a, "pg_catalog.bpchar") == 0)
alignment = 'c';
alignment = TYPALIGN_CHAR;
else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@ -402,13 +402,13 @@ DefineType(ParseState *pstate, List *names, List *parameters)
char *a = defGetString(storageEl);
if (pg_strcasecmp(a, "plain") == 0)
storage = 'p';
storage = TYPSTORAGE_PLAIN;
else if (pg_strcasecmp(a, "external") == 0)
storage = 'e';
storage = TYPSTORAGE_EXTERNAL;
else if (pg_strcasecmp(a, "extended") == 0)
storage = 'x';
storage = TYPSTORAGE_EXTENDED;
else if (pg_strcasecmp(a, "main") == 0)
storage = 'm';
storage = TYPSTORAGE_MAIN;
else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
@ -643,8 +643,8 @@ DefineType(ParseState *pstate, List *names, List *parameters)
*/
array_type = makeArrayTypeName(typeName, typeNamespace);
/* alignment must be 'i' or 'd' for arrays */
alignment = (alignment == 'd') ? 'd' : 'i';
/* alignment must be TYPALIGN_INT or TYPALIGN_DOUBLE for arrays */
alignment = (alignment == TYPALIGN_DOUBLE) ? TYPALIGN_DOUBLE : TYPALIGN_INT;
TypeCreate(array_oid, /* force assignment of this type OID */
array_type, /* type name */
@ -672,7 +672,7 @@ DefineType(ParseState *pstate, List *names, List *parameters)
NULL, /* binary default isn't sent either */
false, /* never passed by value */
alignment, /* see above */
'x', /* ARRAY is always toastable */
TYPSTORAGE_EXTENDED, /* ARRAY is always toastable */
-1, /* typMod (Domains only) */
0, /* Array dimensions of typbasetype */
false, /* Type NOT NULL */
@ -1078,8 +1078,8 @@ DefineDomain(CreateDomainStmt *stmt)
*/
domainArrayName = makeArrayTypeName(domainName, domainNamespace);
/* alignment must be 'i' or 'd' for arrays */
alignment = (alignment == 'd') ? 'd' : 'i';
/* alignment must be TYPALIGN_INT or TYPALIGN_DOUBLE for arrays */
alignment = (alignment == TYPALIGN_DOUBLE) ? TYPALIGN_DOUBLE : TYPALIGN_INT;
TypeCreate(domainArrayOid, /* force assignment of this type OID */
domainArrayName, /* type name */
@ -1107,7 +1107,7 @@ DefineDomain(CreateDomainStmt *stmt)
NULL, /* binary default isn't sent either */
false, /* never passed by value */
alignment, /* see above */
'x', /* ARRAY is always toastable */
TYPSTORAGE_EXTENDED, /* ARRAY is always toastable */
-1, /* typMod (Domains only) */
0, /* Array dimensions of typbasetype */
false, /* Type NOT NULL */
@ -1221,8 +1221,8 @@ DefineEnum(CreateEnumStmt *stmt)
NULL, /* never a default type value */
NULL, /* binary default isn't sent either */
true, /* always passed by value */
'i', /* int alignment */
'p', /* TOAST strategy always plain */
TYPALIGN_INT, /* int alignment */
TYPSTORAGE_PLAIN, /* TOAST strategy always plain */
-1, /* typMod (Domains only) */
0, /* Array dimensions of typbasetype */
false, /* Type NOT NULL */
@ -1261,8 +1261,8 @@ DefineEnum(CreateEnumStmt *stmt)
NULL, /* never a default type value */
NULL, /* binary default isn't sent either */
false, /* never passed by value */
'i', /* enums have align i, so do their arrays */
'x', /* ARRAY is always toastable */
TYPALIGN_INT, /* enums have int align, so do their arrays */
TYPSTORAGE_EXTENDED, /* ARRAY is always toastable */
-1, /* typMod (Domains only) */
0, /* Array dimensions of typbasetype */
false, /* Type NOT NULL */
@ -1516,8 +1516,8 @@ DefineRange(CreateRangeStmt *stmt)
get_typlenbyvalalign(rangeSubtype,
&subtyplen, &subtypbyval, &subtypalign);
/* alignment must be 'i' or 'd' for ranges */
alignment = (subtypalign == 'd') ? 'd' : 'i';
/* alignment must be TYPALIGN_INT or TYPALIGN_DOUBLE for ranges */
alignment = (subtypalign == TYPALIGN_DOUBLE) ? TYPALIGN_DOUBLE : TYPALIGN_INT;
/* Allocate OID for array type */
rangeArrayOid = AssignTypeArrayOid();
@ -1550,7 +1550,7 @@ DefineRange(CreateRangeStmt *stmt)
NULL, /* no binary form available either */
false, /* never passed by value */
alignment, /* alignment */
'x', /* TOAST strategy (always extended) */
TYPSTORAGE_EXTENDED, /* TOAST strategy (always extended) */
-1, /* typMod (Domains only) */
0, /* Array dimensions of typbasetype */
false, /* Type NOT NULL */
@ -1592,7 +1592,7 @@ DefineRange(CreateRangeStmt *stmt)
NULL, /* binary default isn't sent either */
false, /* never passed by value */
alignment, /* alignment - same as range's */
'x', /* ARRAY is always toastable */
TYPSTORAGE_EXTENDED, /* ARRAY is always toastable */
-1, /* typMod (Domains only) */
0, /* Array dimensions of typbasetype */
false, /* Type NOT NULL */