From 1b105f9472bdb9a68f709778afafb494997267bd Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Wed, 10 Dec 2025 07:36:46 +0900 Subject: Use palloc_object() and palloc_array() in backend code The idea is to encourage more the use of these new routines across the tree, as these offer stronger type safety guarantees than palloc(). This batch of changes includes most of the trivial changes suggested by the author for src/backend/. A total of 334 files are updated here. Among these files, 48 of them have their build change slightly; these are caused by line number changes as the new allocation formulas are simpler, shaving around 100 lines of code in total. Similar work has been done in 0c3c5c3b06a3 and 31d3847a37be. Author: David Geier Discussion: https://postgr.es/m/ad0748d4-3080-436e-b0bc-ac8f86a3466a@gmail.com --- src/backend/commands/analyze.c | 6 +++--- src/backend/commands/cluster.c | 4 ++-- src/backend/commands/copy.c | 2 +- src/backend/commands/copyfrom.c | 4 ++-- src/backend/commands/copyto.c | 4 ++-- src/backend/commands/createas.c | 2 +- src/backend/commands/dbcommands.c | 2 +- src/backend/commands/event_trigger.c | 24 +++++++++++----------- src/backend/commands/explain.c | 2 +- src/backend/commands/explain_dr.c | 2 +- src/backend/commands/explain_state.c | 7 ++----- src/backend/commands/extension.c | 4 ++-- src/backend/commands/functioncmds.c | 2 +- src/backend/commands/matview.c | 4 ++-- src/backend/commands/opclasscmds.c | 12 +++++------ src/backend/commands/policy.c | 6 +++--- src/backend/commands/publicationcmds.c | 6 +++--- src/backend/commands/seclabel.c | 2 +- src/backend/commands/subscriptioncmds.c | 4 ++-- src/backend/commands/tablecmds.c | 36 ++++++++++++++++----------------- src/backend/commands/trigger.c | 8 ++++---- src/backend/commands/tsearchcmds.c | 8 ++++---- src/backend/commands/typecmds.c | 4 ++-- src/backend/commands/user.c | 4 ++-- src/backend/commands/vacuumparallel.c | 6 +++--- 25 files changed, 81 insertions(+), 84 deletions(-) (limited to 'src/backend/commands') diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index b2429170679..5e2a7a8234e 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -1079,7 +1079,7 @@ examine_attribute(Relation onerel, int attnum, Node *index_expr) /* * Create the VacAttrStats struct. */ - stats = (VacAttrStats *) palloc0(sizeof(VacAttrStats)); + stats = palloc0_object(VacAttrStats); stats->attstattarget = attstattarget; /* @@ -1910,7 +1910,7 @@ std_typanalyze(VacAttrStats *stats) NULL); /* Save the operator info for compute_stats routines */ - mystats = (StdAnalyzeData *) palloc(sizeof(StdAnalyzeData)); + mystats = palloc_object(StdAnalyzeData); mystats->eqopr = eqopr; mystats->eqfunc = OidIsValid(eqopr) ? get_opcode(eqopr) : InvalidOid; mystats->ltopr = ltopr; @@ -2865,7 +2865,7 @@ compute_scalar_stats(VacAttrStatsP stats, /* Must copy the target values into anl_context */ old_context = MemoryContextSwitchTo(stats->anl_context); - corrs = (float4 *) palloc(sizeof(float4)); + corrs = palloc_object(float4); MemoryContextSwitchTo(old_context); /*---------- diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c index d1e772efb72..2120c85ccb4 100644 --- a/src/backend/commands/cluster.c +++ b/src/backend/commands/cluster.c @@ -1672,7 +1672,7 @@ get_tables_to_cluster(MemoryContext cluster_context) /* Use a permanent memory context for the result list */ old_context = MemoryContextSwitchTo(cluster_context); - rtc = (RelToCluster *) palloc(sizeof(RelToCluster)); + rtc = palloc_object(RelToCluster); rtc->tableOid = index->indrelid; rtc->indexOid = index->indexrelid; rtcs = lappend(rtcs, rtc); @@ -1726,7 +1726,7 @@ get_tables_to_cluster_partitioned(MemoryContext cluster_context, Oid indexOid) /* Use a permanent memory context for the result list */ old_context = MemoryContextSwitchTo(cluster_context); - rtc = (RelToCluster *) palloc(sizeof(RelToCluster)); + rtc = palloc_object(RelToCluster); rtc->tableOid = relid; rtc->indexOid = indexrelid; rtcs = lappend(rtcs, rtc); diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 28e878c3688..6454a39a01f 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -561,7 +561,7 @@ ProcessCopyOptions(ParseState *pstate, /* Support external use for option sanity checking */ if (opts_out == NULL) - opts_out = (CopyFormatOptions *) palloc0(sizeof(CopyFormatOptions)); + opts_out = palloc0_object(CopyFormatOptions); opts_out->file_encoding = -1; diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c index 12781963b4f..2ae3d2ba86e 100644 --- a/src/backend/commands/copyfrom.c +++ b/src/backend/commands/copyfrom.c @@ -364,7 +364,7 @@ CopyMultiInsertBufferInit(ResultRelInfo *rri) { CopyMultiInsertBuffer *buffer; - buffer = (CopyMultiInsertBuffer *) palloc(sizeof(CopyMultiInsertBuffer)); + buffer = palloc_object(CopyMultiInsertBuffer); memset(buffer->slots, 0, sizeof(TupleTableSlot *) * MAX_BUFFERED_TUPLES); buffer->resultRelInfo = rri; buffer->bistate = (rri->ri_FdwRoutine == NULL) ? GetBulkInsertState() : NULL; @@ -1558,7 +1558,7 @@ BeginCopyFrom(ParseState *pstate, }; /* Allocate workspace and zero all fields */ - cstate = (CopyFromStateData *) palloc0(sizeof(CopyFromStateData)); + cstate = palloc0_object(CopyFromStateData); /* * We allocate everything used by a cstate in a new memory context. This diff --git a/src/backend/commands/copyto.c b/src/backend/commands/copyto.c index cef452584e5..dae91630ac3 100644 --- a/src/backend/commands/copyto.c +++ b/src/backend/commands/copyto.c @@ -720,7 +720,7 @@ BeginCopyTo(ParseState *pstate, /* Allocate workspace and zero all fields */ - cstate = (CopyToStateData *) palloc0(sizeof(CopyToStateData)); + cstate = palloc0_object(CopyToStateData); /* * We allocate everything used by a cstate in a new memory context. This @@ -1527,7 +1527,7 @@ copy_dest_destroy(DestReceiver *self) DestReceiver * CreateCopyDestReceiver(void) { - DR_copy *self = (DR_copy *) palloc(sizeof(DR_copy)); + DR_copy *self = palloc_object(DR_copy); self->pub.receiveSlot = copy_dest_receive; self->pub.rStartup = copy_dest_startup; diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c index 1ccc2e55c64..ddc45e3aa0d 100644 --- a/src/backend/commands/createas.c +++ b/src/backend/commands/createas.c @@ -439,7 +439,7 @@ CreateTableAsRelExists(CreateTableAsStmt *ctas) DestReceiver * CreateIntoRelDestReceiver(IntoClause *intoClause) { - DR_intorel *self = (DR_intorel *) palloc0(sizeof(DR_intorel)); + DR_intorel *self = palloc0_object(DR_intorel); self->pub.receiveSlot = intorel_receive; self->pub.rStartup = intorel_startup; diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index da85cd2d435..d1f3be89b35 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -431,7 +431,7 @@ ScanSourceDatabasePgClassTuple(HeapTupleData *tuple, Oid tbid, Oid dbid, classForm->oid); /* Prepare a rel info element and add it to the list. */ - relinfo = (CreateDBRelInfo *) palloc(sizeof(CreateDBRelInfo)); + relinfo = palloc_object(CreateDBRelInfo); if (OidIsValid(classForm->reltablespace)) relinfo->rlocator.spcOid = classForm->reltablespace; else diff --git a/src/backend/commands/event_trigger.c b/src/backend/commands/event_trigger.c index f34868da5ab..e7182308130 100644 --- a/src/backend/commands/event_trigger.c +++ b/src/backend/commands/event_trigger.c @@ -364,7 +364,7 @@ filter_list_to_array(List *filterlist) int i = 0, l = list_length(filterlist); - data = (Datum *) palloc(l * sizeof(Datum)); + data = palloc_array(Datum, l); foreach(lc, filterlist) { @@ -1286,7 +1286,7 @@ EventTriggerSQLDropAddObject(const ObjectAddress *object, bool original, bool no oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); - obj = palloc0(sizeof(SQLDropObject)); + obj = palloc0_object(SQLDropObject); obj->address = *object; obj->original = original; obj->normal = normal; @@ -1726,7 +1726,7 @@ EventTriggerCollectSimpleCommand(ObjectAddress address, oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); - command = palloc(sizeof(CollectedCommand)); + command = palloc_object(CollectedCommand); command->type = SCT_Simple; command->in_extension = creating_extension; @@ -1762,7 +1762,7 @@ EventTriggerAlterTableStart(Node *parsetree) oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); - command = palloc(sizeof(CollectedCommand)); + command = palloc_object(CollectedCommand); command->type = SCT_AlterTable; command->in_extension = creating_extension; @@ -1818,7 +1818,7 @@ EventTriggerCollectAlterTableSubcmd(Node *subcmd, ObjectAddress address) oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); - newsub = palloc(sizeof(CollectedATSubcmd)); + newsub = palloc_object(CollectedATSubcmd); newsub->address = address; newsub->parsetree = copyObject(subcmd); @@ -1892,7 +1892,7 @@ EventTriggerCollectGrant(InternalGrant *istmt) /* * This is tedious, but necessary. */ - icopy = palloc(sizeof(InternalGrant)); + icopy = palloc_object(InternalGrant); memcpy(icopy, istmt, sizeof(InternalGrant)); icopy->objects = list_copy(istmt->objects); icopy->grantees = list_copy(istmt->grantees); @@ -1901,7 +1901,7 @@ EventTriggerCollectGrant(InternalGrant *istmt) icopy->col_privs = lappend(icopy->col_privs, copyObject(lfirst(cell))); /* Now collect it, using the copied InternalGrant */ - command = palloc(sizeof(CollectedCommand)); + command = palloc_object(CollectedCommand); command->type = SCT_Grant; command->in_extension = creating_extension; command->d.grant.istmt = icopy; @@ -1932,7 +1932,7 @@ EventTriggerCollectAlterOpFam(AlterOpFamilyStmt *stmt, Oid opfamoid, oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); - command = palloc(sizeof(CollectedCommand)); + command = palloc_object(CollectedCommand); command->type = SCT_AlterOpFamily; command->in_extension = creating_extension; ObjectAddressSet(command->d.opfam.address, @@ -1965,7 +1965,7 @@ EventTriggerCollectCreateOpClass(CreateOpClassStmt *stmt, Oid opcoid, oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); - command = palloc0(sizeof(CollectedCommand)); + command = palloc0_object(CollectedCommand); command->type = SCT_CreateOpClass; command->in_extension = creating_extension; ObjectAddressSet(command->d.createopc.address, @@ -1999,12 +1999,12 @@ EventTriggerCollectAlterTSConfig(AlterTSConfigurationStmt *stmt, Oid cfgId, oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); - command = palloc0(sizeof(CollectedCommand)); + command = palloc0_object(CollectedCommand); command->type = SCT_AlterTSConfig; command->in_extension = creating_extension; ObjectAddressSet(command->d.atscfg.address, TSConfigRelationId, cfgId); - command->d.atscfg.dictIds = palloc(sizeof(Oid) * ndicts); + command->d.atscfg.dictIds = palloc_array(Oid, ndicts); memcpy(command->d.atscfg.dictIds, dictIds, sizeof(Oid) * ndicts); command->d.atscfg.ndicts = ndicts; command->parsetree = (Node *) copyObject(stmt); @@ -2033,7 +2033,7 @@ EventTriggerCollectAlterDefPrivs(AlterDefaultPrivilegesStmt *stmt) oldcxt = MemoryContextSwitchTo(currentEventTriggerState->cxt); - command = palloc0(sizeof(CollectedCommand)); + command = palloc0_object(CollectedCommand); command->type = SCT_AlterDefaultPrivileges; command->d.defprivs.objtype = stmt->action->objtype; command->in_extension = creating_extension; diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index 7e699f8595e..5a6390631eb 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -4980,7 +4980,7 @@ ExplainCreateWorkersState(int num_workers) { ExplainWorkersState *wstate; - wstate = (ExplainWorkersState *) palloc(sizeof(ExplainWorkersState)); + wstate = palloc_object(ExplainWorkersState); wstate->num_workers = num_workers; wstate->worker_inited = (bool *) palloc0(num_workers * sizeof(bool)); wstate->worker_str = (StringInfoData *) diff --git a/src/backend/commands/explain_dr.c b/src/backend/commands/explain_dr.c index 95685d7e88d..5833aa80fb5 100644 --- a/src/backend/commands/explain_dr.c +++ b/src/backend/commands/explain_dr.c @@ -276,7 +276,7 @@ CreateExplainSerializeDestReceiver(ExplainState *es) { SerializeDestReceiver *self; - self = (SerializeDestReceiver *) palloc0(sizeof(SerializeDestReceiver)); + self = palloc0_object(SerializeDestReceiver); self->pub.receiveSlot = serializeAnalyzeReceive; self->pub.rStartup = serializeAnalyzeStartup; diff --git a/src/backend/commands/explain_state.c b/src/backend/commands/explain_state.c index dae256809d2..a6623f8fa52 100644 --- a/src/backend/commands/explain_state.c +++ b/src/backend/commands/explain_state.c @@ -60,7 +60,7 @@ static int ExplainExtensionOptionsAllocated = 0; ExplainState * NewExplainState(void) { - ExplainState *es = (ExplainState *) palloc0(sizeof(ExplainState)); + ExplainState *es = palloc0_object(ExplainState); /* Set default options (most fields can be left as zeroes). */ es->costs = true; @@ -294,10 +294,7 @@ SetExplainExtensionState(ExplainState *es, int extension_id, void *opaque) int i; i = pg_nextpower2_32(extension_id + 1); - es->extension_state = (void **) - repalloc0(es->extension_state, - es->extension_state_allocated * sizeof(void *), - i * sizeof(void *)); + es->extension_state = repalloc0_array(es->extension_state, void *, es->extension_state_allocated, i); es->extension_state_allocated = i; } diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index ebc204c4462..c43b74e319e 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -724,7 +724,7 @@ read_extension_aux_control_file(const ExtensionControlFile *pcontrol, /* * Flat-copy the struct. Pointer fields share values with original. */ - acontrol = (ExtensionControlFile *) palloc(sizeof(ExtensionControlFile)); + acontrol = palloc_object(ExtensionControlFile); memcpy(acontrol, pcontrol, sizeof(ExtensionControlFile)); /* @@ -1349,7 +1349,7 @@ get_ext_ver_info(const char *versionname, List **evi_list) return evi; } - evi = (ExtensionVersionInfo *) palloc(sizeof(ExtensionVersionInfo)); + evi = palloc_object(ExtensionVersionInfo); evi->name = pstrdup(versionname); evi->reachable = NIL; evi->installable = false; diff --git a/src/backend/commands/functioncmds.c b/src/backend/commands/functioncmds.c index 59d00638ee6..8a435cd93db 100644 --- a/src/backend/commands/functioncmds.c +++ b/src/backend/commands/functioncmds.c @@ -913,7 +913,7 @@ interpret_AS_clause(Oid languageOid, const char *languageName, { SQLFunctionParseInfoPtr pinfo; - pinfo = (SQLFunctionParseInfoPtr) palloc0(sizeof(SQLFunctionParseInfo)); + pinfo = palloc0_object(SQLFunctionParseInfo); pinfo->fname = funcname; pinfo->nargs = list_length(parameterTypes); diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c index a5c579ce112..a1fd4cab35b 100644 --- a/src/backend/commands/matview.c +++ b/src/backend/commands/matview.c @@ -463,7 +463,7 @@ refresh_matview_datafill(DestReceiver *dest, Query *query, DestReceiver * CreateTransientRelDestReceiver(Oid transientoid) { - DR_transientrel *self = (DR_transientrel *) palloc0(sizeof(DR_transientrel)); + DR_transientrel *self = palloc0_object(DR_transientrel); self->pub.receiveSlot = transientrel_receive; self->pub.rStartup = transientrel_startup; @@ -713,7 +713,7 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner, * include all rows. */ tupdesc = matviewRel->rd_att; - opUsedForQual = (Oid *) palloc0(sizeof(Oid) * relnatts); + opUsedForQual = palloc0_array(Oid, relnatts); foundUniqueIndex = false; indexoidlist = RelationGetIndexList(matviewRel); diff --git a/src/backend/commands/opclasscmds.c b/src/backend/commands/opclasscmds.c index a6dd8eab518..992ae789b00 100644 --- a/src/backend/commands/opclasscmds.c +++ b/src/backend/commands/opclasscmds.c @@ -523,7 +523,7 @@ DefineOpClass(CreateOpClassStmt *stmt) #endif /* Save the info */ - member = (OpFamilyMember *) palloc0(sizeof(OpFamilyMember)); + member = palloc0_object(OpFamilyMember); member->is_func = false; member->object = operOid; member->number = item->number; @@ -547,7 +547,7 @@ DefineOpClass(CreateOpClassStmt *stmt) get_func_name(funcOid)); #endif /* Save the info */ - member = (OpFamilyMember *) palloc0(sizeof(OpFamilyMember)); + member = palloc0_object(OpFamilyMember); member->is_func = true; member->object = funcOid; member->number = item->number; @@ -940,7 +940,7 @@ AlterOpFamilyAdd(AlterOpFamilyStmt *stmt, Oid amoid, Oid opfamilyoid, #endif /* Save the info */ - member = (OpFamilyMember *) palloc0(sizeof(OpFamilyMember)); + member = palloc0_object(OpFamilyMember); member->is_func = false; member->object = operOid; member->number = item->number; @@ -970,7 +970,7 @@ AlterOpFamilyAdd(AlterOpFamilyStmt *stmt, Oid amoid, Oid opfamilyoid, #endif /* Save the info */ - member = (OpFamilyMember *) palloc0(sizeof(OpFamilyMember)); + member = palloc0_object(OpFamilyMember); member->is_func = true; member->object = funcOid; member->number = item->number; @@ -1058,7 +1058,7 @@ AlterOpFamilyDrop(AlterOpFamilyStmt *stmt, Oid amoid, Oid opfamilyoid, item->number, maxOpNumber))); processTypesSpec(item->class_args, &lefttype, &righttype); /* Save the info */ - member = (OpFamilyMember *) palloc0(sizeof(OpFamilyMember)); + member = palloc0_object(OpFamilyMember); member->is_func = false; member->number = item->number; member->lefttype = lefttype; @@ -1074,7 +1074,7 @@ AlterOpFamilyDrop(AlterOpFamilyStmt *stmt, Oid amoid, Oid opfamilyoid, item->number, maxProcNumber))); processTypesSpec(item->class_args, &lefttype, &righttype); /* Save the info */ - member = (OpFamilyMember *) palloc0(sizeof(OpFamilyMember)); + member = palloc0_object(OpFamilyMember); member->is_func = true; member->number = item->number; member->lefttype = lefttype; diff --git a/src/backend/commands/policy.c b/src/backend/commands/policy.c index 83056960fe4..5bd5f8c9968 100644 --- a/src/backend/commands/policy.c +++ b/src/backend/commands/policy.c @@ -144,7 +144,7 @@ policy_role_list_to_array(List *roles, int *num_roles) if (roles == NIL) { *num_roles = 1; - role_oids = (Datum *) palloc(*num_roles * sizeof(Datum)); + role_oids = palloc_array(Datum, *num_roles); role_oids[0] = ObjectIdGetDatum(ACL_ID_PUBLIC); return role_oids; @@ -471,7 +471,7 @@ RemoveRoleFromObjectPolicy(Oid roleid, Oid classid, Oid policy_id) * Ordinarily there'd be exactly one, but we must cope with duplicate * mentions, since CREATE/ALTER POLICY historically have allowed that. */ - role_oids = (Datum *) palloc(num_roles * sizeof(Datum)); + role_oids = palloc_array(Datum, num_roles); for (i = 0, j = 0; i < num_roles; i++) { if (roles[i] != roleid) @@ -945,7 +945,7 @@ AlterPolicy(AlterPolicyStmt *stmt) nitems = ARR_DIMS(policy_roles)[0]; - role_oids = (Datum *) palloc(nitems * sizeof(Datum)); + role_oids = palloc_array(Datum, nitems); for (i = 0; i < nitems; i++) role_oids[i] = ObjectIdGetDatum(roles[i]); diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index 1faf3a8c372..a1983508950 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -1345,7 +1345,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup, */ if (!found) { - oldrel = palloc(sizeof(PublicationRelInfo)); + oldrel = palloc_object(PublicationRelInfo); oldrel->whereClause = NULL; oldrel->columns = NIL; oldrel->relation = table_open(oldrelid, @@ -1757,7 +1757,7 @@ OpenTableList(List *tables) continue; } - pub_rel = palloc(sizeof(PublicationRelInfo)); + pub_rel = palloc_object(PublicationRelInfo); pub_rel->relation = rel; pub_rel->whereClause = t->whereClause; pub_rel->columns = t->columns; @@ -1826,7 +1826,7 @@ OpenTableList(List *tables) /* find_all_inheritors already got lock */ rel = table_open(childrelid, NoLock); - pub_rel = palloc(sizeof(PublicationRelInfo)); + pub_rel = palloc_object(PublicationRelInfo); pub_rel->relation = rel; /* child inherits WHERE clause from parent */ pub_rel->whereClause = t->whereClause; diff --git a/src/backend/commands/seclabel.c b/src/backend/commands/seclabel.c index cee5d7bbb9c..07bed6e1487 100644 --- a/src/backend/commands/seclabel.c +++ b/src/backend/commands/seclabel.c @@ -573,7 +573,7 @@ register_label_provider(const char *provider_name, check_object_relabel_type hoo MemoryContext oldcxt; oldcxt = MemoryContextSwitchTo(TopMemoryContext); - provider = palloc(sizeof(LabelProvider)); + provider = palloc_object(LabelProvider); provider->provider_name = pstrdup(provider_name); provider->hook = hook; label_provider_list = lappend(label_provider_list, provider); diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index 8c856af3493..abbcaff0838 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -566,7 +566,7 @@ publicationListToArray(List *publist) ALLOCSET_DEFAULT_SIZES); oldcxt = MemoryContextSwitchTo(memcxt); - datums = (Datum *) palloc(sizeof(Datum) * list_length(publist)); + datums = palloc_array(Datum, list_length(publist)); check_duplicates_in_publist(publist, datums); @@ -1055,7 +1055,7 @@ AlterSubscription_refresh(Subscription *sub, bool copy_data, { char state; XLogRecPtr statelsn; - SubRemoveRels *remove_rel = palloc(sizeof(SubRemoveRels)); + SubRemoveRels *remove_rel = palloc_object(SubRemoveRels); /* * Lock pg_subscription_rel with AccessExclusiveLock to diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 07e5b95782e..1c9ef53be20 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -999,7 +999,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, Assert(colDef->cooked_default == NULL); - rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault)); + rawEnt = palloc_object(RawColumnDefault); rawEnt->attnum = attnum; rawEnt->raw_default = colDef->raw_default; rawEnt->generated = colDef->generated; @@ -1009,7 +1009,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, { CookedConstraint *cooked; - cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint)); + cooked = palloc_object(CookedConstraint); cooked->contype = CONSTR_DEFAULT; cooked->conoid = InvalidOid; /* until created */ cooked->name = NULL; @@ -6568,7 +6568,7 @@ ATGetQueueEntry(List **wqueue, Relation rel) * Not there, so add it. Note that we make a copy of the relation's * existing descriptor before anything interesting can happen to it. */ - tab = (AlteredTableInfo *) palloc0(sizeof(AlteredTableInfo)); + tab = palloc0_object(AlteredTableInfo); tab->relid = relid; tab->rel = NULL; /* set later */ tab->relkind = rel->rd_rel->relkind; @@ -7406,7 +7406,7 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel, { RawColumnDefault *rawEnt; - rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault)); + rawEnt = palloc_object(RawColumnDefault); rawEnt->attnum = attribute->attnum; rawEnt->raw_default = copyObject(colDef->raw_default); rawEnt->generated = colDef->generated; @@ -7507,7 +7507,7 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel, defval = expression_planner(defval); /* Add the new default to the newvals list */ - newval = (NewColumnValue *) palloc0(sizeof(NewColumnValue)); + newval = palloc0_object(NewColumnValue); newval->attnum = attribute->attnum; newval->expr = defval; newval->is_generated = (colDef->generated != '\0'); @@ -8176,7 +8176,7 @@ ATExecColumnDefault(Relation rel, const char *colName, /* SET DEFAULT */ RawColumnDefault *rawEnt; - rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault)); + rawEnt = palloc_object(RawColumnDefault); rawEnt->attnum = attnum; rawEnt->raw_default = newDefault; rawEnt->generated = '\0'; @@ -8704,7 +8704,7 @@ ATExecSetExpression(AlteredTableInfo *tab, Relation rel, const char *colName, false, false); /* Prepare to store the new expression, in the catalogs */ - rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault)); + rawEnt = palloc_object(RawColumnDefault); rawEnt->attnum = attnum; rawEnt->raw_default = newExpr; rawEnt->generated = attgenerated; @@ -8721,7 +8721,7 @@ ATExecSetExpression(AlteredTableInfo *tab, Relation rel, const char *colName, /* Prepare for table rewrite */ defval = (Expr *) build_column_default(rel, attnum); - newval = (NewColumnValue *) palloc0(sizeof(NewColumnValue)); + newval = palloc0_object(NewColumnValue); newval->attnum = attnum; newval->expr = expression_planner(defval); newval->is_generated = true; @@ -9949,7 +9949,7 @@ ATAddCheckNNConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel, { NewConstraint *newcon; - newcon = (NewConstraint *) palloc0(sizeof(NewConstraint)); + newcon = palloc0_object(NewConstraint); newcon->name = ccon->name; newcon->contype = ccon->contype; newcon->qual = ccon->expr; @@ -10946,7 +10946,7 @@ addFkRecurseReferenced(Constraint *fkconstraint, Relation rel, false); if (map) { - mapped_pkattnum = palloc(sizeof(AttrNumber) * numfks); + mapped_pkattnum = palloc_array(AttrNumber, numfks); for (int j = 0; j < numfks; j++) mapped_pkattnum[j] = map->attnums[pkattnum[j] - 1]; } @@ -11081,7 +11081,7 @@ addFkRecurseReferencing(List **wqueue, Constraint *fkconstraint, Relation rel, tab = ATGetQueueEntry(wqueue, rel); - newcon = (NewConstraint *) palloc0(sizeof(NewConstraint)); + newcon = palloc0_object(NewConstraint); newcon->name = get_constraint_name(parentConstr); newcon->contype = CONSTR_FOREIGN; newcon->refrelid = RelationGetRelid(pkrel); @@ -12503,7 +12503,7 @@ ATExecAlterConstrEnforceability(List **wqueue, ATAlterConstraint *cmdcon, AlteredTableInfo *tab; NewConstraint *newcon; - newcon = (NewConstraint *) palloc0(sizeof(NewConstraint)); + newcon = palloc0_object(NewConstraint); newcon->name = fkconstraint->conname; newcon->contype = CONSTR_FOREIGN; newcon->refrelid = currcon->confrelid; @@ -13019,7 +13019,7 @@ QueueFKConstraintValidation(List **wqueue, Relation conrel, Relation fkrel, /* for now this is all we need */ fkconstraint->conname = pstrdup(NameStr(con->conname)); - newcon = (NewConstraint *) palloc0(sizeof(NewConstraint)); + newcon = palloc0_object(NewConstraint); newcon->name = fkconstraint->conname; newcon->contype = CONSTR_FOREIGN; newcon->refrelid = con->confrelid; @@ -13167,7 +13167,7 @@ QueueCheckConstraintValidation(List **wqueue, Relation conrel, Relation rel, } /* Queue validation for phase 3 */ - newcon = (NewConstraint *) palloc0(sizeof(NewConstraint)); + newcon = palloc0_object(NewConstraint); newcon->name = constrName; newcon->contype = CONSTR_CHECK; newcon->refrelid = InvalidOid; @@ -14522,7 +14522,7 @@ ATPrepAlterColumnType(List **wqueue, * Add a work queue item to make ATRewriteTable update the column * contents. */ - newval = (NewColumnValue *) palloc0(sizeof(NewColumnValue)); + newval = palloc0_object(NewColumnValue); newval->attnum = attnum; newval->expr = (Expr *) transform; newval->is_generated = false; @@ -19262,7 +19262,7 @@ register_on_commit_action(Oid relid, OnCommitAction action) oldcxt = MemoryContextSwitchTo(CacheMemoryContext); - oc = (OnCommitItem *) palloc(sizeof(OnCommitItem)); + oc = palloc_object(OnCommitItem); oc->relid = relid; oc->oncommit = action; oc->creating_subid = GetCurrentSubTransactionId(); @@ -20575,8 +20575,8 @@ AttachPartitionEnsureIndexes(List **wqueue, Relation rel, Relation attachrel) idxes = RelationGetIndexList(rel); attachRelIdxs = RelationGetIndexList(attachrel); - attachrelIdxRels = palloc(sizeof(Relation) * list_length(attachRelIdxs)); - attachInfos = palloc(sizeof(IndexInfo *) * list_length(attachRelIdxs)); + attachrelIdxRels = palloc_array(Relation, list_length(attachRelIdxs)); + attachInfos = palloc_array(IndexInfo *, list_length(attachRelIdxs)); /* Build arrays of all existing indexes and their IndexInfos */ foreach_oid(cldIdxId, attachRelIdxs) diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c index 579ac8d76ae..12c97f2c023 100644 --- a/src/backend/commands/trigger.c +++ b/src/backend/commands/trigger.c @@ -1991,7 +1991,7 @@ RelationBuildTriggers(Relation relation) } /* Build trigdesc */ - trigdesc = (TriggerDesc *) palloc0(sizeof(TriggerDesc)); + trigdesc = palloc0_object(TriggerDesc); trigdesc->triggers = triggers; trigdesc->numtriggers = numtrigs; for (i = 0; i < numtrigs; i++) @@ -2096,7 +2096,7 @@ CopyTriggerDesc(TriggerDesc *trigdesc) if (trigdesc == NULL || trigdesc->numtriggers <= 0) return NULL; - newdesc = (TriggerDesc *) palloc(sizeof(TriggerDesc)); + newdesc = palloc_object(TriggerDesc); memcpy(newdesc, trigdesc, sizeof(TriggerDesc)); trigger = (Trigger *) palloc(trigdesc->numtriggers * sizeof(Trigger)); @@ -4901,7 +4901,7 @@ GetAfterTriggersTableData(Oid relid, CmdType cmdType) oldcxt = MemoryContextSwitchTo(CurTransactionContext); - table = (AfterTriggersTableData *) palloc0(sizeof(AfterTriggersTableData)); + table = palloc0_object(AfterTriggersTableData); table->relid = relid; table->cmdType = cmdType; qs->tables = lappend(qs->tables, table); @@ -5050,7 +5050,7 @@ MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType) MemoryContextSwitchTo(oldcxt); /* Now build the TransitionCaptureState struct, in caller's context */ - state = (TransitionCaptureState *) palloc0(sizeof(TransitionCaptureState)); + state = palloc0_object(TransitionCaptureState); state->tcs_delete_old_table = need_old_del; state->tcs_update_old_table = need_old_upd; state->tcs_update_new_table = need_new_upd; diff --git a/src/backend/commands/tsearchcmds.c b/src/backend/commands/tsearchcmds.c index dc7df736fb8..ec4580e17c9 100644 --- a/src/backend/commands/tsearchcmds.c +++ b/src/backend/commands/tsearchcmds.c @@ -1027,7 +1027,7 @@ DefineTSConfiguration(List *names, List *parameters, ObjectAddress *copied) * know that they will be used. */ max_slots = MAX_CATALOG_MULTI_INSERT_BYTES / sizeof(FormData_pg_ts_config_map); - slot = palloc(sizeof(TupleTableSlot *) * max_slots); + slot = palloc_array(TupleTableSlot *, max_slots); ScanKeyInit(&skey, Anum_pg_ts_config_map_mapcfg, @@ -1261,7 +1261,7 @@ getTokenTypes(Oid prsId, List *tokennames) { if (strcmp(strVal(val), list[j].alias) == 0) { - TSTokenTypeItem *ts = (TSTokenTypeItem *) palloc0(sizeof(TSTokenTypeItem)); + TSTokenTypeItem *ts = palloc0_object(TSTokenTypeItem); ts->num = list[j].lexid; ts->name = pstrdup(strVal(val)); @@ -1344,7 +1344,7 @@ MakeConfigurationMapping(AlterTSConfigurationStmt *stmt, * Convert list of dictionary names to array of dict OIDs */ ndict = list_length(stmt->dicts); - dictIds = (Oid *) palloc(sizeof(Oid) * ndict); + dictIds = palloc_array(Oid, ndict); i = 0; foreach(c, stmt->dicts) { @@ -1432,7 +1432,7 @@ MakeConfigurationMapping(AlterTSConfigurationStmt *stmt, /* Allocate the slots to use and initialize them */ nslots = Min(ntoken * ndict, MAX_CATALOG_MULTI_INSERT_BYTES / sizeof(FormData_pg_ts_config_map)); - slot = palloc(sizeof(TupleTableSlot *) * nslots); + slot = palloc_array(TupleTableSlot *, nslots); for (i = 0; i < nslots; i++) slot[i] = MakeSingleTupleTableSlot(RelationGetDescr(relMap), &TTSOpsHeapTuple); diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c index 47d5047fe8b..0eb8e0a2bb0 100644 --- a/src/backend/commands/typecmds.c +++ b/src/backend/commands/typecmds.c @@ -3447,10 +3447,10 @@ get_rels_with_domain(Oid domainOid, LOCKMODE lockmode) } /* Build the RelToCheck entry with enough space for all atts */ - rtc = (RelToCheck *) palloc(sizeof(RelToCheck)); + rtc = palloc_object(RelToCheck); rtc->rel = rel; rtc->natts = 0; - rtc->atts = (int *) palloc(sizeof(int) * RelationGetNumberOfAttributes(rel)); + rtc->atts = palloc_array(int, RelationGetNumberOfAttributes(rel)); result = lappend(result, rtc); } diff --git a/src/backend/commands/user.c b/src/backend/commands/user.c index 6ae42ea5656..ef4de1e7fd1 100644 --- a/src/backend/commands/user.c +++ b/src/backend/commands/user.c @@ -1904,7 +1904,7 @@ AddRoleMems(Oid currentUserId, const char *rolename, Oid roleid, else { Oid objectId; - Oid *newmembers = palloc(sizeof(Oid)); + Oid *newmembers = palloc_object(Oid); /* * The values for these options can be taken directly from 'popt'. @@ -2295,7 +2295,7 @@ initialize_revoke_actions(CatCList *memlist) if (memlist->n_members == 0) return NULL; - result = palloc(sizeof(RevokeRoleGrantAction) * memlist->n_members); + result = palloc_array(RevokeRoleGrantAction, memlist->n_members); for (i = 0; i < memlist->n_members; i++) result[i] = RRG_NOOP; return result; diff --git a/src/backend/commands/vacuumparallel.c b/src/backend/commands/vacuumparallel.c index 0feea1d30ec..8a37c08871a 100644 --- a/src/backend/commands/vacuumparallel.c +++ b/src/backend/commands/vacuumparallel.c @@ -268,7 +268,7 @@ parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes, /* * Compute the number of parallel vacuum workers to launch */ - will_parallel_vacuum = (bool *) palloc0(sizeof(bool) * nindexes); + will_parallel_vacuum = palloc0_array(bool, nindexes); parallel_workers = parallel_vacuum_compute_workers(indrels, nindexes, nrequested_workers, will_parallel_vacuum); @@ -279,7 +279,7 @@ parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes, return NULL; } - pvs = (ParallelVacuumState *) palloc0(sizeof(ParallelVacuumState)); + pvs = palloc0_object(ParallelVacuumState); pvs->indrels = indrels; pvs->nindexes = nindexes; pvs->will_parallel_vacuum = will_parallel_vacuum; @@ -444,7 +444,7 @@ parallel_vacuum_end(ParallelVacuumState *pvs, IndexBulkDeleteResult **istats) if (indstats->istat_updated) { - istats[i] = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult)); + istats[i] = palloc0_object(IndexBulkDeleteResult); memcpy(istats[i], &indstats->istat, sizeof(IndexBulkDeleteResult)); } else -- cgit v1.2.3