1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Replace many MemSet calls with struct initialization

This replaces all MemSet() calls with struct initialization where that
is easily and obviously possible.  (For example, some cases have to
worry about padding bits, so I left those.)

(The same could be done with appropriate memset() calls, but this
patch is part of an effort to phase out MemSet(), so it doesn't touch
memset() calls.)

Reviewed-by: Ranier Vilela <ranier.vf@gmail.com>
Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org>
Discussion: https://www.postgresql.org/message-id/9847b13c-b785-f4e2-75c3-12ec77a3b05c@enterprisedb.com
This commit is contained in:
Peter Eisentraut
2022-07-16 08:42:15 +02:00
parent c94ae9d827
commit 9fd45870c1
51 changed files with 200 additions and 468 deletions

View File

@ -554,12 +554,10 @@ report_corruption_internal(Tuplestorestate *tupstore, TupleDesc tupdesc,
BlockNumber blkno, OffsetNumber offnum,
AttrNumber attnum, char *msg)
{
Datum values[HEAPCHECK_RELATION_COLS];
bool nulls[HEAPCHECK_RELATION_COLS];
Datum values[HEAPCHECK_RELATION_COLS] = {0};
bool nulls[HEAPCHECK_RELATION_COLS] = {0};
HeapTuple tuple;
MemSet(values, 0, sizeof(values));
MemSet(nulls, 0, sizeof(nulls));
values[0] = Int64GetDatum(blkno);
values[1] = Int32GetDatum(offnum);
values[2] = Int32GetDatum(attnum);

View File

@ -26,9 +26,7 @@ blcostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
double *indexPages)
{
IndexOptInfo *index = path->indexinfo;
GenericCosts costs;
MemSet(&costs, 0, sizeof(costs));
GenericCosts costs = {0};
/* We have to visit all index tuples anyway */
costs.numIndexTuples = index->tuples;

View File

@ -202,7 +202,7 @@ brin_page_items(PG_FUNCTION_ARGS)
for (;;)
{
Datum values[7];
bool nulls[7];
bool nulls[7] = {0};
/*
* This loop is called once for every attribute of every tuple in the
@ -230,8 +230,6 @@ brin_page_items(PG_FUNCTION_ARGS)
else
attno++;
MemSet(nulls, 0, sizeof(nulls));
if (unusedItem)
{
values[0] = UInt16GetDatum(offset);
@ -334,7 +332,7 @@ brin_metapage_info(PG_FUNCTION_ARGS)
BrinMetaPageData *meta;
TupleDesc tupdesc;
Datum values[4];
bool nulls[4];
bool nulls[4] = {0};
HeapTuple htup;
if (!superuser())
@ -354,7 +352,6 @@ brin_metapage_info(PG_FUNCTION_ARGS)
/* Extract values from the metapage */
meta = (BrinMetaPageData *) PageGetContents(page);
MemSet(nulls, 0, sizeof(nulls));
values[0] = CStringGetTextDatum(psprintf("0x%08X", meta->brinMagic));
values[1] = Int32GetDatum(meta->brinVersion);
values[2] = Int32GetDatum(meta->pagesPerRange);

View File

@ -238,7 +238,7 @@ hash_page_stats(PG_FUNCTION_ARGS)
Page page;
int j;
Datum values[9];
bool nulls[9];
bool nulls[9] = {0};
HashPageStat stat;
HeapTuple tuple;
TupleDesc tupleDesc;
@ -261,8 +261,6 @@ hash_page_stats(PG_FUNCTION_ARGS)
elog(ERROR, "return type must be a row type");
tupleDesc = BlessTupleDesc(tupleDesc);
MemSet(nulls, 0, sizeof(nulls));
j = 0;
values[j++] = Int32GetDatum(stat.live_items);
values[j++] = Int32GetDatum(stat.dead_items);
@ -303,7 +301,7 @@ hash_page_items(PG_FUNCTION_ARGS)
Page page;
Datum result;
Datum values[3];
bool nulls[3];
bool nulls[3] = {0};
uint32 hashkey;
HeapTuple tuple;
FuncCallContext *fctx;
@ -361,8 +359,6 @@ hash_page_items(PG_FUNCTION_ARGS)
itup = (IndexTuple) PageGetItem(uargs->page, id);
MemSet(nulls, 0, sizeof(nulls));
j = 0;
values[j++] = Int32GetDatum((int32) uargs->offset);
values[j++] = PointerGetDatum(&itup->t_tid);
@ -409,7 +405,7 @@ hash_bitmap_info(PG_FUNCTION_ARGS)
int i,
j;
Datum values[3];
bool nulls[3];
bool nulls[3] = {0};
uint32 *freep;
if (!superuser())
@ -495,8 +491,6 @@ hash_bitmap_info(PG_FUNCTION_ARGS)
elog(ERROR, "return type must be a row type");
tupleDesc = BlessTupleDesc(tupleDesc);
MemSet(nulls, 0, sizeof(nulls));
j = 0;
values[j++] = Int64GetDatum((int64) bitmapblkno);
values[j++] = Int32GetDatum(bitmapbit);
@ -526,7 +520,7 @@ hash_metapage_info(PG_FUNCTION_ARGS)
int i,
j;
Datum values[16];
bool nulls[16];
bool nulls[16] = {0};
Datum spares[HASH_MAX_SPLITPOINTS];
Datum mapp[HASH_MAX_BITMAPS];
@ -544,8 +538,6 @@ hash_metapage_info(PG_FUNCTION_ARGS)
metad = HashPageGetMeta(page);
MemSet(nulls, 0, sizeof(nulls));
j = 0;
values[j++] = Int64GetDatum((int64) metad->hashm_magic);
values[j++] = Int64GetDatum((int64) metad->hashm_version);

View File

@ -507,8 +507,8 @@ Datum
heap_tuple_infomask_flags(PG_FUNCTION_ARGS)
{
#define HEAP_TUPLE_INFOMASK_COLS 2
Datum values[HEAP_TUPLE_INFOMASK_COLS];
bool nulls[HEAP_TUPLE_INFOMASK_COLS];
Datum values[HEAP_TUPLE_INFOMASK_COLS] = {0};
bool nulls[HEAP_TUPLE_INFOMASK_COLS] = {0};
uint16 t_infomask = PG_GETARG_INT16(0);
uint16 t_infomask2 = PG_GETARG_INT16(1);
int cnt = 0;
@ -530,10 +530,6 @@ heap_tuple_infomask_flags(PG_FUNCTION_ARGS)
bitcnt = pg_popcount((const char *) &t_infomask, sizeof(uint16)) +
pg_popcount((const char *) &t_infomask2, sizeof(uint16));
/* Initialize values and NULL flags arrays */
MemSet(values, 0, sizeof(values));
MemSet(nulls, 0, sizeof(nulls));
/* If no flags, return a set of empty arrays */
if (bitcnt <= 0)
{

View File

@ -814,12 +814,11 @@ apw_detach_shmem(int code, Datum arg)
static void
apw_start_leader_worker(void)
{
BackgroundWorker worker;
BackgroundWorker worker = {0};
BackgroundWorkerHandle *handle;
BgwHandleStatus status;
pid_t pid;
MemSet(&worker, 0, sizeof(BackgroundWorker));
worker.bgw_flags = BGWORKER_SHMEM_ACCESS;
worker.bgw_start_time = BgWorkerStart_ConsistentState;
strcpy(worker.bgw_library_name, "pg_prewarm");
@ -856,10 +855,9 @@ apw_start_leader_worker(void)
static void
apw_start_database_worker(void)
{
BackgroundWorker worker;
BackgroundWorker worker = {0};
BackgroundWorkerHandle *handle;
MemSet(&worker, 0, sizeof(BackgroundWorker));
worker.bgw_flags =
BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
worker.bgw_start_time = BgWorkerStart_ConsistentState;

View File

@ -1854,8 +1854,8 @@ pg_stat_statements_info(PG_FUNCTION_ARGS)
{
pgssGlobalStats stats;
TupleDesc tupdesc;
Datum values[PG_STAT_STATEMENTS_INFO_COLS];
bool nulls[PG_STAT_STATEMENTS_INFO_COLS];
Datum values[PG_STAT_STATEMENTS_INFO_COLS] = {0};
bool nulls[PG_STAT_STATEMENTS_INFO_COLS] = {0};
if (!pgss || !pgss_hash)
ereport(ERROR,
@ -1866,9 +1866,6 @@ pg_stat_statements_info(PG_FUNCTION_ARGS)
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
MemSet(values, 0, sizeof(values));
MemSet(nulls, 0, sizeof(nulls));
/* Read global statistics for pg_stat_statements */
{
volatile pgssSharedState *s = (volatile pgssSharedState *) pgss;

View File

@ -75,7 +75,7 @@ pg_visibility_map(PG_FUNCTION_ARGS)
Buffer vmbuffer = InvalidBuffer;
TupleDesc tupdesc;
Datum values[2];
bool nulls[2];
bool nulls[2] = {0};
rel = relation_open(relid, AccessShareLock);
@ -88,7 +88,6 @@ pg_visibility_map(PG_FUNCTION_ARGS)
errmsg("invalid block number")));
tupdesc = pg_visibility_tupdesc(false, false);
MemSet(nulls, 0, sizeof(nulls));
mapbits = (int32) visibilitymap_get_status(rel, blkno, &vmbuffer);
if (vmbuffer != InvalidBuffer)
@ -117,7 +116,7 @@ pg_visibility(PG_FUNCTION_ARGS)
Page page;
TupleDesc tupdesc;
Datum values[3];
bool nulls[3];
bool nulls[3] = {0};
rel = relation_open(relid, AccessShareLock);
@ -130,7 +129,6 @@ pg_visibility(PG_FUNCTION_ARGS)
errmsg("invalid block number")));
tupdesc = pg_visibility_tupdesc(false, true);
MemSet(nulls, 0, sizeof(nulls));
mapbits = (int32) visibilitymap_get_status(rel, blkno, &vmbuffer);
if (vmbuffer != InvalidBuffer)
@ -188,10 +186,9 @@ pg_visibility_map_rel(PG_FUNCTION_ARGS)
if (info->next < info->count)
{
Datum values[3];
bool nulls[3];
bool nulls[3] = {0};
HeapTuple tuple;
MemSet(nulls, 0, sizeof(nulls));
values[0] = Int64GetDatum(info->next);
values[1] = BoolGetDatum((info->bits[info->next] & (1 << 0)) != 0);
values[2] = BoolGetDatum((info->bits[info->next] & (1 << 1)) != 0);
@ -233,10 +230,9 @@ pg_visibility_rel(PG_FUNCTION_ARGS)
if (info->next < info->count)
{
Datum values[4];
bool nulls[4];
bool nulls[4] = {0};
HeapTuple tuple;
MemSet(nulls, 0, sizeof(nulls));
values[0] = Int64GetDatum(info->next);
values[1] = BoolGetDatum((info->bits[info->next] & (1 << 0)) != 0);
values[2] = BoolGetDatum((info->bits[info->next] & (1 << 1)) != 0);
@ -266,7 +262,7 @@ pg_visibility_map_summary(PG_FUNCTION_ARGS)
int64 all_frozen = 0;
TupleDesc tupdesc;
Datum values[2];
bool nulls[2];
bool nulls[2] = {0};
rel = relation_open(relid, AccessShareLock);
@ -300,7 +296,6 @@ pg_visibility_map_summary(PG_FUNCTION_ARGS)
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "all_frozen", INT8OID, -1, 0);
tupdesc = BlessTupleDesc(tupdesc);
MemSet(nulls, 0, sizeof(nulls));
values[0] = Int64GetDatum(all_visible);
values[1] = Int64GetDatum(all_frozen);

View File

@ -229,8 +229,8 @@ pg_get_wal_record_info(PG_FUNCTION_ARGS)
{
#define PG_GET_WAL_RECORD_INFO_COLS 11
Datum result;
Datum values[PG_GET_WAL_RECORD_INFO_COLS];
bool nulls[PG_GET_WAL_RECORD_INFO_COLS];
Datum values[PG_GET_WAL_RECORD_INFO_COLS] = {0};
bool nulls[PG_GET_WAL_RECORD_INFO_COLS] = {0};
XLogRecPtr lsn;
XLogRecPtr curr_lsn;
XLogRecPtr first_record;
@ -266,9 +266,6 @@ pg_get_wal_record_info(PG_FUNCTION_ARGS)
errmsg("could not read WAL at %X/%X",
LSN_FORMAT_ARGS(first_record))));
MemSet(values, 0, sizeof(values));
MemSet(nulls, 0, sizeof(nulls));
GetWALRecordInfo(xlogreader, first_record, values, nulls,
PG_GET_WAL_RECORD_INFO_COLS);
@ -334,8 +331,8 @@ GetWALRecordsInfo(FunctionCallInfo fcinfo, XLogRecPtr start_lsn,
XLogRecPtr first_record;
XLogReaderState *xlogreader;
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
Datum values[PG_GET_WAL_RECORDS_INFO_COLS];
bool nulls[PG_GET_WAL_RECORDS_INFO_COLS];
Datum values[PG_GET_WAL_RECORDS_INFO_COLS] = {0};
bool nulls[PG_GET_WAL_RECORDS_INFO_COLS] = {0};
SetSingleFuncCall(fcinfo, 0);
@ -343,9 +340,6 @@ GetWALRecordsInfo(FunctionCallInfo fcinfo, XLogRecPtr start_lsn,
Assert(xlogreader);
MemSet(values, 0, sizeof(values));
MemSet(nulls, 0, sizeof(nulls));
while (ReadNextXLogRecord(xlogreader, first_record) &&
xlogreader->EndRecPtr <= end_lsn)
{
@ -556,17 +550,15 @@ GetWalStats(FunctionCallInfo fcinfo, XLogRecPtr start_lsn,
#define PG_GET_WAL_STATS_COLS 9
XLogRecPtr first_record;
XLogReaderState *xlogreader;
XLogStats stats;
XLogStats stats = {0};
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
Datum values[PG_GET_WAL_STATS_COLS];
bool nulls[PG_GET_WAL_STATS_COLS];
Datum values[PG_GET_WAL_STATS_COLS] = {0};
bool nulls[PG_GET_WAL_STATS_COLS] = {0};
SetSingleFuncCall(fcinfo, 0);
xlogreader = InitXLogReaderState(start_lsn, &first_record);
MemSet(&stats, 0, sizeof(stats));
while (ReadNextXLogRecord(xlogreader, first_record) &&
xlogreader->EndRecPtr <= end_lsn)
{
@ -578,9 +570,6 @@ GetWalStats(FunctionCallInfo fcinfo, XLogRecPtr start_lsn,
pfree(xlogreader->private_data);
XLogReaderFree(xlogreader);
MemSet(values, 0, sizeof(values));
MemSet(nulls, 0, sizeof(nulls));
GetXLogSummaryStats(&stats, rsinfo, values, nulls,
PG_GET_WAL_STATS_COLS,
stats_per_record);

View File

@ -575,7 +575,7 @@ pgstathashindex(PG_FUNCTION_ARGS)
HeapTuple tuple;
TupleDesc tupleDesc;
Datum values[8];
bool nulls[8];
bool nulls[8] = {0};
Buffer metabuf;
HashMetaPage metap;
float8 free_percent;
@ -697,7 +697,6 @@ pgstathashindex(PG_FUNCTION_ARGS)
/*
* Build and return the tuple
*/
MemSet(nulls, 0, sizeof(nulls));
values[0] = Int32GetDatum(stats.version);
values[1] = Int64GetDatum((int64) stats.bucket_pages);
values[2] = Int64GetDatum((int64) stats.overflow_pages);

View File

@ -1678,8 +1678,8 @@ postgres_fdw_get_connections(PG_FUNCTION_ARGS)
while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
{
ForeignServer *server;
Datum values[POSTGRES_FDW_GET_CONNECTIONS_COLS];
bool nulls[POSTGRES_FDW_GET_CONNECTIONS_COLS];
Datum values[POSTGRES_FDW_GET_CONNECTIONS_COLS] = {0};
bool nulls[POSTGRES_FDW_GET_CONNECTIONS_COLS] = {0};
/* We only look for open remote connections */
if (!entry->conn)
@ -1687,9 +1687,6 @@ postgres_fdw_get_connections(PG_FUNCTION_ARGS)
server = GetForeignServerExtended(entry->serverid, FSV_MISSING_OK);
MemSet(values, 0, sizeof(values));
MemSet(nulls, 0, sizeof(nulls));
/*
* The foreign server may have been dropped in current explicit
* transaction. It is not possible to drop the server from another

View File

@ -3307,7 +3307,7 @@ estimate_path_cost_size(PlannerInfo *root,
{
RelOptInfo *outerrel = fpinfo->outerrel;
PgFdwRelationInfo *ofpinfo;
AggClauseCosts aggcosts;
AggClauseCosts aggcosts = {0};
double input_rows;
int numGroupCols;
double numGroups = 1;
@ -3331,7 +3331,6 @@ estimate_path_cost_size(PlannerInfo *root,
input_rows = ofpinfo->rows;
/* Collect statistics about aggregates for estimating costs. */
MemSet(&aggcosts, 0, sizeof(AggClauseCosts));
if (root->parse->hasAggs)
{
get_agg_clause_costs(root, AGGSPLIT_SIMPLE, &aggcosts);