1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-14 08:21:07 +03:00

Assorted examples of expanded type-safer palloc/pg_malloc API

This adds some uses of the new palloc/pg_malloc variants here and
there as a demonstration and test.  This is kept separate from the
actual API patch, since the latter might be backpatched at some point.

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/bb755632-2a43-d523-36f8-a1e7a389a907@enterprisedb.com
This commit is contained in:
Peter Eisentraut
2022-09-12 08:31:56 +02:00
parent 2016055a92
commit 5015e1e1b5
12 changed files with 90 additions and 111 deletions

View File

@ -229,10 +229,10 @@ CheckIndexCompatible(Oid oldId,
*/
indexInfo = makeIndexInfo(numberOfAttributes, numberOfAttributes,
accessMethodId, NIL, NIL, false, false, false, false);
typeObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
collationObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
coloptions = (int16 *) palloc(numberOfAttributes * sizeof(int16));
typeObjectId = palloc_array(Oid, numberOfAttributes);
collationObjectId = palloc_array(Oid, numberOfAttributes);
classObjectId = palloc_array(Oid, numberOfAttributes);
coloptions = palloc_array(int16, numberOfAttributes);
ComputeIndexAttrs(indexInfo,
typeObjectId, collationObjectId, classObjectId,
coloptions, attributeList,
@ -895,10 +895,10 @@ DefineIndex(Oid relationId,
!concurrent,
concurrent);
typeObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
collationObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
coloptions = (int16 *) palloc(numberOfAttributes * sizeof(int16));
typeObjectId = palloc_array(Oid, numberOfAttributes);
collationObjectId = palloc_array(Oid, numberOfAttributes);
classObjectId = palloc_array(Oid, numberOfAttributes);
coloptions = palloc_array(int16, numberOfAttributes);
ComputeIndexAttrs(indexInfo,
typeObjectId, collationObjectId, classObjectId,
coloptions, allIndexParams,
@ -1210,7 +1210,7 @@ DefineIndex(Oid relationId,
if ((!stmt->relation || stmt->relation->inh) && partdesc->nparts > 0)
{
int nparts = partdesc->nparts;
Oid *part_oids = palloc(sizeof(Oid) * nparts);
Oid *part_oids = palloc_array(Oid, nparts);
bool invalidate_parent = false;
Relation parentIndex;
TupleDesc parentDesc;
@ -1786,9 +1786,9 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
if (exclusionOpNames)
{
Assert(list_length(exclusionOpNames) == nkeycols);
indexInfo->ii_ExclusionOps = (Oid *) palloc(sizeof(Oid) * nkeycols);
indexInfo->ii_ExclusionProcs = (Oid *) palloc(sizeof(Oid) * nkeycols);
indexInfo->ii_ExclusionStrats = (uint16 *) palloc(sizeof(uint16) * nkeycols);
indexInfo->ii_ExclusionOps = palloc_array(Oid, nkeycols);
indexInfo->ii_ExclusionProcs = palloc_array(Oid, nkeycols);
indexInfo->ii_ExclusionStrats = palloc_array(uint16, nkeycols);
nextExclOp = list_head(exclusionOpNames);
}
else
@ -2112,7 +2112,7 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
if (!indexInfo->ii_OpclassOptions)
indexInfo->ii_OpclassOptions =
palloc0(sizeof(Datum) * indexInfo->ii_NumIndexAttrs);
palloc0_array(Datum, indexInfo->ii_NumIndexAttrs);
indexInfo->ii_OpclassOptions[attn] =
transformRelOptions((Datum) 0, attribute->opclassopts,
@ -3459,7 +3459,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
/* Save the list of relation OIDs in private context */
oldcontext = MemoryContextSwitchTo(private_context);
idx = palloc(sizeof(ReindexIndexInfo));
idx = palloc_object(ReindexIndexInfo);
idx->indexId = cellOid;
/* other fields set later */
@ -3508,7 +3508,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
oldcontext = MemoryContextSwitchTo(private_context);
idx = palloc(sizeof(ReindexIndexInfo));
idx = palloc_object(ReindexIndexInfo);
idx->indexId = cellOid;
indexIds = lappend(indexIds, idx);
/* other fields set later */
@ -3589,7 +3589,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* Save the list of relation OIDs in private context. Note
* that invalid indexes are allowed here.
*/
idx = palloc(sizeof(ReindexIndexInfo));
idx = palloc_object(ReindexIndexInfo);
idx->indexId = relationOid;
indexIds = lappend(indexIds, idx);
/* other fields set later */
@ -3734,7 +3734,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
oldcontext = MemoryContextSwitchTo(private_context);
newidx = palloc(sizeof(ReindexIndexInfo));
newidx = palloc_object(ReindexIndexInfo);
newidx->indexId = newIndexId;
newidx->safe = idx->safe;
newidx->tableId = idx->tableId;
@ -3748,10 +3748,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* avoid multiple locks taken on the same relation, instead we rely on
* parentRelationIds built earlier.
*/
lockrelid = palloc(sizeof(*lockrelid));
lockrelid = palloc_object(LockRelId);
*lockrelid = indexRel->rd_lockInfo.lockRelId;
relationLocks = lappend(relationLocks, lockrelid);
lockrelid = palloc(sizeof(*lockrelid));
lockrelid = palloc_object(LockRelId);
*lockrelid = newIndexRel->rd_lockInfo.lockRelId;
relationLocks = lappend(relationLocks, lockrelid);
@ -3783,11 +3783,11 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
oldcontext = MemoryContextSwitchTo(private_context);
/* Add lockrelid of heap relation to the list of locked relations */
lockrelid = palloc(sizeof(*lockrelid));
lockrelid = palloc_object(LockRelId);
*lockrelid = heapRelation->rd_lockInfo.lockRelId;
relationLocks = lappend(relationLocks, lockrelid);
heaplocktag = (LOCKTAG *) palloc(sizeof(LOCKTAG));
heaplocktag = palloc_object(LOCKTAG);
/* Save the LOCKTAG for this parent relation for the wait phase */
SET_LOCKTAG_RELATION(*heaplocktag, lockrelid->dbId, lockrelid->relId);