pgfincore:代码优化以及calloc更换

This commit is contained in:
jkshen
2022-09-06 18:34:32 +08:00
parent 4a35068edb
commit 38193ff66e

View File

@ -139,7 +139,7 @@ static bool pgfadvise_file(char *filename, int advice, pgfadviseStruct *pgfdv);
extern Datum pgfadvise_loader(PG_FUNCTION_ARGS);
static bool pgfadvise_loader_file(char *filename, bool willneed,
bool dontneed, VarBit *databit, pgfloaderStruct *pgfloader);
static char *getRelpath(ListCell *partitionCell, Relation rel, bool isSubPartition, text *forkName);
extern Datum pgfincore(PG_FUNCTION_ARGS);
static bool pgfincore_file(char *filename, pgfincoreStruct *pgfncr);
@ -387,19 +387,13 @@ Datum pgfadvise(PG_FUNCTION_ARGS)
} else if (fctx->isSubPartitionTable) {
fctx->subPartitionIdList = RelationGetSubPartitionList(fctx->rel, AccessShareLock);
fctx->subPartitionCell = list_head(fctx->subPartitionIdList);
Partition subPartition = (Partition)lfirst(fctx->subPartitionCell);
Relation subPartionRel = SubPartitionGetRelation(fctx->rel, subPartition, AccessShareLock);
fctx->relationpath = relpathpg(subPartionRel, forkName);
fctx->relationpath = getRelpath(fctx->subPartitionCell, fctx->rel, true, forkName);
fctx->segcount = 0;
releaseDummyRelation(&subPartionRel);
} else if (fctx->isPartitionTable) {
fctx->partitionIdList = relationGetPartitionList(fctx->rel, AccessShareLock);
fctx->partitionCell = list_head(fctx->partitionIdList);
Partition partition = (Partition)lfirst(fctx->partitionCell);
Relation partitionRel = partitionGetRelation(fctx->rel, partition);
fctx->relationpath = relpathpg(partitionRel, forkName);
fctx->relationpath = getRelpath(fctx->partitionCell, fctx->rel, false, forkName);
fctx->segcount = 0;
releaseDummyRelation(&partitionRel);
}
@ -431,20 +425,14 @@ Datum pgfadvise(PG_FUNCTION_ARGS)
if (fctx->isPartitionTable || fctx->isSubPartitionTable) {
if (fctx->isSubPartitionTable && lnext(fctx->subPartitionCell)) {
fctx->subPartitionCell = lnext(fctx->subPartitionCell);
Partition subPartition = (Partition)lfirst(fctx->subPartitionCell);
Relation subPartionRel = SubPartitionGetRelation(fctx->rel,subPartition,AccessShareLock);
fctx->relationpath = relpathpg(subPartionRel, fctx->forkName);
fctx->relationpath = getRelpath(fctx->subPartitionCell, fctx->rel, true, fctx->forkName);
fctx->segcount = 0;
releaseDummyRelation(&subPartionRel);
errno_t rc = snprintf_s(filename, MAXPGPATH, MAXPGPATH-1, "%s", fctx->relationpath);
securec_check_ss(rc, "\0", "\0");
} else if (fctx->isPartitionTable && lnext(fctx->partitionCell)) {
fctx->partitionCell = lnext(fctx->partitionCell);
Partition partition = (Partition)lfirst(fctx->partitionCell);
Relation partitionRel = partitionGetRelation(fctx->rel,partition);
fctx->relationpath = relpathpg(partitionRel, fctx->forkName);
fctx->relationpath = getRelpath(fctx->partitionCell, fctx->rel, false, fctx->forkName);
fctx->segcount = 0;
releaseDummyRelation(&partitionRel);
errno_t rc = snprintf_s(filename, MAXPGPATH, MAXPGPATH-1, "%s", fctx->relationpath);
securec_check_ss(rc, "\0", "\0");
} else {
@ -916,7 +904,7 @@ static bool pgfincore_file(char *filename, pgfincoreStruct *pgfncr)
}
/* Prepare our vector containing all blocks information */
vec = (unsigned char * ) calloc(1, (st.st_size+pgfncr->pageSize-1)/pgfncr->pageSize);
vec = (unsigned char *) palloc0((st.st_size+pgfncr->pageSize-1)/pgfncr->pageSize);
if ((void *)0 == vec) {
munmap(pa, st.st_size);
FreeFile(fp);
@ -930,7 +918,9 @@ static bool pgfincore_file(char *filename, pgfincoreStruct *pgfncr)
munmap(pa, st.st_size);
ereport(ERROR, (errmsg("mincore(%p, %lld, %p): %s\n",
pa, (long long int)st.st_size, vec, strerror(save_errno))));
free(vec);
if (vec != NULL) {
pfree(vec);
}
FreeFile(fp);
return false;
}
@ -992,7 +982,9 @@ static bool pgfincore_file(char *filename, pgfincoreStruct *pgfncr)
/*
* free and close
*/
free(vec);
if (vec != NULL) {
pfree(vec);
}
munmap(pa, st.st_size);
FreeFile(fp);
@ -1004,6 +996,19 @@ static bool pgfincore_file(char *filename, pgfincoreStruct *pgfncr)
return true;
}
static char *getRelpath(ListCell *partitionCell, Relation rel, bool isSubPartition, text *forkName) {
Partition partition = (Partition)lfirst(partitionCell);
Relation partitionRel = NULL;
if (isSubPartition) {
partitionRel = SubPartitionGetRelation(rel, partition, AccessShareLock);
} else {
partitionRel = partitionGetRelation(rel, partition);
}
char *relationpath = relpathpg(partitionRel,forkName);
releaseDummyRelation(&partitionRel);
return relationpath;
}
/*
* pgfincore is a function that handle the process to have a sharelock
* on the relation and to walk the segments.
@ -1114,19 +1119,13 @@ Datum pgfincore(PG_FUNCTION_ARGS)
} else if (fctx->isSubPartitionTable) {
fctx->subPartitionIdList = RelationGetSubPartitionList(fctx->rel, AccessShareLock);
fctx->subPartitionCell = list_head(fctx->subPartitionIdList);
Partition subPartition = (Partition)lfirst(fctx->subPartitionCell);
Relation subPartionRel = SubPartitionGetRelation(fctx->rel, subPartition, AccessShareLock);
fctx->relationpath = relpathpg(subPartionRel, forkName);
fctx->relationpath = getRelpath(fctx->subPartitionCell, fctx->rel, true, forkName);
fctx->segcount = 0;
releaseDummyRelation(&subPartionRel);
} else if (fctx->isPartitionTable) {
fctx->partitionIdList = relationGetPartitionList(fctx->rel, AccessShareLock);
fctx->partitionCell = list_head(fctx->partitionIdList);
Partition partition = (Partition)lfirst(fctx->partitionCell);
Relation partitionRel = partitionGetRelation(fctx->rel, partition);
fctx->relationpath = relpathpg(partitionRel, forkName);
fctx->relationpath = getRelpath(fctx->partitionCell, fctx->rel, false, forkName);
fctx->segcount = 0;
releaseDummyRelation(&partitionRel);
}
/* And finally we keep track of our initialization */
@ -1157,20 +1156,14 @@ Datum pgfincore(PG_FUNCTION_ARGS)
if (fctx->isPartitionTable || fctx->isSubPartitionTable) {
if (fctx->isSubPartitionTable && lnext(fctx->subPartitionCell)) {
fctx->subPartitionCell = lnext(fctx->subPartitionCell);
Partition subPartition = (Partition)lfirst(fctx->subPartitionCell);
Relation subPartionRel = SubPartitionGetRelation(fctx->rel,subPartition,AccessShareLock);
fctx->relationpath = relpathpg(subPartionRel, fctx->forkName);
fctx->relationpath = getRelpath(fctx->subPartitionCell, fctx->rel, true, fctx->forkName);
fctx->segcount = 0;
releaseDummyRelation(&subPartionRel);
errno_t rc = snprintf_s(filename, MAXPGPATH, MAXPGPATH-1, "%s", fctx->relationpath);
securec_check_ss(rc, "\0", "\0");
} else if (fctx->isPartitionTable && lnext(fctx->partitionCell)) {
fctx->partitionCell = lnext(fctx->partitionCell);
Partition partition = (Partition)lfirst(fctx->partitionCell);
Relation partitionRel = partitionGetRelation(fctx->rel,partition);
fctx->relationpath = relpathpg(partitionRel, fctx->forkName);
fctx->partitionCell = lnext(fctx->partitionCell);
fctx->relationpath = getRelpath(fctx->partitionCell, fctx->rel, false, fctx->forkName);
fctx->segcount = 0;
releaseDummyRelation(&partitionRel);
errno_t rc = snprintf_s(filename, MAXPGPATH, MAXPGPATH-1, "%s", fctx->relationpath);
securec_check_ss(rc, "\0", "\0");
} else {