From 2ec69545207c06012e5ce8b36e37cf5f95b19eea Mon Sep 17 00:00:00 2001 From: Ashwin Agrawal Date: Mon, 25 Sep 2017 22:05:40 -0700 Subject: [PATCH] pgindent appendonly directory. --- src/backend/access/appendonly/aosegfiles.c | 985 +++++++++--------- .../access/appendonly/appendonly_compaction.c | 455 ++++---- .../access/appendonly/appendonly_visimap.c | 411 ++++---- .../appendonly/appendonly_visimap_entry.c | 310 +++--- .../appendonly/appendonly_visimap_store.c | 287 +++-- .../appendonly/appendonly_visimap_udf.c | 180 ++-- src/backend/access/appendonly/appendonlyam.c | 6 +- .../appendonly/appendonlyblockdirectory.c | 717 ++++++------- src/backend/access/appendonly/appendonlytid.c | 49 +- .../access/appendonly/appendonlywriter.c | 887 ++++++++-------- .../access/appendonly/test/aomd_test.c | 60 +- .../appendonly/test/appendonly_visimap_test.c | 15 +- 12 files changed, 2211 insertions(+), 2151 deletions(-) diff --git a/src/backend/access/appendonly/aosegfiles.c b/src/backend/access/appendonly/aosegfiles.c index 38e29c2ab5..55753c81de 100644 --- a/src/backend/access/appendonly/aosegfiles.c +++ b/src/backend/access/appendonly/aosegfiles.c @@ -45,22 +45,22 @@ static Datum ao_compression_ratio_internal(Oid relid); static void UpdateFileSegInfo_internal(Relation parentrel, - int segno, - int64 eof, - int64 eof_uncompressed, - int64 tuples_added, - int64 varblocks_added, - int64 modcount_added, - FileSegInfoState newState); + int segno, + int64 eof, + int64 eof_uncompressed, + int64 tuples_added, + int64 varblocks_added, + int64 modcount_added, + FileSegInfoState newState); static void PrintPgaosegAndGprelationNodeEntries(FileSegInfo **allseginfo, - int totalsegs, - bool *segmentFileNumMap); + int totalsegs, + bool *segmentFileNumMap); static void CheckAOConsistencyWithGpRelationNode(Snapshot snapshot, - Relation rel, - int totalsegs, - FileSegInfo ** allseginfo); + Relation rel, + int totalsegs, + FileSegInfo **allseginfo); /* ------------------------------------------------------------------------ * @@ -107,7 +107,7 @@ InsertInitialSegnoEntry(Relation parentrel, int segno) formatVersion = AORelationVersion_GetLatest(); InsertFastSequenceEntry(parentrel->rd_appendonly->segrelid, - (int64)segno, + (int64) segno, 0); pg_aoseg_rel = heap_open(parentrel->rd_appendonly->segrelid, RowExclusiveLock); @@ -157,18 +157,18 @@ InsertInitialSegnoEntry(Relation parentrel, int segno) FileSegInfo * GetFileSegInfo(Relation parentrel, Snapshot appendOnlyMetaDataSnapshot, int segno) { - Relation pg_aoseg_rel; - TupleDesc pg_aoseg_dsc; - HeapScanDesc aoscan; - HeapTuple tuple = NULL; - HeapTuple fstuple = NULL; - int tuple_segno = InvalidFileSegNumber; - bool isNull; - FileSegInfo *fsinfo; + Relation pg_aoseg_rel; + TupleDesc pg_aoseg_dsc; + HeapScanDesc aoscan; + HeapTuple tuple = NULL; + HeapTuple fstuple = NULL; + int tuple_segno = InvalidFileSegNumber; + bool isNull; + FileSegInfo *fsinfo; /* - * Check the pg_aoseg relation to be certain the ao table segment file - * is there. + * Check the pg_aoseg relation to be certain the ao table segment file is + * there. */ pg_aoseg_rel = heap_open(parentrel->rd_appendonly->segrelid, AccessShareLock); pg_aoseg_dsc = RelationGetDescr(pg_aoseg_rel); @@ -196,10 +196,10 @@ GetFileSegInfo(Relation parentrel, Snapshot appendOnlyMetaDataSnapshot, int segn segno, ItemPointerToString(&fstuple->t_self), DatumGetInt64( - fastgetattr(fstuple, Anum_pg_aoseg_eof, pg_aoseg_dsc, &isNull)), + fastgetattr(fstuple, Anum_pg_aoseg_eof, pg_aoseg_dsc, &isNull)), ItemPointerToString2(&tuple->t_self), DatumGetInt64( - fastgetattr(tuple, Anum_pg_aoseg_eof, pg_aoseg_dsc, &isNull))))); + fastgetattr(tuple, Anum_pg_aoseg_eof, pg_aoseg_dsc, &isNull))))); else fstuple = heap_copytuple(tuple); } @@ -217,72 +217,73 @@ GetFileSegInfo(Relation parentrel, Snapshot appendOnlyMetaDataSnapshot, int segn /* get the eof */ fsinfo->eof = DatumGetInt64( - fastgetattr(fstuple, Anum_pg_aoseg_eof, pg_aoseg_dsc, &isNull)); + fastgetattr(fstuple, Anum_pg_aoseg_eof, pg_aoseg_dsc, &isNull)); - if(isNull) + if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("got invalid eof value: NULL"))); + errmsg("got invalid eof value: NULL"))); if (fsinfo->eof < 0) ereport(ERROR, (errcode(ERRCODE_GP_INTERNAL_ERROR), - errmsg("Invalid eof " INT64_FORMAT " for relation %s", - fsinfo->eof, RelationGetRelationName(parentrel)))); + errmsg("Invalid eof " INT64_FORMAT " for relation %s", + fsinfo->eof, RelationGetRelationName(parentrel)))); /* get the tupcount */ fsinfo->total_tupcount = DatumGetInt64( - fastgetattr(fstuple, Anum_pg_aoseg_tupcount, pg_aoseg_dsc, &isNull)); + fastgetattr(fstuple, Anum_pg_aoseg_tupcount, pg_aoseg_dsc, &isNull)); - if(isNull) + if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("got invalid tupcount value: NULL"))); + errmsg("got invalid tupcount value: NULL"))); /* get the varblock count */ fsinfo->varblockcount = DatumGetInt64( - fastgetattr(fstuple, Anum_pg_aoseg_varblockcount, pg_aoseg_dsc, &isNull)); + fastgetattr(fstuple, Anum_pg_aoseg_varblockcount, pg_aoseg_dsc, &isNull)); - if(isNull) + if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("got invalid varblockcount value: NULL"))); - + errmsg("got invalid varblockcount value: NULL"))); + /* get the modcount */ fsinfo->modcount = DatumGetInt64( - fastgetattr(fstuple, Anum_pg_aoseg_modcount, pg_aoseg_dsc, &isNull)); + fastgetattr(fstuple, Anum_pg_aoseg_modcount, pg_aoseg_dsc, &isNull)); - if(isNull) + if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("got invalid modcount value: NULL"))); + errmsg("got invalid modcount value: NULL"))); /* get the file format version number */ fsinfo->formatversion = DatumGetInt16( - fastgetattr(fstuple, Anum_pg_aoseg_formatversion, pg_aoseg_dsc, &isNull)); + fastgetattr(fstuple, Anum_pg_aoseg_formatversion, pg_aoseg_dsc, &isNull)); - if(isNull) + if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("got invalid formatversion value: NULL"))); + errmsg("got invalid formatversion value: NULL"))); /* get the state */ fsinfo->state = DatumGetInt16( - fastgetattr(fstuple, Anum_pg_aoseg_state, pg_aoseg_dsc, &isNull)); + fastgetattr(fstuple, Anum_pg_aoseg_state, pg_aoseg_dsc, &isNull)); - if(isNull) + if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("got invalid state value: NULL"))); + errmsg("got invalid state value: NULL"))); /* get the uncompressed eof */ fsinfo->eof_uncompressed = DatumGetInt64( - fastgetattr(fstuple, Anum_pg_aoseg_eofuncompressed, pg_aoseg_dsc, &isNull)); - if(isNull) + fastgetattr(fstuple, Anum_pg_aoseg_eofuncompressed, pg_aoseg_dsc, &isNull)); + if (isNull) { /* * NULL is allowed. Tables that were created before the release of the - * eof_uncompressed catalog column will have a NULL instead of a value. + * eof_uncompressed catalog column will have a NULL instead of a + * value. */ fsinfo->eof_uncompressed = InvalidUncompressedEof; } @@ -311,25 +312,25 @@ GetAllFileSegInfo(Relation parentrel, Snapshot appendOnlyMetaDataSnapshot, int *totalsegs) { - Relation pg_aoseg_rel; - FileSegInfo **result; + Relation pg_aoseg_rel; + FileSegInfo **result; Assert(RelationIsAoRows(parentrel)); pg_aoseg_rel = heap_open(parentrel->rd_appendonly->segrelid, AccessShareLock); result = GetAllFileSegInfo_pg_aoseg_rel( - RelationGetRelationName(parentrel), - pg_aoseg_rel, - appendOnlyMetaDataSnapshot, - totalsegs); + RelationGetRelationName(parentrel), + pg_aoseg_rel, + appendOnlyMetaDataSnapshot, + totalsegs); heap_close(pg_aoseg_rel, AccessShareLock); CheckAOConsistencyWithGpRelationNode(appendOnlyMetaDataSnapshot, - parentrel, - *totalsegs, - result); + parentrel, + *totalsegs, + result); return result; } @@ -341,15 +342,15 @@ GetAllFileSegInfo(Relation parentrel, static int aoFileSegInfoCmp(const void *left, const void *right) { - FileSegInfo *leftSegInfo = *((FileSegInfo **)left); - FileSegInfo *rightSegInfo = *((FileSegInfo **)right); - + FileSegInfo *leftSegInfo = *((FileSegInfo **) left); + FileSegInfo *rightSegInfo = *((FileSegInfo **) right); + if (leftSegInfo->segno < rightSegInfo->segno) return -1; - + if (leftSegInfo->segno > rightSegInfo->segno) return 1; - + return 0; } @@ -359,28 +360,30 @@ GetAllFileSegInfo_pg_aoseg_rel(char *relationName, Snapshot appendOnlyMetaDataSnapshot, int *totalsegs) { - TupleDesc pg_aoseg_dsc; - HeapTuple tuple; - HeapScanDesc aoscan; - FileSegInfo **allseginfo; - int seginfo_no; - int seginfo_slot_no = AO_FILESEGINFO_ARRAY_SIZE; - Datum segno, - eof, - eof_uncompressed, - tupcount, - varblockcount, - modcount, - formatversion, - state; - bool isNull; + TupleDesc pg_aoseg_dsc; + HeapTuple tuple; + HeapScanDesc aoscan; + FileSegInfo **allseginfo; + int seginfo_no; + int seginfo_slot_no = AO_FILESEGINFO_ARRAY_SIZE; + Datum segno, + eof, + eof_uncompressed, + tupcount, + varblockcount, + modcount, + formatversion, + state; + bool isNull; + pg_aoseg_dsc = RelationGetDescr(pg_aoseg_rel); - /* MPP-16407: - * Initialize the segment file information array, we first allocate 8 slot for the array, - * then array will be dynamically expanded later if necessary. + /* + * MPP-16407: Initialize the segment file information array, we first + * allocate 8 slot for the array, then array will be dynamically expanded + * later if necessary. */ - allseginfo = (FileSegInfo **) palloc0(sizeof(FileSegInfo*) * seginfo_slot_no); + allseginfo = (FileSegInfo **) palloc0(sizeof(FileSegInfo *) * seginfo_slot_no); seginfo_no = 0; /* @@ -393,16 +396,17 @@ GetAllFileSegInfo_pg_aoseg_rel(char *relationName, if (seginfo_no >= seginfo_slot_no) { seginfo_slot_no *= 2; - allseginfo = (FileSegInfo **) repalloc(allseginfo, sizeof(FileSegInfo*) * seginfo_slot_no); + allseginfo = (FileSegInfo **) repalloc(allseginfo, sizeof(FileSegInfo *) * seginfo_slot_no); } FileSegInfo *oneseginfo; - allseginfo[seginfo_no] = (FileSegInfo *)palloc0(sizeof(FileSegInfo)); + + allseginfo[seginfo_no] = (FileSegInfo *) palloc0(sizeof(FileSegInfo)); oneseginfo = allseginfo[seginfo_no]; GetTupleVisibilitySummary( - tuple, - &oneseginfo->tupleVisibilitySummary); + tuple, + &oneseginfo->tupleVisibilitySummary); segno = fastgetattr(tuple, Anum_pg_aoseg_segno, pg_aoseg_dsc, &isNull); if (isNull) @@ -413,7 +417,7 @@ GetAllFileSegInfo_pg_aoseg_rel(char *relationName, /* get the eof */ eof = fastgetattr(tuple, Anum_pg_aoseg_eof, pg_aoseg_dsc, &isNull); - if(isNull) + if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("got invalid eof value: NULL"))); @@ -421,7 +425,7 @@ GetAllFileSegInfo_pg_aoseg_rel(char *relationName, /* get the tupcount */ tupcount = fastgetattr(tuple, Anum_pg_aoseg_tupcount, pg_aoseg_dsc, &isNull); - if(isNull) + if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("got invalid tupcount value: NULL"))); @@ -429,7 +433,7 @@ GetAllFileSegInfo_pg_aoseg_rel(char *relationName, /* get the varblock count */ varblockcount = fastgetattr(tuple, Anum_pg_aoseg_varblockcount, pg_aoseg_dsc, &isNull); - if(isNull) + if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("got invalid varblockcount value: NULL"))); @@ -437,7 +441,7 @@ GetAllFileSegInfo_pg_aoseg_rel(char *relationName, /* get the modcount */ modcount = fastgetattr(tuple, Anum_pg_aoseg_modcount, pg_aoseg_dsc, &isNull); - if(isNull) + if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("got invalid modcount value: NULL"))); @@ -447,25 +451,26 @@ GetAllFileSegInfo_pg_aoseg_rel(char *relationName, formatversion = fastgetattr(tuple, Anum_pg_aoseg_formatversion, pg_aoseg_dsc, &isNull); Assert(!isNull || appendOnlyMetaDataSnapshot == SnapshotAny); if (!isNull) - oneseginfo->formatversion = (int64)DatumGetInt16(formatversion); + oneseginfo->formatversion = (int64) DatumGetInt16(formatversion); /* get the state */ state = fastgetattr(tuple, Anum_pg_aoseg_state, pg_aoseg_dsc, &isNull); Assert(!isNull || appendOnlyMetaDataSnapshot == SnapshotAny); - if(isNull) + if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("got invalid state value: NULL"))); else - oneseginfo->state = (int64)DatumGetInt16(state); + oneseginfo->state = (int64) DatumGetInt16(state); /* get the uncompressed eof */ eof_uncompressed = fastgetattr(tuple, Anum_pg_aoseg_eofuncompressed, pg_aoseg_dsc, &isNull); - if(isNull) + if (isNull) { /* - * NULL is allowed. Tables that were created before the release of the - * eof_uncompressed catalog column will have a NULL instead of a value. + * NULL is allowed. Tables that were created before the release of + * the eof_uncompressed catalog column will have a NULL instead of + * a value. */ oneseginfo->eof_uncompressed = InvalidUncompressedEof; } @@ -473,10 +478,10 @@ GetAllFileSegInfo_pg_aoseg_rel(char *relationName, oneseginfo->eof_uncompressed += DatumGetInt64(eof_uncompressed); elogif(Debug_appendonly_print_scan, LOG, - "Append-only found existing segno %d with eof " INT64_FORMAT " for table '%s'", - oneseginfo->segno, - oneseginfo->eof, - relationName); + "Append-only found existing segno %d with eof " INT64_FORMAT " for table '%s'", + oneseginfo->segno, + oneseginfo->eof, + relationName); seginfo_no++; CHECK_FOR_INTERRUPTS(); @@ -494,74 +499,74 @@ GetAllFileSegInfo_pg_aoseg_rel(char *relationName, /* * Sort allseginfo by the order of segment file number. * - * Currently this is only needed when building a bitmap index to guarantee the tids - * are in the ascending order. But since this array is pretty small, we just sort - * the array for all cases. + * Currently this is only needed when building a bitmap index to guarantee + * the tids are in the ascending order. But since this array is pretty + * small, we just sort the array for all cases. */ - qsort((char *)allseginfo, *totalsegs, sizeof(FileSegInfo *), aoFileSegInfoCmp); + qsort((char *) allseginfo, *totalsegs, sizeof(FileSegInfo *), aoFileSegInfoCmp); return allseginfo; } void SetFileSegInfoState(Relation parentrel, - int segno, - FileSegInfoState newState) + int segno, + FileSegInfoState newState) { elogif(Debug_appendonly_print_compaction, LOG, - "Set segfile info state: segno %d, table '%s', new state %d", - segno, - RelationGetRelationName(parentrel), - newState); + "Set segfile info state: segno %d, table '%s', new state %d", + segno, + RelationGetRelationName(parentrel), + newState); UpdateFileSegInfo_internal(parentrel, - segno, - -1, - -1, - 0, - 0, - 0, - newState); + segno, + -1, + -1, + 0, + 0, + 0, + newState); } void ClearFileSegInfo(Relation parentrel, - int segno, - FileSegInfoState newState) + int segno, + FileSegInfoState newState) { LockAcquireResult acquireResult; - - Relation pg_aoseg_rel; - TupleDesc pg_aoseg_dsc; - HeapScanDesc aoscan; - HeapTuple tuple = NULL; - HeapTuple new_tuple; - int tuple_segno = InvalidFileSegNumber; - Datum *new_record; - bool *new_record_nulls; - bool *new_record_repl; - bool isNull; - - Assert(RelationIsAoRows(parentrel)); + + Relation pg_aoseg_rel; + TupleDesc pg_aoseg_dsc; + HeapScanDesc aoscan; + HeapTuple tuple = NULL; + HeapTuple new_tuple; + int tuple_segno = InvalidFileSegNumber; + Datum *new_record; + bool *new_record_nulls; + bool *new_record_repl; + bool isNull; + + Assert(RelationIsAoRows(parentrel)); Assert(newState >= AOSEG_STATE_USECURRENT && newState <= AOSEG_STATE_AWAITING_DROP); elogif(Debug_appendonly_print_compaction, LOG, - "Clear seg file info: segno %d table '%s'", - segno, - RelationGetRelationName(parentrel)); + "Clear seg file info: segno %d table '%s'", + segno, + RelationGetRelationName(parentrel)); /* * Verify we already have the write-lock! */ acquireResult = LockRelationAppendOnlySegmentFile( - &parentrel->rd_node, - segno, - AccessExclusiveLock, - /* dontWait */ false); + &parentrel->rd_node, + segno, + AccessExclusiveLock, + /* dontWait */ false); if (acquireResult != LOCKACQUIRE_ALREADY_HELD) { elog(ERROR, "Should already have the (transaction-scope) write-lock on Append-Only segment file #%d, " - "relation %s", segno, RelationGetRelationName(parentrel)); + "relation %s", segno, RelationGetRelationName(parentrel)); } /* @@ -584,7 +589,7 @@ ClearFileSegInfo(Relation parentrel, if (!HeapTupleIsValid(tuple)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("append-only table \"%s\" file segment \"%d\" entry " + errmsg("append-only table \"%s\" file segment \"%d\" entry " "does not exist", RelationGetRelationName(parentrel), segno))); @@ -613,7 +618,7 @@ ClearFileSegInfo(Relation parentrel, } new_tuple = heap_modify_tuple(tuple, pg_aoseg_dsc, new_record, - new_record_nulls, new_record_repl); + new_record_nulls, new_record_repl); simple_heap_update(pg_aoseg_rel, &tuple->t_self, new_tuple); heap_freetuple(new_tuple); @@ -648,70 +653,70 @@ UpdateFileSegInfo(Relation parentrel, elog(DEBUG3, "UpdateFileSegInfo called. segno = %d", segno); UpdateFileSegInfo_internal(parentrel, - segno, - eof, - eof_uncompressed, - tuples_added, - varblocks_added, - modcount_added, - newState); + segno, + eof, + eof_uncompressed, + tuples_added, + varblocks_added, + modcount_added, + newState); } /* * Update the eof and filetupcount of an append only table. * * The parameters eof and eof_uncompressed can be negative. - * In this case, the columns are not updated. + * In this case, the columns are not updated. * * The parameters tuples_added and varblocks_added can be negative, e.g. after * a AO compaction. */ static void UpdateFileSegInfo_internal(Relation parentrel, - int segno, - int64 eof, - int64 eof_uncompressed, - int64 tuples_added, - int64 varblocks_added, - int64 modcount_added, - FileSegInfoState newState) + int segno, + int64 eof, + int64 eof_uncompressed, + int64 tuples_added, + int64 varblocks_added, + int64 modcount_added, + FileSegInfoState newState) { LockAcquireResult acquireResult; - - Relation pg_aoseg_rel; - TupleDesc pg_aoseg_dsc; - HeapScanDesc aoscan; - HeapTuple tuple = NULL; - HeapTuple new_tuple; - int tuple_segno = InvalidFileSegNumber; - int64 filetupcount; - int64 filevarblockcount; - int64 new_tuple_count; - int64 new_varblock_count; - int64 new_modcount; - int64 old_eof; - int64 old_eof_uncompressed; - int64 old_modcount; - Datum *new_record; - bool *new_record_nulls; - bool *new_record_repl; - bool isNull; - - Assert(RelationIsAoRows(parentrel)); + + Relation pg_aoseg_rel; + TupleDesc pg_aoseg_dsc; + HeapScanDesc aoscan; + HeapTuple tuple = NULL; + HeapTuple new_tuple; + int tuple_segno = InvalidFileSegNumber; + int64 filetupcount; + int64 filevarblockcount; + int64 new_tuple_count; + int64 new_varblock_count; + int64 new_modcount; + int64 old_eof; + int64 old_eof_uncompressed; + int64 old_modcount; + Datum *new_record; + bool *new_record_nulls; + bool *new_record_repl; + bool isNull; + + Assert(RelationIsAoRows(parentrel)); Assert(newState >= AOSEG_STATE_USECURRENT && newState <= AOSEG_STATE_AWAITING_DROP); /* * Verify we already have the write-lock! */ acquireResult = LockRelationAppendOnlySegmentFile( - &parentrel->rd_node, - segno, - AccessExclusiveLock, - /* dontWait */ false); + &parentrel->rd_node, + segno, + AccessExclusiveLock, + /* dontWait */ false); if (acquireResult != LOCKACQUIRE_ALREADY_HELD) { elog(ERROR, "Should already have the (transaction-scope) write-lock on Append-Only segment file #%d, " - "relation %s", segno, RelationGetRelationName(parentrel)); + "relation %s", segno, RelationGetRelationName(parentrel)); } /* @@ -734,7 +739,7 @@ UpdateFileSegInfo_internal(Relation parentrel, if (!HeapTupleIsValid(tuple)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("append-only table \"%s\" file segment \"%d\" entry " + errmsg("append-only table \"%s\" file segment \"%d\" entry " "does not exist", RelationGetRelationName(parentrel), segno))); @@ -752,13 +757,12 @@ UpdateFileSegInfo_internal(Relation parentrel, errmsg("got invalid pg_aoseg eof value: NULL"))); /* - * For AO by design due to append-only nature, - * new end-of-file (EOF) to be recorded in aoseg table has to be greater - * than currently stored EOF value, as new writes must move it forward only. - * If new end-of-file value is less than currently stored end-of-file - * something is incorrect and updating the same will yeild incorrect result - * during reads. Hence abort the write transaction trying to update the - * incorrect EOF value. + * For AO by design due to append-only nature, new end-of-file (EOF) to be + * recorded in aoseg table has to be greater than currently stored EOF + * value, as new writes must move it forward only. If new end-of-file + * value is less than currently stored end-of-file something is incorrect + * and updating the same will yeild incorrect result during reads. Hence + * abort the write transaction trying to update the incorrect EOF value. */ if (eof < 0) { @@ -767,9 +771,9 @@ UpdateFileSegInfo_internal(Relation parentrel, else if (eof < old_eof) { elog(ERROR, "Unexpected compressed EOF for relation %s, relfilenode %u, segment file %d. " - "EOF " INT64_FORMAT " to be updated cannot be smaller than current EOF " INT64_FORMAT " in pg_aoseg", - RelationGetRelationName(parentrel), parentrel->rd_node.relNode, - segno, eof, old_eof); + "EOF " INT64_FORMAT " to be updated cannot be smaller than current EOF " INT64_FORMAT " in pg_aoseg", + RelationGetRelationName(parentrel), parentrel->rd_node.relNode, + segno, eof, old_eof); } old_eof_uncompressed = DatumGetInt64(fastgetattr(tuple, @@ -788,9 +792,9 @@ UpdateFileSegInfo_internal(Relation parentrel, else if (eof_uncompressed < old_eof_uncompressed) { elog(ERROR, "Unexpected EOF for relation %s, relfilenode %u, segment file %d." - "EOF " INT64_FORMAT " to be updated cannot be smaller than current EOF " INT64_FORMAT " in pg_aoseg", - RelationGetRelationName(parentrel), parentrel->rd_node.relNode, - segno, eof_uncompressed, old_eof_uncompressed); + "EOF " INT64_FORMAT " to be updated cannot be smaller than current EOF " INT64_FORMAT " in pg_aoseg", + RelationGetRelationName(parentrel), parentrel->rd_node.relNode, + segno, eof_uncompressed, old_eof_uncompressed); } /* get the current tuple count so we can add to it */ @@ -801,7 +805,7 @@ UpdateFileSegInfo_internal(Relation parentrel, if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("got invalid pg_aoseg filetupcount value: NULL"))); + errmsg("got invalid pg_aoseg filetupcount value: NULL"))); /* calculate the new tuple count */ new_tuple_count = filetupcount + tuples_added; @@ -815,7 +819,7 @@ UpdateFileSegInfo_internal(Relation parentrel, if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("got invalid pg_aoseg varblockcount value: NULL"))); + errmsg("got invalid pg_aoseg varblockcount value: NULL"))); /* calculate the new tuple count */ new_varblock_count = filevarblockcount + varblocks_added; @@ -829,7 +833,7 @@ UpdateFileSegInfo_internal(Relation parentrel, if (isNull) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("got invalid pg_aoseg modcount value: NULL"))); + errmsg("got invalid pg_aoseg modcount value: NULL"))); /* calculate the new mod count */ new_modcount = old_modcount + modcount_added; @@ -863,7 +867,7 @@ UpdateFileSegInfo_internal(Relation parentrel, * update the tuple in the pg_aoseg table */ new_tuple = heap_modify_tuple(tuple, pg_aoseg_dsc, new_record, - new_record_nulls, new_record_repl); + new_record_nulls, new_record_repl); simple_heap_update(pg_aoseg_rel, &tuple->t_self, new_tuple); @@ -888,20 +892,22 @@ FileSegTotals * GetSegFilesTotals(Relation parentrel, Snapshot appendOnlyMetaDataSnapshot) { - Relation pg_aoseg_rel; - TupleDesc pg_aoseg_dsc; - HeapTuple tuple; - HeapScanDesc aoscan; - FileSegTotals *result; - Datum eof, - eof_uncompressed, - tupcount, - varblockcount, - state; - bool isNull; - - Assert(RelationIsAoRows(parentrel)); /* doesn't fit for AO column store. should implement same for CO */ - + Relation pg_aoseg_rel; + TupleDesc pg_aoseg_dsc; + HeapTuple tuple; + HeapScanDesc aoscan; + FileSegTotals *result; + Datum eof, + eof_uncompressed, + tupcount, + varblockcount, + state; + bool isNull; + + Assert(RelationIsAoRows(parentrel)); /* doesn't fit for AO column + * store. should implement same + * for CO */ + result = (FileSegTotals *) palloc0(sizeof(FileSegTotals)); pg_aoseg_rel = heap_open(parentrel->rd_appendonly->segrelid, AccessShareLock); @@ -917,7 +923,7 @@ GetSegFilesTotals(Relation parentrel, Snapshot appendOnlyMetaDataSnapshot) eof_uncompressed = fastgetattr(tuple, Anum_pg_aoseg_eofuncompressed, pg_aoseg_dsc, &isNull); state = fastgetattr(tuple, Anum_pg_aoseg_state, pg_aoseg_dsc, &isNull); - if(isNull) + if (isNull) result->totalbytesuncompressed = InvalidUncompressedEof; else result->totalbytesuncompressed += DatumGetInt64(eof_uncompressed); @@ -946,18 +952,19 @@ GetSegFilesTotals(Relation parentrel, Snapshot appendOnlyMetaDataSnapshot) * Get the total bytes for a specific AO table from the pg_aoseg table on this local segdb. * (A version of GetSegFilesTotals that just gets the total bytes.) */ -int64 GetAOTotalBytes(Relation parentrel, Snapshot appendOnlyMetaDataSnapshot) +int64 +GetAOTotalBytes(Relation parentrel, Snapshot appendOnlyMetaDataSnapshot) { - Relation pg_aoseg_rel; - TupleDesc pg_aoseg_dsc; - HeapTuple tuple; - HeapScanDesc aoscan; - int64 result; - Datum eof; - bool isNull; + Relation pg_aoseg_rel; + TupleDesc pg_aoseg_dsc; + HeapTuple tuple; + HeapScanDesc aoscan; + int64 result; + Datum eof; + bool isNull; - Assert(RelationIsAoRows(parentrel)); + Assert(RelationIsAoRows(parentrel)); result = 0; @@ -984,41 +991,39 @@ int64 GetAOTotalBytes(Relation parentrel, Snapshot appendOnlyMetaDataSnapshot) PG_FUNCTION_INFO_V1(gp_aoseg_history); -extern Datum -gp_aoseg_history(PG_FUNCTION_ARGS); +extern Datum gp_aoseg_history(PG_FUNCTION_ARGS); Datum gp_aoseg_history(PG_FUNCTION_ARGS) { - int aoRelOid = PG_GETARG_OID(0); + int aoRelOid = PG_GETARG_OID(0); typedef struct Context { - Oid aoRelOid; + Oid aoRelOid; FileSegInfo **aoSegfileArray; - int totalAoSegFiles; + int totalAoSegFiles; - int segfileArrayIndex; + int segfileArrayIndex; } Context; - + FuncCallContext *funcctx; - Context *context; + Context *context; if (SRF_IS_FIRSTCALL()) { TupleDesc tupdesc; MemoryContext oldcontext; - Relation aocsRel; - Relation pg_aoseg_rel; + Relation aocsRel; + Relation pg_aoseg_rel; /* create a function context for cross-call persistence */ funcctx = SRF_FIRSTCALL_INIT(); /* - * switch to memory context appropriate for multiple function - * calls + * switch to memory context appropriate for multiple function calls */ oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); @@ -1073,25 +1078,25 @@ gp_aoseg_history(PG_FUNCTION_ARGS) context->aoRelOid = aoRelOid; aocsRel = heap_open(aoRelOid, NoLock); - if(!RelationIsAoRows(aocsRel)) + if (!RelationIsAoRows(aocsRel)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("'%s' is not an append-only row relation", + errmsg("'%s' is not an append-only row relation", RelationGetRelationName(aocsRel)))); pg_aoseg_rel = heap_open(aocsRel->rd_appendonly->segrelid, NoLock); - - context->aoSegfileArray = - GetAllFileSegInfo_pg_aoseg_rel( - RelationGetRelationName(aocsRel), - pg_aoseg_rel, - SnapshotAny, // Get ALL tuples from pg_aoseg_% including aborted and in-progress ones. - &context->totalAoSegFiles); + + context->aoSegfileArray = + GetAllFileSegInfo_pg_aoseg_rel( + RelationGetRelationName(aocsRel), + pg_aoseg_rel, + SnapshotAny, //Get ALL tuples from pg_aoseg_ % including aborted and in - progress ones. + & context->totalAoSegFiles); heap_close(pg_aoseg_rel, NoLock); heap_close(aocsRel, NoLock); - // Iteration position. + /* Iteration position. */ context->segfileArrayIndex = 0; funcctx->user_fctx = (void *) context; @@ -1118,7 +1123,7 @@ gp_aoseg_history(PG_FUNCTION_ARGS) { break; } - + /* * Form tuple with appropriate data. */ @@ -1128,9 +1133,9 @@ gp_aoseg_history(PG_FUNCTION_ARGS) aoSegfile = context->aoSegfileArray[context->segfileArrayIndex]; GetTupleVisibilitySummaryDatums( - &values[0], - &nulls[0], - &aoSegfile->tupleVisibilitySummary); + &values[0], + &nulls[0], + &aoSegfile->tupleVisibilitySummary); values[11] = Int32GetDatum(aoSegfile->segno); values[12] = Int64GetDatum(aoSegfile->total_tupcount); @@ -1143,7 +1148,7 @@ gp_aoseg_history(PG_FUNCTION_ARGS) tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); - // Indicate we emitted one segment file. + /* Indicate we emitted one segment file. */ context->segfileArrayIndex++; SRF_RETURN_NEXT(funcctx, result); @@ -1157,16 +1162,16 @@ gp_aoseg_history(PG_FUNCTION_ARGS) static Datum gp_update_aorow_master_stats_internal(Relation parentrel, Snapshot appendOnlyMetaDataSnapshot) { - StringInfoData sqlstmt; - Relation aosegrel; - bool connected = false; - char aoseg_relname[NAMEDATALEN]; - int proc; - int ret; - int64 total_count = 0; - MemoryContext oldcontext = CurrentMemoryContext; - - Assert(RelationIsAoRows(parentrel)); + StringInfoData sqlstmt; + Relation aosegrel; + bool connected = false; + char aoseg_relname[NAMEDATALEN]; + int proc; + int ret; + int64 total_count = 0; + MemoryContext oldcontext = CurrentMemoryContext; + + Assert(RelationIsAoRows(parentrel)); /* * get the name of the aoseg relation @@ -1180,8 +1185,8 @@ gp_update_aorow_master_stats_internal(Relation parentrel, Snapshot appendOnlyMet */ initStringInfo(&sqlstmt); appendStringInfo(&sqlstmt, "select segno,sum(tupcount) " - "from gp_dist_random('pg_aoseg.%s') " - "group by (segno)", aoseg_relname); + "from gp_dist_random('pg_aoseg.%s') " + "group by (segno)", aoseg_relname); PG_TRY(); { @@ -1201,22 +1206,22 @@ gp_update_aorow_master_stats_internal(Relation parentrel, Snapshot appendOnlyMet if (ret > 0 && SPI_tuptable != NULL) { - TupleDesc tupdesc = SPI_tuptable->tupdesc; + TupleDesc tupdesc = SPI_tuptable->tupdesc; SPITupleTable *tuptable = SPI_tuptable; - int i; + int i; /* * Iterate through each result tuple */ for (i = 0; i < proc; i++) { - HeapTuple tuple = tuptable->vals[i]; + HeapTuple tuple = tuptable->vals[i]; FileSegInfo *fsinfo = NULL; int qe_segno; - int64 qe_tupcount; - char *val_segno; - char *val_tupcount; - MemoryContext cxt_save; + int64 qe_tupcount; + char *val_segno; + char *val_tupcount; + MemoryContext cxt_save; /* * Get totals from QE's for a specific segment @@ -1236,24 +1241,20 @@ gp_update_aorow_master_stats_internal(Relation parentrel, Snapshot appendOnlyMet total_count += qe_tupcount; /* - * Get the numbers on the QD for this segment + * Get the numbers on the QD for this segment. CONSIDER: For + * integrity, we should lock ALL segment files first before + * executing the query. And, the query of the segments (the + * SPI_execute) and the update (UpdateFileSegInfo) should be + * in the same transaction. If there are concurrent + * Append-Only inserts, we can end up with the wrong + * answer. NOTE: This is a transaction scope lock that must be + * held until commit / abort. */ - - - // CONSIDER: For integrity, we should lock ALL segment files first before - // executing the query. And, the query of the segments (the SPI_execute) - // and the update (UpdateFileSegInfo) should be in the same transaction. - // - // If there are concurrent Append-Only inserts, we can end up with - // the wrong answer... - // - // NOTE: This is a transaction scope lock that must be held until commit / abort. - // LockRelationAppendOnlySegmentFile( - &parentrel->rd_node, - qe_segno, - AccessExclusiveLock, - /* dontWait */ false); + &parentrel->rd_node, + qe_segno, + AccessExclusiveLock, + /* dontWait */ false); fsinfo = GetFileSegInfo(parentrel, appendOnlyMetaDataSnapshot, qe_segno); if (fsinfo == NULL) @@ -1266,24 +1267,24 @@ gp_update_aorow_master_stats_internal(Relation parentrel, Snapshot appendOnlyMet /* * check if numbers match. */ - if(fsinfo->total_tupcount != qe_tupcount) + if (fsinfo->total_tupcount != qe_tupcount) { - int64 tupcount_diff = qe_tupcount - fsinfo->total_tupcount; + int64 tupcount_diff = qe_tupcount - fsinfo->total_tupcount; elog(DEBUG3, "gp_update_ao_master_stats: updating " - "segno %d with tupcount %d", qe_segno, - (int)qe_tupcount); + "segno %d with tupcount %d", qe_segno, + (int) qe_tupcount); /* * QD tup count != QE tup count. update QD count by * passing in the diff (may be negative sometimes). */ - UpdateFileSegInfo_internal(parentrel, qe_segno, -1, -1, - tupcount_diff, 0, 1, AOSEG_STATE_USECURRENT); + UpdateFileSegInfo_internal(parentrel, qe_segno, -1, -1, + tupcount_diff, 0, 1, AOSEG_STATE_USECURRENT); } else elog(DEBUG3, "gp_update_ao_master_stats: no need to " - "update segno %d. it is synced", qe_segno); + "update segno %d. it is synced", qe_segno); pfree(fsinfo); @@ -1324,8 +1325,7 @@ gp_update_aorow_master_stats_internal(Relation parentrel, Snapshot appendOnlyMet PG_FUNCTION_INFO_V1(gp_aoseg_name); -extern Datum -gp_aoseg_name(PG_FUNCTION_ARGS); +extern Datum gp_aoseg_name(PG_FUNCTION_ARGS); /* * UDF to get aoseg information by relation name @@ -1333,40 +1333,39 @@ gp_aoseg_name(PG_FUNCTION_ARGS); Datum gp_aoseg_name(PG_FUNCTION_ARGS) { - int aoRelOid; - RangeVar *parentrv; - text *relname = PG_GETARG_TEXT_P(0); + int aoRelOid; + RangeVar *parentrv; + text *relname = PG_GETARG_TEXT_P(0); parentrv = makeRangeVarFromNameList(textToQualifiedNameList(relname)); aoRelOid = RangeVarGetRelid(parentrv, false); typedef struct Context { - Oid aoRelOid; + Oid aoRelOid; FileSegInfo **aoSegfileArray; - int totalAoSegFiles; + int totalAoSegFiles; - int segfileArrayIndex; + int segfileArrayIndex; } Context; - + FuncCallContext *funcctx; - Context *context; + Context *context; if (SRF_IS_FIRSTCALL()) { TupleDesc tupdesc; MemoryContext oldcontext; - Relation aocsRel; - Relation pg_aoseg_rel; + Relation aocsRel; + Relation pg_aoseg_rel; /* create a function context for cross-call persistence */ funcctx = SRF_FIRSTCALL_INIT(); /* - * switch to memory context appropriate for multiple function - * calls + * switch to memory context appropriate for multiple function calls */ oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); @@ -1401,16 +1400,16 @@ gp_aoseg_name(PG_FUNCTION_ARGS) context->aoRelOid = aoRelOid; aocsRel = heap_open(aoRelOid, NoLock); - if(!RelationIsAoRows(aocsRel)) + if (!RelationIsAoRows(aocsRel)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("'%s' is not an append-only row relation", + errmsg("'%s' is not an append-only row relation", RelationGetRelationName(aocsRel)))); pg_aoseg_rel = heap_open(aocsRel->rd_appendonly->segrelid, NoLock); - - context->aoSegfileArray = - GetAllFileSegInfo_pg_aoseg_rel(RelationGetRelationName(aocsRel), + + context->aoSegfileArray = + GetAllFileSegInfo_pg_aoseg_rel(RelationGetRelationName(aocsRel), pg_aoseg_rel, SnapshotNow, &context->totalAoSegFiles); @@ -1418,7 +1417,7 @@ gp_aoseg_name(PG_FUNCTION_ARGS) heap_close(pg_aoseg_rel, NoLock); heap_close(aocsRel, NoLock); - // Iteration position. + /* Iteration position. */ context->segfileArrayIndex = 0; funcctx->user_fctx = (void *) context; @@ -1445,7 +1444,7 @@ gp_aoseg_name(PG_FUNCTION_ARGS) { break; } - + /* * Form tuple with appropriate data. */ @@ -1466,7 +1465,7 @@ gp_aoseg_name(PG_FUNCTION_ARGS) tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); - // Indicate we emitted one segment file. + /* Indicate we emitted one segment file. */ context->segfileArrayIndex++; SRF_RETURN_NEXT(funcctx, result); @@ -1497,16 +1496,16 @@ gp_aoseg_name(PG_FUNCTION_ARGS) static Datum gp_update_ao_master_stats_internal(Oid relid, Snapshot appendOnlyMetaDataSnapshot) { - Relation parentrel; - Datum returnDatum; + Relation parentrel; + Datum returnDatum; /* open the parent (main) relation */ parentrel = heap_open(relid, RowExclusiveLock); - if(!RelationIsAoRows(parentrel) && !RelationIsAoCols(parentrel)) + if (!RelationIsAoRows(parentrel) && !RelationIsAoCols(parentrel)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("'%s' is not an append-only relation", + errmsg("'%s' is not an append-only relation", RelationGetRelationName(parentrel)))); if (RelationIsAoRows(parentrel)) @@ -1517,7 +1516,7 @@ gp_update_ao_master_stats_internal(Oid relid, Snapshot appendOnlyMetaDataSnapsho { returnDatum = gp_update_aocol_master_stats_internal(parentrel, appendOnlyMetaDataSnapshot); } - + heap_close(parentrel, RowExclusiveLock); return returnDatum; @@ -1526,9 +1525,9 @@ gp_update_ao_master_stats_internal(Oid relid, Snapshot appendOnlyMetaDataSnapsho Datum gp_update_ao_master_stats_name(PG_FUNCTION_ARGS) { - RangeVar *parentrv; - text *relname = PG_GETARG_TEXT_P(0); - Oid relid; + RangeVar *parentrv; + text *relname = PG_GETARG_TEXT_P(0); + Oid relid; Assert(Gp_role == GP_ROLE_DISPATCH); @@ -1556,8 +1555,8 @@ gp_update_ao_master_stats_oid(PG_FUNCTION_ARGS) typedef struct { - int index; - int rows; + int index; + int rows; } QueryInfo; @@ -1577,27 +1576,27 @@ Datum get_ao_distribution_oid(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; - MemoryContext oldcontext; - AclResult aclresult; - QueryInfo *query_block = NULL; - StringInfoData sqlstmt; - Relation parentrel; - Relation aosegrel; - char aoseg_relname[NAMEDATALEN]; - int ret; - Oid relid = PG_GETARG_OID(0); + MemoryContext oldcontext; + AclResult aclresult; + QueryInfo *query_block = NULL; + StringInfoData sqlstmt; + Relation parentrel; + Relation aosegrel; + char aoseg_relname[NAMEDATALEN]; + int ret; + Oid relid = PG_GETARG_OID(0); Assert(Gp_role == GP_ROLE_DISPATCH); /* - * stuff done only on the first call of the function. In here we - * execute the query, gather the result rows and keep them in our - * context so that we could return them in the next calls to this func. + * stuff done only on the first call of the function. In here we execute + * the query, gather the result rows and keep them in our context so that + * we could return them in the next calls to this func. */ if (SRF_IS_FIRSTCALL()) { - bool connected = false; - Oid segrelid; + bool connected = false; + Oid segrelid; funcctx = SRF_FIRSTCALL_INIT(); @@ -1605,30 +1604,30 @@ get_ao_distribution_oid(PG_FUNCTION_ARGS) parentrel = heap_open(relid, AccessShareLock); /* - * check permission to SELECT from this table (this function - * is effectively a form of COUNT(*) FROM TABLE + * check permission to SELECT from this table (this function is + * effectively a form of COUNT(*) FROM TABLE */ aclresult = pg_class_aclcheck(parentrel->rd_id, GetUserId(), - ACL_SELECT); + ACL_SELECT); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, - ACL_KIND_CLASS, - RelationGetRelationName(parentrel)); + ACL_KIND_CLASS, + RelationGetRelationName(parentrel)); /* * verify this is an AO relation */ - if(!RelationIsAoRows(parentrel) && !RelationIsAoCols(parentrel)) + if (!RelationIsAoRows(parentrel) && !RelationIsAoCols(parentrel)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("'%s' is not an append-only relation", + errmsg("'%s' is not an append-only relation", RelationGetRelationName(parentrel)))); GetAppendOnlyEntryAuxOids(RelationGetRelid(parentrel), SnapshotNow, &segrelid, NULL, NULL, NULL, NULL); Assert(OidIsValid(segrelid)); - + /* * get the name of the aoseg relation */ @@ -1642,15 +1641,15 @@ get_ao_distribution_oid(PG_FUNCTION_ARGS) initStringInfo(&sqlstmt); if (RelationIsAoRows(parentrel)) appendStringInfo(&sqlstmt, "select gp_segment_id,sum(tupcount)::bigint " - "from gp_dist_random('pg_aoseg.%s') " - "group by (gp_segment_id)", aoseg_relname); + "from gp_dist_random('pg_aoseg.%s') " + "group by (gp_segment_id)", aoseg_relname); else { Assert(RelationIsAoCols(parentrel)); appendStringInfo(&sqlstmt, "select gp_segment_id,sum(tupcount)::bigint " - "from gp_dist_random('pg_aoseg.%s') " - "group by (gp_segment_id)", aoseg_relname); + "from gp_dist_random('pg_aoseg.%s') " + "group by (gp_segment_id)", aoseg_relname); } PG_TRY(); @@ -1669,9 +1668,12 @@ get_ao_distribution_oid(PG_FUNCTION_ARGS) if (ret > 0 && SPI_tuptable != NULL) { - QueryInfo *query_block_state = NULL; + QueryInfo *query_block_state = NULL; - /* switch to memory context appropriate for multiple function calls */ + /* + * switch to memory context appropriate for multiple function + * calls + */ oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); funcctx->tuple_desc = BlessTupleDesc(SPI_tuptable->tupdesc); @@ -1680,11 +1682,11 @@ get_ao_distribution_oid(PG_FUNCTION_ARGS) * Allocate cross-call state, so that we can keep track of * where we're at in the processing. */ - query_block_state = (QueryInfo *) palloc0( sizeof(QueryInfo) ); - funcctx->user_fctx = (int *)query_block_state; + query_block_state = (QueryInfo *) palloc0(sizeof(QueryInfo)); + funcctx->user_fctx = (int *) query_block_state; - query_block_state->index = 0; - query_block_state->rows = SPI_processed; + query_block_state->index = 0; + query_block_state->rows = SPI_processed; MemoryContextSwitchTo(oldcontext); } } @@ -1710,35 +1712,33 @@ get_ao_distribution_oid(PG_FUNCTION_ARGS) funcctx = SRF_PERCALL_SETUP(); - query_block = (QueryInfo *)funcctx->user_fctx; - if ( query_block->index < query_block->rows ) + query_block = (QueryInfo *) funcctx->user_fctx; + if (query_block->index < query_block->rows) { /* - * Get heaptuple from SPI, then deform it, and reform it using - * our tuple desc. - * If we don't do this, but rather try to pass the tuple from SPI - * directly back, we get an error because - * the tuple desc that is associated with the SPI call - * has not been blessed. + * Get heaptuple from SPI, then deform it, and reform it using our + * tuple desc. If we don't do this, but rather try to pass the tuple + * from SPI directly back, we get an error because the tuple desc that + * is associated with the SPI call has not been blessed. */ - HeapTuple tuple = SPI_tuptable->vals[query_block->index++]; - TupleDesc tupleDesc = funcctx->tuple_desc; + HeapTuple tuple = SPI_tuptable->vals[query_block->index++]; + TupleDesc tupleDesc = funcctx->tuple_desc; - Datum *values = (Datum *) palloc(tupleDesc->natts * sizeof(Datum)); - bool *nulls = (bool *) palloc(tupleDesc->natts * sizeof(bool)); + Datum *values = (Datum *) palloc(tupleDesc->natts * sizeof(Datum)); + bool *nulls = (bool *) palloc(tupleDesc->natts * sizeof(bool)); - HeapTuple res = NULL; - Datum result; + HeapTuple res = NULL; + Datum result; heap_deform_tuple(tuple, tupleDesc, values, nulls); - res = heap_form_tuple(tupleDesc, values, nulls ); + res = heap_form_tuple(tupleDesc, values, nulls); pfree(values); pfree(nulls); /* make the tuple into a datum */ - result = HeapTupleGetDatum(res); + result = HeapTupleGetDatum(res); SRF_RETURN_NEXT(funcctx, result); } @@ -1759,29 +1759,29 @@ Datum get_ao_distribution_name(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; - MemoryContext oldcontext; - AclResult aclresult; - QueryInfo *query_block = NULL; - StringInfoData sqlstmt; - RangeVar *parentrv; - Relation parentrel; - Relation aosegrel; - char aoseg_relname[NAMEDATALEN]; - int ret; - text *relname = PG_GETARG_TEXT_P(0); - Oid relid; + MemoryContext oldcontext; + AclResult aclresult; + QueryInfo *query_block = NULL; + StringInfoData sqlstmt; + RangeVar *parentrv; + Relation parentrel; + Relation aosegrel; + char aoseg_relname[NAMEDATALEN]; + int ret; + text *relname = PG_GETARG_TEXT_P(0); + Oid relid; Assert(Gp_role == GP_ROLE_DISPATCH); /* - * stuff done only on the first call of the function. In here we - * execute the query, gather the result rows and keep them in our - * context so that we could return them in the next calls to this func. + * stuff done only on the first call of the function. In here we execute + * the query, gather the result rows and keep them in our context so that + * we could return them in the next calls to this func. */ if (SRF_IS_FIRSTCALL()) { - bool connected = false; - Oid segrelid = InvalidOid; + bool connected = false; + Oid segrelid = InvalidOid; funcctx = SRF_FIRSTCALL_INIT(); @@ -1792,26 +1792,26 @@ get_ao_distribution_name(PG_FUNCTION_ARGS) parentrel = heap_openrv(parentrv, AccessShareLock); /* - * check permission to SELECT from this table (this function - * is effectively a form of COUNT(*) FROM TABLE + * check permission to SELECT from this table (this function is + * effectively a form of COUNT(*) FROM TABLE */ aclresult = pg_class_aclcheck(parentrel->rd_id, GetUserId(), - ACL_SELECT); + ACL_SELECT); if (aclresult != ACLCHECK_OK) aclcheck_error(aclresult, - ACL_KIND_CLASS, - RelationGetRelationName(parentrel)); + ACL_KIND_CLASS, + RelationGetRelationName(parentrel)); /* * verify this is an AO relation */ - if(!RelationIsAoRows(parentrel) && !RelationIsAoCols(parentrel)) + if (!RelationIsAoRows(parentrel) && !RelationIsAoCols(parentrel)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("'%s' is not an append-only relation", + errmsg("'%s' is not an append-only relation", RelationGetRelationName(parentrel)))); - + GetAppendOnlyEntryAuxOids(RelationGetRelid(parentrel), SnapshotNow, &segrelid, NULL, NULL, NULL, NULL); @@ -1829,8 +1829,8 @@ get_ao_distribution_name(PG_FUNCTION_ARGS) */ initStringInfo(&sqlstmt); appendStringInfo(&sqlstmt, "select gp_segment_id,sum(tupcount) " - "from gp_dist_random('pg_aoseg.%s') " - "group by (gp_segment_id)", aoseg_relname); + "from gp_dist_random('pg_aoseg.%s') " + "group by (gp_segment_id)", aoseg_relname); PG_TRY(); { @@ -1848,9 +1848,12 @@ get_ao_distribution_name(PG_FUNCTION_ARGS) if (ret > 0 && SPI_tuptable != NULL) { - QueryInfo *query_block_state = NULL; + QueryInfo *query_block_state = NULL; - /* switch to memory context appropriate for multiple function calls */ + /* + * switch to memory context appropriate for multiple function + * calls + */ oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); funcctx->tuple_desc = BlessTupleDesc(SPI_tuptable->tupdesc); @@ -1859,11 +1862,11 @@ get_ao_distribution_name(PG_FUNCTION_ARGS) * Allocate cross-call state, so that we can keep track of * where we're at in the processing. */ - query_block_state = (QueryInfo *) palloc0( sizeof(QueryInfo) ); - funcctx->user_fctx = (int *)query_block_state; + query_block_state = (QueryInfo *) palloc0(sizeof(QueryInfo)); + funcctx->user_fctx = (int *) query_block_state; - query_block_state->index = 0; - query_block_state->rows = SPI_processed; + query_block_state->index = 0; + query_block_state->rows = SPI_processed; MemoryContextSwitchTo(oldcontext); } } @@ -1889,35 +1892,33 @@ get_ao_distribution_name(PG_FUNCTION_ARGS) funcctx = SRF_PERCALL_SETUP(); - query_block = (QueryInfo *)funcctx->user_fctx; - if ( query_block->index < query_block->rows ) + query_block = (QueryInfo *) funcctx->user_fctx; + if (query_block->index < query_block->rows) { /* - * Get heaptuple from SPI, then deform it, and reform it using - * our tuple desc. - * If we don't do this, but rather try to pass the tuple from SPI - * directly back, we get an error because - * the tuple desc that is associated with the SPI call - * has not been blessed. + * Get heaptuple from SPI, then deform it, and reform it using our + * tuple desc. If we don't do this, but rather try to pass the tuple + * from SPI directly back, we get an error because the tuple desc that + * is associated with the SPI call has not been blessed. */ - HeapTuple tuple = SPI_tuptable->vals[query_block->index++]; - TupleDesc tupleDesc = funcctx->tuple_desc; + HeapTuple tuple = SPI_tuptable->vals[query_block->index++]; + TupleDesc tupleDesc = funcctx->tuple_desc; - Datum *values = (Datum *) palloc(tupleDesc->natts * sizeof(Datum)); - bool *nulls = (bool *) palloc(tupleDesc->natts * sizeof(bool)); + Datum *values = (Datum *) palloc(tupleDesc->natts * sizeof(Datum)); + bool *nulls = (bool *) palloc(tupleDesc->natts * sizeof(bool)); - HeapTuple res = NULL; - Datum result; + HeapTuple res = NULL; + Datum result; heap_deform_tuple(tuple, tupleDesc, values, nulls); - res = heap_form_tuple(tupleDesc, values, nulls ); + res = heap_form_tuple(tupleDesc, values, nulls); pfree(values); pfree(nulls); /* make the tuple into a datum */ - result = HeapTupleGetDatum(res); + result = HeapTupleGetDatum(res); SRF_RETURN_NEXT(funcctx, result); } @@ -1947,9 +1948,9 @@ get_ao_distribution_name(PG_FUNCTION_ARGS) Datum get_ao_compression_ratio_name(PG_FUNCTION_ARGS) { - RangeVar *parentrv; - text *relname = PG_GETARG_TEXT_P(0); - Oid relid; + RangeVar *parentrv; + text *relname = PG_GETARG_TEXT_P(0); + Oid relid; Assert(Gp_role == GP_ROLE_DISPATCH); @@ -1978,14 +1979,15 @@ get_ao_compression_ratio_oid(PG_FUNCTION_ARGS) static Datum aorow_compression_ratio_internal(Relation parentrel) { - StringInfoData sqlstmt; - Relation aosegrel; - bool connected = false; - char aoseg_relname[NAMEDATALEN]; - int proc; - int ret; - float8 compress_ratio = -1; /* the default, meaning "not available" */ - Oid segrelid = InvalidOid; + StringInfoData sqlstmt; + Relation aosegrel; + bool connected = false; + char aoseg_relname[NAMEDATALEN]; + int proc; + int ret; + float8 compress_ratio = -1; /* the default, meaning "not + * available" */ + Oid segrelid = InvalidOid; GetAppendOnlyEntryAuxOids(RelationGetRelid(parentrel), SnapshotNow, &segrelid, @@ -2005,12 +2007,12 @@ aorow_compression_ratio_internal(Relation parentrel) initStringInfo(&sqlstmt); if (Gp_role == GP_ROLE_DISPATCH) appendStringInfo(&sqlstmt, "select sum(eof), sum(eofuncompressed) " - "from gp_dist_random('pg_aoseg.%s')", - aoseg_relname); + "from gp_dist_random('pg_aoseg.%s')", + aoseg_relname); else appendStringInfo(&sqlstmt, "select eof, eofuncompressed " - "from pg_aoseg.%s", - aoseg_relname); + "from pg_aoseg.%s", + aoseg_relname); PG_TRY(); { @@ -2029,11 +2031,11 @@ aorow_compression_ratio_internal(Relation parentrel) if (ret > 0 && SPI_tuptable != NULL) { - TupleDesc tupdesc = SPI_tuptable->tupdesc; - SPITupleTable *tuptable = SPI_tuptable; - HeapTuple tuple = tuptable->vals[0]; - int64 eof; - int64 eof_uncomp; + TupleDesc tupdesc = SPI_tuptable->tupdesc; + SPITupleTable *tuptable = SPI_tuptable; + HeapTuple tuple = tuptable->vals[0]; + int64 eof; + int64 eof_uncomp; /* we expect only 1 tuple */ Assert(proc == 1); @@ -2043,8 +2045,8 @@ aorow_compression_ratio_internal(Relation parentrel) * tables upgraded from GPDB 3.3 the eofuncompressed column could * contain NULL, this is fixed in more recent upgrades. */ - char *attr1 = SPI_getvalue(tuple, tupdesc, 1); - char *attr2 = SPI_getvalue(tuple, tupdesc, 2); + char *attr1 = SPI_getvalue(tuple, tupdesc, 1); + char *attr2 = SPI_getvalue(tuple, tupdesc, 2); if (NULL == attr1 || NULL == attr2) { @@ -2091,16 +2093,16 @@ aorow_compression_ratio_internal(Relation parentrel) static Datum ao_compression_ratio_internal(Oid relid) { - Relation parentrel; - Datum returnDatum; + Relation parentrel; + Datum returnDatum; /* open the parent (main) relation */ parentrel = heap_open(relid, AccessShareLock); - if(!RelationIsAoRows(parentrel) && !RelationIsAoCols(parentrel)) + if (!RelationIsAoRows(parentrel) && !RelationIsAoCols(parentrel)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("'%s' is not an append-only relation", + errmsg("'%s' is not an append-only relation", RelationGetRelationName(parentrel)))); if (RelationIsAoRows(parentrel)) @@ -2122,10 +2124,10 @@ FreeAllSegFileInfo(FileSegInfo **allSegInfo, int totalSegFiles) { Assert(allSegInfo); - for(int file_no = 0; file_no < totalSegFiles; file_no++) + for (int file_no = 0; file_no < totalSegFiles; file_no++) { Assert(allSegInfo[file_no] != NULL); - + pfree(allSegInfo[file_no]); } } @@ -2134,23 +2136,24 @@ FreeAllSegFileInfo(FileSegInfo **allSegInfo, int totalSegFiles) void PrintPgaosegAndGprelationNodeEntries(FileSegInfo **allseginfo, int totalsegs, bool *segmentFileNumMap) { - char segnumArray[1000]; - char delimiter[5] = " "; - char tmp[10] = {0}; + char segnumArray[1000]; + char delimiter[5] = " "; + char tmp[10] = {0}; + memset(segnumArray, 0, sizeof(segnumArray)); - char *head = segnumArray; - const char *tail = segnumArray + sizeof(segnumArray); + char *head = segnumArray; + const char *tail = segnumArray + sizeof(segnumArray); - for (int i = 0 ; i < totalsegs && allseginfo; i++) + for (int i = 0; i < totalsegs && allseginfo; i++) { snprintf(tmp, sizeof(tmp), "%d:%jd", allseginfo[i]->segno, allseginfo[i]->eof); - if (strlen(tmp) + strlen(delimiter) >= (tail - head)) - break; + if (strlen(tmp) + strlen(delimiter) >= (tail - head)) + break; - head += strlcpy(head, tmp, tail - head); - head += strlcpy(head, delimiter, tail - head); + head += strlcpy(head, tmp, tail - head); + head += strlcpy(head, delimiter, tail - head); } elog(LOG, "pg_aoseg segno:eof entries: %s", segnumArray); @@ -2175,42 +2178,42 @@ PrintPgaosegAndGprelationNodeEntries(FileSegInfo **allseginfo, int totalsegs, bo void -CheckAOConsistencyWithGpRelationNode( Snapshot snapshot, Relation rel, int totalsegs, FileSegInfo ** allseginfo) +CheckAOConsistencyWithGpRelationNode(Snapshot snapshot, Relation rel, int totalsegs, FileSegInfo **allseginfo) { GpRelationNodeScan gpRelationNodeScan; - int segmentFileNum = 0; + int segmentFileNum = 0; ItemPointerData persistentTid; - int64 persistentSerialNum = 0; - int segmentCount = 0; - Relation gp_relation_node; + int64 persistentSerialNum = 0; + int segmentCount = 0; + Relation gp_relation_node; if (!gp_appendonly_verify_eof) { return; } - /* - * gp_relation_node always has a zero. Hence we use Max segment file number plus 1 in order - * to accomodate the zero + /* + * gp_relation_node always has a zero. Hence we use Max segment file + * number plus 1 in order to accomodate the zero */ - const int num_gp_relation_node_entries = AOTupleId_MaxSegmentFileNum + 1; - bool *segmentFileNumMap = (bool*) palloc0( sizeof(bool) * num_gp_relation_node_entries); - int i = 0; - int j = 0; + const int num_gp_relation_node_entries = AOTupleId_MaxSegmentFileNum + 1; + bool *segmentFileNumMap = (bool *) palloc0(sizeof(bool) * num_gp_relation_node_entries); + int i = 0; + int j = 0; gp_relation_node = heap_open(GpRelationNodeRelationId, AccessShareLock); GpRelationNodeBeginScan( - snapshot, - gp_relation_node, - rel->rd_id, - rel->rd_rel->reltablespace, - rel->rd_rel->relfilenode, - &gpRelationNodeScan); + snapshot, + gp_relation_node, + rel->rd_id, + rel->rd_rel->reltablespace, + rel->rd_rel->relfilenode, + &gpRelationNodeScan); while ((NULL != GpRelationNodeGetNext( - &gpRelationNodeScan, - &segmentFileNum, - &persistentTid, - &persistentSerialNum))) + &gpRelationNodeScan, + &segmentFileNum, + &persistentTid, + &persistentSerialNum))) { if (segmentFileNumMap[segmentFileNum] != true) { @@ -2222,9 +2225,9 @@ CheckAOConsistencyWithGpRelationNode( Snapshot snapshot, Relation rel, int total { PrintPgaosegAndGprelationNodeEntries(allseginfo, totalsegs, segmentFileNumMap); elog(ERROR, "gp_relation_node (%d) has more entries than pg_aoseg (%d) for relation %s", - segmentCount, - totalsegs, - RelationGetRelationName(rel)); + segmentCount, + totalsegs, + RelationGetRelationName(rel)); } } @@ -2243,14 +2246,14 @@ CheckAOConsistencyWithGpRelationNode( Snapshot snapshot, Relation rel, int total { if (segmentFileNumMap[j] == true) { - while(i < totalsegs && allseginfo[i]->segno != j) + while (i < totalsegs && allseginfo[i]->segno != j) { if (allseginfo[i]->eof != 0 && segmentFileNumMap[allseginfo[i]->segno] == false) { PrintPgaosegAndGprelationNodeEntries(allseginfo, totalsegs, segmentFileNumMap); elog(ERROR, "Missing pg_aoseg entry %d in gp_relation_node for %s", - allseginfo[i]->segno, RelationGetRelationName(rel)); + allseginfo[i]->segno, RelationGetRelationName(rel)); } i++; } @@ -2265,13 +2268,14 @@ CheckAOConsistencyWithGpRelationNode( Snapshot snapshot, Relation rel, int total { PrintPgaosegAndGprelationNodeEntries(allseginfo, totalsegs, segmentFileNumMap); elog(ERROR, "Missing gp_relation_node entry %d in pg_aoseg for %s", - j, RelationGetRelationName(rel)); + j, RelationGetRelationName(rel)); } } } /* - * Check for any extra entries in pg_aoseg which are not present in gp_relation_node + * Check for any extra entries in pg_aoseg which are not present in + * gp_relation_node */ while (i < totalsegs) { @@ -2280,11 +2284,10 @@ CheckAOConsistencyWithGpRelationNode( Snapshot snapshot, Relation rel, int total { PrintPgaosegAndGprelationNodeEntries(allseginfo, totalsegs, segmentFileNumMap); elog(ERROR, "Missing pg_aoseg entry %d in gp_relation_node for %s", - allseginfo[i]->segno, RelationGetRelationName(rel)); + allseginfo[i]->segno, RelationGetRelationName(rel)); } i++; } pfree(segmentFileNumMap); } - diff --git a/src/backend/access/appendonly/appendonly_compaction.c b/src/backend/access/appendonly/appendonly_compaction.c index e34ee92bdf..a07421f990 100644 --- a/src/backend/access/appendonly/appendonly_compaction.c +++ b/src/backend/access/appendonly/appendonly_compaction.c @@ -52,34 +52,34 @@ /* * Drops a segment file. * - */ + */ static void AppendOnlyCompaction_DropSegmentFile(Relation aorel, - int segno) + int segno) { - ItemPointerData persistentTid; - int64 persistentSerialNum; + ItemPointerData persistentTid; + int64 persistentSerialNum; if (!ReadGpRelationNode( - aorel->rd_rel->reltablespace, - aorel->rd_rel->relfilenode, - segno, - &persistentTid, - &persistentSerialNum)) + aorel->rd_rel->reltablespace, + aorel->rd_rel->relfilenode, + segno, + &persistentTid, + &persistentSerialNum)) { /* There is nothing to drop */ return; } - elogif(Debug_appendonly_print_compaction, LOG, - "Drop segment file: segno %d", segno); + elogif(Debug_appendonly_print_compaction, LOG, + "Drop segment file: segno %d", segno); MirroredFileSysObj_ScheduleDropAppendOnlyFile( - &aorel->rd_node, - segno, - RelationGetRelationName(aorel), - &persistentTid, - persistentSerialNum); + &aorel->rd_node, + segno, + RelationGetRelationName(aorel), + &persistentTid, + persistentSerialNum); DeleteGpRelationNodeTuple(aorel, segno); } @@ -87,10 +87,10 @@ AppendOnlyCompaction_DropSegmentFile(Relation aorel, /* * Calculates the ratio of hidden tuples as a double between 0 and 100 */ -static double +static double AppendOnlyCompaction_GetHideRatio(int64 hiddenTupcount, int64 totalTupcount) { - double hideRatio; + double hideRatio; if (hiddenTupcount == 0 || totalTupcount == 0) { @@ -105,88 +105,89 @@ AppendOnlyCompaction_GetHideRatio(int64 hiddenTupcount, int64 totalTupcount) */ bool AppendOnlyCompaction_ShouldCompact( - Relation aoRelation, - int segno, - int64 segmentTotalTupcount, - bool isFull) + Relation aoRelation, + int segno, + int64 segmentTotalTupcount, + bool isFull) { - bool result; + bool result; AppendOnlyVisimap visiMap; - int64 hiddenTupcount; - double hideRatio; + int64 hiddenTupcount; + double hideRatio; Assert(RelationIsAoRows(aoRelation) || RelationIsAoCols(aoRelation)); if (!gp_appendonly_compaction) { ereport(LOG, - (errmsg("Append-only compaction skipped on relation %s, segment file num %d", - RelationGetRelationName(aoRelation), - segno), - errdetail("Compaction is disabled"))); + (errmsg("Append-only compaction skipped on relation %s, segment file num %d", + RelationGetRelationName(aoRelation), + segno), + errdetail("Compaction is disabled"))); /* Disable compaction by global guc. */ return false; } AppendOnlyVisimap_Init(&visiMap, - aoRelation->rd_appendonly->visimaprelid, - aoRelation->rd_appendonly->visimapidxid, - ShareLock, - SnapshotNow); + aoRelation->rd_appendonly->visimaprelid, + aoRelation->rd_appendonly->visimapidxid, + ShareLock, + SnapshotNow); hiddenTupcount = AppendOnlyVisimap_GetSegmentFileHiddenTupleCount( - &visiMap, segno); + &visiMap, segno); result = true; if (isFull && hiddenTupcount > 0) { - /* - * if it is a full vacuum and there is any obsolete data, do a compaction + /* + * if it is a full vacuum and there is any obsolete data, do a + * compaction */ result = true; } else { - hideRatio = AppendOnlyCompaction_GetHideRatio(hiddenTupcount, segmentTotalTupcount); - if (hideRatio <= gp_appendonly_compaction_threshold || gp_appendonly_compaction_threshold == 0) - { + hideRatio = AppendOnlyCompaction_GetHideRatio(hiddenTupcount, segmentTotalTupcount); + if (hideRatio <= gp_appendonly_compaction_threshold || gp_appendonly_compaction_threshold == 0) + { if (hiddenTupcount > 0) { - ereportif(Debug_appendonly_print_compaction, LOG, - (errmsg("Append-only compaction skipped on relation %s, segment file num %d, " - "hidden tupcount " INT64_FORMAT ", total tupcount " INT64_FORMAT ", " - "hide ratio %lf%%, threshold %d%%", - RelationGetRelationName(aoRelation), - segno, - hiddenTupcount, segmentTotalTupcount, - hideRatio, gp_appendonly_compaction_threshold))); + ereportif(Debug_appendonly_print_compaction, LOG, + (errmsg("Append-only compaction skipped on relation %s, segment file num %d, " + "hidden tupcount " INT64_FORMAT ", total tupcount " INT64_FORMAT ", " + "hide ratio %lf%%, threshold %d%%", + RelationGetRelationName(aoRelation), + segno, + hiddenTupcount, segmentTotalTupcount, + hideRatio, gp_appendonly_compaction_threshold))); ereport(LOG, - (errmsg("Append-only compaction skipped on relation %s, segment file num %d", - RelationGetRelationName(aoRelation), - segno), - errdetail("Ratio of obsolete tuples below threshold (%lf%% vs %d%%)", - hideRatio, gp_appendonly_compaction_threshold))); + (errmsg("Append-only compaction skipped on relation %s, segment file num %d", + RelationGetRelationName(aoRelation), + segno), + errdetail("Ratio of obsolete tuples below threshold (%lf%% vs %d%%)", + hideRatio, gp_appendonly_compaction_threshold))); } else { - ereportif(Debug_appendonly_print_compaction, LOG, - (errmsg("Append-only compaction skipped on relation %s, segment file num %d, " - "hidden tupcount " INT64_FORMAT ", total tupcount " INT64_FORMAT ", " - "hide ratio %lf%%, threshold %d%%", - RelationGetRelationName(aoRelation), - segno, - hiddenTupcount, segmentTotalTupcount, - hideRatio, gp_appendonly_compaction_threshold))); + ereportif(Debug_appendonly_print_compaction, LOG, + (errmsg("Append-only compaction skipped on relation %s, segment file num %d, " + "hidden tupcount " INT64_FORMAT ", total tupcount " INT64_FORMAT ", " + "hide ratio %lf%%, threshold %d%%", + RelationGetRelationName(aoRelation), + segno, + hiddenTupcount, segmentTotalTupcount, + hideRatio, gp_appendonly_compaction_threshold))); } result = false; } - elogif(Debug_appendonly_print_compaction, LOG, - "Schedule compaction: " - "segno %d, " - "hidden tupcount " INT64_FORMAT ", total tupcount " INT64_FORMAT ", " - "hide ratio %lf%%, threshold %d%%", - segno, - hiddenTupcount, segmentTotalTupcount, - hideRatio, gp_appendonly_compaction_threshold); + elogif(Debug_appendonly_print_compaction, LOG, + "Schedule compaction: " + "segno %d, " + "hidden tupcount " INT64_FORMAT ", total tupcount " INT64_FORMAT ", " + "hide ratio %lf%%, threshold %d%%", + segno, + hiddenTupcount, segmentTotalTupcount, + hideRatio, gp_appendonly_compaction_threshold); } AppendOnlyVisimap_Finish(&visiMap, ShareLock); return result; @@ -200,22 +201,22 @@ AppendOnlyCompaction_ShouldCompact( * For the segment file is truncates to the eof. */ static void -AppendOnlySegmentFileTruncateToEOF(Relation aorel, - FileSegInfo *fsinfo) +AppendOnlySegmentFileTruncateToEOF(Relation aorel, + FileSegInfo *fsinfo) { - const char* relname = RelationGetRelationName(aorel); + const char *relname = RelationGetRelationName(aorel); MirroredAppendOnlyOpen mirroredOpened; - int32 fileSegNo; - char filenamepath[MAXPGPATH]; - int segno; - int64 segeof; + int32 fileSegNo; + char filenamepath[MAXPGPATH]; + int segno; + int64 segeof; Assert(fsinfo); Assert(RelationIsAoRows(aorel)); segno = fsinfo->segno; relname = RelationGetRelationName(aorel); - segeof = (int64)fsinfo->eof; + segeof = (int64) fsinfo->eof; /* Open and truncate the relation segfile beyond its eof */ MakeAOSegmentFileName(aorel, segno, -1, &fileSegNo, filenamepath); @@ -235,45 +236,45 @@ AppendOnlySegmentFileTruncateToEOF(Relation aorel, CloseAOSegmentFile(&mirroredOpened); elogif(Debug_appendonly_print_compaction, LOG, - "Successfully truncated AO ROW relation \"%s.%s\", relation id %u, relfilenode %u (physical segment file #%d, logical EOF " INT64_FORMAT ")", - get_namespace_name(RelationGetNamespace(aorel)), - relname, - aorel->rd_id, - aorel->rd_node.relNode, - segno, - segeof); + "Successfully truncated AO ROW relation \"%s.%s\", relation id %u, relfilenode %u (physical segment file #%d, logical EOF " INT64_FORMAT ")", + get_namespace_name(RelationGetNamespace(aorel)), + relname, + aorel->rd_id, + aorel->rd_node.relNode, + segno, + segeof); } else { elogif(Debug_appendonly_print_compaction, LOG, - "No gp_relation_node entry for AO ROW relation \"%s.%s\", relation id %u, relfilenode %u (physical segment file #%d, logical EOF " INT64_FORMAT ")", - get_namespace_name(RelationGetNamespace(aorel)), - relname, - aorel->rd_id, - aorel->rd_node.relNode, - segno, - segeof); + "No gp_relation_node entry for AO ROW relation \"%s.%s\", relation id %u, relfilenode %u (physical segment file #%d, logical EOF " INT64_FORMAT ")", + get_namespace_name(RelationGetNamespace(aorel)), + relname, + aorel->rd_id, + aorel->rd_node.relNode, + segno, + segeof); } } static void AppendOnlyMoveTuple(MemTuple tuple, - TupleTableSlot *slot, + TupleTableSlot *slot, MemTupleBinding *mt_bind, AppendOnlyInsertDesc insertDesc, ResultRelInfo *resultRelInfo, EState *estate) { - AOTupleId *oldAoTupleId; - Oid tupleOid; - AOTupleId newAoTupleId; + AOTupleId *oldAoTupleId; + Oid tupleOid; + AOTupleId newAoTupleId; Assert(resultRelInfo); Assert(slot); Assert(mt_bind); Assert(estate); - oldAoTupleId = (AOTupleId*)slot_get_ctid(slot); + oldAoTupleId = (AOTupleId *) slot_get_ctid(slot); /* Extract all the values of the tuple */ slot_getallattrs(slot); @@ -286,29 +287,29 @@ AppendOnlyMoveTuple(MemTuple tuple, /* insert index' tuples if needed */ if (resultRelInfo->ri_NumIndices > 0) { - ExecInsertIndexTuples(slot, (ItemPointer)&newAoTupleId, estate, true); + ExecInsertIndexTuples(slot, (ItemPointer) &newAoTupleId, estate, true); ResetPerTupleExprContext(estate); } - elogif(Debug_appendonly_print_compaction, DEBUG5, - "Compaction: Moved tuple (%d," INT64_FORMAT ") -> (%d," INT64_FORMAT ")", - AOTupleIdGet_segmentFileNum(oldAoTupleId), AOTupleIdGet_rowNum(oldAoTupleId), - AOTupleIdGet_segmentFileNum(&newAoTupleId), AOTupleIdGet_rowNum(&newAoTupleId)); + elogif(Debug_appendonly_print_compaction, DEBUG5, + "Compaction: Moved tuple (%d," INT64_FORMAT ") -> (%d," INT64_FORMAT ")", + AOTupleIdGet_segmentFileNum(oldAoTupleId), AOTupleIdGet_rowNum(oldAoTupleId), + AOTupleIdGet_segmentFileNum(&newAoTupleId), AOTupleIdGet_rowNum(&newAoTupleId)); } void AppendOnlyThrowAwayTuple( - Relation rel, - MemTuple tuple, - TupleTableSlot *slot, - MemTupleBinding *mt_bind) + Relation rel, + MemTuple tuple, + TupleTableSlot *slot, + MemTupleBinding *mt_bind) { - AOTupleId *oldAoTupleId; + AOTupleId *oldAoTupleId; Assert(slot); Assert(mt_bind); - oldAoTupleId = (AOTupleId*)slot_get_ctid(slot); + oldAoTupleId = (AOTupleId *) slot_get_ctid(slot); /* Extract all the values of the tuple */ slot_getallattrs(slot); @@ -317,9 +318,9 @@ AppendOnlyThrowAwayTuple( toast_delete(rel, (HeapTuple) tuple, mt_bind); } - elogif(Debug_appendonly_print_compaction, DEBUG5, - "Compaction: Throw away tuple (%d," INT64_FORMAT ")", - AOTupleIdGet_segmentFileNum(oldAoTupleId), AOTupleIdGet_rowNum(oldAoTupleId)); + elogif(Debug_appendonly_print_compaction, DEBUG5, + "Compaction: Throw away tuple (%d," INT64_FORMAT ")", + AOTupleIdGet_segmentFileNum(oldAoTupleId), AOTupleIdGet_rowNum(oldAoTupleId)); } /* @@ -328,24 +329,24 @@ AppendOnlyThrowAwayTuple( * */ static void -AppendOnlySegmentFileFullCompaction(Relation aorel, - AppendOnlyInsertDesc insertDesc, - FileSegInfo* fsinfo) +AppendOnlySegmentFileFullCompaction(Relation aorel, + AppendOnlyInsertDesc insertDesc, + FileSegInfo *fsinfo) { - const char* relname; + const char *relname; AppendOnlyVisimap visiMap; AppendOnlyScanDesc scanDesc; - TupleDesc tupDesc; - MemTuple tuple; - TupleTableSlot *slot; + TupleDesc tupDesc; + MemTuple tuple; + TupleTableSlot *slot; MemTupleBinding *mt_bind; - int compact_segno; - int64 movedTupleCount = 0; + int compact_segno; + int64 movedTupleCount = 0; ResultRelInfo *resultRelInfo; - EState *estate; - AOTupleId *aoTupleId; - int64 tupleCount = 0; - int64 tuplePerPage = INT_MAX; + EState *estate; + AOTupleId *aoTupleId; + int64 tupleCount = 0; + int64 tuplePerPage = INT_MAX; Assert(Gp_role == GP_ROLE_EXECUTE || Gp_role == GP_ROLE_UTILITY); Assert(RelationIsAoRows(aorel)); @@ -359,24 +360,24 @@ AppendOnlySegmentFileFullCompaction(Relation aorel, relname = RelationGetRelationName(aorel); AppendOnlyVisimap_Init(&visiMap, - aorel->rd_appendonly->visimaprelid, - aorel->rd_appendonly->visimapidxid, - ShareUpdateExclusiveLock, - SnapshotNow); + aorel->rd_appendonly->visimaprelid, + aorel->rd_appendonly->visimapidxid, + ShareUpdateExclusiveLock, + SnapshotNow); elogif(Debug_appendonly_print_compaction, - LOG, "Compact AO segno %d, relation %s, insert segno %d", - compact_segno, relname, insertDesc->storageWrite.segmentFileNum); + LOG, "Compact AO segno %d, relation %s, insert segno %d", + compact_segno, relname, insertDesc->storageWrite.segmentFileNum); /* * Todo: We need to limit the scan to one file and we need to avoid to * lock the file again. - * - * We use SnapshotAny to get visible and invisible tuples. + * + * We use SnapshotAny to get visible and invisible tuples. */ scanDesc = appendonly_beginrangescan(aorel, - SnapshotAny, SnapshotNow, - &compact_segno, 1, 0, NULL); + SnapshotAny, SnapshotNow, + &compact_segno, 1, 0, NULL); tupDesc = RelationGetDescr(aorel); slot = MakeSingleTupleTableSlot(tupDesc); @@ -390,7 +391,7 @@ AppendOnlySegmentFileFullCompaction(Relation aorel, resultRelInfo = makeNode(ResultRelInfo); resultRelInfo->ri_RangeTableIndex = 1; /* dummy */ resultRelInfo->ri_RelationDesc = aorel; - resultRelInfo->ri_TrigDesc = NULL; /* we don't fire triggers */ + resultRelInfo->ri_TrigDesc = NULL; /* we don't fire triggers */ ExecOpenIndices(resultRelInfo); estate->es_result_relations = resultRelInfo; estate->es_num_result_relations = 1; @@ -404,27 +405,27 @@ AppendOnlySegmentFileFullCompaction(Relation aorel, /* Check interrupts as this may take time. */ CHECK_FOR_INTERRUPTS(); - aoTupleId = (AOTupleId*)slot_get_ctid(slot); + aoTupleId = (AOTupleId *) slot_get_ctid(slot); if (AppendOnlyVisimap_IsVisible(&scanDesc->visibilityMap, aoTupleId)) { AppendOnlyMoveTuple(tuple, - slot, - mt_bind, - insertDesc, - resultRelInfo, - estate); + slot, + mt_bind, + insertDesc, + resultRelInfo, + estate); movedTupleCount++; } else { /* Tuple is invisible and needs to be dropped */ - AppendOnlyThrowAwayTuple(aorel, - tuple, - slot, - mt_bind); + AppendOnlyThrowAwayTuple(aorel, + tuple, + slot, + mt_bind); } - /* + /* * Check for vacuum delay point after approximately a var block */ tupleCount++; @@ -467,11 +468,12 @@ AppendOnlySegmentFileFullCompaction(Relation aorel, * Checks if the correct relation lock is held. * It does so be acquiring the lock in a no-wait mode. * If it didn't hold the lock before, it is released immediately. - */ -bool + */ +bool HasLockForSegmentFileDrop(Relation aorel) { LockAcquireResult acquireResult = LockRelationNoWait(aorel, AccessExclusiveLock); + switch (acquireResult) { case LOCKACQUIRE_ALREADY_HELD: @@ -481,12 +483,12 @@ HasLockForSegmentFileDrop(Relation aorel) case LOCKACQUIRE_OK: UnlockRelation(aorel, AccessExclusiveLock); #ifdef USE_ASSERT_CHECKING - acquireResult = LockRelationNoWait(aorel, ShareUpdateExclusiveLock); - if (acquireResult != LOCKACQUIRE_ALREADY_HELD) - { - elog(ERROR, "Don't hold access exclusive lock during drop"); - return false; - } + acquireResult = LockRelationNoWait(aorel, ShareUpdateExclusiveLock); + if (acquireResult != LOCKACQUIRE_ALREADY_HELD) + { + elog(ERROR, "Don't hold access exclusive lock during drop"); + return false; + } #endif return false; default: @@ -501,28 +503,29 @@ HasLockForSegmentFileDrop(Relation aorel) * In non-utility mode, all compaction segment files should be * marked as in-use/in-compaction in the appendonlywriter.c code. * - */ + */ void AppendOnlyDrop(Relation aorel, List *compaction_segno) { - const char* relname; - int total_segfiles; - FileSegInfo** segfile_array; - int i, segno; - FileSegInfo* fsinfo; + const char *relname; + int total_segfiles; + FileSegInfo **segfile_array; + int i, + segno; + FileSegInfo *fsinfo; - Assert (Gp_role == GP_ROLE_EXECUTE || Gp_role == GP_ROLE_UTILITY); - Assert (RelationIsAoRows(aorel)); + Assert(Gp_role == GP_ROLE_EXECUTE || Gp_role == GP_ROLE_UTILITY); + Assert(RelationIsAoRows(aorel)); relname = RelationGetRelationName(aorel); - elogif (Debug_appendonly_print_compaction, LOG, - "Drop AO relation %s", relname); + elogif(Debug_appendonly_print_compaction, LOG, + "Drop AO relation %s", relname); /* Get information about all the file segments we need to scan */ segfile_array = GetAllFileSegInfo(aorel, SnapshotNow, &total_segfiles); - for(i = 0 ; i < total_segfiles ; i++) + for (i = 0; i < total_segfiles; i++) { segno = segfile_array[i]->segno; if (!list_member_int(compaction_segno, segno)) @@ -531,15 +534,17 @@ AppendOnlyDrop(Relation aorel, List *compaction_segno) } /* - * Try to get the transaction write-lock for the Append-Only segment file. + * Try to get the transaction write-lock for the Append-Only segment + * file. * - * NOTE: This is a transaction scope lock that must be held until commit / abort. + * NOTE: This is a transaction scope lock that must be held until + * commit / abort. */ LockRelationAppendOnlySegmentFile( - &aorel->rd_node, - segfile_array[i]->segno, - AccessExclusiveLock, - false); + &aorel->rd_node, + segfile_array[i]->segno, + AccessExclusiveLock, + false); /* Re-fetch under the write lock to get latest committed eof. */ fsinfo = GetFileSegInfo(aorel, SnapshotNow, segno); @@ -550,7 +555,7 @@ AppendOnlyDrop(Relation aorel, List *compaction_segno) Assert(!HasSerializableBackends(false)); AppendOnlyCompaction_DropSegmentFile(aorel, segno); ClearFileSegInfo(aorel, segno, - AOSEG_STATE_DEFAULT); + AOSEG_STATE_DEFAULT); } pfree(fsinfo); } @@ -570,41 +575,44 @@ AppendOnlyDrop(Relation aorel, List *compaction_segno) void AppendOnlyTruncateToEOF(Relation aorel) { - const char* relname; - int total_segfiles; - FileSegInfo** segfile_array; - int i, segno; + const char *relname; + int total_segfiles; + FileSegInfo **segfile_array; + int i, + segno; LockAcquireResult acquireResult; - FileSegInfo* fsinfo; + FileSegInfo *fsinfo; - Assert (RelationIsAoRows(aorel)); + Assert(RelationIsAoRows(aorel)); relname = RelationGetRelationName(aorel); - elogif (Debug_appendonly_print_compaction, LOG, - "Compact AO relation %s", relname); + elogif(Debug_appendonly_print_compaction, LOG, + "Compact AO relation %s", relname); /* Get information about all the file segments we need to scan */ segfile_array = GetAllFileSegInfo(aorel, SnapshotNow, &total_segfiles); - for(i = 0 ; i < total_segfiles ; i++) + for (i = 0; i < total_segfiles; i++) { segno = segfile_array[i]->segno; /* - * Try to get the transaction write-lock for the Append-Only segment file. + * Try to get the transaction write-lock for the Append-Only segment + * file. * - * NOTE: This is a transaction scope lock that must be held until commit / abort. + * NOTE: This is a transaction scope lock that must be held until + * commit / abort. */ acquireResult = LockRelationAppendOnlySegmentFile( - &aorel->rd_node, - segfile_array[i]->segno, - AccessExclusiveLock, - /* dontWait */ true); + &aorel->rd_node, + segfile_array[i]->segno, + AccessExclusiveLock, + /* dontWait */ true); if (acquireResult == LOCKACQUIRE_NOT_AVAIL) { elog(DEBUG5, "truncate skips AO segfile %d, " - "relation %s", segfile_array[i]->segno, relname); + "relation %s", segfile_array[i]->segno, relname); continue; } @@ -613,8 +621,8 @@ AppendOnlyTruncateToEOF(Relation aorel) /* * This should not occur since this segfile info was found by the - * "all" method, but better to catch for trouble shooting - * (possibly index corruption?) + * "all" method, but better to catch for trouble shooting (possibly + * index corruption?) */ if (fsinfo == NULL) elog(ERROR, "file seginfo for AO relation %s %u/%u/%u (segno=%u) is missing", @@ -644,36 +652,37 @@ AppendOnlyTruncateToEOF(Relation aorel) * When the insert segno is negative, only truncate to eof operations * can be executed. * - * The caller is required to hold either an AccessExclusiveLock (vacuum full) + * The caller is required to hold either an AccessExclusiveLock (vacuum full) * or a ShareLock on the relation. - */ + */ void -AppendOnlyCompact(Relation aorel, - List* compaction_segno, - int insert_segno, - bool isFull) +AppendOnlyCompact(Relation aorel, + List *compaction_segno, + int insert_segno, + bool isFull) { - const char* relname; - int total_segfiles; - FileSegInfo** segfile_array; + const char *relname; + int total_segfiles; + FileSegInfo **segfile_array; AppendOnlyInsertDesc insertDesc = NULL; - int i, segno; - FileSegInfo* fsinfo; + int i, + segno; + FileSegInfo *fsinfo; - Assert (Gp_role == GP_ROLE_EXECUTE || Gp_role == GP_ROLE_UTILITY); + Assert(Gp_role == GP_ROLE_EXECUTE || Gp_role == GP_ROLE_UTILITY); Assert(insert_segno >= 0); relname = RelationGetRelationName(aorel); - elogif (Debug_appendonly_print_compaction, LOG, - "Compact AO relation %s", relname); + elogif(Debug_appendonly_print_compaction, LOG, + "Compact AO relation %s", relname); /* Get information about all the file segments we need to scan */ segfile_array = GetAllFileSegInfo(aorel, SnapshotNow, &total_segfiles); insertDesc = appendonly_insert_init(aorel, insert_segno, false); - for(i = 0 ; i < total_segfiles ; i++) + for (i = 0; i < total_segfiles; i++) { segno = segfile_array[i]->segno; if (!list_member_int(compaction_segno, segno)) @@ -687,23 +696,25 @@ AppendOnlyCompact(Relation aorel, } /* - * Try to get the transaction write-lock for the Append-Only segment file. + * Try to get the transaction write-lock for the Append-Only segment + * file. * - * NOTE: This is a transaction scope lock that must be held until commit / abort. + * NOTE: This is a transaction scope lock that must be held until + * commit / abort. */ LockRelationAppendOnlySegmentFile( - &aorel->rd_node, - segfile_array[i]->segno, - AccessExclusiveLock, - false); + &aorel->rd_node, + segfile_array[i]->segno, + AccessExclusiveLock, + false); /* Re-fetch under the write lock to get latest committed eof. */ fsinfo = GetFileSegInfo(aorel, SnapshotNow, segno); /* * This should not occur since this segfile info was found by the - * "all" method, but better to catch for trouble shooting - * (possibly index corruption?) + * "all" method, but better to catch for trouble shooting (possibly + * index corruption?) */ if (fsinfo == NULL) elog(ERROR, "file seginfo for AO relation %s %u/%u/%u (segno=%u) is missing", @@ -714,12 +725,12 @@ AppendOnlyCompact(Relation aorel, segno); if (AppendOnlyCompaction_ShouldCompact(aorel, - fsinfo->segno, fsinfo->total_tupcount, isFull)) + fsinfo->segno, fsinfo->total_tupcount, isFull)) { AppendOnlySegmentFileFullCompaction(aorel, - insertDesc, - fsinfo); - } + insertDesc, + fsinfo); + } pfree(fsinfo); } @@ -746,19 +757,19 @@ AppendOnlyCompact(Relation aorel, bool AppendOnlyCompaction_IsRelationEmpty(Relation aorel) { - Relation pg_aoseg_rel; - TupleDesc pg_aoseg_dsc; - HeapTuple tuple; - HeapScanDesc aoscan; - int Anum_tupcount; - bool empty = true; + Relation pg_aoseg_rel; + TupleDesc pg_aoseg_dsc; + HeapTuple tuple; + HeapScanDesc aoscan; + int Anum_tupcount; + bool empty = true; Assert(RelationIsAoRows(aorel) || RelationIsAoCols(aorel)); pg_aoseg_rel = heap_open(aorel->rd_appendonly->segrelid, AccessShareLock); pg_aoseg_dsc = RelationGetDescr(pg_aoseg_rel); aoscan = heap_beginscan(pg_aoseg_rel, SnapshotNow, 0, NULL); - Anum_tupcount = RelationIsAoRows(aorel)? Anum_pg_aoseg_tupcount: Anum_pg_aocs_tupcount; + Anum_tupcount = RelationIsAoRows(aorel) ? Anum_pg_aoseg_tupcount : Anum_pg_aocs_tupcount; while ((tuple = heap_getnext(aoscan, ForwardScanDirection)) != NULL && empty) { diff --git a/src/backend/access/appendonly/appendonly_visimap.c b/src/backend/access/appendonly/appendonly_visimap.c index 5f3914ade1..6cdd8efdc5 100644 --- a/src/backend/access/appendonly/appendonly_visimap.c +++ b/src/backend/access/appendonly/appendonly_visimap.c @@ -29,19 +29,19 @@ typedef struct AppendOnlyVisiMapDeleteKey /* * Segno of the dirty visimap entry. * - * MPP-23546: Changed the type of segno from int to uint64. With - * uint (4-bytes), additional 4-bytes were being used for padding. - * The padding bits may differ for two keys causing two otherwise - * equal objects to be treated as unequal by hash functions. - * Keeping type to uint64 does not change the value of - * sizeof(AppendOnlyVisiMapDeleteKey) but eliminates padding. + * MPP-23546: Changed the type of segno from int to uint64. With uint + * (4-bytes), additional 4-bytes were being used for padding. The padding + * bits may differ for two keys causing two otherwise equal objects to be + * treated as unequal by hash functions. Keeping type to uint64 does not + * change the value of sizeof(AppendOnlyVisiMapDeleteKey) but eliminates + * padding. */ - uint64 segno; + uint64 segno; /* * First row num of the dirty visimap entry. */ - uint64 firstRowNum; + uint64 firstRowNum; } AppendOnlyVisiMapDeleteKey; /* @@ -55,10 +55,10 @@ typedef struct AppendOnlyVisiMapDeleteData AppendOnlyVisiMapDeleteKey key; /* - * Offset of the latest dirty version of the visimap bitmap in - * the spill file. + * Offset of the latest dirty version of the visimap bitmap in the spill + * file. */ - uint64 workFileOffset; + uint64 workFileOffset; /* * Tuple id of the visimap entry if the visimap entry existed before. @@ -69,21 +69,21 @@ typedef struct AppendOnlyVisiMapDeleteData static void AppendOnlyVisimap_Store( - AppendOnlyVisimap *visiMap); + AppendOnlyVisimap *visiMap); static void AppendOnlyVisimap_Find( - AppendOnlyVisimap *visiMap, - AOTupleId *tupleId); + AppendOnlyVisimap *visiMap, + AOTupleId *tupleId); /* * Finishes the visimap operations. * No other function should be called with the given * visibility map after this function has been called. - */ + */ void AppendOnlyVisimap_Finish( - AppendOnlyVisimap *visiMap, - LOCKMODE lockmode) + AppendOnlyVisimap *visiMap, + LOCKMODE lockmode) { if (AppendOnlyVisimapEntry_HasChanged(&visiMap->visimapEntry)) { @@ -102,14 +102,14 @@ AppendOnlyVisimap_Finish( * * It assumes a zero-allocated visibility map. * Should not be called twice. - */ + */ void AppendOnlyVisimap_Init( - AppendOnlyVisimap *visiMap, - Oid visimapRelid, - Oid visimapIdxid, - LOCKMODE lockmode, - Snapshot appendOnlyMetaDataSnapshot) + AppendOnlyVisimap *visiMap, + Oid visimapRelid, + Oid visimapIdxid, + LOCKMODE lockmode, + Snapshot appendOnlyMetaDataSnapshot) { MemoryContext oldContext; @@ -118,24 +118,24 @@ AppendOnlyVisimap_Init( Assert(OidIsValid(visimapIdxid)); visiMap->memoryContext = AllocSetContextCreate( - CurrentMemoryContext, - "VisiMapContext", - ALLOCSET_DEFAULT_MINSIZE, - ALLOCSET_DEFAULT_INITSIZE, - ALLOCSET_DEFAULT_MAXSIZE); + CurrentMemoryContext, + "VisiMapContext", + ALLOCSET_DEFAULT_MINSIZE, + ALLOCSET_DEFAULT_INITSIZE, + ALLOCSET_DEFAULT_MAXSIZE); oldContext = MemoryContextSwitchTo( - visiMap->memoryContext); - + visiMap->memoryContext); + AppendOnlyVisimapEntry_Init(&visiMap->visimapEntry, - visiMap->memoryContext); + visiMap->memoryContext); AppendOnlyVisimapStore_Init(&visiMap->visimapStore, - visimapRelid, - visimapIdxid, - lockmode, - appendOnlyMetaDataSnapshot, - visiMap->memoryContext); + visimapRelid, + visimapIdxid, + lockmode, + appendOnlyMetaDataSnapshot, + visiMap->memoryContext); MemoryContextSwitchTo(oldContext); } @@ -149,29 +149,29 @@ AppendOnlyVisimap_Init( * stored. * Should not be called when the append-only table has no relation * Assumes that the visibility has been initialized and not finished. - */ + */ static void AppendOnlyVisimap_Find( - AppendOnlyVisimap *visiMap, - AOTupleId *aoTupleId) + AppendOnlyVisimap *visiMap, + AOTupleId *aoTupleId) { Assert(visiMap); Assert(aoTupleId); - - elogif (Debug_appendonly_print_visimap, LOG, - "Append-only visi map: Find entry for " - "(tupleId) = %s", - AOTupleIdToString(aoTupleId)); + + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map: Find entry for " + "(tupleId) = %s", + AOTupleIdToString(aoTupleId)); if (!AppendOnlyVisimapStore_Find(&visiMap->visimapStore, - AOTupleIdGet_segmentFileNum(aoTupleId), - AppendOnlyVisimapEntry_GetFirstRowNum( - &visiMap->visimapEntry, aoTupleId), - &visiMap->visimapEntry)) + AOTupleIdGet_segmentFileNum(aoTupleId), + AppendOnlyVisimapEntry_GetFirstRowNum( + &visiMap->visimapEntry, aoTupleId), + &visiMap->visimapEntry)) { /* * There is no entry that covers the given tuple id. - */ + */ AppendOnlyVisimapEntry_New(&visiMap->visimapEntry, aoTupleId); } } @@ -182,21 +182,21 @@ AppendOnlyVisimap_Find( * a tuple to be visible to the user. * * Assumes that the visibility has been initialized and not finished. - */ + */ bool AppendOnlyVisimap_IsVisible( - AppendOnlyVisimap *visiMap, - AOTupleId *aoTupleId) + AppendOnlyVisimap *visiMap, + AOTupleId *aoTupleId) { Assert(visiMap); - - elogif (Debug_appendonly_print_visimap, LOG, - "Append-only visi map: Visibility check: " - "(tupleId) = %s", - AOTupleIdToString(aoTupleId)); + + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map: Visibility check: " + "(tupleId) = %s", + AOTupleIdToString(aoTupleId)); if (!AppendOnlyVisimapEntry_CoversTuple(&visiMap->visimapEntry, - aoTupleId)) + aoTupleId)) { /* if necessary persist the current entry before moving. */ if (AppendOnlyVisimapEntry_HasChanged(&visiMap->visimapEntry)) @@ -206,10 +206,10 @@ AppendOnlyVisimap_IsVisible( AppendOnlyVisimap_Find(visiMap, aoTupleId); } - + /* visimap entry is now positioned to cover the aoTupleId */ return AppendOnlyVisimapEntry_IsVisible(&visiMap->visimapEntry, - aoTupleId); + aoTupleId); } /* @@ -222,33 +222,33 @@ AppendOnlyVisimap_IsVisible( * that is usually wasteful. * * Assumes that the visibility has been initialized and not finished. - */ + */ void AppendOnlyVisimap_Store( - AppendOnlyVisimap *visiMap) + AppendOnlyVisimap *visiMap) { Assert(visiMap); Assert(AppendOnlyVisimapEntry_IsValid(&visiMap->visimapEntry)); AppendOnlyVisimapStore_Store(&visiMap->visimapStore, &visiMap->visimapEntry); - + } /* * Deletes all visibility information for the given segment file. - */ + */ void AppendOnlyVisimap_DeleteSegmentFile( - AppendOnlyVisimap *visiMap, - int segno) + AppendOnlyVisimap *visiMap, + int segno) { Assert(visiMap); elogif(Debug_appendonly_print_visimap, LOG, - "Delete visimap for segment file %d", segno); + "Delete visimap for segment file %d", segno); AppendOnlyVisimapStore_DeleteSegmentFile(&visiMap->visimapStore, - segno); + segno); } /* @@ -256,11 +256,11 @@ AppendOnlyVisimap_DeleteSegmentFile( */ int64 AppendOnlyVisimap_GetRelationHiddenTupleCount( - AppendOnlyVisimap *visiMap) + AppendOnlyVisimap *visiMap) { Assert(visiMap); return AppendOnlyVisimapStore_GetRelationHiddenTupleCount( - &visiMap->visimapStore, &visiMap->visimapEntry); + &visiMap->visimapStore, &visiMap->visimapEntry); } /* @@ -268,36 +268,36 @@ AppendOnlyVisimap_GetRelationHiddenTupleCount( */ int64 AppendOnlyVisimap_GetSegmentFileHiddenTupleCount( - AppendOnlyVisimap *visiMap, - int segno) + AppendOnlyVisimap *visiMap, + int segno) { Assert(visiMap); return AppendOnlyVisimapStore_GetSegmentFileHiddenTupleCount( - &visiMap->visimapStore, &visiMap->visimapEntry, segno); + &visiMap->visimapStore, &visiMap->visimapEntry, segno); } /* * Starts a new scan for invisible tuple ids. - */ + */ void AppendOnlyVisimapScan_Init( - AppendOnlyVisimapScan* visiMapScan, - Oid visimapRelid, - Oid visimapIdxid, - LOCKMODE lockmode, - Snapshot appendonlyMetadataSnapshot) + AppendOnlyVisimapScan *visiMapScan, + Oid visimapRelid, + Oid visimapIdxid, + LOCKMODE lockmode, + Snapshot appendonlyMetadataSnapshot) { Assert(visiMapScan); Assert(OidIsValid(visimapRelid)); Assert(OidIsValid(visimapIdxid)); AppendOnlyVisimap_Init(&visiMapScan->visimap, visimapRelid, visimapIdxid, - lockmode, - appendonlyMetadataSnapshot); + lockmode, + appendonlyMetadataSnapshot); visiMapScan->indexScan = AppendOnlyVisimapStore_BeginScan( - &visiMapScan->visimap.visimapStore, - 0, - NULL); + &visiMapScan->visimap.visimapStore, + 0, + NULL); visiMapScan->isFinished = false; } @@ -307,53 +307,52 @@ AppendOnlyVisimapScan_Init( * If there was a previous successful call to this function during this can, * the tupleId parameter should contain the value of the last call. * The contents of tupleId is undefined if false is returned. - */ + */ bool AppendOnlyVisimapScan_GetNextInvisible( - AppendOnlyVisimapScan *visiMapScan, - AOTupleId *tupleId) + AppendOnlyVisimapScan *visiMapScan, + AOTupleId *tupleId) { - bool found; + bool found; Assert(visiMapScan); Assert(tupleId); Assert(!visiMapScan->isFinished); - found=false; + found = false; while (!found && !visiMapScan->isFinished) { if (!AppendOnlyVisimapEntry_IsValid( - &visiMapScan->visimap.visimapEntry)) + &visiMapScan->visimap.visimapEntry)) { if (!AppendOnlyVisimapStore_GetNext( - &visiMapScan->visimap.visimapStore, - visiMapScan->indexScan, - ForwardScanDirection, - &visiMapScan->visimap.visimapEntry, - NULL)) + &visiMapScan->visimap.visimapStore, + visiMapScan->indexScan, + ForwardScanDirection, + &visiMapScan->visimap.visimapEntry, + NULL)) { - visiMapScan->isFinished=true; + visiMapScan->isFinished = true; return false; } AOTupleIdInit_Init(tupleId); } if (!AppendOnlyVisimapEntry_GetNextInvisible( - &visiMapScan->visimap.visimapEntry, - tupleId)) + &visiMapScan->visimap.visimapEntry, + tupleId)) { /* - * no more invisible tuples in this visimap entry. - * Try next one + * no more invisible tuples in this visimap entry. Try next one */ AppendOnlyVisimapEntry_Reset(&visiMapScan->visimap.visimapEntry); - } + } else { /* Found a tuple. The tuple is is already in the out parameter. */ found = true; } - + } return true; @@ -361,15 +360,15 @@ AppendOnlyVisimapScan_GetNextInvisible( /* * Finishes a visimap scan. - */ + */ void AppendOnlyVisimapScan_Finish( - AppendOnlyVisimapScan *visiMapScan, - LOCKMODE lockmode) + AppendOnlyVisimapScan *visiMapScan, + LOCKMODE lockmode) { AppendOnlyVisimapStore_EndScan( - &visiMapScan->visimap.visimapStore, - visiMapScan->indexScan); + &visiMapScan->visimap.visimapStore, + visiMapScan->indexScan); AppendOnlyVisimap_Finish(&visiMapScan->visimap, lockmode); } @@ -396,6 +395,7 @@ hash_compare_keys(const void *key1, const void *key2, Size keysize) Assert(keysize == sizeof(AppendOnlyVisiMapDeleteKey)); AppendOnlyVisiMapDeleteKey *k1 = (AppendOnlyVisiMapDeleteKey *) key1; AppendOnlyVisiMapDeleteKey *k2 = (AppendOnlyVisiMapDeleteKey *) key2; + if ((k1->segno == k2->segno) && (k1->firstRowNum == k2->firstRowNum)) { return 0; @@ -405,17 +405,17 @@ hash_compare_keys(const void *key1, const void *key2, Size keysize) /* * Inits the visimap delete helper structure. - * + * * This prepares the hash table and opens the temporary file. */ void AppendOnlyVisimapDelete_Init( - AppendOnlyVisimapDelete *visiMapDelete, - AppendOnlyVisimap *visiMap) + AppendOnlyVisimapDelete *visiMapDelete, + AppendOnlyVisimap *visiMap) { HASHCTL hash_ctl; - char fileName[MAXPGPATH]; - int len; + char fileName[MAXPGPATH]; + int len; Assert(visiMapDelete); Assert(visiMap); @@ -429,9 +429,9 @@ AppendOnlyVisimapDelete_Init( hash_ctl.match = hash_compare_keys; hash_ctl.hcxt = visiMap->memoryContext; visiMapDelete->dirtyEntryCache = hash_create("VisimapEntryCache", - 4, /* start small and extend */ - &hash_ctl, - HASH_ELEM | HASH_FUNCTION | HASH_COMPARE); + 4, /* start small and extend */ + &hash_ctl, + HASH_ELEM | HASH_FUNCTION | HASH_COMPARE); len = snprintf(fileName, sizeof(fileName), "%s/visimap_delete", PG_TEMP_FILES_DIR); if (len > MAXPGPATH - 1) @@ -449,21 +449,21 @@ static void AppendOnlyVisimapDelete_RebuildEntry(AppendOnlyVisimapEntry *visimapEntry, int segno, int64 firstRowNum, ItemPointer tid) { MemoryContext oldContext; - size_t dataSize; + size_t dataSize; - visimapEntry->segmentFileNum =segno; + visimapEntry->segmentFileNum = segno; visimapEntry->firstRowNum = firstRowNum; - dataSize = VARSIZE(visimapEntry->data) - - offsetof(AppendOnlyVisimapData, data); + dataSize = VARSIZE(visimapEntry->data) - + offsetof(AppendOnlyVisimapData, data); oldContext = MemoryContextSwitchTo(visimapEntry->memoryContext); AppendOnlyVisiMapEnty_ReadData(visimapEntry, dataSize); MemoryContextSwitchTo(oldContext); - /* - * We only stash away a visimap entry when it is dirty. Thus, we mark - * the visimap entry again as dirty during unstash + /* + * We only stash away a visimap entry when it is dirty. Thus, we mark the + * visimap entry again as dirty during unstash */ visimapEntry->dirty = true; memcpy(&visimapEntry->tupleTid, tid, sizeof(ItemPointerData)); @@ -474,34 +474,35 @@ AppendOnlyVisimapDelete_RebuildEntry(AppendOnlyVisimapEntry *visimapEntry, int s */ static void AppendOnlyVisimapDelete_Unstash( - AppendOnlyVisimapDelete *visiMapDelete, int segno, int64 firstRowNum, AppendOnlyVisiMapDeleteData *deleteData) + AppendOnlyVisimapDelete *visiMapDelete, int segno, int64 firstRowNum, AppendOnlyVisiMapDeleteData *deleteData) { AppendOnlyVisimap *visiMap; - uint64 len, dataLen; + uint64 len, + dataLen; AppendOnlyVisiMapDeleteKey key; Assert(visiMapDelete); visiMap = visiMapDelete->visiMap; - elogif (Debug_appendonly_print_visimap, LOG, - "Append-only visi map delete: Unstash dirty visimap entry %d/" INT64_FORMAT - ", offset " INT64_FORMAT, - segno, firstRowNum, deleteData->workFileOffset); + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map delete: Unstash dirty visimap entry %d/" INT64_FORMAT + ", offset " INT64_FORMAT, + segno, firstRowNum, deleteData->workFileOffset); if (ExecWorkFile_Seek(visiMapDelete->workfile, deleteData->workFileOffset, SEEK_SET) != 0) { elog(ERROR, "Failed to seek to visimap delete spill location: %d/" INT64_FORMAT - ", offset " INT64_FORMAT, - segno, firstRowNum, deleteData->workFileOffset); + ", offset " INT64_FORMAT, + segno, firstRowNum, deleteData->workFileOffset); } len = ExecWorkFile_Read(visiMapDelete->workfile, &key, sizeof(key)); if (len != sizeof(key)) { elog(ERROR, "Failed to read visimap delete spill data: %d/" INT64_FORMAT - ", offset " INT64_FORMAT, - segno, firstRowNum, deleteData->workFileOffset); + ", offset " INT64_FORMAT, + segno, firstRowNum, deleteData->workFileOffset); } Assert(key.segno == segno); Assert(key.firstRowNum == firstRowNum); @@ -514,19 +515,19 @@ AppendOnlyVisimapDelete_Unstash( dataLen = VARSIZE(visiMap->visimapEntry.data); /* Now read the remaining part of the entry */ - len = ExecWorkFile_Read(visiMapDelete->workfile, - ((char *)visiMap->visimapEntry.data) + 4, dataLen - 4); + len = ExecWorkFile_Read(visiMapDelete->workfile, + ((char *) visiMap->visimapEntry.data) + 4, dataLen - 4); if (len != dataLen - 4) { elog(ERROR, "Failed to read visimap delete spill data: %d/" INT64_FORMAT - ", offset " INT64_FORMAT ", len " INT64_FORMAT, - segno, firstRowNum, deleteData->workFileOffset, dataLen); + ", offset " INT64_FORMAT ", len " INT64_FORMAT, + segno, firstRowNum, deleteData->workFileOffset, dataLen); } AppendOnlyVisimapDelete_RebuildEntry(&visiMap->visimapEntry, - segno, - firstRowNum, - &deleteData->tupleTid); + segno, + firstRowNum, + &deleteData->tupleTid); } /* @@ -539,14 +540,14 @@ AppendOnlyVisimapDelete_Unstash( * stored. * Should not be called when the append-only table has no relation * Assumes that the visibility has been initialized and not finished. - */ + */ static void AppendOnlyVisimapDelete_Find( - AppendOnlyVisimapDelete *visiMapDelete, - AOTupleId *aoTupleId) + AppendOnlyVisimapDelete *visiMapDelete, + AOTupleId *aoTupleId) { AppendOnlyVisimap *visiMap; - uint64 firstRowNum; + uint64 firstRowNum; AppendOnlyVisiMapDeleteData *r; AppendOnlyVisiMapDeleteKey key; @@ -556,27 +557,28 @@ AppendOnlyVisimapDelete_Find( visiMap = visiMapDelete->visiMap; firstRowNum = AppendOnlyVisimapEntry_GetFirstRowNum( - &visiMap->visimapEntry, aoTupleId); + &visiMap->visimapEntry, aoTupleId); key.segno = AOTupleIdGet_segmentFileNum(aoTupleId); key.firstRowNum = firstRowNum; - elogif (Debug_appendonly_print_visimap, LOG, - "Append-only visi map delete: Search dirty visimap entry " - INT64_FORMAT "/" INT64_FORMAT, key.segno, key.firstRowNum); + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map delete: Search dirty visimap entry " + INT64_FORMAT "/" INT64_FORMAT, key.segno, key.firstRowNum); + + bool found = false; - bool found = false; r = hash_search(visiMapDelete->dirtyEntryCache, &key, - HASH_FIND, &found); + HASH_FIND, &found); if (found) { Assert(r); - elogif (Debug_appendonly_print_visimap, LOG, - "Append-only visi map delete: Found dirty visimap entry " - INT64_FORMAT "/" INT64_FORMAT, - r->key.segno, r->key.firstRowNum); + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map delete: Found dirty visimap entry " + INT64_FORMAT "/" INT64_FORMAT, + r->key.segno, r->key.firstRowNum); Assert(r->key.firstRowNum == key.firstRowNum); Assert(r->key.segno == key.segno); AppendOnlyVisimapDelete_Unstash(visiMapDelete, key.segno, key.firstRowNum, r); @@ -584,14 +586,14 @@ AppendOnlyVisimapDelete_Find( else { if (!AppendOnlyVisimapStore_Find(&visiMap->visimapStore, - AOTupleIdGet_segmentFileNum(aoTupleId), - AppendOnlyVisimapEntry_GetFirstRowNum( - &visiMap->visimapEntry, aoTupleId), - &visiMap->visimapEntry)) + AOTupleIdGet_segmentFileNum(aoTupleId), + AppendOnlyVisimapEntry_GetFirstRowNum( + &visiMap->visimapEntry, aoTupleId), + &visiMap->visimapEntry)) { /* * There is no entry that covers the given tuple id. - */ + */ AppendOnlyVisimapEntry_New(&visiMap->visimapEntry, aoTupleId); } } @@ -601,17 +603,17 @@ AppendOnlyVisimapDelete_Find( * This function stashes away a dirty visimap entry. * It stores the compression bitmap in the spill file and * sets the meta information in the hash table. - */ + */ static void AppendOnlyVisimapDelete_Stash( - AppendOnlyVisimapDelete *visiMapDelete) + AppendOnlyVisimapDelete *visiMapDelete) { AppendOnlyVisimap *visiMap; AppendOnlyVisiMapDeleteData *r; AppendOnlyVisiMapDeleteKey key; MemoryContext oldContext; - bool found; - int64 offset; + bool found; + int64 offset; Assert(visiMapDelete); visiMap = visiMapDelete->visiMap; @@ -621,7 +623,7 @@ AppendOnlyVisimapDelete_Stash( key.firstRowNum = visiMap->visimapEntry.firstRowNum; found = false; r = hash_search(visiMapDelete->dirtyEntryCache, &key, - HASH_ENTER, &found); + HASH_ENTER, &found); if (!found) { @@ -636,16 +638,16 @@ AppendOnlyVisimapDelete_Stash( offset = ExecWorkFile_GetSize(visiMapDelete->workfile); - elogif (Debug_appendonly_print_visimap, LOG, - "Append-only visi map delete: Stash dirty visimap entry %d/" INT64_FORMAT, - visiMap->visimapEntry.segmentFileNum, visiMap->visimapEntry.firstRowNum); + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map delete: Stash dirty visimap entry %d/" INT64_FORMAT, + visiMap->visimapEntry.segmentFileNum, visiMap->visimapEntry.firstRowNum); if (ExecWorkFile_Seek(visiMapDelete->workfile, offset, SEEK_SET) != 0) { elog(ERROR, "Failed to seek to visimap delete spill location: offset " INT64_FORMAT, offset); } if (!ExecWorkFile_Write(visiMapDelete->workfile, &key, - sizeof(key))) + sizeof(key))) { elog(ERROR, "Failed to write visimap delete spill key information: " "segno " INT64_FORMAT ", first row " INT64_FORMAT ", offset " @@ -653,7 +655,7 @@ AppendOnlyVisimapDelete_Stash( key.segno, key.firstRowNum, offset, sizeof(key)); } if (!ExecWorkFile_Write(visiMapDelete->workfile, visiMap->visimapEntry.data, - VARSIZE(visiMap->visimapEntry.data))) + VARSIZE(visiMap->visimapEntry.data))) { elog(ERROR, "Failed to write visimap delete spill key information: " "segno " INT64_FORMAT ", first row " INT64_FORMAT ", offset " @@ -671,7 +673,7 @@ AppendOnlyVisimapDelete_Stash( /* * Hides a given tuple id. * If the tuple is not in the current visimap range, the current - * visimap entry is stashed away and the correct one is loaded or + * visimap entry is stashed away and the correct one is loaded or * read from the spill file. * * Then, the bit of the tuple is set. @@ -684,23 +686,23 @@ AppendOnlyVisimapDelete_Stash( */ HTSU_Result AppendOnlyVisimapDelete_Hide( - AppendOnlyVisimapDelete *visiMapDelete, AOTupleId *aoTupleId) + AppendOnlyVisimapDelete *visiMapDelete, AOTupleId *aoTupleId) { AppendOnlyVisimap *visiMap; Assert(visiMapDelete); Assert(aoTupleId); - elogif (Debug_appendonly_print_visimap, LOG, - "Append-only visi map delete: Hide tuple " - "(tupleId) = %s", - AOTupleIdToString(aoTupleId)); + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map delete: Hide tuple " + "(tupleId) = %s", + AOTupleIdToString(aoTupleId)); visiMap = visiMapDelete->visiMap; Assert(visiMap); if (!AppendOnlyVisimapEntry_CoversTuple(&visiMap->visimapEntry, - aoTupleId)) + aoTupleId)) { /* if necessary persist the current entry before moving. */ if (AppendOnlyVisimapEntry_HasChanged(&visiMap->visimapEntry)) @@ -718,9 +720,11 @@ static void AppendOnlyVisimapDelete_WriteBackStashedEntries(AppendOnlyVisimapDelete *visiMapDelete) { AppendOnlyVisiMapDeleteData *deleteData; - int64 len, dataLen, currentOffset = 0; + int64 len, + dataLen, + currentOffset = 0; AppendOnlyVisimap *visiMap; - bool found; + bool found; AppendOnlyVisiMapDeleteKey key; visiMap = visiMapDelete->visiMap; @@ -741,7 +745,7 @@ AppendOnlyVisimapDelete_WriteBackStashedEntries(AppendOnlyVisimapDelete *visiMap len = ExecWorkFile_Read(visiMapDelete->workfile, &key, sizeof(key)); while (len == sizeof(key)) { - elogif(Debug_appendonly_print_visimap, LOG, + elogif(Debug_appendonly_print_visimap, LOG, "Append-only visi map delete: Got next dirty visimap: " INT64_FORMAT "/" INT64_FORMAT ", offset " INT64_FORMAT, key.segno, key.firstRowNum, currentOffset); @@ -756,23 +760,25 @@ AppendOnlyVisimapDelete_WriteBackStashedEntries(AppendOnlyVisimapDelete *visiMap Assert(dataLen <= APPENDONLY_VISIMAP_DATA_BUFFER_SIZE); /* Now read the remaining part of the entry */ - len = ExecWorkFile_Read(visiMapDelete->workfile, - ((char *)visiMap->visimapEntry.data) + 4, dataLen - 4); + len = ExecWorkFile_Read(visiMapDelete->workfile, + ((char *) visiMap->visimapEntry.data) + 4, dataLen - 4); if (len != (dataLen - 4)) { elog(ERROR, "Failed to read visimap delete spill data"); } - /* Now we search the hash entry and check if we here have the most recent - * version of the visimap entry */ + /* + * Now we search the hash entry and check if we here have the most + * recent version of the visimap entry + */ found = false; deleteData = hash_search(visiMapDelete->dirtyEntryCache, &key, - HASH_FIND, &found); + HASH_FIND, &found); if (!found) { elog(ERROR, "Found a stashed visimap entry without corresponding meta data: " - "offset " INT64_FORMAT, currentOffset); + "offset " INT64_FORMAT, currentOffset); } Assert(deleteData); Assert(deleteData->key.firstRowNum == key.firstRowNum); @@ -780,19 +786,21 @@ AppendOnlyVisimapDelete_WriteBackStashedEntries(AppendOnlyVisimapDelete *visiMap if (currentOffset != deleteData->workFileOffset) { elogif(Debug_appendonly_print_visimap, LOG, - "Append-only visi map delete: Found out-dated stashed dirty visimap: " - "current offset " INT64_FORMAT ", expected offset " INT64_FORMAT, - currentOffset, deleteData->workFileOffset); + "Append-only visi map delete: Found out-dated stashed dirty visimap: " + "current offset " INT64_FORMAT ", expected offset " INT64_FORMAT, + currentOffset, deleteData->workFileOffset); } else { - /* Until this point on the data field of the visimap entry has valid - * information. After this the visimap entry is fully rebuild. + /* + * Until this point on the data field of the visimap entry has + * valid information. After this the visimap entry is fully + * rebuild. */ AppendOnlyVisimapDelete_RebuildEntry(&visiMap->visimapEntry, - deleteData->key.segno, - deleteData->key.firstRowNum, - &deleteData->tupleTid); + deleteData->key.segno, + deleteData->key.firstRowNum, + &deleteData->tupleTid); AppendOnlyVisimap_Store(visiMapDelete->visiMap); } @@ -808,16 +816,16 @@ AppendOnlyVisimapDelete_WriteBackStashedEntries(AppendOnlyVisimapDelete *visiMap /* * Finishes the delete operation. - * All the dirty visimap entries are read from the spill file and + * All the dirty visimap entries are read from the spill file and * stored in the visimap heap table. */ void AppendOnlyVisimapDelete_Finish( - AppendOnlyVisimapDelete *visiMapDelete) + AppendOnlyVisimapDelete *visiMapDelete) { AppendOnlyVisiMapDeleteData *deleteData; AppendOnlyVisimap *visiMap; - bool found; + bool found; AppendOnlyVisiMapDeleteKey key; visiMap = visiMapDelete->visiMap; @@ -825,23 +833,22 @@ AppendOnlyVisimapDelete_Finish( elogif(Debug_appendonly_print_visimap, LOG, "Write-back all dirty visimap entries"); - /* - * Write back the current change because it is be definition the - * newest. + /* + * Write back the current change because it is be definition the newest. */ if (AppendOnlyVisimapEntry_HasChanged(&visiMap->visimapEntry)) { AppendOnlyVisimap_Store(visiMapDelete->visiMap); - /* - * Make the hash map entry invalid so that we do not overwrite the entry - * later + /* + * Make the hash map entry invalid so that we do not overwrite the + * entry later */ key.segno = visiMap->visimapEntry.segmentFileNum; key.firstRowNum = visiMap->visimapEntry.firstRowNum; found = false; deleteData = hash_search(visiMapDelete->dirtyEntryCache, &key, - HASH_FIND, &found); + HASH_FIND, &found); if (found) { diff --git a/src/backend/access/appendonly/appendonly_visimap_entry.c b/src/backend/access/appendonly/appendonly_visimap_entry.c index 70a6f08e68..4448f6143a 100644 --- a/src/backend/access/appendonly/appendonly_visimap_entry.c +++ b/src/backend/access/appendonly/appendonly_visimap_entry.c @@ -21,13 +21,13 @@ /* * Frees the data allocated by the visimap entry. - * + * * No other function should be called on the visibility map entry * after this function has been called. - */ + */ void AppendOnlyVisimapEntry_Finish( - AppendOnlyVisimapEntry *visiMapEntry) + AppendOnlyVisimapEntry *visiMapEntry) { Assert(visiMapEntry); @@ -45,14 +45,14 @@ AppendOnlyVisimapEntry_Finish( ; * Assumes a zero-allocated visimap entry data structure. * - * Until appendonly_visimap_copyout or appendonly_visimap_clear is called, + * Until appendonly_visimap_copyout or appendonly_visimap_clear is called, * the data structure * is not usable for visibility checks or updates. - */ + */ void AppendOnlyVisimapEntry_Init( - AppendOnlyVisimapEntry *visiMapEntry, - MemoryContext memoryContext) + AppendOnlyVisimapEntry *visiMapEntry, + MemoryContext memoryContext) { Assert(visiMapEntry); @@ -68,12 +68,12 @@ AppendOnlyVisimapEntry_Init( /* * Resets the visibility map data structure. * - * It puts the entry into the identical state as after a + * It puts the entry into the identical state as after a * call to AppendOnlyVisimapEntry_Init. - */ + */ void AppendOnlyVisimapEntry_Reset( - AppendOnlyVisimapEntry *visiMapEntry) + AppendOnlyVisimapEntry *visiMapEntry) { Assert(visiMapEntry); @@ -89,12 +89,12 @@ AppendOnlyVisimapEntry_Reset( * Initializes a previously unused entry that covers the given tuple id. * The tuple is not marked as updated as no state has been changed yet. * - * Note that the firstRowNum is not the rowNum of the tuple id. - */ + * Note that the firstRowNum is not the rowNum of the tuple id. + */ void AppendOnlyVisimapEntry_New( - AppendOnlyVisimapEntry* visiMapEntry, - AOTupleId *tupleId) + AppendOnlyVisimapEntry *visiMapEntry, + AOTupleId *tupleId) { Assert(visiMapEntry); Assert(tupleId); @@ -105,22 +105,22 @@ AppendOnlyVisimapEntry_New( visiMapEntry->segmentFileNum = AOTupleIdGet_segmentFileNum(tupleId); visiMapEntry->firstRowNum = AppendOnlyVisimapEntry_GetFirstRowNum(visiMapEntry, - tupleId); + tupleId); - ItemPointerSetInvalid(&visiMapEntry->tupleTid); + ItemPointerSetInvalid(&visiMapEntry->tupleTid); - elogif(Debug_appendonly_print_visimap, LOG, - "Append-only visi map entry: New entry " - "(segNum, firstRowNum) = (%u, " INT64_FORMAT ")", - visiMapEntry->segmentFileNum, - visiMapEntry->firstRowNum); + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map entry: New entry " + "(segNum, firstRowNum) = (%u, " INT64_FORMAT ")", + visiMapEntry->segmentFileNum, + visiMapEntry->firstRowNum); } static Datum AppendOnlyVisimap_GetAttrNotNull(HeapTuple t, TupleDesc td, int attr) { - Datum d; - bool isNull; + Datum d; + bool isNull; d = fastgetattr(t, attr, td, &isNull); if (isNull) @@ -132,17 +132,18 @@ AppendOnlyVisimap_GetAttrNotNull(HeapTuple t, TupleDesc td, int attr) void AppendOnlyVisiMapEnty_ReadData( - AppendOnlyVisimapEntry *visiMapEntry, size_t dataSize) + AppendOnlyVisimapEntry *visiMapEntry, size_t dataSize) { - int newWordCount; + int newWordCount; Assert(visiMapEntry); Assert(CurrentMemoryContext == visiMapEntry->memoryContext); BitmapDecompressState decompressState; + BitmapDecompress_Init(&decompressState, - visiMapEntry->data->data, - dataSize); + visiMapEntry->data->data, + dataSize); if (BitmapDecompress_HasError(&decompressState)) { @@ -151,17 +152,17 @@ AppendOnlyVisiMapEnty_ReadData( bms_free(visiMapEntry->bitmap); - newWordCount = + newWordCount = BitmapDecompress_GetBlockCount(&decompressState); if (newWordCount > 0) { visiMapEntry->bitmap = palloc0( - offsetof(Bitmapset, words) + (newWordCount * sizeof(bitmapword))); + offsetof(Bitmapset, words) + (newWordCount * sizeof(bitmapword))); visiMapEntry->bitmap->nwords = newWordCount; BitmapDecompress_Decompress( - &decompressState, - visiMapEntry->bitmap->words, - newWordCount); + &decompressState, + visiMapEntry->bitmap->words, + newWordCount); } else if (newWordCount == 0) { @@ -169,8 +170,8 @@ AppendOnlyVisiMapEnty_ReadData( } else { - elog(ERROR, - "illegal visimap block count: visimap block count %d", newWordCount); + elog(ERROR, + "illegal visimap block count: visimap block count %d", newWordCount); } } @@ -181,37 +182,37 @@ AppendOnlyVisiMapEnty_ReadData( * Should only be called with values and nulls provides * by a successful read from the aovisimap table using * an AppendOnlyVisimapIndex data structure. - */ + */ void AppendOnlyVisimapEntry_Copyout( - AppendOnlyVisimapEntry *visiMapEntry, - HeapTuple tuple, - TupleDesc tupleDesc) + AppendOnlyVisimapEntry *visiMapEntry, + HeapTuple tuple, + TupleDesc tupleDesc) { struct varlena *value; struct varlena *detoast_value; - MemoryContext oldContext; - size_t dataSize; - Datum d; - bool isNull; + MemoryContext oldContext; + size_t dataSize; + Datum d; + bool isNull; Assert(visiMapEntry); Assert(!visiMapEntry->dirty); Assert(tuple); Assert(tupleDesc); - Assert(!visiMapEntry->dirty); /* entry should not contain dirty data */ + Assert(!visiMapEntry->dirty); /* entry should not contain dirty data */ d = AppendOnlyVisimap_GetAttrNotNull(tuple, tupleDesc, Anum_pg_aovisimap_segno); visiMapEntry->segmentFileNum = DatumGetInt64(d); - + d = AppendOnlyVisimap_GetAttrNotNull(tuple, tupleDesc, Anum_pg_aovisimap_firstrownum); visiMapEntry->firstRowNum = DatumGetInt64(d); - elogif (Debug_appendonly_print_visimap, LOG, - "Append-only visi map entry: copy out: " - "segNo %u firstRowNum " INT64_FORMAT, - visiMapEntry->segmentFileNum, - visiMapEntry->firstRowNum); + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map entry: copy out: " + "segNo %u firstRowNum " INT64_FORMAT, + visiMapEntry->segmentFileNum, + visiMapEntry->firstRowNum); d = fastgetattr(tuple, Anum_pg_aovisimap_visimap, tupleDesc, &isNull); if (isNull) @@ -224,7 +225,7 @@ AppendOnlyVisimapEntry_Copyout( } else { - value = (struct varlena*)DatumGetPointer(d); + value = (struct varlena *) DatumGetPointer(d); detoast_value = pg_detoast_datum(value); oldContext = MemoryContextSwitchTo(visiMapEntry->memoryContext); @@ -233,14 +234,15 @@ AppendOnlyVisimapEntry_Copyout( Assert(visiMapEntry->data); Assert(APPENDONLY_VISIMAP_DATA_BUFFER_SIZE >= VARSIZE(detoast_value)); memcpy(visiMapEntry->data, detoast_value, VARSIZE(detoast_value)); - - dataSize = VARSIZE(detoast_value) - + + dataSize = VARSIZE(detoast_value) - offsetof(AppendOnlyVisimapData, data); AppendOnlyVisiMapEnty_ReadData(visiMapEntry, dataSize); MemoryContextSwitchTo(oldContext); - if (detoast_value != value) { + if (detoast_value != value) + { pfree(detoast_value); detoast_value = NULL; } @@ -250,10 +252,10 @@ AppendOnlyVisimapEntry_Copyout( /* * Returns the hidden tuple count value from a visimap entry heap tuple. * - */ + */ int64 AppendOnlyVisimapEntry_GetHiddenTupleCount( - AppendOnlyVisimapEntry *visiMapEntry) + AppendOnlyVisimapEntry *visiMapEntry) { Assert(visiMapEntry); @@ -262,15 +264,16 @@ AppendOnlyVisimapEntry_GetHiddenTupleCount( void AppendOnlyVisimapEntry_WriteData( - AppendOnlyVisimapEntry *visiMapEntry) + AppendOnlyVisimapEntry *visiMapEntry) { - int bitmapSize, compressedBitmapSize; + int bitmapSize, + compressedBitmapSize; Assert(visiMapEntry); Assert(CurrentMemoryContext == visiMapEntry->memoryContext); Assert(AppendOnlyVisimapEntry_IsValid(visiMapEntry)); - bitmapSize = (visiMapEntry->bitmap ? (visiMapEntry->bitmap->nwords * sizeof(uint32)): 0); + bitmapSize = (visiMapEntry->bitmap ? (visiMapEntry->bitmap->nwords * sizeof(uint32)) : 0); bitmapSize += BITMAP_COMPRESSION_HEADER_SIZE; Assert(visiMapEntry->data); @@ -278,16 +281,17 @@ AppendOnlyVisimapEntry_WriteData( visiMapEntry->data->version = 1; compressedBitmapSize = Bitmap_Compress( - BITMAP_COMPRESSION_TYPE_DEFAULT, - (visiMapEntry->bitmap ? visiMapEntry->bitmap->words : NULL), - (visiMapEntry->bitmap ? visiMapEntry->bitmap->nwords : 0), - visiMapEntry->data->data, - bitmapSize); + BITMAP_COMPRESSION_TYPE_DEFAULT, + (visiMapEntry->bitmap ? visiMapEntry->bitmap->words : NULL), + (visiMapEntry->bitmap ? visiMapEntry->bitmap->nwords : 0), + visiMapEntry->data->data, + bitmapSize); Assert(compressedBitmapSize >= BITMAP_COMPRESSION_HEADER_SIZE); SET_VARSIZE(visiMapEntry->data, - offsetof(AppendOnlyVisimapData, data) + compressedBitmapSize); + offsetof(AppendOnlyVisimapData, data) + compressedBitmapSize); } + /** * Persist the entry information to heap tuple value/nulls. * Should only be called after a call to AppendOnlyVisimapEntry_copyout @@ -295,12 +299,12 @@ AppendOnlyVisimapEntry_WriteData( * * May be called when visimap entry is not updated. However, that is usually * wasteful. - */ + */ void AppendOnlyVisimapEntry_Write( - AppendOnlyVisimapEntry *visiMapEntry, - Datum* values, - bool* nulls) + AppendOnlyVisimapEntry *visiMapEntry, + Datum *values, + bool *nulls) { MemoryContext oldContext; @@ -309,10 +313,10 @@ AppendOnlyVisimapEntry_Write( Assert(nulls); Assert(AppendOnlyVisimapEntry_IsValid(visiMapEntry)); - elogif (Debug_appendonly_print_visimap, LOG, - "Append-only visi map entry: write (segno, firstRowNum) = " - "(%d, " INT64_FORMAT ")", - visiMapEntry->segmentFileNum, visiMapEntry->firstRowNum); + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map entry: write (segno, firstRowNum) = " + "(%d, " INT64_FORMAT ")", + visiMapEntry->segmentFileNum, visiMapEntry->firstRowNum); values[Anum_pg_aovisimap_segno - 1] = Int32GetDatum(visiMapEntry->segmentFileNum); nulls[Anum_pg_aovisimap_segno - 1] = false; @@ -339,10 +343,10 @@ AppendOnlyVisimapEntry_Write( /* * Returns true iff all entries in the visimap entry are visible. - */ + */ static bool AppendOnlyVisimapEntry_AreAllVisible( - AppendOnlyVisimapEntry *visiMapEntry) + AppendOnlyVisimapEntry *visiMapEntry) { Assert(visiMapEntry); Assert(AppendOnlyVisimapEntry_IsValid(visiMapEntry)); @@ -355,12 +359,12 @@ AppendOnlyVisimapEntry_AreAllVisible( * visibility map entry). * * Assumes that the current visibility map entry covers the row number. - */ + */ static void AppendOnlyVisimapEntry_GetRownumOffset( - AppendOnlyVisimapEntry *visiMapEntry, - int64 rowNum, - int64* rowNumOffset) + AppendOnlyVisimapEntry *visiMapEntry, + int64 rowNum, + int64 *rowNumOffset) { Assert(visiMapEntry); Assert(rowNum >= 0); @@ -383,10 +387,10 @@ AppendOnlyVisimapEntry_GetRownumOffset( */ bool AppendOnlyVisimapEntry_CoversTuple( - AppendOnlyVisimapEntry *visiMapEntry, - AOTupleId *tupleId) + AppendOnlyVisimapEntry *visiMapEntry, + AOTupleId *tupleId) { - int rowNum; + int rowNum; Assert(visiMapEntry); Assert(tupleId); @@ -395,27 +399,27 @@ AppendOnlyVisimapEntry_CoversTuple( { return false; } - if (visiMapEntry->segmentFileNum != - AOTupleIdGet_segmentFileNum(tupleId)) + if (visiMapEntry->segmentFileNum != + AOTupleIdGet_segmentFileNum(tupleId)) { return false; } rowNum = AOTupleIdGet_rowNum(tupleId); return (visiMapEntry->firstRowNum <= rowNum) - && ((visiMapEntry->firstRowNum + APPENDONLY_VISIMAP_MAX_RANGE) > rowNum); + && ((visiMapEntry->firstRowNum + APPENDONLY_VISIMAP_MAX_RANGE) > rowNum); } /* * Returns the matching first row number of a given * AO tuple id. - */ + */ int64 AppendOnlyVisimapEntry_GetFirstRowNum( - AppendOnlyVisimapEntry *visiMapEntry, - AOTupleId *tupleId) + AppendOnlyVisimapEntry *visiMapEntry, + AOTupleId *tupleId) { - (void)visiMapEntry; - int rowNum; + (void) visiMapEntry; + int rowNum; rowNum = AOTupleIdGet_rowNum(tupleId); return (rowNum / APPENDONLY_VISIMAP_MAX_RANGE) * APPENDONLY_VISIMAP_MAX_RANGE; @@ -431,14 +435,15 @@ AppendOnlyVisimapEntry_GetFirstRowNum( * The final visibility also depends on other information, e.g. if the * original transaction has been aborted. Such information is * not stored in the visimap. - */ + */ bool AppendOnlyVisimapEntry_IsVisible( - AppendOnlyVisimapEntry *visiMapEntry, - AOTupleId *tupleId) + AppendOnlyVisimapEntry *visiMapEntry, + AOTupleId *tupleId) { - int64 rowNum, rowNumOffset; - bool visibilityBit; + int64 rowNum, + rowNumOffset; + bool visibilityBit; Assert(visiMapEntry); Assert(AppendOnlyVisimapEntry_IsValid(visiMapEntry)); @@ -446,44 +451,44 @@ AppendOnlyVisimapEntry_IsVisible( rowNum = AOTupleIdGet_rowNum(tupleId); - elogif(Debug_appendonly_print_visimap, LOG, - "Append-only visi map entry: Check row visibility: " - "firstRowNum " INT64_FORMAT ", rowNum " INT64_FORMAT, - visiMapEntry->firstRowNum, rowNum); + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map entry: Check row visibility: " + "firstRowNum " INT64_FORMAT ", rowNum " INT64_FORMAT, + visiMapEntry->firstRowNum, rowNum); if (AppendOnlyVisimapEntry_AreAllVisible(visiMapEntry)) { - elogif(Debug_appendonly_print_visimap, LOG, - "Append-only visi map entry: All entries are visibile: " - "(firstRowNum, rowNum) = (" INT64_FORMAT ", " INT64_FORMAT ")", - visiMapEntry->firstRowNum, rowNum); + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map entry: All entries are visibile: " + "(firstRowNum, rowNum) = (" INT64_FORMAT ", " INT64_FORMAT ")", + visiMapEntry->firstRowNum, rowNum); return true; } Assert(rowNum >= visiMapEntry->firstRowNum); - + rowNumOffset = 0; AppendOnlyVisimapEntry_GetRownumOffset(visiMapEntry, - rowNum, &rowNumOffset); + rowNum, &rowNumOffset); visibilityBit = !bms_is_member(rowNumOffset, - visiMapEntry->bitmap); + visiMapEntry->bitmap); - elogif(Debug_appendonly_print_visimap, LOG, - "Append-only visi map entry: (firstRowNum, rowNum, visible) = " - "(" INT64_FORMAT ", " INT64_FORMAT ", %d)", - visiMapEntry->firstRowNum, rowNum, (int)visibilityBit); + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map entry: (firstRowNum, rowNum, visible) = " + "(" INT64_FORMAT ", " INT64_FORMAT ", %d)", + visiMapEntry->firstRowNum, rowNum, (int) visibilityBit); - return visibilityBit; + return visibilityBit; } /* * The minimal size (in uint32's elements) the entry array needs to have to * cover the given offset - */ + */ static uint32 AppendOnlyVisimapEntry_GetMinimalSizeToCover(int64 offset) { - uint32 minSize; + uint32 minSize; Assert(offset >= 0); @@ -507,13 +512,14 @@ AppendOnlyVisimapEntry_GetMinimalSizeToCover(int64 offset) * * This function is only modifying the bitmap. The caller needs to take * care that change is persisted. - */ -HTSU_Result + */ +HTSU_Result AppendOnlyVisimapEntry_HideTuple( - AppendOnlyVisimapEntry *visiMapEntry, - AOTupleId *tupleId) + AppendOnlyVisimapEntry *visiMapEntry, + AOTupleId *tupleId) { - int64 rowNum, rowNumOffset; + int64 rowNum, + rowNumOffset; MemoryContext oldContext; HTSU_Result result; @@ -521,33 +527,35 @@ AppendOnlyVisimapEntry_HideTuple( Assert(tupleId); Assert(AppendOnlyVisimapEntry_IsValid(visiMapEntry)); Assert(AppendOnlyVisimapEntry_CoversTuple(visiMapEntry, tupleId)); - + rowNum = AOTupleIdGet_rowNum(tupleId); - elogif (Debug_appendonly_print_visimap, LOG, - "Append-only visi map entry: Hide tuple: " - "firstRowNum " INT64_FORMAT ", rowNum " INT64_FORMAT, - visiMapEntry->firstRowNum, rowNum); + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map entry: Hide tuple: " + "firstRowNum " INT64_FORMAT ", rowNum " INT64_FORMAT, + visiMapEntry->firstRowNum, rowNum); rowNumOffset = 0; AppendOnlyVisimapEntry_GetRownumOffset(visiMapEntry, - rowNum, - &rowNumOffset); + rowNum, + &rowNumOffset); oldContext = MemoryContextSwitchTo(visiMapEntry->memoryContext); - /* enlarge the bitmap by a power of two. - * this avoids the O(n*n) resizing policy of the original bitmap set */ + /* + * enlarge the bitmap by a power of two. this avoids the O(n*n) resizing + * policy of the original bitmap set + */ if (!bms_covers_member(visiMapEntry->bitmap, rowNumOffset)) - visiMapEntry->bitmap = - bms_resize(visiMapEntry->bitmap, - AppendOnlyVisimapEntry_GetMinimalSizeToCover(rowNumOffset)); + visiMapEntry->bitmap = + bms_resize(visiMapEntry->bitmap, + AppendOnlyVisimapEntry_GetMinimalSizeToCover(rowNumOffset)); if (!bms_is_member(rowNumOffset, visiMapEntry->bitmap)) { visiMapEntry->bitmap = bms_add_member(visiMapEntry->bitmap, rowNumOffset); result = HeapTupleMayBeUpdated; - } + } else if (visiMapEntry->dirty) { /* The bit was already set and it was this command */ @@ -567,10 +575,10 @@ AppendOnlyVisimapEntry_HideTuple( /** * Returns true iff the visi map entry needs to be persisted. - */ -bool + */ +bool AppendOnlyVisimapEntry_HasChanged( - AppendOnlyVisimapEntry *visiMapEntry) + AppendOnlyVisimapEntry *visiMapEntry) { Assert(visiMapEntry); return visiMapEntry->dirty; @@ -580,9 +588,9 @@ AppendOnlyVisimapEntry_HasChanged( * Returns true iff the entry contains valid data. * That is either CopyOut or New has been called. */ -bool +bool AppendOnlyVisimapEntry_IsValid( - AppendOnlyVisimapEntry *visiMapEntry) + AppendOnlyVisimapEntry *visiMapEntry) { Assert(visiMapEntry); return (visiMapEntry->segmentFileNum >= 0 && @@ -591,42 +599,43 @@ AppendOnlyVisimapEntry_IsValid( /* * Return the next invisible tuple id in the given visibility map. - */ + */ bool AppendOnlyVisimapEntry_GetNextInvisible( - AppendOnlyVisimapEntry *visiMapEntry, - AOTupleId *tupleId) + AppendOnlyVisimapEntry *visiMapEntry, + AOTupleId *tupleId) { - int64 currentBitmapOffset, rowNum; - int offset; + int64 currentBitmapOffset, + rowNum; + int offset; Assert(visiMapEntry); Assert(AppendOnlyVisimapEntry_IsValid(visiMapEntry)); Assert(tupleId); - currentBitmapOffset = -1; /* before the first */ + currentBitmapOffset = -1; /* before the first */ if (AppendOnlyVisimapEntry_CoversTuple( - visiMapEntry, tupleId)) + visiMapEntry, tupleId)) { AppendOnlyVisimapEntry_GetRownumOffset( - visiMapEntry, - AOTupleIdGet_rowNum(tupleId), - ¤tBitmapOffset); + visiMapEntry, + AOTupleIdGet_rowNum(tupleId), + ¤tBitmapOffset); } currentBitmapOffset++; offset = bms_first_from(visiMapEntry->bitmap, - currentBitmapOffset); + currentBitmapOffset); if (offset >= 0) { rowNum = visiMapEntry->firstRowNum + offset; AOTupleIdInit_Init(tupleId); AOTupleIdInit_segmentFileNum( - tupleId, - visiMapEntry->segmentFileNum); + tupleId, + visiMapEntry->segmentFileNum); AOTupleIdInit_rowNum( - tupleId, - rowNum); + tupleId, + rowNum); return true; } else @@ -634,4 +643,3 @@ AppendOnlyVisimapEntry_GetNextInvisible( return false; } } - diff --git a/src/backend/access/appendonly/appendonly_visimap_store.c b/src/backend/access/appendonly/appendonly_visimap_store.c index fe3bab20b8..e43bc91284 100644 --- a/src/backend/access/appendonly/appendonly_visimap_store.c +++ b/src/backend/access/appendonly/appendonly_visimap_store.c @@ -26,14 +26,14 @@ /* * Frees the data allocated by the visimap store - * + * * No function using the visibility map store should be called * after this function call. - */ + */ void AppendOnlyVisimapStore_Finish( - AppendOnlyVisimapStore *visiMapStore, - LOCKMODE lockmode) + AppendOnlyVisimapStore *visiMapStore, + LOCKMODE lockmode) { if (visiMapStore->scanKeys) { @@ -46,23 +46,23 @@ AppendOnlyVisimapStore_Finish( } /* - * Inits the visimap store. + * Inits the visimap store. * The store is ready for usage after this function call. * * Assumes a zero-allocated visimap store data structure. * Assumes that the visimap memory context is active. - */ + */ void AppendOnlyVisimapStore_Init( - AppendOnlyVisimapStore *visiMapStore, - Oid visimapRelid, - Oid visimapIdxid, - LOCKMODE lockmode, - Snapshot snapshot, - MemoryContext memoryContext) + AppendOnlyVisimapStore *visiMapStore, + Oid visimapRelid, + Oid visimapIdxid, + LOCKMODE lockmode, + Snapshot snapshot, + MemoryContext memoryContext) { - TupleDesc heapTupleDesc; - ScanKey scanKey; + TupleDesc heapTupleDesc; + ScanKey scanKey; Assert(visiMapStore); Assert(CurrentMemoryContext == memoryContext); @@ -73,9 +73,9 @@ AppendOnlyVisimapStore_Init( visiMapStore->memoryContext = memoryContext; visiMapStore->visimapRelation = heap_open( - visimapRelid, lockmode); + visimapRelid, lockmode); visiMapStore->visimapIndex = index_open( - visimapIdxid, lockmode); + visimapIdxid, lockmode); heapTupleDesc = RelationGetDescr(visiMapStore->visimapRelation); @@ -83,21 +83,21 @@ AppendOnlyVisimapStore_Init( visiMapStore->scanKeys = palloc0(sizeof(ScanKeyData) * APPENDONLY_VISIMAP_INDEX_SCAN_KEY_NUM); - // scan key: segno + /* scan key: segno */ scanKey = visiMapStore->scanKeys; ScanKeyInit(scanKey, - Anum_pg_aovisimap_segno, /* segno */ - BTEqualStrategyNumber, - F_INT4EQ, - 0); - - // scan key: firstRowNum + Anum_pg_aovisimap_segno, /* segno */ + BTEqualStrategyNumber, + F_INT4EQ, + 0); + + /* scan key: firstRowNum */ scanKey++; ScanKeyInit(scanKey, - Anum_pg_aovisimap_firstrownum, /* attribute number to scan */ - BTEqualStrategyNumber, /* strategy */ - F_INT8EQ, /* reg proc to use */ - 0); + Anum_pg_aovisimap_firstrownum, /* attribute number to scan */ + BTEqualStrategyNumber, /* strategy */ + F_INT8EQ, /* reg proc to use */ + 0); } /* @@ -110,42 +110,42 @@ AppendOnlyVisimapStore_Init( * returned by AppendOnlyVisimapStore_find. * * Should not be called twice in the same command. - */ + */ void AppendOnlyVisimapStore_Store( - AppendOnlyVisimapStore* visiMapStore, - AppendOnlyVisimapEntry* visiMapEntry) + AppendOnlyVisimapStore *visiMapStore, + AppendOnlyVisimapEntry *visiMapEntry) { MemoryContext oldContext; - Relation visimapRelation; - TupleDesc heapTupleDesc; - HeapTuple tuple; - Datum values[Natts_pg_aovisimap]; - bool nulls[Natts_pg_aovisimap]; + Relation visimapRelation; + TupleDesc heapTupleDesc; + HeapTuple tuple; + Datum values[Natts_pg_aovisimap]; + bool nulls[Natts_pg_aovisimap]; Assert(visiMapStore); Assert(visiMapEntry); - elogif (Debug_appendonly_print_visimap, LOG, - "Append-only visi map store: Store visimap entry: " - "(segFileNum, firstRowNum) = (%u, " INT64_FORMAT ")", - visiMapEntry->segmentFileNum, visiMapEntry->firstRowNum); + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map store: Store visimap entry: " + "(segFileNum, firstRowNum) = (%u, " INT64_FORMAT ")", + visiMapEntry->segmentFileNum, visiMapEntry->firstRowNum); oldContext = MemoryContextSwitchTo(visiMapStore->memoryContext); AppendOnlyVisimapEntry_Write(visiMapEntry, values, - nulls); + nulls); visimapRelation = visiMapStore->visimapRelation; heapTupleDesc = RelationGetDescr(visimapRelation); tuple = heap_form_tuple(heapTupleDesc, - values, - nulls); + values, + nulls); /* - * Write out the visimap entry to the relation. - * If this visimap entry already in the relation, we update - * the row. Otherwise, a new row is inserted. + * Write out the visimap entry to the relation. If this visimap entry + * already in the relation, we update the row. Otherwise, a new row is + * inserted. */ if (ItemPointerIsValid(&visiMapEntry->tupleTid)) { @@ -157,13 +157,13 @@ AppendOnlyVisimapStore_Store( } CatalogUpdateIndexes(visimapRelation, tuple); - + heap_freetuple(tuple); - + MemoryContextSwitchTo(oldContext); - // Invalidate the data after storing it. - ItemPointerSetInvalid(&visiMapEntry->tupleTid); + /* Invalidate the data after storing it. */ + ItemPointerSetInvalid(&visiMapEntry->tupleTid); } /** @@ -171,7 +171,7 @@ AppendOnlyVisimapStore_Store( * segmentFileNum and firstRowNum. * * Note: The firstRowNum needs to be a valid firstRowNum. It is - * especially not the tuple id of the append-only tuple checked, updated, + * especially not the tuple id of the append-only tuple checked, updated, * or deleted. * * Returns true if there is such a tuple and @@ -182,46 +182,46 @@ AppendOnlyVisimapStore_Store( */ bool AppendOnlyVisimapStore_Find( - AppendOnlyVisimapStore* visiMapStore, - int32 segmentFileNum, - int64 firstRowNum, - AppendOnlyVisimapEntry* visiMapEntry) + AppendOnlyVisimapStore *visiMapStore, + int32 segmentFileNum, + int64 firstRowNum, + AppendOnlyVisimapEntry *visiMapEntry) { - ScanKey scanKeys; + ScanKey scanKeys; IndexScanDesc indexScan; Assert(visiMapStore); Assert(visiMapEntry); Assert(RelationIsValid(visiMapStore->visimapRelation)); Assert(RelationIsValid(visiMapStore->visimapIndex)); - - elogif (Debug_appendonly_print_visimap, LOG, - "Append-only visi map store: Load entry: " - "(segFileNum, firstRowNum) = (%u, " INT64_FORMAT ")", - segmentFileNum, firstRowNum); + + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map store: Load entry: " + "(segFileNum, firstRowNum) = (%u, " INT64_FORMAT ")", + segmentFileNum, firstRowNum); scanKeys = visiMapStore->scanKeys; scanKeys[0].sk_argument = Int32GetDatum(segmentFileNum); scanKeys[1].sk_argument = Int64GetDatum(firstRowNum); indexScan = AppendOnlyVisimapStore_BeginScan( - visiMapStore, - APPENDONLY_VISIMAP_INDEX_SCAN_KEY_NUM, - scanKeys); + visiMapStore, + APPENDONLY_VISIMAP_INDEX_SCAN_KEY_NUM, + scanKeys); if (!AppendOnlyVisimapStore_GetNext( - visiMapStore, - indexScan, - BackwardScanDirection, - visiMapEntry, - &visiMapEntry->tupleTid)) + visiMapStore, + indexScan, + BackwardScanDirection, + visiMapEntry, + &visiMapEntry->tupleTid)) { - elogif(Debug_appendonly_print_visimap, LOG, - "Append-only visi map store: Visimap entry does not exist: " - "(segFileNum, firstRowNum) = (%u, " INT64_FORMAT ")", - segmentFileNum, firstRowNum); - - // failed to lookup row + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map store: Visimap entry does not exist: " + "(segFileNum, firstRowNum) = (%u, " INT64_FORMAT ")", + segmentFileNum, firstRowNum); + + /* failed to lookup row */ AppendOnlyVisimapStore_EndScan(visiMapStore, indexScan); return false; } @@ -238,9 +238,9 @@ AppendOnlyVisimapStore_Find( */ static HeapTuple AppendOnlyVisimapStore_GetNextTuple( - AppendOnlyVisimapStore *visiMapStore, - IndexScanDesc indexScan, - ScanDirection scanDirection) + AppendOnlyVisimapStore *visiMapStore, + IndexScanDesc indexScan, + ScanDirection scanDirection) { Assert(visiMapStore); Assert(RelationIsValid(visiMapStore->visimapRelation)); @@ -263,14 +263,14 @@ AppendOnlyVisimapStore_GetNextTuple( */ bool AppendOnlyVisimapStore_GetNext( - AppendOnlyVisimapStore *visiMapStore, - IndexScanDesc indexScan, - ScanDirection scanDirection, - AppendOnlyVisimapEntry* visiMapEntry, - ItemPointerData *tupleTid) + AppendOnlyVisimapStore *visiMapStore, + IndexScanDesc indexScan, + ScanDirection scanDirection, + AppendOnlyVisimapEntry *visiMapEntry, + ItemPointerData *tupleTid) { - HeapTuple tuple; - TupleDesc heapTupleDesc; + HeapTuple tuple; + TupleDesc heapTupleDesc; Assert(visiMapStore); Assert(RelationIsValid(visiMapStore->visimapRelation)); @@ -286,7 +286,7 @@ AppendOnlyVisimapStore_GetNext( if (visiMapEntry) { AppendOnlyVisimapEntry_Copyout(visiMapEntry, tuple, - heapTupleDesc); + heapTupleDesc); } if (tupleTid) { @@ -298,11 +298,11 @@ AppendOnlyVisimapStore_GetNext( /* * Deletes all visibility map information from a given * segment file. - */ + */ void AppendOnlyVisimapStore_DeleteSegmentFile( - AppendOnlyVisimapStore *visiMapStore, - int segmentFileNum) + AppendOnlyVisimapStore *visiMapStore, + int segmentFileNum) { ScanKeyData scanKey; IndexScanDesc indexScan; @@ -311,66 +311,66 @@ AppendOnlyVisimapStore_DeleteSegmentFile( Assert(visiMapStore); Assert(RelationIsValid(visiMapStore->visimapRelation)); Assert(RelationIsValid(visiMapStore->visimapIndex)); - - elogif(Debug_appendonly_print_visimap, LOG, - "Append-only visi map store: Delete segment file: " - "(segFileNum) = (%u)", segmentFileNum); + + elogif(Debug_appendonly_print_visimap, LOG, + "Append-only visi map store: Delete segment file: " + "(segFileNum) = (%u)", segmentFileNum); ScanKeyInit(&scanKey, - Anum_pg_aovisimap_segno, /* segno */ - BTEqualStrategyNumber, - F_INT4EQ, - Int32GetDatum(segmentFileNum)); + Anum_pg_aovisimap_segno, /* segno */ + BTEqualStrategyNumber, + F_INT4EQ, + Int32GetDatum(segmentFileNum)); indexScan = AppendOnlyVisimapStore_BeginScan( - visiMapStore, - 1, - &scanKey); + visiMapStore, + 1, + &scanKey); while (AppendOnlyVisimapStore_GetNext(visiMapStore, - indexScan, - ForwardScanDirection, - NULL, - &tid)) + indexScan, + ForwardScanDirection, + NULL, + &tid)) { simple_heap_delete(visiMapStore->visimapRelation, - &tid); + &tid); } AppendOnlyVisimapStore_EndScan(visiMapStore, indexScan); } /* * Returns the number of hidden tuples in a given segment file - */ + */ int64 AppendOnlyVisimapStore_GetSegmentFileHiddenTupleCount( - AppendOnlyVisimapStore *visiMapStore, - AppendOnlyVisimapEntry *visiMapEntry, - int segmentFileNum) + AppendOnlyVisimapStore *visiMapStore, + AppendOnlyVisimapEntry *visiMapEntry, + int segmentFileNum) { ScanKeyData scanKey; IndexScanDesc indexScan; - int64 hiddenTupcount = 0; + int64 hiddenTupcount = 0; Assert(visiMapStore); Assert(visiMapEntry); Assert(RelationIsValid(visiMapStore->visimapRelation)); Assert(RelationIsValid(visiMapStore->visimapIndex)); - + ScanKeyInit(&scanKey, - Anum_pg_aovisimap_segno, /* segno */ - BTEqualStrategyNumber, - F_INT4EQ, - Int32GetDatum(segmentFileNum)); + Anum_pg_aovisimap_segno, /* segno */ + BTEqualStrategyNumber, + F_INT4EQ, + Int32GetDatum(segmentFileNum)); indexScan = AppendOnlyVisimapStore_BeginScan( - visiMapStore, - 1, - &scanKey); - + visiMapStore, + 1, + &scanKey); + while (AppendOnlyVisimapStore_GetNext(visiMapStore, - indexScan, ForwardScanDirection, - visiMapEntry, NULL)) + indexScan, ForwardScanDirection, + visiMapEntry, NULL)) { hiddenTupcount += AppendOnlyVisimapEntry_GetHiddenTupleCount(visiMapEntry); } @@ -380,28 +380,28 @@ AppendOnlyVisimapStore_GetSegmentFileHiddenTupleCount( /* * Returns the number of hidden tuples in a given releation - */ + */ int64 AppendOnlyVisimapStore_GetRelationHiddenTupleCount( - AppendOnlyVisimapStore *visiMapStore, - AppendOnlyVisimapEntry *visiMapEntry) + AppendOnlyVisimapStore *visiMapStore, + AppendOnlyVisimapEntry *visiMapEntry) { IndexScanDesc indexScan; - int64 hiddenTupcount = 0; + int64 hiddenTupcount = 0; Assert(visiMapStore); Assert(visiMapEntry); Assert(RelationIsValid(visiMapStore->visimapRelation)); Assert(RelationIsValid(visiMapStore->visimapIndex)); - + indexScan = AppendOnlyVisimapStore_BeginScan( - visiMapStore, - 0, - NULL); - + visiMapStore, + 0, + NULL); + while (AppendOnlyVisimapStore_GetNext(visiMapStore, - indexScan, ForwardScanDirection, - visiMapEntry, NULL)) + indexScan, ForwardScanDirection, + visiMapEntry, NULL)) { hiddenTupcount += AppendOnlyVisimapEntry_GetHiddenTupleCount(visiMapEntry); } @@ -413,35 +413,34 @@ AppendOnlyVisimapStore_GetRelationHiddenTupleCount( * Starts a scan over the visimap store. * * Parameter keys may be NULL iff nkeys is zero. - */ + */ IndexScanDesc AppendOnlyVisimapStore_BeginScan( - AppendOnlyVisimapStore *visiMapStore, - int nkeys, - ScanKey keys) + AppendOnlyVisimapStore *visiMapStore, + int nkeys, + ScanKey keys) { Assert(visiMapStore); Assert(RelationIsValid(visiMapStore->visimapRelation)); return index_beginscan( - visiMapStore->visimapRelation, - visiMapStore->visimapIndex, - visiMapStore->snapshot, - nkeys, - keys); + visiMapStore->visimapRelation, + visiMapStore->visimapIndex, + visiMapStore->snapshot, + nkeys, + keys); } /* * Ends a index scan over the visimap store. - */ + */ void AppendOnlyVisimapStore_EndScan( - AppendOnlyVisimapStore *visiMapStore, - IndexScanDesc indexScan) + AppendOnlyVisimapStore *visiMapStore, + IndexScanDesc indexScan) { Assert(visiMapStore); Assert(indexScan); index_endscan(indexScan); } - diff --git a/src/backend/access/appendonly/appendonly_visimap_udf.c b/src/backend/access/appendonly/appendonly_visimap_udf.c index 3562f14513..1fddb7b930 100644 --- a/src/backend/access/appendonly/appendonly_visimap_udf.c +++ b/src/backend/access/appendonly/appendonly_visimap_udf.c @@ -24,41 +24,38 @@ #include "utils/builtins.h" -extern Datum -gp_aovisimap(PG_FUNCTION_ARGS); -extern Datum -gp_aovisimap_name(PG_FUNCTION_ARGS); +extern Datum gp_aovisimap(PG_FUNCTION_ARGS); +extern Datum gp_aovisimap_name(PG_FUNCTION_ARGS); static Datum gp_aovisimap_internal(PG_FUNCTION_ARGS, Oid aoRelOid) { Datum values[3]; bool nulls[3]; - HeapTuple tuple; - Datum result; + HeapTuple tuple; + Datum result; typedef struct Context { - Relation aorel; + Relation aorel; AppendOnlyVisimapScan visiMapScan; - AOTupleId aoTupleId; - + AOTupleId aoTupleId; + } Context; - + FuncCallContext *funcctx; - Context *context; + Context *context; if (SRF_IS_FIRSTCALL()) { TupleDesc tupdesc; MemoryContext oldcontext; - + /* create a function context for cross-call persistence */ funcctx = SRF_FIRSTCALL_INIT(); /* - * switch to memory context appropriate for multiple function - * calls + * switch to memory context appropriate for multiple function calls */ oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); @@ -88,10 +85,10 @@ gp_aovisimap_internal(PG_FUNCTION_ARGS, Oid aoRelOid) } AppendOnlyVisimapScan_Init(&context->visiMapScan, - context->aorel->rd_appendonly->visimaprelid, - context->aorel->rd_appendonly->visimapidxid, - AccessShareLock, - SnapshotNow); + context->aorel->rd_appendonly->visimaprelid, + context->aorel->rd_appendonly->visimapidxid, + AccessShareLock, + SnapshotNow); AOTupleIdInit_Init(&context->aoTupleId); funcctx->user_fctx = (void *) context; @@ -105,14 +102,14 @@ gp_aovisimap_internal(PG_FUNCTION_ARGS, Oid aoRelOid) while (true) { if (!AppendOnlyVisimapScan_GetNextInvisible( - &context->visiMapScan, - &context->aoTupleId)) + &context->visiMapScan, + &context->aoTupleId)) { break; } MemSet(values, 0, sizeof(values)); MemSet(nulls, false, sizeof(nulls)); - values[0] = ItemPointerGetDatum((ItemPointer)&context->aoTupleId); + values[0] = ItemPointerGetDatum((ItemPointer) &context->aoTupleId); values[1] = Int32GetDatum(AOTupleIdGet_segmentFileNum(&context->aoTupleId)); values[2] = Int64GetDatum(AOTupleIdGet_rowNum(&context->aoTupleId)); @@ -121,7 +118,7 @@ gp_aovisimap_internal(PG_FUNCTION_ARGS, Oid aoRelOid) SRF_RETURN_NEXT(funcctx, result); } - + AppendOnlyVisimapScan_Finish(&context->visiMapScan, AccessShareLock); heap_close(context->aorel, AccessShareLock); pfree(context); @@ -132,8 +129,8 @@ gp_aovisimap_internal(PG_FUNCTION_ARGS, Oid aoRelOid) Datum gp_aovisimap(PG_FUNCTION_ARGS) { - Oid relid; - + Oid relid; + relid = PG_GETARG_OID(0); return gp_aovisimap_internal(fcinfo, relid); } @@ -141,9 +138,9 @@ gp_aovisimap(PG_FUNCTION_ARGS) Datum gp_aovisimap_name(PG_FUNCTION_ARGS) { - RangeVar *parentrv; - text *relname = PG_GETARG_TEXT_P(0); - Oid relid; + RangeVar *parentrv; + text *relname = PG_GETARG_TEXT_P(0); + Oid relid; parentrv = makeRangeVarFromNameList(textToQualifiedNameList(relname)); relid = RangeVarGetRelid(parentrv, false); @@ -151,35 +148,33 @@ gp_aovisimap_name(PG_FUNCTION_ARGS) return gp_aovisimap_internal(fcinfo, relid); } - -extern Datum -gp_aovisimap_hidden_info(PG_FUNCTION_ARGS); -extern Datum -gp_aovisimap_hidden_info_name(PG_FUNCTION_ARGS); + +extern Datum gp_aovisimap_hidden_info(PG_FUNCTION_ARGS); +extern Datum gp_aovisimap_hidden_info_name(PG_FUNCTION_ARGS); static Datum gp_aovisimap_hidden_info_internal(PG_FUNCTION_ARGS, Oid aoRelOid) { Datum values[3]; bool nulls[3]; - HeapTuple tuple; - Datum result; + HeapTuple tuple; + Datum result; typedef struct Context { AppendOnlyVisimap visiMap; - Relation parentRelation; + Relation parentRelation; FileSegInfo **appendonlySegfileInfo; AOCSFileSegInfo **aocsSegfileInfo; - int segfile_info_total; + int segfile_info_total; - int i; + int i; } Context; - + FuncCallContext *funcctx; - Context *context; + Context *context; if (SRF_IS_FIRSTCALL()) { @@ -190,8 +185,7 @@ gp_aovisimap_hidden_info_internal(PG_FUNCTION_ARGS, Oid aoRelOid) funcctx = SRF_FIRSTCALL_INIT(); /* - * switch to memory context appropriate for multiple function - * calls + * switch to memory context appropriate for multiple function calls */ oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); @@ -223,23 +217,23 @@ gp_aovisimap_hidden_info_internal(PG_FUNCTION_ARGS, Oid aoRelOid) if (RelationIsAoRows(context->parentRelation)) { context->appendonlySegfileInfo = GetAllFileSegInfo( - context->parentRelation, - SnapshotNow, - &context->segfile_info_total); + context->parentRelation, + SnapshotNow, + &context->segfile_info_total); } else { Assert(RelationIsAoCols(context->parentRelation)); context->aocsSegfileInfo = GetAllAOCSFileSegInfo(context->parentRelation, - SnapshotNow, &context->segfile_info_total); + SnapshotNow, &context->segfile_info_total); } context->i = 0; AppendOnlyVisimap_Init(&context->visiMap, - context->parentRelation->rd_appendonly->visimaprelid, - context->parentRelation->rd_appendonly->visimapidxid, - AccessShareLock, - SnapshotNow); + context->parentRelation->rd_appendonly->visimaprelid, + context->parentRelation->rd_appendonly->visimapidxid, + AccessShareLock, + SnapshotNow); funcctx->user_fctx = (void *) context; @@ -251,17 +245,20 @@ gp_aovisimap_hidden_info_internal(PG_FUNCTION_ARGS, Oid aoRelOid) while (context->i < context->segfile_info_total) { - int64 tupcount; - int segno; + int64 tupcount; + int segno; + if (context->appendonlySegfileInfo) { FileSegInfo *fsinfo = context->appendonlySegfileInfo[context->i]; + tupcount = fsinfo->total_tupcount; segno = fsinfo->segno; } else if (context->aocsSegfileInfo) { AOCSFileSegInfo *fsinfo = context->aocsSegfileInfo[context->i]; + tupcount = fsinfo->total_tupcount; segno = fsinfo->segno; } @@ -274,7 +271,7 @@ gp_aovisimap_hidden_info_internal(PG_FUNCTION_ARGS, Oid aoRelOid) MemSet(nulls, false, sizeof(nulls)); values[0] = Int32GetDatum(segno); values[1] = Int64GetDatum(AppendOnlyVisimap_GetSegmentFileHiddenTupleCount( - &context->visiMap, segno)); + &context->visiMap, segno)); values[2] = Int64GetDatum(tupcount); tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls); @@ -283,7 +280,7 @@ gp_aovisimap_hidden_info_internal(PG_FUNCTION_ARGS, Oid aoRelOid) context->i++; SRF_RETURN_NEXT(funcctx, result); } - + AppendOnlyVisimap_Finish(&context->visiMap, AccessShareLock); if (context->appendonlySegfileInfo) { @@ -308,16 +305,17 @@ gp_aovisimap_hidden_info_internal(PG_FUNCTION_ARGS, Oid aoRelOid) Datum gp_aovisimap_hidden_info(PG_FUNCTION_ARGS) { - Oid aoRelOid = PG_GETARG_OID(0); + Oid aoRelOid = PG_GETARG_OID(0); + return gp_aovisimap_hidden_info_internal(fcinfo, aoRelOid); } Datum gp_aovisimap_hidden_info_name(PG_FUNCTION_ARGS) { - RangeVar *parentrv; - text *relname = PG_GETARG_TEXT_P(0); - Oid relid; + RangeVar *parentrv; + text *relname = PG_GETARG_TEXT_P(0); + Oid relid; parentrv = makeRangeVarFromNameList(textToQualifiedNameList(relname)); relid = RangeVarGetRelid(parentrv, false); @@ -325,16 +323,14 @@ gp_aovisimap_hidden_info_name(PG_FUNCTION_ARGS) return gp_aovisimap_hidden_info_internal(fcinfo, relid); } -extern Datum -gp_aovisimap_entry(PG_FUNCTION_ARGS); -extern Datum -gp_aovisimap_entry_name(PG_FUNCTION_ARGS); +extern Datum gp_aovisimap_entry(PG_FUNCTION_ARGS); +extern Datum gp_aovisimap_entry_name(PG_FUNCTION_ARGS); static void -gp_aovisimap_encode_bitmap(char* bitmapBuffer, Bitmapset *bms) +gp_aovisimap_encode_bitmap(char *bitmapBuffer, Bitmapset *bms) { - int i; - int last = -1; + int i; + int last = -1; Assert(bitmapBuffer); @@ -357,34 +353,33 @@ gp_aovisimap_entry_internal(PG_FUNCTION_ARGS, Oid aoRelOid) { Datum values[4]; bool nulls[4]; - HeapTuple tuple; - Datum result; + HeapTuple tuple; + Datum result; typedef struct Context { AppendOnlyVisimap visiMap; - Relation parentRelation; + Relation parentRelation; IndexScanDesc indexScan; - text *bitmapBuffer; + text *bitmapBuffer; } Context; - + FuncCallContext *funcctx; - Context *context; + Context *context; if (SRF_IS_FIRSTCALL()) { TupleDesc tupdesc; MemoryContext oldcontext; - + /* create a function context for cross-call persistence */ funcctx = SRF_FIRSTCALL_INIT(); /* - * switch to memory context appropriate for multiple function - * calls + * switch to memory context appropriate for multiple function calls */ oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); @@ -416,13 +411,13 @@ gp_aovisimap_entry_internal(PG_FUNCTION_ARGS, Oid aoRelOid) } AppendOnlyVisimap_Init(&context->visiMap, - context->parentRelation->rd_appendonly->visimaprelid, - context->parentRelation->rd_appendonly->visimapidxid, - AccessShareLock, - SnapshotNow); + context->parentRelation->rd_appendonly->visimaprelid, + context->parentRelation->rd_appendonly->visimapidxid, + AccessShareLock, + SnapshotNow); context->indexScan = AppendOnlyVisimapStore_BeginScan(& - context->visiMap.visimapStore, 0, NULL); + context->visiMap.visimapStore, 0, NULL); context->bitmapBuffer = palloc0(VARHDRSZ + APPENDONLY_VISIMAP_MAX_RANGE + 1); @@ -435,10 +430,10 @@ gp_aovisimap_entry_internal(PG_FUNCTION_ARGS, Oid aoRelOid) context = (Context *) funcctx->user_fctx; if (AppendOnlyVisimapStore_GetNext(&context->visiMap.visimapStore, - context->indexScan, - ForwardScanDirection, - &context->visiMap.visimapEntry, - NULL)) + context->indexScan, + ForwardScanDirection, + &context->visiMap.visimapEntry, + NULL)) { AppendOnlyVisimapEntry *visimapEntry = &context->visiMap.visimapEntry; @@ -447,10 +442,10 @@ gp_aovisimap_entry_internal(PG_FUNCTION_ARGS, Oid aoRelOid) values[0] = Int32GetDatum(visimapEntry->segmentFileNum); values[1] = Int64GetDatum(visimapEntry->firstRowNum); values[2] = Int32GetDatum( - (int32)AppendOnlyVisimapEntry_GetHiddenTupleCount(visimapEntry)); - - gp_aovisimap_encode_bitmap(VARDATA(context->bitmapBuffer), - visimapEntry->bitmap); + (int32) AppendOnlyVisimapEntry_GetHiddenTupleCount(visimapEntry)); + + gp_aovisimap_encode_bitmap(VARDATA(context->bitmapBuffer), + visimapEntry->bitmap); SET_VARSIZE(context->bitmapBuffer, APPENDONLY_VISIMAP_MAX_RANGE); values[3] = PointerGetDatum(context->bitmapBuffer); @@ -459,9 +454,9 @@ gp_aovisimap_entry_internal(PG_FUNCTION_ARGS, Oid aoRelOid) SRF_RETURN_NEXT(funcctx, result); } - + AppendOnlyVisimapStore_EndScan(&context->visiMap.visimapStore, - context->indexScan); + context->indexScan); AppendOnlyVisimap_Finish(&context->visiMap, AccessShareLock); heap_close(context->parentRelation, AccessShareLock); @@ -475,16 +470,17 @@ gp_aovisimap_entry_internal(PG_FUNCTION_ARGS, Oid aoRelOid) Datum gp_aovisimap_entry(PG_FUNCTION_ARGS) { - Oid aoRelOid = PG_GETARG_OID(0); + Oid aoRelOid = PG_GETARG_OID(0); + return gp_aovisimap_entry_internal(fcinfo, aoRelOid); } Datum gp_aovisimap_entry_name(PG_FUNCTION_ARGS) { - RangeVar *parentrv; - text *relname = PG_GETARG_TEXT_P(0); - Oid relid; + RangeVar *parentrv; + text *relname = PG_GETARG_TEXT_P(0); + Oid relid; parentrv = makeRangeVarFromNameList(textToQualifiedNameList(relname)); relid = RangeVarGetRelid(parentrv, false); diff --git a/src/backend/access/appendonly/appendonlyam.c b/src/backend/access/appendonly/appendonlyam.c index 73e400748c..1e971a9432 100755 --- a/src/backend/access/appendonly/appendonlyam.c +++ b/src/backend/access/appendonly/appendonlyam.c @@ -1763,8 +1763,8 @@ appendonly_beginrangescan(Relation relation, for (i = 0; i < segfile_count; i++) { - seginfo[i] = GetFileSegInfo(relation, appendOnlyMetaDataSnapshot, - segfile_no_arr[i]); + seginfo[ i] = GetFileSegInfo(relation, appendOnlyMetaDataSnapshot, + segfile_no_arr[i]); } return appendonly_beginrangescan_internal(relation, snapshot, @@ -1975,7 +1975,7 @@ static bool fetchNextBlock(AppendOnlyFetchDesc aoFetchDesc) { AppendOnlyExecutorReadBlock *executorReadBlock = - &aoFetchDesc->executorReadBlock; + &aoFetchDesc->executorReadBlock; /* * Try to read next block. diff --git a/src/backend/access/appendonly/appendonlyblockdirectory.c b/src/backend/access/appendonly/appendonlyblockdirectory.c index 5eae9b1169..66d2f6e3d7 100644 --- a/src/backend/access/appendonly/appendonlyblockdirectory.c +++ b/src/backend/access/appendonly/appendonlyblockdirectory.c @@ -26,58 +26,58 @@ #include "utils/fmgroids.h" #include "cdb/cdbappendonlyam.h" -int gp_blockdirectory_entry_min_range = 0; -int gp_blockdirectory_minipage_size = NUM_MINIPAGE_ENTRIES; +int gp_blockdirectory_entry_min_range = 0; +int gp_blockdirectory_minipage_size = NUM_MINIPAGE_ENTRIES; -static inline uint32 minipage_size(uint32 nEntry) +static inline uint32 +minipage_size(uint32 nEntry) { return offsetof(Minipage, entry) + sizeof(MinipageEntry) * nEntry; } static void load_last_minipage( - AppendOnlyBlockDirectory *blockDirectory, - int64 lastSequence, - int columnGroupNo); + AppendOnlyBlockDirectory *blockDirectory, + int64 lastSequence, + int columnGroupNo); static void init_scankeys( - TupleDesc tupleDesc, - int nkeys, ScanKey scanKeys, - StrategyNumber *strategyNumbers); + TupleDesc tupleDesc, + int nkeys, ScanKey scanKeys, + StrategyNumber *strategyNumbers); static int find_minipage_entry( - Minipage *minipage, - uint32 numEntries, - int64 rowNum); + Minipage *minipage, + uint32 numEntries, + int64 rowNum); static void extract_minipage( - AppendOnlyBlockDirectory *blockDirectory, - HeapTuple tuple, - TupleDesc tupleDesc, - int columnGroupNo); + AppendOnlyBlockDirectory *blockDirectory, + HeapTuple tuple, + TupleDesc tupleDesc, + int columnGroupNo); static void write_minipage(AppendOnlyBlockDirectory *blockDirectory, - int columnGroupNo, - MinipagePerColumnGroup *minipageInfo); -static bool -insert_new_entry(AppendOnlyBlockDirectory *blockDirectory, + int columnGroupNo, + MinipagePerColumnGroup *minipageInfo); +static bool insert_new_entry(AppendOnlyBlockDirectory *blockDirectory, int columnGroupNo, int64 firstRowNum, int64 fileOffset, int64 rowCount, bool addColAction); -void +void AppendOnlyBlockDirectoryEntry_GetBeginRange( - AppendOnlyBlockDirectoryEntry *directoryEntry, - int64 *fileOffset, - int64 *firstRowNum) + AppendOnlyBlockDirectoryEntry *directoryEntry, + int64 *fileOffset, + int64 *firstRowNum) { *fileOffset = directoryEntry->range.fileOffset; *firstRowNum = directoryEntry->range.firstRowNum; } -void +void AppendOnlyBlockDirectoryEntry_GetEndRange( - AppendOnlyBlockDirectoryEntry *directoryEntry, - int64 *afterFileOffset, - int64 *lastRowNum) + AppendOnlyBlockDirectoryEntry *directoryEntry, + int64 *afterFileOffset, + int64 *lastRowNum) { *afterFileOffset = directoryEntry->range.afterFileOffset; *lastRowNum = directoryEntry->range.lastRowNum; @@ -85,11 +85,11 @@ AppendOnlyBlockDirectoryEntry_GetEndRange( bool AppendOnlyBlockDirectoryEntry_RangeHasRow( - AppendOnlyBlockDirectoryEntry *directoryEntry, - int64 checkRowNum) + AppendOnlyBlockDirectoryEntry *directoryEntry, + int64 checkRowNum) { return (checkRowNum >= directoryEntry->range.firstRowNum && - checkRowNum <= directoryEntry->range.lastRowNum); + checkRowNum <= directoryEntry->range.lastRowNum); } /* @@ -101,14 +101,14 @@ static void init_internal(AppendOnlyBlockDirectory *blockDirectory) { MemoryContext oldcxt; - int numScanKeys; - TupleDesc heapTupleDesc; - TupleDesc idxTupleDesc; - int groupNo; - + int numScanKeys; + TupleDesc heapTupleDesc; + TupleDesc idxTupleDesc; + int groupNo; + Assert(blockDirectory->blkdirRel != NULL); Assert(blockDirectory->blkdirIdx != NULL); - + blockDirectory->memoryContext = AllocSetContextCreate(CurrentMemoryContext, "BlockDirectoryContext", @@ -147,7 +147,8 @@ init_internal(AppendOnlyBlockDirectory *blockDirectory) continue; } MinipagePerColumnGroup *minipageInfo = - &blockDirectory->minipages[groupNo]; + &blockDirectory->minipages[groupNo]; + minipageInfo->minipage = palloc0(minipage_size(NUM_MINIPAGE_ENTRIES)); minipageInfo->numMinipageEntries = 0; @@ -167,14 +168,14 @@ init_internal(AppendOnlyBlockDirectory *blockDirectory) */ void AppendOnlyBlockDirectory_Init_forSearch( - AppendOnlyBlockDirectory *blockDirectory, - Snapshot appendOnlyMetaDataSnapshot, - FileSegInfo **segmentFileInfo, - int totalSegfiles, - Relation aoRel, - int numColumnGroups, - bool isAOCol, - bool *proj) + AppendOnlyBlockDirectory *blockDirectory, + Snapshot appendOnlyMetaDataSnapshot, + FileSegInfo **segmentFileInfo, + int totalSegfiles, + Relation aoRel, + int numColumnGroups, + bool isAOCol, + bool *proj) { blockDirectory->aoRel = aoRel; @@ -188,10 +189,10 @@ AppendOnlyBlockDirectory_Init_forSearch( } ereportif(Debug_appendonly_print_blockdirectory, LOG, - (errmsg("Append-only block directory init for search: " - "(totalSegfiles, numColumnGroups, isAOCol)=" - "(%d, %d, %d)", - totalSegfiles, numColumnGroups, isAOCol))); + (errmsg("Append-only block directory init for search: " + "(totalSegfiles, numColumnGroups, isAOCol)=" + "(%d, %d, %d)", + totalSegfiles, numColumnGroups, isAOCol))); blockDirectory->segmentFileInfo = segmentFileInfo; blockDirectory->totalSegfiles = totalSegfiles; @@ -226,16 +227,16 @@ AppendOnlyBlockDirectory_Init_forSearch( */ void AppendOnlyBlockDirectory_Init_forInsert( - AppendOnlyBlockDirectory *blockDirectory, - Snapshot appendOnlyMetaDataSnapshot, - FileSegInfo *segmentFileInfo, - int64 lastSequence, - Relation aoRel, - int segno, - int numColumnGroups, - bool isAOCol) + AppendOnlyBlockDirectory *blockDirectory, + Snapshot appendOnlyMetaDataSnapshot, + FileSegInfo *segmentFileInfo, + int64 lastSequence, + Relation aoRel, + int segno, + int numColumnGroups, + bool isAOCol) { - int groupNo; + int groupNo; blockDirectory->aoRel = aoRel; blockDirectory->appendOnlyMetaDataSnapshot = appendOnlyMetaDataSnapshot; @@ -252,7 +253,7 @@ AppendOnlyBlockDirectory_Init_forInsert( blockDirectory->segmentFileInfo = NULL; blockDirectory->totalSegfiles = -1; blockDirectory->currentSegmentFileInfo = segmentFileInfo; - + blockDirectory->currentSegmentFileNum = segno; blockDirectory->numColumnGroups = numColumnGroups; blockDirectory->isAOCol = isAOCol; @@ -271,15 +272,15 @@ AppendOnlyBlockDirectory_Init_forInsert( init_internal(blockDirectory); ereportif(Debug_appendonly_print_blockdirectory, LOG, - (errmsg("Append-only block directory init for insert: " - "(segno, numColumnGroups, isAOCol, lastSequence)=" - "(%d, %d, %d, " INT64_FORMAT ")", - segno, numColumnGroups, isAOCol, lastSequence))); + (errmsg("Append-only block directory init for insert: " + "(segno, numColumnGroups, isAOCol, lastSequence)=" + "(%d, %d, %d, " INT64_FORMAT ")", + segno, numColumnGroups, isAOCol, lastSequence))); /* * Load the last minipages from the block directory relation. */ - for(groupNo = 0; groupNo < blockDirectory->numColumnGroups; groupNo++) + for (groupNo = 0; groupNo < blockDirectory->numColumnGroups; groupNo++) { load_last_minipage(blockDirectory, lastSequence, groupNo); } @@ -291,13 +292,13 @@ AppendOnlyBlockDirectory_Init_forInsert( */ void AppendOnlyBlockDirectory_Init_addCol( - AppendOnlyBlockDirectory *blockDirectory, - Snapshot appendOnlyMetaDataSnapshot, - FileSegInfo *segmentFileInfo, - Relation aoRel, - int segno, - int numColumnGroups, - bool isAOCol) + AppendOnlyBlockDirectory *blockDirectory, + Snapshot appendOnlyMetaDataSnapshot, + FileSegInfo *segmentFileInfo, + Relation aoRel, + int segno, + int numColumnGroups, + bool isAOCol) { blockDirectory->aoRel = aoRel; blockDirectory->appendOnlyMetaDataSnapshot = appendOnlyMetaDataSnapshot; @@ -314,7 +315,7 @@ AppendOnlyBlockDirectory_Init_addCol( blockDirectory->segmentFileInfo = NULL; blockDirectory->totalSegfiles = -1; blockDirectory->currentSegmentFileInfo = segmentFileInfo; - + blockDirectory->currentSegmentFileNum = segno; blockDirectory->numColumnGroups = numColumnGroups; blockDirectory->isAOCol = isAOCol; @@ -324,10 +325,10 @@ AppendOnlyBlockDirectory_Init_addCol( /* * TODO: refactor the *_addCol* interface so that opening of - * blockdirectory relation and index, init_internal and - * corresponding cleanup in *_End_addCol() is called only once - * during the add-column operation. Currently, this is being - * called for every appendonly segment. + * blockdirectory relation and index, init_internal and corresponding + * cleanup in *_End_addCol() is called only once during the add-column + * operation. Currently, this is being called for every appendonly + * segment. */ blockDirectory->blkdirRel = heap_open(aoRel->rd_appendonly->blkdirrelid, RowExclusiveLock); @@ -342,34 +343,34 @@ AppendOnlyBlockDirectory_Init_addCol( static bool set_directoryentry_range( - AppendOnlyBlockDirectory *blockDirectory, - int columnGroupNo, - int entry_no, - AppendOnlyBlockDirectoryEntry *directoryEntry) + AppendOnlyBlockDirectory *blockDirectory, + int columnGroupNo, + int entry_no, + AppendOnlyBlockDirectoryEntry *directoryEntry) { MinipagePerColumnGroup *minipageInfo = - &blockDirectory->minipages[columnGroupNo]; + &blockDirectory->minipages[columnGroupNo]; FileSegInfo *fsInfo; AOCSFileSegInfo *aocsFsInfo = NULL; MinipageEntry *entry; MinipageEntry *next_entry = NULL; - - Assert(entry_no >= 0 && ((uint32)entry_no) < minipageInfo->numMinipageEntries); - + + Assert(entry_no >= 0 && ((uint32) entry_no) < minipageInfo->numMinipageEntries); + fsInfo = blockDirectory->currentSegmentFileInfo; Assert(fsInfo != NULL); if (blockDirectory->isAOCol) { - aocsFsInfo = (AOCSFileSegInfo *)fsInfo; + aocsFsInfo = (AOCSFileSegInfo *) fsInfo; } entry = &(minipageInfo->minipage->entry[entry_no]); - if (((uint32)entry_no) < minipageInfo->numMinipageEntries - 1) + if (((uint32) entry_no) < minipageInfo->numMinipageEntries - 1) { next_entry = &(minipageInfo->minipage->entry[entry_no + 1]); } - + directoryEntry->range.fileOffset = entry->fileOffset; directoryEntry->range.firstRowNum = entry->firstRowNum; if (next_entry != NULL) @@ -382,7 +383,7 @@ set_directoryentry_range( { directoryEntry->range.afterFileOffset = fsInfo->eof; } - + else { directoryEntry->range.afterFileOffset = @@ -393,14 +394,15 @@ set_directoryentry_range( directoryEntry->range.lastRowNum = entry->firstRowNum + entry->rowCount - 1; if (next_entry == NULL && gp_blockdirectory_entry_min_range != 0) { - directoryEntry->range.lastRowNum = (~(((int64)1) << 63)); /* set to the maximal value */ + directoryEntry->range.lastRowNum = (~(((int64) 1) << 63)); /* set to the maximal + * value */ } /* - * When crashes during inserts, or cancellation during inserts, - * the block directory may contain out-of-date entries. We - * check for the end of file here. If the requested directory entry - * is after the end of file, return false. + * When crashes during inserts, or cancellation during inserts, the block + * directory may contain out-of-date entries. We check for the end of file + * here. If the requested directory entry is after the end of file, return + * false. */ if ((!blockDirectory->isAOCol && directoryEntry->range.fileOffset > fsInfo->eof) || @@ -414,22 +416,22 @@ set_directoryentry_range( { directoryEntry->range.afterFileOffset = fsInfo->eof; } - + if (blockDirectory->isAOCol && directoryEntry->range.afterFileOffset > aocsFsInfo->vpinfo.entry[columnGroupNo].eof) { - directoryEntry->range.afterFileOffset = + directoryEntry->range.afterFileOffset = aocsFsInfo->vpinfo.entry[columnGroupNo].eof; } ereportif(Debug_appendonly_print_blockdirectory, LOG, - (errmsg("Append-only block directory find entry: " - "(columnGroupNo, firstRowNum, fileOffset, lastRowNum, afterFileOffset) = " - "(%d, " INT64_FORMAT ", " INT64_FORMAT ", " INT64_FORMAT ", " INT64_FORMAT ")", - columnGroupNo, directoryEntry->range.firstRowNum, - directoryEntry->range.fileOffset, directoryEntry->range.lastRowNum, - directoryEntry->range.afterFileOffset))); + (errmsg("Append-only block directory find entry: " + "(columnGroupNo, firstRowNum, fileOffset, lastRowNum, afterFileOffset) = " + "(%d, " INT64_FORMAT ", " INT64_FORMAT ", " INT64_FORMAT ", " INT64_FORMAT ")", + columnGroupNo, directoryEntry->range.firstRowNum, + directoryEntry->range.fileOffset, directoryEntry->range.lastRowNum, + directoryEntry->range.afterFileOffset))); return true; } @@ -447,32 +449,32 @@ set_directoryentry_range( */ bool AppendOnlyBlockDirectory_GetEntry( - AppendOnlyBlockDirectory *blockDirectory, - AOTupleId *aoTupleId, - int columnGroupNo, - AppendOnlyBlockDirectoryEntry *directoryEntry) + AppendOnlyBlockDirectory *blockDirectory, + AOTupleId *aoTupleId, + int columnGroupNo, + AppendOnlyBlockDirectoryEntry *directoryEntry) { - int segmentFileNum = AOTupleIdGet_segmentFileNum(aoTupleId); - int64 rowNum = AOTupleIdGet_rowNum(aoTupleId); - int i; - Relation blkdirRel = blockDirectory->blkdirRel; - Relation blkdirIdx = blockDirectory->blkdirIdx; - int numScanKeys = blockDirectory->numScanKeys; - ScanKey scanKeys = blockDirectory->scanKeys; - - TupleDesc heapTupleDesc; + int segmentFileNum = AOTupleIdGet_segmentFileNum(aoTupleId); + int64 rowNum = AOTupleIdGet_rowNum(aoTupleId); + int i; + Relation blkdirRel = blockDirectory->blkdirRel; + Relation blkdirIdx = blockDirectory->blkdirIdx; + int numScanKeys = blockDirectory->numScanKeys; + ScanKey scanKeys = blockDirectory->scanKeys; + + TupleDesc heapTupleDesc; FileSegInfo *fsInfo = NULL; IndexScanDesc idxScanDesc; - HeapTuple tuple = NULL; + HeapTuple tuple = NULL; MinipagePerColumnGroup *minipageInfo = - &blockDirectory->minipages[columnGroupNo]; - int entry_no = -1; - int tmpGroupNo; - + &blockDirectory->minipages[columnGroupNo]; + int entry_no = -1; + int tmpGroupNo; + if (blkdirRel == NULL || blkdirIdx == NULL) { Assert(RelationIsValid(blockDirectory->aoRel)); - + ereport(ERROR, (errcode(ERRCODE_GP_INTERNAL_ERROR), errmsg("Block directory for append-only relation '%s' does not exist", @@ -481,17 +483,17 @@ AppendOnlyBlockDirectory_GetEntry( } ereportif(Debug_appendonly_print_blockdirectory, LOG, - (errmsg("Append-only block directory get entry: " - "(columnGroupNo, segmentFileNum, rowNum) = " - "(%d, %d, " INT64_FORMAT ")", - columnGroupNo, segmentFileNum, rowNum))); + (errmsg("Append-only block directory get entry: " + "(columnGroupNo, segmentFileNum, rowNum) = " + "(%d, %d, " INT64_FORMAT ")", + columnGroupNo, segmentFileNum, rowNum))); /* * If the segment file number is the same as - * blockDirectory->currentSegmentFileNum, the in-memory minipage - * may contain such an entry. We search the in-memory minipage - * first. If such an entry can not be found, we search for the - * appropriate minipage by using the block directory btree index. + * blockDirectory->currentSegmentFileNum, the in-memory minipage may + * contain such an entry. We search the in-memory minipage first. If such + * an entry can not be found, we search for the appropriate minipage by + * using the block directory btree index. */ if (segmentFileNum == blockDirectory->currentSegmentFileNum && minipageInfo->numMinipageEntries > 0) @@ -499,37 +501,38 @@ AppendOnlyBlockDirectory_GetEntry( Assert(blockDirectory->currentSegmentFileInfo != NULL); MinipageEntry *firstentry = - &minipageInfo->minipage->entry[0]; + &minipageInfo->minipage->entry[0]; + if (rowNum >= firstentry->firstRowNum) { /* - * Check if the existing minipage contains the requested - * rowNum. If so, just get it. + * Check if the existing minipage contains the requested rowNum. + * If so, just get it. */ entry_no = find_minipage_entry(minipageInfo->minipage, - minipageInfo->numMinipageEntries, - rowNum); + minipageInfo->numMinipageEntries, + rowNum); if (entry_no != -1) { return set_directoryentry_range(blockDirectory, - columnGroupNo, - entry_no, - directoryEntry); + columnGroupNo, + entry_no, + directoryEntry); } /* - * The given rowNum may point to a tuple that does not exist - * in the AO table any more, either because of cancellation of - * an insert, or due to crashes during an insert. If this is - * the case, rowNum is smaller than the highest entry in - * the in-memory minipage entry. + * The given rowNum may point to a tuple that does not exist in + * the AO table any more, either because of cancellation of an + * insert, or due to crashes during an insert. If this is the + * case, rowNum is smaller than the highest entry in the in-memory + * minipage entry. */ else { MinipageEntry *entry = - &minipageInfo->minipage->entry[minipageInfo->numMinipageEntries - 1]; - + &minipageInfo->minipage->entry[minipageInfo->numMinipageEntries - 1]; + if (rowNum < entry->firstRowNum + entry->rowCount - 1) return false; } @@ -539,20 +542,20 @@ AppendOnlyBlockDirectory_GetEntry( for (i = 0; i < blockDirectory->totalSegfiles; i++) { fsInfo = blockDirectory->segmentFileInfo[i]; - + if (!blockDirectory->isAOCol && segmentFileNum == fsInfo->segno) break; else if (blockDirectory->isAOCol && segmentFileNum == - ((AOCSFileSegInfo*)fsInfo)->segno) + ((AOCSFileSegInfo *) fsInfo)->segno) break; } Assert(fsInfo != NULL); /* - * Search the btree index to find the minipage that contains - * the rowNum. We find the minipages for all column groups, since - * currently we will need to access all columns at the same time. + * Search the btree index to find the minipage that contains the rowNum. + * We find the minipages for all column groups, since currently we will + * need to access all columns at the same time. */ heapTupleDesc = RelationGetDescr(blkdirRel); @@ -570,28 +573,27 @@ AppendOnlyBlockDirectory_GetEntry( scanKeys[0].sk_argument = Int32GetDatum(segmentFileNum); scanKeys[1].sk_argument = Int32GetDatum(tmpGroupNo); scanKeys[2].sk_argument = Int64GetDatum(rowNum); - + idxScanDesc = index_beginscan(blkdirRel, blkdirIdx, blockDirectory->appendOnlyMetaDataSnapshot, numScanKeys, scanKeys); - + tuple = index_getnext(idxScanDesc, BackwardScanDirection); if (tuple != NULL) { /* - * MPP-17061: we need to update currentSegmentFileNum - * & currentSegmentFileInfo at the same time when we - * load the minipage for the block directory entry we - * found, otherwise we would risk having inconsistency - * between currentSegmentFileNum/currentSegmentFileInfo - * and minipage contents, which would cause wrong block - * header offset being returned in following block - * directory entry look up. + * MPP-17061: we need to update currentSegmentFileNum & + * currentSegmentFileInfo at the same time when we load the + * minipage for the block directory entry we found, otherwise we + * would risk having inconsistency between + * currentSegmentFileNum/currentSegmentFileInfo and minipage + * contents, which would cause wrong block header offset being + * returned in following block directory entry look up. */ blockDirectory->currentSegmentFileNum = segmentFileNum; blockDirectory->currentSegmentFileInfo = fsInfo; - + extract_minipage(blockDirectory, tuple, heapTupleDesc, @@ -602,18 +604,19 @@ AppendOnlyBlockDirectory_GetEntry( /* MPP-17061: index look up failed, row is invisible */ index_endscan(idxScanDesc); return false; - } + } index_endscan(idxScanDesc); } { MinipagePerColumnGroup *minipageInfo; + minipageInfo = &blockDirectory->minipages[columnGroupNo]; /* - * Perform a binary search over the minipage to find - * the entry about the AO block. + * Perform a binary search over the minipage to find the entry about + * the AO block. */ entry_no = find_minipage_entry(minipageInfo->minipage, minipageInfo->numMinipageEntries, @@ -636,14 +639,14 @@ AppendOnlyBlockDirectory_GetEntry( entry_no, directoryEntry); } - + return false; } /* * AppendOnlyBlockDirectory_InsertEntry * - * Insert an entry to the block directory. This entry is appended to the + * Insert an entry to the block directory. This entry is appended to the * in-memory minipage. If the minipage is full, it is written to the block * directory relation on disk. After that, the new entry is added to the * new in-memory minipage. @@ -661,12 +664,12 @@ AppendOnlyBlockDirectory_GetEntry( */ bool AppendOnlyBlockDirectory_InsertEntry( - AppendOnlyBlockDirectory *blockDirectory, - int columnGroupNo, - int64 firstRowNum, - int64 fileOffset, - int64 rowCount, - bool addColAction) + AppendOnlyBlockDirectory *blockDirectory, + int columnGroupNo, + int64 firstRowNum, + int64 fileOffset, + int64 rowCount, + bool addColAction) { return insert_new_entry(blockDirectory, columnGroupNo, firstRowNum, fileOffset, rowCount, addColAction); @@ -679,21 +682,21 @@ AppendOnlyBlockDirectory_InsertEntry( */ static bool insert_new_entry( - AppendOnlyBlockDirectory *blockDirectory, - int columnGroupNo, - int64 firstRowNum, - int64 fileOffset, - int64 rowCount, - bool addColAction) + AppendOnlyBlockDirectory *blockDirectory, + int columnGroupNo, + int64 firstRowNum, + int64 fileOffset, + int64 rowCount, + bool addColAction) { MinipageEntry *entry = NULL; MinipagePerColumnGroup *minipageInfo; - int minipageIndex; - int lastEntryNo; + int minipageIndex; + int lastEntryNo; if (rowCount == 0) return false; - + if (blockDirectory->blkdirRel == NULL || blockDirectory->blkdirIdx == NULL) return false; @@ -705,11 +708,11 @@ insert_new_entry( * addColAction. We need to map it to the right index in the minipage * array. */ - int numExistingCols = blockDirectory->aoRel->rd_att->natts - - blockDirectory->numColumnGroups; + int numExistingCols = blockDirectory->aoRel->rd_att->natts - + blockDirectory->numColumnGroups; Assert((numExistingCols >= 0) && (numExistingCols - 1 < columnGroupNo)); - minipageIndex = columnGroupNo-numExistingCols; + minipageIndex = columnGroupNo - numExistingCols; } else { @@ -717,7 +720,7 @@ insert_new_entry( } minipageInfo = &blockDirectory->minipages[minipageIndex]; - Assert(minipageInfo->numMinipageEntries <= (uint32)NUM_MINIPAGE_ENTRIES); + Assert(minipageInfo->numMinipageEntries <= (uint32) NUM_MINIPAGE_ENTRIES); lastEntryNo = minipageInfo->numMinipageEntries - 1; if (lastEntryNo >= 0) @@ -726,29 +729,29 @@ insert_new_entry( Assert(entry->firstRowNum < firstRowNum); Assert(entry->fileOffset < fileOffset); - + if (gp_blockdirectory_entry_min_range > 0 && fileOffset - entry->fileOffset < gp_blockdirectory_entry_min_range) return true; - + /* Update the rowCount in the latest entry */ Assert(entry->rowCount <= firstRowNum - entry->firstRowNum); - ereportif(Debug_appendonly_print_blockdirectory, LOG, - (errmsg("Append-only block directory update entry: " - "(firstRowNum, columnGroupNo, fileOffset, rowCount) = (" INT64_FORMAT - ", %d, " INT64_FORMAT ", " INT64_FORMAT ") at index %d to " - "(firstRowNum, columnGroupNo, fileOffset, rowCount) = (" INT64_FORMAT - ", %d, " INT64_FORMAT ", " INT64_FORMAT ")", - entry->firstRowNum, columnGroupNo, entry->fileOffset, entry->rowCount, - minipageInfo->numMinipageEntries - 1, - entry->firstRowNum, columnGroupNo, entry->fileOffset, - firstRowNum - entry->firstRowNum))); - + ereportif(Debug_appendonly_print_blockdirectory, LOG, + (errmsg("Append-only block directory update entry: " + "(firstRowNum, columnGroupNo, fileOffset, rowCount) = (" INT64_FORMAT + ", %d, " INT64_FORMAT ", " INT64_FORMAT ") at index %d to " + "(firstRowNum, columnGroupNo, fileOffset, rowCount) = (" INT64_FORMAT + ", %d, " INT64_FORMAT ", " INT64_FORMAT ")", + entry->firstRowNum, columnGroupNo, entry->fileOffset, entry->rowCount, + minipageInfo->numMinipageEntries - 1, + entry->firstRowNum, columnGroupNo, entry->fileOffset, + firstRowNum - entry->firstRowNum))); + entry->rowCount = firstRowNum - entry->firstRowNum; } - - if (minipageInfo->numMinipageEntries >= (uint32)gp_blockdirectory_minipage_size) + + if (minipageInfo->numMinipageEntries >= (uint32) gp_blockdirectory_minipage_size) { write_minipage(blockDirectory, columnGroupNo, minipageInfo); @@ -762,22 +765,22 @@ insert_new_entry( minipageInfo->numMinipageEntries * sizeof(MinipageEntry)); minipageInfo->numMinipageEntries = 0; } - - Assert(minipageInfo->numMinipageEntries < (uint32)gp_blockdirectory_minipage_size); + + Assert(minipageInfo->numMinipageEntries < (uint32) gp_blockdirectory_minipage_size); entry = &(minipageInfo->minipage->entry[minipageInfo->numMinipageEntries]); entry->firstRowNum = firstRowNum; entry->fileOffset = fileOffset; entry->rowCount = rowCount; - + minipageInfo->numMinipageEntries++; - + ereportif(Debug_appendonly_print_blockdirectory, LOG, - (errmsg("Append-only block directory insert entry: " - "(firstRowNum, columnGroupNo, fileOffset, rowCount) = (" INT64_FORMAT - ", %d, " INT64_FORMAT ", " INT64_FORMAT ") at index %d", - entry->firstRowNum, columnGroupNo, entry->fileOffset, entry->rowCount, - minipageInfo->numMinipageEntries - 1))); + (errmsg("Append-only block directory insert entry: " + "(firstRowNum, columnGroupNo, fileOffset, rowCount) = (" INT64_FORMAT + ", %d, " INT64_FORMAT ", " INT64_FORMAT ") at index %d", + entry->firstRowNum, columnGroupNo, entry->fileOffset, entry->rowCount, + minipageInfo->numMinipageEntries - 1))); return true; } @@ -787,38 +790,40 @@ insert_new_entry( * * Deletes all block directory entries for given segment file of an * append-only relation. - */ + */ void AppendOnlyBlockDirectory_DeleteSegmentFile(Relation aoRel, - Snapshot snapshot, - int segno, - int columnGroupNo) + Snapshot snapshot, + int segno, + int columnGroupNo) { Assert(OidIsValid(aoRel->rd_appendonly->blkdirrelid)); Assert(OidIsValid(aoRel->rd_appendonly->blkdiridxid)); - Relation blkdirRel = heap_open(aoRel->rd_appendonly->blkdirrelid, RowExclusiveLock); - Relation blkdirIdx = index_open(aoRel->rd_appendonly->blkdiridxid, RowExclusiveLock); + Relation blkdirRel = heap_open(aoRel->rd_appendonly->blkdirrelid, RowExclusiveLock); + Relation blkdirIdx = index_open(aoRel->rd_appendonly->blkdiridxid, RowExclusiveLock); ScanKeyData scanKey; + ScanKeyInit(&scanKey, - 1, /* segno */ - BTEqualStrategyNumber, - F_INT4EQ, - Int32GetDatum(segno)); + 1, /* segno */ + BTEqualStrategyNumber, + F_INT4EQ, + Int32GetDatum(segno)); IndexScanDesc indexScan = index_beginscan( - blkdirRel, - blkdirIdx, - snapshot, - 1, - &scanKey); - - HeapTuple tuple = NULL; + blkdirRel, + blkdirIdx, + snapshot, + 1, + &scanKey); + + HeapTuple tuple = NULL; + while ((tuple = index_getnext(indexScan, ForwardScanDirection)) != NULL) { simple_heap_delete(blkdirRel, - &tuple->t_self); + &tuple->t_self); } index_endscan(indexScan); @@ -837,47 +842,48 @@ init_scankeys(TupleDesc tupleDesc, int nkeys, ScanKey scanKeys, StrategyNumber *strategyNumbers) { - int keyNo; + int keyNo; Assert(nkeys <= tupleDesc->natts); - - for (keyNo = 0; keyNo < nkeys; keyNo ++) + + for (keyNo = 0; keyNo < nkeys; keyNo++) { - ScanKey scanKey = (ScanKey)(((char *)scanKeys) + - keyNo * sizeof(ScanKeyData)); + ScanKey scanKey = (ScanKey) (((char *) scanKeys) + + keyNo * sizeof(ScanKeyData)); RegProcedure opfuncid; StrategyNumber strategyNumber = strategyNumbers[keyNo]; Assert(strategyNumber <= BTMaxStrategyNumber && strategyNumber != InvalidStrategy); - + if (strategyNumber == BTEqualStrategyNumber) { opfuncid = equality_oper_funcid(tupleDesc->attrs[keyNo]->atttypid); ScanKeyEntryInitialize(scanKey, - 0, /* sk_flag */ - keyNo + 1, /* attribute number to scan */ - BTEqualStrategyNumber, /* strategy */ - InvalidOid, /* strategy subtype */ - opfuncid, /* reg proc to use */ - 0 /* constant */ + 0, /* sk_flag */ + keyNo + 1, /* attribute number to scan */ + BTEqualStrategyNumber, /* strategy */ + InvalidOid, /* strategy subtype */ + opfuncid, /* reg proc to use */ + 0 /* constant */ ); } else { - Oid gtOid, leOid; - + Oid gtOid, + leOid; + gtOid = reverse_ordering_oper_opid(tupleDesc->attrs[keyNo]->atttypid); leOid = get_negator(gtOid); opfuncid = get_opcode(leOid); - + ScanKeyEntryInitialize(scanKey, - 0, /* sk_flag */ - keyNo + 1, /* attribute number to scan */ - strategyNumber, /* strategy */ - InvalidOid, /* strategy subtype */ - opfuncid, /* reg proc to use */ - 0 /* constant */ + 0, /* sk_flag */ + keyNo + 1, /* attribute number to scan */ + strategyNumber, /* strategy */ + InvalidOid, /* strategy subtype */ + opfuncid, /* reg proc to use */ + 0 /* constant */ ); } } @@ -901,14 +907,14 @@ copy_out_minipage(MinipagePerColumnGroup *minipageInfo, value = (struct varlena *) DatumGetPointer(minipage_value); detoast_value = pg_detoast_datum(value); - Assert( VARSIZE(detoast_value) <= minipage_size(NUM_MINIPAGE_ENTRIES)); + Assert(VARSIZE(detoast_value) <= minipage_size(NUM_MINIPAGE_ENTRIES)); memcpy(minipageInfo->minipage, detoast_value, VARSIZE(detoast_value)); if (detoast_value != value) pfree(detoast_value); - + Assert(minipageInfo->minipage->nEntry <= NUM_MINIPAGE_ENTRIES); - + minipageInfo->numMinipageEntries = minipageInfo->minipage->nEntry; } @@ -925,15 +931,17 @@ extract_minipage(AppendOnlyBlockDirectory *blockDirectory, TupleDesc tupleDesc, int columnGroupNo) { - Datum *values = blockDirectory->values; - bool *nulls = blockDirectory->nulls; + Datum *values = blockDirectory->values; + bool *nulls = blockDirectory->nulls; MinipagePerColumnGroup *minipageInfo = - &blockDirectory->minipages[columnGroupNo]; + &blockDirectory->minipages[columnGroupNo]; FileSegInfo *fsInfo = blockDirectory->currentSegmentFileInfo; - int64 eof; - int start, end, mid=0; - bool found = false; - + int64 eof; + int start, + end, + mid = 0; + bool found = false; + heap_deform_tuple(tuple, tupleDesc, values, nulls); Assert(blockDirectory->currentSegmentFileNum == @@ -949,15 +957,15 @@ extract_minipage(AppendOnlyBlockDirectory *blockDirectory, ItemPointerCopy(&tuple->t_self, &minipageInfo->tupleTid); /* - * When crashes during inserts, or cancellation during inserts, - * there are out-of-date minipage entries in the block directory. - * We reset those entries here. + * When crashes during inserts, or cancellation during inserts, there are + * out-of-date minipage entries in the block directory. We reset those + * entries here. */ Assert(fsInfo != NULL); if (!blockDirectory->isAOCol) eof = fsInfo->eof; else - eof = ((AOCSFileSegInfo *)fsInfo)->vpinfo.entry[columnGroupNo].eof; + eof = ((AOCSFileSegInfo *) fsInfo)->vpinfo.entry[columnGroupNo].eof; start = 0; end = minipageInfo->numMinipageEntries - 1; @@ -996,25 +1004,25 @@ load_last_minipage(AppendOnlyBlockDirectory *blockDirectory, int64 lastSequence, int columnGroupNo) { - Relation blkdirRel = blockDirectory->blkdirRel; - Relation blkdirIdx = blockDirectory->blkdirIdx; - TupleDesc heapTupleDesc; + Relation blkdirRel = blockDirectory->blkdirRel; + Relation blkdirIdx = blockDirectory->blkdirIdx; + TupleDesc heapTupleDesc; IndexScanDesc idxScanDesc; - HeapTuple tuple = NULL; + HeapTuple tuple = NULL; MemoryContext oldcxt; - int numScanKeys = blockDirectory->numScanKeys; - ScanKey scanKeys = blockDirectory->scanKeys; + int numScanKeys = blockDirectory->numScanKeys; + ScanKey scanKeys = blockDirectory->scanKeys; #ifdef USE_ASSERT_CHECKING StrategyNumber *strategyNumbers = blockDirectory->strategyNumbers; -#endif /* USE_ASSERT_CHECKING */ - +#endif /* USE_ASSERT_CHECKING */ + Assert(blockDirectory->aoRel != NULL); Assert(blockDirectory->blkdirRel != NULL); Assert(blockDirectory->blkdirIdx != NULL); oldcxt = MemoryContextSwitchTo(blockDirectory->memoryContext); - + heapTupleDesc = RelationGetDescr(blkdirRel); Assert(numScanKeys == 3); @@ -1025,20 +1033,20 @@ load_last_minipage(AppendOnlyBlockDirectory *blockDirectory, Assert(strategyNumbers != NULL); if (lastSequence == 0) lastSequence = 1; - + scanKeys[0].sk_argument = Int32GetDatum(blockDirectory->currentSegmentFileNum); scanKeys[1].sk_argument = Int32GetDatum(columnGroupNo); scanKeys[2].sk_argument = Int64GetDatum(lastSequence); /* - * Search the btree to find the entry in the block directory - * that contains the last minipage. + * Search the btree to find the entry in the block directory that contains + * the last minipage. */ idxScanDesc = index_beginscan(blkdirRel, blkdirIdx, blockDirectory->appendOnlyMetaDataSnapshot, numScanKeys, scanKeys); - + tuple = index_getnext(idxScanDesc, BackwardScanDirection); if (tuple != NULL) { @@ -1047,17 +1055,17 @@ load_last_minipage(AppendOnlyBlockDirectory *blockDirectory, heapTupleDesc, columnGroupNo); } - + index_endscan(idxScanDesc); MemoryContextSwitchTo(oldcxt); ereportif(Debug_appendonly_print_blockdirectory, LOG, - (errmsg("Append-only block directory load last minipage: " - "(columnGroupNo, lastSequence, nEntries) = (%d, " INT64_FORMAT ", %u)", - columnGroupNo, lastSequence, - blockDirectory->minipages[columnGroupNo].numMinipageEntries))); - + (errmsg("Append-only block directory load last minipage: " + "(columnGroupNo, lastSequence, nEntries) = (%d, " INT64_FORMAT ", %u)", + columnGroupNo, lastSequence, + blockDirectory->minipages[columnGroupNo].numMinipageEntries))); + } /* @@ -1072,22 +1080,23 @@ find_minipage_entry(Minipage *minipage, uint32 numEntries, int64 rowNum) { - int start_no, end_no; - int entry_no; + int start_no, + end_no; + int entry_no; MinipageEntry *entry; - + start_no = 0; end_no = numEntries - 1; while (start_no <= end_no) { entry_no = start_no + (end_no - start_no + 1) / 2; Assert(entry_no >= start_no && entry_no <= end_no); - + entry = &(minipage->entry[entry_no]); - + Assert(entry->firstRowNum > 0); Assert(entry->rowCount > 0); - + if (entry->firstRowNum <= rowNum && entry->firstRowNum + entry->rowCount > rowNum) break; @@ -1100,7 +1109,7 @@ find_minipage_entry(Minipage *minipage, start_no = entry_no + 1; } } - + if (start_no <= end_no) return entry_no; else @@ -1116,19 +1125,19 @@ static void write_minipage(AppendOnlyBlockDirectory *blockDirectory, int columnGroupNo, MinipagePerColumnGroup *minipageInfo) { - HeapTuple tuple; + HeapTuple tuple; MemoryContext oldcxt; - Datum *values = blockDirectory->values; - bool *nulls = blockDirectory->nulls; - Relation blkdirRel = blockDirectory->blkdirRel; - TupleDesc heapTupleDesc = RelationGetDescr(blkdirRel); - + Datum *values = blockDirectory->values; + bool *nulls = blockDirectory->nulls; + Relation blkdirRel = blockDirectory->blkdirRel; + TupleDesc heapTupleDesc = RelationGetDescr(blkdirRel); + Assert(minipageInfo->numMinipageEntries > 0); oldcxt = MemoryContextSwitchTo(blockDirectory->memoryContext); - + Assert(blkdirRel != NULL); - + values[Anum_pg_aoblkdir_segno - 1] = Int32GetDatum(blockDirectory->currentSegmentFileNum); nulls[Anum_pg_aoblkdir_segno - 1] = false; @@ -1153,41 +1162,41 @@ write_minipage(AppendOnlyBlockDirectory *blockDirectory, nulls, NULL, NULL); - + /* - * Write out the minipage to the block directory relation. - * If this minipage is already in the relation, we update - * the row. Otherwise, a new row is inserted. + * Write out the minipage to the block directory relation. If this + * minipage is already in the relation, we update the row. Otherwise, a + * new row is inserted. */ if (ItemPointerIsValid(&minipageInfo->tupleTid)) { ereportif(Debug_appendonly_print_blockdirectory, LOG, - (errmsg("Append-only block directory update a minipage: " - "(segno, columnGroupNo, nEntries, firstRowNum) = " - "(%d, %d, %u, " INT64_FORMAT ")", - blockDirectory->currentSegmentFileNum, - columnGroupNo, minipageInfo->numMinipageEntries, - minipageInfo->minipage->entry[0].firstRowNum))); + (errmsg("Append-only block directory update a minipage: " + "(segno, columnGroupNo, nEntries, firstRowNum) = " + "(%d, %d, %u, " INT64_FORMAT ")", + blockDirectory->currentSegmentFileNum, + columnGroupNo, minipageInfo->numMinipageEntries, + minipageInfo->minipage->entry[0].firstRowNum))); simple_heap_update(blkdirRel, &minipageInfo->tupleTid, tuple); } else { ereportif(Debug_appendonly_print_blockdirectory, LOG, - (errmsg("Append-only block directory insert a minipage: " - "(segno, columnGroupNo, nEntries, firstRowNum) = " - "(%d, %d, %u, " INT64_FORMAT ")", - blockDirectory->currentSegmentFileNum, - columnGroupNo, minipageInfo->numMinipageEntries, - minipageInfo->minipage->entry[0].firstRowNum))); + (errmsg("Append-only block directory insert a minipage: " + "(segno, columnGroupNo, nEntries, firstRowNum) = " + "(%d, %d, %u, " INT64_FORMAT ")", + blockDirectory->currentSegmentFileNum, + columnGroupNo, minipageInfo->numMinipageEntries, + minipageInfo->minipage->entry[0].firstRowNum))); simple_heap_insert(blkdirRel, tuple); } - + CatalogUpdateIndexes(blkdirRel, tuple); - + heap_freetuple(tuple); - + MemoryContextSwitchTo(oldcxt); } @@ -1195,9 +1204,9 @@ write_minipage(AppendOnlyBlockDirectory *blockDirectory, void AppendOnlyBlockDirectory_End_forInsert( - AppendOnlyBlockDirectory *blockDirectory) + AppendOnlyBlockDirectory *blockDirectory) { - int groupNo; + int groupNo; if (blockDirectory->blkdirRel == NULL || blockDirectory->blkdirIdx == NULL) @@ -1205,34 +1214,34 @@ AppendOnlyBlockDirectory_End_forInsert( for (groupNo = 0; groupNo < blockDirectory->numColumnGroups; groupNo++) { MinipagePerColumnGroup *minipageInfo = - &blockDirectory->minipages[groupNo]; - + &blockDirectory->minipages[groupNo]; + if (minipageInfo->numMinipageEntries > 0) { write_minipage(blockDirectory, groupNo, minipageInfo); ereportif(Debug_appendonly_print_blockdirectory, LOG, - (errmsg("Append-only block directory end of insert write minipage: " - "(columnGroupNo, nEntries) = (%d, %u)", - groupNo, minipageInfo->numMinipageEntries))); + (errmsg("Append-only block directory end of insert write minipage: " + "(columnGroupNo, nEntries) = (%d, %u)", + groupNo, minipageInfo->numMinipageEntries))); } - + pfree(minipageInfo->minipage); } ereportif(Debug_appendonly_print_blockdirectory, LOG, - (errmsg("Append-only block directory end for insert: " - "(segno, numColumnGroups, isAOCol)=" - "(%d, %d, %d)", - blockDirectory->currentSegmentFileNum, - blockDirectory->numColumnGroups, - blockDirectory->isAOCol))); + (errmsg("Append-only block directory end for insert: " + "(segno, numColumnGroups, isAOCol)=" + "(%d, %d, %d)", + blockDirectory->currentSegmentFileNum, + blockDirectory->numColumnGroups, + blockDirectory->isAOCol))); pfree(blockDirectory->values); pfree(blockDirectory->nulls); pfree(blockDirectory->minipages); pfree(blockDirectory->scanKeys); pfree(blockDirectory->strategyNumbers); - + index_close(blockDirectory->blkdirIdx, RowExclusiveLock); heap_close(blockDirectory->blkdirRel, RowExclusiveLock); @@ -1241,10 +1250,10 @@ AppendOnlyBlockDirectory_End_forInsert( void AppendOnlyBlockDirectory_End_forSearch( - AppendOnlyBlockDirectory *blockDirectory) + AppendOnlyBlockDirectory *blockDirectory) { - int groupNo; - + int groupNo; + if (blockDirectory->blkdirRel == NULL || blockDirectory->blkdirIdx == NULL) return; @@ -1256,19 +1265,19 @@ AppendOnlyBlockDirectory_End_forSearch( } ereportif(Debug_appendonly_print_blockdirectory, LOG, - (errmsg("Append-only block directory end for search: " - "(totalSegfiles, numColumnGroups, isAOCol)=" - "(%d, %d, %d)", - blockDirectory->totalSegfiles, - blockDirectory->numColumnGroups, - blockDirectory->isAOCol))); + (errmsg("Append-only block directory end for search: " + "(totalSegfiles, numColumnGroups, isAOCol)=" + "(%d, %d, %d)", + blockDirectory->totalSegfiles, + blockDirectory->numColumnGroups, + blockDirectory->isAOCol))); pfree(blockDirectory->values); pfree(blockDirectory->nulls); pfree(blockDirectory->minipages); pfree(blockDirectory->scanKeys); pfree(blockDirectory->strategyNumbers); - + index_close(blockDirectory->blkdirIdx, AccessShareLock); heap_close(blockDirectory->blkdirRel, AccessShareLock); @@ -1277,13 +1286,13 @@ AppendOnlyBlockDirectory_End_forSearch( void AppendOnlyBlockDirectory_End_addCol( - AppendOnlyBlockDirectory *blockDirectory) + AppendOnlyBlockDirectory *blockDirectory) { - int groupNo; + int groupNo; /* newly added columns have attribute number beginning with this */ - AttrNumber colno = blockDirectory->aoRel->rd_att->natts - - blockDirectory->numColumnGroups; + AttrNumber colno = blockDirectory->aoRel->rd_att->natts - + blockDirectory->numColumnGroups; if (blockDirectory->blkdirRel == NULL || blockDirectory->blkdirIdx == NULL) @@ -1291,7 +1300,7 @@ AppendOnlyBlockDirectory_End_addCol( for (groupNo = 0; groupNo < blockDirectory->numColumnGroups; groupNo++) { MinipagePerColumnGroup *minipageInfo = - &blockDirectory->minipages[groupNo]; + &blockDirectory->minipages[groupNo]; if (minipageInfo->numMinipageEntries > 0) { @@ -1305,26 +1314,26 @@ AppendOnlyBlockDirectory_End_addCol( } ereportif(Debug_appendonly_print_blockdirectory, LOG, - (errmsg("Append-only block directory end for insert: " - "(segno, numColumnGroups, isAOCol)=" - "(%d, %d, %d)", - blockDirectory->currentSegmentFileNum, - blockDirectory->numColumnGroups, - blockDirectory->isAOCol))); + (errmsg("Append-only block directory end for insert: " + "(segno, numColumnGroups, isAOCol)=" + "(%d, %d, %d)", + blockDirectory->currentSegmentFileNum, + blockDirectory->numColumnGroups, + blockDirectory->isAOCol))); pfree(blockDirectory->values); pfree(blockDirectory->nulls); pfree(blockDirectory->minipages); pfree(blockDirectory->scanKeys); pfree(blockDirectory->strategyNumbers); + /* - * We already hold transaction-scope exclusive lock on the AOCS - * relation. Let's defer release of locks on block directory as - * well until the end of alter-table transaction. + * We already hold transaction-scope exclusive lock on the AOCS relation. + * Let's defer release of locks on block directory as well until the end + * of alter-table transaction. */ index_close(blockDirectory->blkdirIdx, NoLock); heap_close(blockDirectory->blkdirRel, NoLock); MemoryContextDelete(blockDirectory->memoryContext); } - diff --git a/src/backend/access/appendonly/appendonlytid.c b/src/backend/access/appendonly/appendonlytid.c index 6ee32fccd4..e477983698 100755 --- a/src/backend/access/appendonly/appendonlytid.c +++ b/src/backend/access/appendonly/appendonlytid.c @@ -50,8 +50,8 @@ gpaotidin(PG_FUNCTION_ARGS) char *p, *coord[NAOTIDARGS]; int i; - AOTupleId *result; - unsigned int segmentFileNum; + AOTupleId *result; + unsigned int segmentFileNum; int64 rowNum; char *badp; @@ -65,7 +65,7 @@ gpaotidin(PG_FUNCTION_ARGS) errmsg("invalid input syntax for type gpaotid: \"%s\"", str))); -// UNDONE: Move +/* UNDONE: Move */ #define AOTupleId_MaxSegmentFileNum 127 errno = 0; @@ -85,11 +85,11 @@ gpaotidin(PG_FUNCTION_ARGS) errmsg("invalid input syntax for type gpaotid: \"%s\"", str))); - result = (AOTupleId*) palloc(sizeof(AOTupleId)); + result = (AOTupleId *) palloc(sizeof(AOTupleId)); AOTupleIdInit_Init(result); - AOTupleIdInit_segmentFileNum(result,segmentFileNum); - AOTupleIdInit_rowNum(result,rowNum); + AOTupleIdInit_segmentFileNum(result, segmentFileNum); + AOTupleIdInit_rowNum(result, rowNum); PG_RETURN_AOTID(result); } @@ -101,9 +101,9 @@ gpaotidin(PG_FUNCTION_ARGS) Datum gpaotidout(PG_FUNCTION_ARGS) { - AOTupleId *aoTupleId = PG_GETARG_AOTID(0); - unsigned int segmentFileNum; - int64 rowNum; + AOTupleId *aoTupleId = PG_GETARG_AOTID(0); + unsigned int segmentFileNum; + int64 rowNum; char buf[32]; segmentFileNum = AOTupleIdGet_segmentFileNum(aoTupleId); @@ -122,19 +122,19 @@ Datum gpaotidrecv(PG_FUNCTION_ARGS) { StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); - AOTupleId *result; - unsigned int segmentFileNum; + AOTupleId *result; + unsigned int segmentFileNum; int64 rowNum; segmentFileNum = pq_getmsgint(buf, sizeof(segmentFileNum)); - // UNDONE: pg_getmsgint doesn't handle 8 byte integers... + /* UNDONE: pg_getmsgint doesn't handle 8 byte integers... */ rowNum = pq_getmsgint(buf, sizeof(rowNum)); - result = (AOTupleId*) palloc(sizeof(ItemPointerData)); + result = (AOTupleId *) palloc(sizeof(ItemPointerData)); AOTupleIdInit_Init(result); - AOTupleIdInit_segmentFileNum(result,segmentFileNum); - AOTupleIdInit_rowNum(result,rowNum); + AOTupleIdInit_segmentFileNum(result, segmentFileNum); + AOTupleIdInit_rowNum(result, rowNum); PG_RETURN_AOTID(result); } @@ -145,9 +145,9 @@ gpaotidrecv(PG_FUNCTION_ARGS) Datum gpaotidsend(PG_FUNCTION_ARGS) { - AOTupleId *aoTupleId = PG_GETARG_AOTID(0); - unsigned int segmentFileNum; - int64 rowNum; + AOTupleId *aoTupleId = PG_GETARG_AOTID(0); + unsigned int segmentFileNum; + int64 rowNum; StringInfoData buf; segmentFileNum = AOTupleIdGet_segmentFileNum(aoTupleId); @@ -155,7 +155,7 @@ gpaotidsend(PG_FUNCTION_ARGS) pq_begintypsend(&buf); pq_sendint(&buf, segmentFileNum, sizeof(segmentFileNum)); - // UNDONE: pq_sendint doesn't handle 8 byte integers... + /* UNDONE: pq_sendint doesn't handle 8 byte integers... */ pq_sendint(&buf, rowNum, sizeof(rowNum)); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } @@ -163,15 +163,16 @@ gpaotidsend(PG_FUNCTION_ARGS) #define MAX_AO_TUPLE_ID_BUFFER 25 static char AOTupleIdBuffer[MAX_AO_TUPLE_ID_BUFFER]; -char* AOTupleIdToString(AOTupleId * aoTupleId) +char * +AOTupleIdToString(AOTupleId *aoTupleId) { - int segmentFileNum = AOTupleIdGet_segmentFileNum(aoTupleId); - int64 rowNum = AOTupleIdGet_rowNum(aoTupleId); - int snprintfResult; + int segmentFileNum = AOTupleIdGet_segmentFileNum(aoTupleId); + int64 rowNum = AOTupleIdGet_rowNum(aoTupleId); + int snprintfResult; snprintfResult = snprintf(AOTupleIdBuffer, MAX_AO_TUPLE_ID_BUFFER, "(%d," INT64_FORMAT ")", - segmentFileNum, rowNum); + segmentFileNum, rowNum); Assert(snprintfResult >= 0); Assert(snprintfResult < MAX_AO_TUPLE_ID_BUFFER); diff --git a/src/backend/access/appendonly/appendonlywriter.c b/src/backend/access/appendonly/appendonlywriter.c index ce58797ca9..d0fbe3be4c 100644 --- a/src/backend/access/appendonly/appendonlywriter.c +++ b/src/backend/access/appendonly/appendonlywriter.c @@ -18,11 +18,11 @@ #include "postgres.h" #include "access/appendonly_compaction.h" -#include "access/appendonlytid.h" /* AOTupleId_MaxRowNum */ +#include "access/appendonlytid.h" /* AOTupleId_MaxRowNum */ #include "access/appendonlywriter.h" -#include "access/aocssegfiles.h" /* AOCS */ -#include "access/heapam.h" /* heap_open */ -#include "access/transam.h" /* InvalidTransactionId */ +#include "access/aocssegfiles.h" /* AOCS */ +#include "access/heapam.h" /* heap_open */ +#include "access/transam.h" /* InvalidTransactionId */ #include "access/xact.h" #include "catalog/pg_appendonly_fn.h" #include "catalog/pg_authid.h" @@ -32,7 +32,7 @@ #include "utils/lsyscache.h" #include "utils/snapmgr.h" -#include "cdb/cdbvars.h" /* Gp_role */ +#include "cdb/cdbvars.h" /* Gp_role */ #include "cdb/cdbdisp_query.h" #include "cdb/cdbdispatchresult.h" #include "cdb/cdbtm.h" @@ -42,13 +42,13 @@ /* * GUC variables */ -int MaxAppendOnlyTables; /* Max # of tables */ +int MaxAppendOnlyTables; /* Max # of tables */ /* * Global Variables */ -static HTAB *AppendOnlyHash; /* Hash of AO tables */ -AppendOnlyWriterData *AppendOnlyWriter; +static HTAB *AppendOnlyHash; /* Hash of AO tables */ +AppendOnlyWriterData *AppendOnlyWriter; static bool appendOnlyInsertXact = false; /* @@ -75,7 +75,7 @@ AppendOnlyWriterShmemSize(void) Insist(Gp_role == GP_ROLE_DISPATCH); /* The hash of append only relations */ - size = hash_estimate_size((Size)MaxAppendOnlyTables, + size = hash_estimate_size((Size) MaxAppendOnlyTables, sizeof(AORelHashEntryData)); /* The writer structure. */ @@ -102,12 +102,13 @@ InitAppendOnlyWriter(void) /* Create the writer structure. */ AppendOnlyWriter = (AppendOnlyWriterData *) - ShmemInitStruct("Append Only Writer Data", - sizeof(AppendOnlyWriterData), - &found); + ShmemInitStruct("Append Only Writer Data", + sizeof(AppendOnlyWriterData), + &found); if (found && AppendOnlyHash) - return; /* We are already initialized and have a hash table */ + return; /* We are already initialized and have a hash + * table */ /* Specify that we have no AO rel information yet. */ AppendOnlyWriter->num_existing_aorels = 0; @@ -132,8 +133,8 @@ InitAppendOnlyWriter(void) static bool AOHashTableInit(void) { - HASHCTL info; - int hash_flags; + HASHCTL info; + int hash_flags; Insist(Gp_role == GP_ROLE_DISPATCH); @@ -145,8 +146,8 @@ AOHashTableInit(void) hash_flags = (HASH_ELEM | HASH_FUNCTION); ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("AOHashTableInit: Creating hash table for %d append only tables", - MaxAppendOnlyTables))); + (errmsg("AOHashTableInit: Creating hash table for %d append only tables", + MaxAppendOnlyTables))); AppendOnlyHash = ShmemInitHash("Append Only Hash", MaxAppendOnlyTables, @@ -171,27 +172,26 @@ AOHashTableInit(void) static bool AORelCreateHashEntry(Oid relid) { - bool exists = false; - FileSegInfo **allfsinfo = NULL; - AOCSFileSegInfo **aocsallfsinfo = NULL; - int i; - int total_segfiles = 0; - AORelHashEntry aoHashEntry = NULL; - Relation aorel; - bool *awaiting_drop = NULL; + bool exists = false; + FileSegInfo **allfsinfo = NULL; + AOCSFileSegInfo **aocsallfsinfo = NULL; + int i; + int total_segfiles = 0; + AORelHashEntry aoHashEntry = NULL; + Relation aorel; + bool *awaiting_drop = NULL; Insist(Gp_role == GP_ROLE_DISPATCH); /* - * Momentarily release the AOSegFileLock so we can safely access the system catalog - * (i.e. without risking a deadlock). + * Momentarily release the AOSegFileLock so we can safely access the + * system catalog (i.e. without risking a deadlock). */ LWLockRelease(AOSegFileLock); /* - * Now get all the segment files information for this relation - * from the QD aoseg table. then update our segment file array - * in this hash entry. + * Now get all the segment files information for this relation from the QD + * aoseg table. then update our segment file array in this hash entry. */ aorel = heap_open(relid, RowExclusiveLock); @@ -219,13 +219,13 @@ AORelCreateHashEntry(Oid relid) * Note: Another session may have raced-in and created it. */ LWLockAcquire(AOSegFileLock, LW_EXCLUSIVE); - + aoHashEntry = AppendOnlyRelHashNew(relid, &exists); /* Does an entry for this relation exists already? exit early */ - if(exists) + if (exists) { - if(allfsinfo) + if (allfsinfo) { FreeAllSegFileInfo(allfsinfo, total_segfiles); pfree(allfsinfo); @@ -240,8 +240,8 @@ AORelCreateHashEntry(Oid relid) /* * If the new aoHashEntry pointer is NULL, then we are out of allowed # of - * AO tables used for write concurrently, and don't have any unused entries - * to free + * AO tables used for write concurrently, and don't have any unused + * entries to free */ if (!aoHashEntry) { @@ -252,11 +252,11 @@ AORelCreateHashEntry(Oid relid) Insist(aoHashEntry->relid == relid); aoHashEntry->txns_using_rel = 0; - + /* * Initialize all segfile array to zero */ - for (i = 0 ; i < MAX_AOREL_CONCURRENCY ; i++) + for (i = 0; i < MAX_AOREL_CONCURRENCY; i++) { aoHashEntry->relsegfiles[i].state = AVAILABLE; aoHashEntry->relsegfiles[i].xid = InvalidTransactionId; @@ -269,14 +269,15 @@ AORelCreateHashEntry(Oid relid) } /* - * update the tupcount and formatVersion of each 'segment' file in the append - * only hash according to the information in the pg_aoseg table. + * update the tupcount and formatVersion of each 'segment' file in the + * append only hash according to the information in the pg_aoseg table. */ - for (i = 0 ; i < total_segfiles; i++) + for (i = 0; i < total_segfiles; i++) { - int segno; - int64 total_tupcount; - int16 formatversion; + int segno; + int64 total_tupcount; + int16 formatversion; + if (allfsinfo) { segno = allfsinfo[i]->segno; @@ -294,8 +295,8 @@ AORelCreateHashEntry(Oid relid) if (awaiting_drop[segno]) { ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("Found segment num with awaiting drop state on at least one segment node: " - "relation %d, segno %d", relid, segno))); + (errmsg("Found segment num with awaiting drop state on at least one segment node: " + "relation %d, segno %d", relid, segno))); aoHashEntry->relsegfiles[segno].state = AWAITING_DROP_READY; } aoHashEntry->relsegfiles[segno].total_tupcount = total_tupcount; @@ -305,7 +306,7 @@ AORelCreateHashEntry(Oid relid) /* record the fact that another hash entry is now taken */ AppendOnlyWriter->num_existing_aorels++; - if(allfsinfo) + if (allfsinfo) { FreeAllSegFileInfo(allfsinfo, total_segfiles); pfree(allfsinfo); @@ -321,8 +322,8 @@ AORelCreateHashEntry(Oid relid) } ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("AORelCreateHashEntry: Created a hash entry for append-only " - "relation %d ", relid))); + (errmsg("AORelCreateHashEntry: Created a hash entry for append-only " + "relation %d ", relid))); return true; } @@ -337,8 +338,8 @@ AORelCreateHashEntry(Oid relid) bool AORelRemoveHashEntry(Oid relid) { - bool found; - void *aoentry; + bool found; + void *aoentry; Insist(Gp_role == GP_ROLE_DISPATCH); @@ -348,10 +349,10 @@ AORelRemoveHashEntry(Oid relid) &found); ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("AORelRemoveHashEntry: Remove hash entry for inactive append-only " - "relation %d (found %s)", - relid, - (aoentry != NULL ? "true" : "false")))); + (errmsg("AORelRemoveHashEntry: Remove hash entry for inactive append-only " + "relation %d (found %s)", + relid, + (aoentry != NULL ? "true" : "false")))); if (aoentry == NULL) return false; @@ -368,8 +369,8 @@ AORelRemoveHashEntry(Oid relid) static AORelHashEntry AORelLookupHashEntry(Oid relid) { - bool found; - AORelHashEntryData *aoentry; + bool found; + AORelHashEntryData *aoentry; Insist(Gp_role == GP_ROLE_DISPATCH); @@ -391,31 +392,31 @@ AORelLookupHashEntry(Oid relid) static AORelHashEntry AORelGetHashEntry(Oid relid) { - AORelHashEntryData *aoentry = AORelLookupHashEntry(relid); + AORelHashEntryData *aoentry = AORelLookupHashEntry(relid); - if(!aoentry) + if (!aoentry) ereport(ERROR, (errmsg("expected an AO hash entry for relid %d but " "found none", relid))); ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("AORelGetHashEntry: Retrieved hash entry for append-only relation " - "%d", relid))); + (errmsg("AORelGetHashEntry: Retrieved hash entry for append-only relation " + "%d", relid))); return (AORelHashEntry) aoentry; } -/* +/* * Gets or creates the AORelHashEntry. - * + * * Assumes that the AOSegFileLock is acquired. * The AOSegFileLock will still be acquired when this function returns, expect * if it errors out. */ -static AORelHashEntryData * +static AORelHashEntryData * AORelGetOrCreateHashEntry(Oid relid) { - - AORelHashEntryData *aoentry = NULL; + + AORelHashEntryData *aoentry = NULL; aoentry = AORelLookupHashEntry(relid); if (aoentry != NULL) @@ -423,22 +424,22 @@ AORelGetOrCreateHashEntry(Oid relid) return aoentry; } - /* + /* * We need to create a hash entry for this relation. * - * However, we need to access the pg_appendonly system catalog - * table, so AORelCreateHashEntry will carefully release the AOSegFileLock, - * gather the information, and then re-acquire AOSegFileLock. + * However, we need to access the pg_appendonly system catalog table, so + * AORelCreateHashEntry will carefully release the AOSegFileLock, gather + * the information, and then re-acquire AOSegFileLock. */ - if(!AORelCreateHashEntry(relid)) + if (!AORelCreateHashEntry(relid)) { LWLockRelease(AOSegFileLock); ereport(ERROR, (errmsg("can't have more than %d different append-only " - "tables open for writing data at the same time. " - "if tables are heavily partitioned or if your " - "workload requires, increase the value of " - "max_appendonly_tables and retry", - MaxAppendOnlyTables))); + "tables open for writing data at the same time. " + "if tables are heavily partitioned or if your " + "workload requires, increase the value of " + "max_appendonly_tables and retry", + MaxAppendOnlyTables))); } /* get the hash entry for this relation (must exist) */ @@ -456,36 +457,36 @@ AORelGetOrCreateHashEntry(Oid relid) static AORelHashEntry AppendOnlyRelHashNew(Oid relid, bool *exists) { - AORelHashEntryData *aorelentry = NULL; + AORelHashEntryData *aorelentry = NULL; Insist(Gp_role == GP_ROLE_DISPATCH); /* - * We do not want to exceed the max number of allowed entries since we don't - * drop entries when we're done with them (so we could reuse them). Note - * that the hash table will allow going past max_size (it's not a hard - * limit) so we do the check ourselves. + * We do not want to exceed the max number of allowed entries since we + * don't drop entries when we're done with them (so we could reuse them). + * Note that the hash table will allow going past max_size (it's not a + * hard limit) so we do the check ourselves. * * Therefore, check for this condition first and deal with it if exists. */ - if(AppendOnlyWriter->num_existing_aorels >= MaxAppendOnlyTables) + if (AppendOnlyWriter->num_existing_aorels >= MaxAppendOnlyTables) { /* see if there's already an existing entry for this relid */ aorelentry = AORelLookupHashEntry(relid); /* - * already have an entry for this rel? reuse it. - * don't have? try to drop an unused entry and create our entry there + * already have an entry for this rel? reuse it. don't have? try to + * drop an unused entry and create our entry there */ - if(aorelentry) + if (aorelentry) { - *exists = true; /* reuse */ + *exists = true; /* reuse */ return NULL; } else { HASH_SEQ_STATUS status; - AORelHashEntryData *hentry; + AORelHashEntryData *hentry; Assert(AppendOnlyWriter->num_existing_aorels == MaxAppendOnlyTables); @@ -496,13 +497,13 @@ AppendOnlyRelHashNew(Oid relid, bool *exists) while ((hentry = (AORelHashEntryData *) hash_seq_search(&status)) != NULL) { - if(hentry->txns_using_rel == 0) + if (hentry->txns_using_rel == 0) { ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("AppendOnlyRelHashNew: Appendonly Writer removing an " - "unused entry (rel %d) to make " - "room for a new one (rel %d)", - hentry->relid, relid))); + (errmsg("AppendOnlyRelHashNew: Appendonly Writer removing an " + "unused entry (rel %d) to make " + "room for a new one (rel %d)", + hentry->relid, relid))); /* remove this unused entry */ /* TODO: remove the LRU entry, not just any unused one */ @@ -538,7 +539,7 @@ AppendOnlyRelHashNew(Oid relid, bool *exists) exists); /* caller should check "exists" to know if relid entry exists already */ - if(*exists) + if (*exists) return NULL; return (AORelHashEntry) aorelentry; @@ -553,16 +554,17 @@ AppendOnlyRelHashNew(Oid relid, bool *exists) * rows we don't allow inserting more data into it anymore. */ static -int64 segfileMaxRowThreshold(void) +int64 +segfileMaxRowThreshold(void) { - int64 maxallowed = (int64)AOTupleId_MaxRowNum; + int64 maxallowed = (int64) AOTupleId_MaxRowNum; - if(maxallowed < 0) + if (maxallowed < 0) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("int64 out of range"))); - return (SEGFILE_CAPACITY_THRESHOLD * maxallowed); + return (SEGFILE_CAPACITY_THRESHOLD * maxallowed); } /* @@ -576,7 +578,7 @@ usedByConcurrentTransaction(AOSegfileStatus *segfilestat, int segno) { DistributedSnapshot *ds; DistributedTransactionId latestWriteXid = - segfilestat->latestWriteXid; + segfilestat->latestWriteXid; /* * If latestWriteXid is invalid, simple return false. @@ -584,47 +586,47 @@ usedByConcurrentTransaction(AOSegfileStatus *segfilestat, int segno) if (!TransactionIdIsValid(latestWriteXid)) { ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("usedByConcurrentTransaction: latestWriterXid %u of segno %d not in use, so it is NOT considered concurrent", - latestWriteXid, - segno))); + (errmsg("usedByConcurrentTransaction: latestWriterXid %u of segno %d not in use, so it is NOT considered concurrent", + latestWriteXid, + segno))); return false; } - + /* - * If the current transaction is started earlier than latestWriteXid, - * this segno is surely used by a concurrent transaction. So - * return true here. + * If the current transaction is started earlier than latestWriteXid, this + * segno is surely used by a concurrent transaction. So return true here. */ if (TransactionIdPrecedes(getDistributedTransactionId(), latestWriteXid)) { ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("usedByConcurrentTransaction: current distributed transaction id %u preceeds latestWriterXid %x of segno %d, so it is considered concurrent", - getDistributedTransactionId(), - latestWriteXid, - segno))); + (errmsg("usedByConcurrentTransaction: current distributed transaction id %u preceeds latestWriterXid %x of segno %d, so it is considered concurrent", + getDistributedTransactionId(), + latestWriteXid, + segno))); return true; } - + /* - * Obtain the snapshot that is taken at the beginning of the - * transaction. We can't simply call GetTransactionSnapshot() here because - * it will create a new distributed snapshot for non-serializable - * transaction isolation level, and it may be too late. + * Obtain the snapshot that is taken at the beginning of the transaction. + * We can't simply call GetTransactionSnapshot() here because it will + * create a new distributed snapshot for non-serializable transaction + * isolation level, and it may be too late. */ - Snapshot snapshot = NULL; - if (SerializableSnapshot == NULL) - { - Assert(LatestSnapshot == NULL); - snapshot = GetTransactionSnapshot(); + Snapshot snapshot = NULL; + + if (SerializableSnapshot == NULL) + { + Assert(LatestSnapshot == NULL); + snapshot = GetTransactionSnapshot(); } - else if (LatestSnapshot != NULL) - { - snapshot = LatestSnapshot; - } - else - { - snapshot = SerializableSnapshot; + else if (LatestSnapshot != NULL) + { + snapshot = LatestSnapshot; + } + else + { + snapshot = SerializableSnapshot; } if (Debug_appendonly_print_segfile_choice) @@ -638,11 +640,11 @@ usedByConcurrentTransaction(AOSegfileStatus *segfilestat, int segno) if (snapshot->haveDistribSnapshot) LogDistributedSnapshotInfo(snapshot, "Used snapshot: "); } - + if (!snapshot->haveDistribSnapshot) { - ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("usedByConcurrentTransaction: snapshot is not distributed, so it is NOT considered concurrent"))); + ereportif(Debug_appendonly_print_segfile_choice, LOG, + (errmsg("usedByConcurrentTransaction: snapshot is not distributed, so it is NOT considered concurrent"))); return false; } @@ -652,34 +654,34 @@ usedByConcurrentTransaction(AOSegfileStatus *segfilestat, int segno) if (latestWriteXid >= ds->xmax) { ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("usedByConcurrentTransaction: latestWriterXid %x of segno %d is >= distributed snapshot xmax %u, so it is considered concurrent", - latestWriteXid, - segno, - ds->xmax))); + (errmsg("usedByConcurrentTransaction: latestWriterXid %x of segno %d is >= distributed snapshot xmax %u, so it is considered concurrent", + latestWriteXid, + segno, + ds->xmax))); return true; } /* * If latestWriteXid is in in-progress, return true. */ - int32 inProgressCount = ds->count; - + int32 inProgressCount = ds->count; + for (int inProgressNo = 0; inProgressNo < inProgressCount; inProgressNo++) { if (latestWriteXid == ds->inProgressXidArray[inProgressNo]) { ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("usedByConcurrentTransaction: latestWriterXid %x of segno %d is equal to distributed snapshot in-flight %u, so it is considered concurrent", - latestWriteXid, - segno, - ds->inProgressXidArray[inProgressNo]))); + (errmsg("usedByConcurrentTransaction: latestWriterXid %x of segno %d is equal to distributed snapshot in-flight %u, so it is considered concurrent", + latestWriteXid, + segno, + ds->inProgressXidArray[inProgressNo]))); return true; } } - + ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("usedByConcurrentTransaction: snapshot can see latestWriteXid %u as committed, so it is NOT considered concurrent", - latestWriteXid))); + (errmsg("usedByConcurrentTransaction: snapshot can see latestWriteXid %u as committed, so it is NOT considered concurrent", + latestWriteXid))); return false; } @@ -688,7 +690,7 @@ DeregisterSegnoForCompactionDrop(Oid relid, List *compactedSegmentFileList) { TransactionId CurrentXid = GetTopTransactionId(); AORelHashEntryData *aoentry; - int i; + int i; Assert(Gp_role != GP_ROLE_EXECUTE); if (Gp_role == GP_ROLE_UTILITY) @@ -707,16 +709,16 @@ DeregisterSegnoForCompactionDrop(Oid relid, List *compactedSegmentFileList) Assert(aoentry); aoentry->txns_using_rel++; - for (i = 0 ; i < MAX_AOREL_CONCURRENCY ; i++) + for (i = 0; i < MAX_AOREL_CONCURRENCY; i++) { AOSegfileStatus *segfilestat = &aoentry->relsegfiles[i]; if (list_member_int(compactedSegmentFileList, i)) { ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("Deregister segno %d for drop " - "relation \"%s\" (%d)", i, - get_rel_name(relid), relid))); + (errmsg("Deregister segno %d for drop " + "relation \"%s\" (%d)", i, + get_rel_name(relid), relid))); Assert(segfilestat->state == COMPACTED_AWAITING_DROP); segfilestat->xid = CurrentXid; @@ -724,7 +726,7 @@ DeregisterSegnoForCompactionDrop(Oid relid, List *compactedSegmentFileList) segfilestat->state = COMPACTED_DROP_SKIPPED; } } - + LWLockRelease(AOSegFileLock); return; } @@ -734,7 +736,7 @@ RegisterSegnoForCompactionDrop(Oid relid, List *compactedSegmentFileList) { TransactionId CurrentXid = GetTopTransactionId(); AORelHashEntryData *aoentry; - int i; + int i; Assert(Gp_role != GP_ROLE_EXECUTE); if (Gp_role == GP_ROLE_UTILITY) @@ -753,16 +755,16 @@ RegisterSegnoForCompactionDrop(Oid relid, List *compactedSegmentFileList) Assert(aoentry); aoentry->txns_using_rel++; - for (i = 0 ; i < MAX_AOREL_CONCURRENCY ; i++) + for (i = 0; i < MAX_AOREL_CONCURRENCY; i++) { AOSegfileStatus *segfilestat = &aoentry->relsegfiles[i]; if (list_member_int(compactedSegmentFileList, i)) { ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("Register segno %d for drop " - "relation \"%s\" (%d)", i, - get_rel_name(relid), relid))); + (errmsg("Register segno %d for drop " + "relation \"%s\" (%d)", i, + get_rel_name(relid), relid))); Assert(segfilestat->state == COMPACTED_AWAITING_DROP); segfilestat->xid = CurrentXid; @@ -770,7 +772,7 @@ RegisterSegnoForCompactionDrop(Oid relid, List *compactedSegmentFileList) segfilestat->state = DROP_USE; } } - + LWLockRelease(AOSegFileLock); return; } @@ -781,8 +783,8 @@ RegisterSegnoForCompactionDrop(Oid relid, List *compactedSegmentFileList) * This function determines which segment to use for the next * compaction run. * - * If a list with more than one entry is returned, all these segments should be - * compacted. This is only returned in utility mode as the usual ways to + * If a list with more than one entry is returned, all these segments should be + * compacted. This is only returned in utility mode as the usual ways to * determine a segment for compaction are not available. * If NIL is returned, no segment should be compacted. This usually * means that all segments are clean or empty. @@ -791,15 +793,15 @@ RegisterSegnoForCompactionDrop(Oid relid, List *compactedSegmentFileList) */ List * SetSegnoForCompaction(Relation rel, - List *compactedSegmentFileList, - List *insertedSegmentFileList, - bool *is_drop) + List *compactedSegmentFileList, + List *insertedSegmentFileList, + bool *is_drop) { TransactionId CurrentXid = GetTopTransactionId(); - int usesegno; - int i; + int usesegno; + int i; AORelHashEntryData *aoentry; - int64 segzero_tupcount = 0; + int64 segzero_tupcount = 0; Assert(Gp_role != GP_ROLE_EXECUTE); Assert(is_drop); @@ -813,8 +815,9 @@ SetSegnoForCompaction(Relation rel, else { /* Compact all segments */ - List *compaction_segno_list = NIL; - for (int i = 1; i < MAX_AOREL_CONCURRENCY;i++) + List *compaction_segno_list = NIL; + + for (int i = 1; i < MAX_AOREL_CONCURRENCY; i++) { compaction_segno_list = lappend_int(compaction_segno_list, i); } @@ -823,14 +826,14 @@ SetSegnoForCompaction(Relation rel, } ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("SetSegnoForCompaction: " - "Choosing a compaction segno for append-only " - "relation \"%s\" (%d)", - RelationGetRelationName(rel), RelationGetRelid(rel)))); - - /* - * On the first call in a vacuum run, get an updated estimate from segno 0. - * We do this before aquiring the AOSegFileLock. + (errmsg("SetSegnoForCompaction: " + "Choosing a compaction segno for append-only " + "relation \"%s\" (%d)", + RelationGetRelationName(rel), RelationGetRelid(rel)))); + + /* + * On the first call in a vacuum run, get an updated estimate from segno + * 0. We do this before aquiring the AOSegFileLock. */ if (!compactedSegmentFileList) { @@ -846,33 +849,34 @@ SetSegnoForCompaction(Relation rel, aoentry = AORelGetOrCreateHashEntry(RelationGetRelid(rel)); Assert(aoentry); - /* + /* * On the first call in a vacuum run, set the estimated for segno 0 */ if (!compactedSegmentFileList) { AOSegfileStatus *segfilestat = &aoentry->relsegfiles[0]; + segfilestat->total_tupcount = segzero_tupcount; } /* First: Always check if some segment is awaiting a drop */ usesegno = APPENDONLY_COMPACTION_SEGNO_INVALID; - for (i = 0; i < MAX_AOREL_CONCURRENCY ; i++) + for (i = 0; i < MAX_AOREL_CONCURRENCY; i++) { AOSegfileStatus *segfilestat = &aoentry->relsegfiles[i]; ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("segment file %i for append-only relation \"%s\" (%d): state %d", - i, RelationGetRelationName(rel), RelationGetRelid(rel), segfilestat->state))); + (errmsg("segment file %i for append-only relation \"%s\" (%d): state %d", + i, RelationGetRelationName(rel), RelationGetRelid(rel), segfilestat->state))); if ((segfilestat->state == AWAITING_DROP_READY || - segfilestat->state == COMPACTED_AWAITING_DROP) && + segfilestat->state == COMPACTED_AWAITING_DROP) && !usedByConcurrentTransaction(segfilestat, i)) { ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("Found segment awaiting drop for append-only relation \"%s\" (%d)", - RelationGetRelationName(rel), RelationGetRelid(rel)))); - + (errmsg("Found segment awaiting drop for append-only relation \"%s\" (%d)", + RelationGetRelationName(rel), RelationGetRelid(rel)))); + *is_drop = true; usesegno = i; break; @@ -881,43 +885,43 @@ SetSegnoForCompaction(Relation rel, if (usesegno <= 0) { - for (i = 0; i < MAX_AOREL_CONCURRENCY ; i++) + for (i = 0; i < MAX_AOREL_CONCURRENCY; i++) { AOSegfileStatus *segfilestat = &aoentry->relsegfiles[i]; - bool in_compaction_list = list_member_int(compactedSegmentFileList, i); - bool in_inserted_list = list_member_int(insertedSegmentFileList, i); - + bool in_compaction_list = list_member_int(compactedSegmentFileList, i); + bool in_inserted_list = list_member_int(insertedSegmentFileList, i); + ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("SetSegnoForCompaction: " - "Check segno %d for appendonly relation \"%s\" (%d): " - "total tupcount " INT64_FORMAT ", state %d, " - "in compacted list %d, in inserted list %d", - i, RelationGetRelationName(rel), RelationGetRelid(rel), - segfilestat->total_tupcount, segfilestat->state, - in_compaction_list, in_inserted_list))); - - /* + (errmsg("SetSegnoForCompaction: " + "Check segno %d for appendonly relation \"%s\" (%d): " + "total tupcount " INT64_FORMAT ", state %d, " + "in compacted list %d, in inserted list %d", + i, RelationGetRelationName(rel), RelationGetRelid(rel), + segfilestat->total_tupcount, segfilestat->state, + in_compaction_list, in_inserted_list))); + + /* * Select when there are tuples and it is available. */ if ((segfilestat->total_tupcount > 0) && - segfilestat->state == AVAILABLE && - !usedByConcurrentTransaction(segfilestat, i) && - !in_compaction_list && - !in_inserted_list) + segfilestat->state == AVAILABLE && + !usedByConcurrentTransaction(segfilestat, i) && + !in_compaction_list && + !in_inserted_list) { usesegno = i; break; } } } - + if (usesegno != APPENDONLY_COMPACTION_SEGNO_INVALID) { /* mark this segno as in use */ if (aoentry->relsegfiles[usesegno].xid != CurrentXid) { aoentry->txns_using_rel++; - } + } if (*is_drop) { aoentry->relsegfiles[usesegno].state = PSEUDO_COMPACTION_USE; @@ -931,17 +935,17 @@ SetSegnoForCompaction(Relation rel, appendOnlyInsertXact = true; ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("Compaction segment chosen for append-only relation \"%s\" (%d) " - "is %d (tupcount " INT64_FORMAT ", txns count %d)", - RelationGetRelationName(rel), RelationGetRelid(rel), usesegno, - aoentry->relsegfiles[usesegno].total_tupcount, - aoentry->txns_using_rel))); + (errmsg("Compaction segment chosen for append-only relation \"%s\" (%d) " + "is %d (tupcount " INT64_FORMAT ", txns count %d)", + RelationGetRelationName(rel), RelationGetRelid(rel), usesegno, + aoentry->relsegfiles[usesegno].total_tupcount, + aoentry->txns_using_rel))); } else { ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("No compaction segment chosen for append-only relation \"%s\" (%d)", - RelationGetRelationName(rel), RelationGetRelid(rel)))); + (errmsg("No compaction segment chosen for append-only relation \"%s\" (%d)", + RelationGetRelationName(rel), RelationGetRelid(rel)))); } LWLockRelease(AOSegFileLock); @@ -967,18 +971,19 @@ SetSegnoForCompaction(Relation rel, * as it has before been set by SetSegnoForCompaction. * * Should only be called in DISPATCH and UTILITY mode. - */ + */ int SetSegnoForCompactionInsert(Relation rel, - List *compacted_segno, - List *compactedSegmentFileList, - List *insertedSegmentFileList) + List *compacted_segno, + List *compactedSegmentFileList, + List *insertedSegmentFileList) { - int i, usesegno = -1; - bool segno_chosen = false; - AORelHashEntryData *aoentry = NULL; - TransactionId CurrentXid = GetTopTransactionId(); - int64 min_tupcount; + int i, + usesegno = -1; + bool segno_chosen = false; + AORelHashEntryData *aoentry = NULL; + TransactionId CurrentXid = GetTopTransactionId(); + int64 min_tupcount; Assert(Gp_role != GP_ROLE_EXECUTE); @@ -988,10 +993,10 @@ SetSegnoForCompactionInsert(Relation rel, } ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("SetSegnoForCompactionInsert: " - "Choosing a segno for append-only " - "relation \"%s\" (%d)", - RelationGetRelationName(rel), RelationGetRelid(rel)))); + (errmsg("SetSegnoForCompactionInsert: " + "Choosing a segno for append-only " + "relation \"%s\" (%d)", + RelationGetRelationName(rel), RelationGetRelid(rel)))); LWLockAcquire(AOSegFileLock, LW_EXCLUSIVE); @@ -999,20 +1004,20 @@ SetSegnoForCompactionInsert(Relation rel, Assert(aoentry); ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("SetSegnoForCompaction: got the hash entry for relation \"%s\" (%d).", - RelationGetRelationName(rel), RelationGetRelid(rel)))); + (errmsg("SetSegnoForCompaction: got the hash entry for relation \"%s\" (%d).", + RelationGetRelationName(rel), RelationGetRelid(rel)))); min_tupcount = INT64_MAX; usesegno = 0; - /* + /* * Never use segno 0 for inserts (unless in utility mode) */ - for (i = 1; i < MAX_AOREL_CONCURRENCY ; i++) + for (i = 1; i < MAX_AOREL_CONCURRENCY; i++) { AOSegfileStatus *segfilestat = &aoentry->relsegfiles[i]; - bool in_compaction_list = list_member_int(compacted_segno, i) || - list_member_int(compactedSegmentFileList, i); + bool in_compaction_list = list_member_int(compacted_segno, i) || + list_member_int(compactedSegmentFileList, i); if (segfilestat->total_tupcount < min_tupcount && segfilestat->state == AVAILABLE && @@ -1030,27 +1035,27 @@ SetSegnoForCompactionInsert(Relation rel, /* we already used this segno in our txn. use it again */ usesegno = i; segno_chosen = true; - + ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("SetSegnoForCompaction: reusing segno %d for append-" - "only relation " - "%d. there are " INT64_FORMAT " tuples " - "added to it from previous operations " - "in this not yet committed txn.", - usesegno, RelationGetRelid(rel), - (int64) segfilestat->tupsadded))); + (errmsg("SetSegnoForCompaction: reusing segno %d for append-" + "only relation " + "%d. there are " INT64_FORMAT " tuples " + "added to it from previous operations " + "in this not yet committed txn.", + usesegno, RelationGetRelid(rel), + (int64) segfilestat->tupsadded))); break; } } - if(!segno_chosen) + if (!segno_chosen) { LWLockRelease(AOSegFileLock); ereport(ERROR, (errmsg("could not find segment file to use for " - "inserting into relation %s (%d).", + "inserting into relation %s (%d).", RelationGetRelationName(rel), RelationGetRelid(rel)))); } - + Insist(usesegno != RESERVED_SEGNO); /* mark this segno as in use */ @@ -1064,8 +1069,8 @@ SetSegnoForCompactionInsert(Relation rel, Assert(usesegno != RESERVED_SEGNO); ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("Segno chosen for append-only relation \"%s\" (%d) " - "is %d", RelationGetRelationName(rel), RelationGetRelid(rel), usesegno))); + (errmsg("Segno chosen for append-only relation \"%s\" (%d) " + "is %d", RelationGetRelationName(rel), RelationGetRelid(rel), usesegno))); return usesegno; } @@ -1108,12 +1113,13 @@ int SetSegnoForWrite(Relation rel, int existingsegno) { /* these vars are used in GP_ROLE_DISPATCH only */ - int i, usesegno = -1; - bool segno_chosen = false; - AORelHashEntryData *aoentry = NULL; - TransactionId CurrentXid = GetTopTransactionId(); + int i, + usesegno = -1; + bool segno_chosen = false; + AORelHashEntryData *aoentry = NULL; + TransactionId CurrentXid = GetTopTransactionId(); - switch(Gp_role) + switch (Gp_role) { case GP_ROLE_EXECUTE: @@ -1131,9 +1137,9 @@ SetSegnoForWrite(Relation rel, int existingsegno) Assert(existingsegno == InvalidFileSegNumber); ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("SetSegnoForWrite: Choosing a segno for append-only " - "relation \"%s\" (%d) ", - RelationGetRelationName(rel), RelationGetRelid(rel)))); + (errmsg("SetSegnoForWrite: Choosing a segno for append-only " + "relation \"%s\" (%d) ", + RelationGetRelationName(rel), RelationGetRelid(rel)))); LWLockAcquire(AOSegFileLock, LW_EXCLUSIVE); @@ -1142,14 +1148,14 @@ SetSegnoForWrite(Relation rel, int existingsegno) aoentry->txns_using_rel++; ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("SetSegnoForWrite: got the hash entry for relation \"%s\" (%d). " - "setting txns_using_rel to %d", - RelationGetRelationName(rel), RelationGetRelid(rel), - aoentry->txns_using_rel))); + (errmsg("SetSegnoForWrite: got the hash entry for relation \"%s\" (%d). " + "setting txns_using_rel to %d", + RelationGetRelationName(rel), RelationGetRelid(rel), + aoentry->txns_using_rel))); /* - * Now pick the not in use segment and is not over the - * allowed size threshold (90% full). + * Now pick the not in use segment and is not over the allowed + * size threshold (90% full). * * However, if we already picked a segno for a previous statement * in this very same transaction we are still in (explicit txn) we @@ -1157,11 +1163,11 @@ SetSegnoForWrite(Relation rel, int existingsegno) * * Never use segno 0 for inserts (unless in utility mode) */ - for (i = 1 ; i < MAX_AOREL_CONCURRENCY ; i++) + for (i = 1; i < MAX_AOREL_CONCURRENCY; i++) { AOSegfileStatus *segfilestat = &aoentry->relsegfiles[i]; - if(!segfilestat->isfull) + if (!segfilestat->isfull) { if (segfilestat->state == AVAILABLE && segfilestat->formatversion == AORelationVersion_GetLatest() && @@ -1171,45 +1177,45 @@ SetSegnoForWrite(Relation rel, int existingsegno) /* * this segno is avaiable and not full. use it. * - * Notice that we don't break out of the loop quite yet. - * We still need to check the rest of the segnos, if our - * txn is already using one of them. see below. + * Notice that we don't break out of the loop quite + * yet. We still need to check the rest of the segnos, + * if our txn is already using one of them. see below. */ usesegno = i; segno_chosen = true; } - if(segfilestat->xid == CurrentXid) + if (segfilestat->xid == CurrentXid) { /* we already used this segno in our txn. use it again */ usesegno = i; segno_chosen = true; - aoentry->txns_using_rel--; /* same txn. re-adjust */ - + aoentry->txns_using_rel--; /* same txn. re-adjust */ + ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("SetSegnoForWrite: reusing segno %d for append-" - "only relation " - "%d. there are " INT64_FORMAT " tuples " - "added to it from previous operations " - "in this not yet committed txn. decrementing" - "txns_using_rel back to %d", - usesegno, RelationGetRelid(rel), - (int64) segfilestat->tupsadded, - aoentry->txns_using_rel))); + (errmsg("SetSegnoForWrite: reusing segno %d for append-" + "only relation " + "%d. there are " INT64_FORMAT " tuples " + "added to it from previous operations " + "in this not yet committed txn. decrementing" + "txns_using_rel back to %d", + usesegno, RelationGetRelid(rel), + (int64) segfilestat->tupsadded, + aoentry->txns_using_rel))); break; } } } - if(!segno_chosen) + if (!segno_chosen) { LWLockRelease(AOSegFileLock); ereport(ERROR, (errmsg("could not find segment file to use for " - "inserting into relation %s (%d).", + "inserting into relation %s (%d).", RelationGetRelationName(rel), RelationGetRelid(rel)))); } - + Insist(usesegno != RESERVED_SEGNO); /* mark this segno as in use */ @@ -1223,12 +1229,12 @@ SetSegnoForWrite(Relation rel, int existingsegno) Assert(usesegno != RESERVED_SEGNO); ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("Segno chosen for append-only relation \"%s\" (%d) " - "is %d", RelationGetRelationName(rel), RelationGetRelid(rel), usesegno))); + (errmsg("Segno chosen for append-only relation \"%s\" (%d) " + "is %d", RelationGetRelationName(rel), RelationGetRelid(rel), usesegno))); return usesegno; - /* fix this for dispatch agent. for now it's broken anyway. */ + /* fix this for dispatch agent. for now it's broken anyway. */ default: Assert(false); return InvalidFileSegNumber; @@ -1248,15 +1254,16 @@ SetSegnoForWrite(Relation rel, int existingsegno) * if eventually no data will get inserted into it (since we can't know ahead * of time). */ -List *assignPerRelSegno(List *all_relids) +List * +assignPerRelSegno(List *all_relids) { - ListCell *cell; - List *mapping = NIL; + ListCell *cell; + List *mapping = NIL; foreach(cell, all_relids) { - Oid cur_relid = lfirst_oid(cell); - Relation rel = heap_open(cur_relid, NoLock); + Oid cur_relid = lfirst_oid(cell); + Relation rel = heap_open(cur_relid, NoLock); if (RelationIsAoCols(rel) || RelationIsAoRows(rel)) { @@ -1272,9 +1279,9 @@ List *assignPerRelSegno(List *all_relids) mapping = lappend(mapping, n); ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("assignPerRelSegno: Appendonly Writer assigned segno %d to " - "relid %d for this write operation", - n->segno, n->relid))); + (errmsg("assignPerRelSegno: Appendonly Writer assigned segno %d to " + "relid %d for this write operation", + n->segno, n->relid))); } heap_close(rel, NoLock); @@ -1294,18 +1301,20 @@ static int64 * GetTotalTupleCountFromSegments(Relation parentrel, int segno) { - StringInfoData sqlstmt; - Relation aosegrel; + StringInfoData sqlstmt; + Relation aosegrel; CdbPgResults cdb_pgresults = {NULL, 0}; - int i, j; - int64 *total_tupcount = NULL; - Oid save_userid; - bool save_secdefcxt; + int i, + j; + int64 *total_tupcount = NULL; + Oid save_userid; + bool save_secdefcxt; /* * get the name of the aoseg relation */ aosegrel = heap_open(parentrel->rd_appendonly->segrelid, AccessShareLock); + /* * assemble our query string */ @@ -1320,12 +1329,12 @@ GetTotalTupleCountFromSegments(Relation parentrel, total_tupcount = palloc0(sizeof(int64) * MAX_AOREL_CONCURRENCY); /* - * Dispatch the query to segments. Although this path is not critical - * to use SPI, it is desirable to avoid another MPP query here, too. + * Dispatch the query to segments. Although this path is not critical to + * use SPI, it is desirable to avoid another MPP query here, too. * * Since pg_aoseg namespace is a part of catalog, we need a superuser - * privilege. This is just a band-aid and we need to revisit - * the mechanism to synchronize pg_aoseg between master and segment. + * privilege. This is just a band-aid and we need to revisit the + * mechanism to synchronize pg_aoseg between master and segment. */ GetUserIdAndContext(&save_userid, &save_secdefcxt); SetUserIdAndContext(BOOTSTRAP_SUPERUSERID, true); @@ -1339,7 +1348,7 @@ GetTotalTupleCountFromSegments(Relation parentrel, for (i = 0; i < cdb_pgresults.numResults; i++) { - struct pg_result * pgresult = cdb_pgresults.pg_results[i]; + struct pg_result *pgresult = cdb_pgresults.pg_results[i]; if (PQresultStatus(pgresult) != PGRES_TUPLES_OK) ereport(ERROR, @@ -1357,14 +1366,14 @@ GetTotalTupleCountFromSegments(Relation parentrel, /* We don't expect NULL, but sanity check. */ if (PQgetisnull(pgresult, j, 0) == 1) elog(ERROR, "unexpected NULL in tupcount in results[%d]: %s", - i, sqlstmt.data); + i, sqlstmt.data); if (PQgetisnull(pgresult, j, 1) == 1) elog(ERROR, "unexpected NULL in segno in results[%d]: %s", - i, sqlstmt.data); + i, sqlstmt.data); value = PQgetvalue(pgresult, j, 0); tupcount = DatumGetFloat8( - DirectFunctionCall1(float8in, CStringGetDatum(value))); + DirectFunctionCall1(float8in, CStringGetDatum(value))); value = PQgetvalue(pgresult, j, 1); segno = pg_atoi(value, sizeof(int32), 0); total_tupcount[segno] += tupcount; @@ -1396,13 +1405,14 @@ GetTotalTupleCountFromSegments(Relation parentrel, static bool * GetFileSegStateInfoFromSegments(Relation parentrel) { - StringInfoData sqlstmt; - Relation aosegrel; + StringInfoData sqlstmt; + Relation aosegrel; CdbPgResults cdb_pgresults = {NULL, 0}; - int i, j; - bool *awaiting_drop; - Oid save_userid; - bool save_secdefcxt; + int i, + j; + bool *awaiting_drop; + Oid save_userid; + bool save_secdefcxt; Assert(RelationIsAoRows(parentrel) || RelationIsAoCols(parentrel)); @@ -1410,6 +1420,7 @@ GetFileSegStateInfoFromSegments(Relation parentrel) * get the name of the aoseg relation */ aosegrel = heap_open(parentrel->rd_appendonly->segrelid, AccessShareLock); + /* * assemble our query string */ @@ -1421,21 +1432,21 @@ GetFileSegStateInfoFromSegments(Relation parentrel) elogif(Debug_appendonly_print_segfile_choice, LOG, "Get awaiting drop state from segments: " "releation %s (%d)", - RelationGetRelationName(parentrel), - RelationGetRelid(parentrel)); + RelationGetRelationName(parentrel), + RelationGetRelid(parentrel)); /* Allocate result array to be returned. */ awaiting_drop = palloc0(sizeof(bool) * MAX_AOREL_CONCURRENCY); /* - * We should not use SPI here because the code path is between - * the initialization of slice table and query dispatch. SPI - * would create another slice table and bump gp_interconnect_id, - * which would cause inconsistent order of execution. + * We should not use SPI here because the code path is between the + * initialization of slice table and query dispatch. SPI would create + * another slice table and bump gp_interconnect_id, which would cause + * inconsistent order of execution. * * Since pg_aoseg namespace is a part of catalog, we need a superuser - * privilege. This is just a band-aid and we need to revisit - * the mechanism to synchronize pg_aoseg between master and segment. + * privilege. This is just a band-aid and we need to revisit the + * mechanism to synchronize pg_aoseg between master and segment. */ GetUserIdAndContext(&save_userid, &save_secdefcxt); SetUserIdAndContext(BOOTSTRAP_SUPERUSERID, true); @@ -1449,7 +1460,7 @@ GetFileSegStateInfoFromSegments(Relation parentrel) for (i = 0; i < cdb_pgresults.numResults; i++) { - struct pg_result* pgresult = cdb_pgresults.pg_results[i]; + struct pg_result *pgresult = cdb_pgresults.pg_results[i]; if (PQresultStatus(pgresult) != PGRES_TUPLES_OK) ereport(ERROR, @@ -1467,10 +1478,10 @@ GetFileSegStateInfoFromSegments(Relation parentrel) /* We don't expect NULL, but sanity check. */ if (PQgetisnull(pgresult, j, 0) == 1) elog(ERROR, "unexpected NULL in state in results[%d]: %s", - i, sqlstmt.data); + i, sqlstmt.data); if (PQgetisnull(pgresult, j, 1) == 1) elog(ERROR, "unexpected NULL in segno in results[%d}: %s", - i, sqlstmt.data); + i, sqlstmt.data); value = PQgetvalue(pgresult, j, 0); qe_state = pg_atoi(value, sizeof(int32), 0); value = PQgetvalue(pgresult, j, 1); @@ -1487,9 +1498,9 @@ GetFileSegStateInfoFromSegments(Relation parentrel) elogif(Debug_appendonly_print_segfile_choice, LOG, "Found awaiting drop segment file: " "releation %s (%d), segno = %d", - RelationGetRelationName(parentrel), - RelationGetRelid(parentrel), - segno); + RelationGetRelationName(parentrel), + RelationGetRelid(parentrel), + segno); } } } @@ -1519,8 +1530,8 @@ UpdateMasterAosegTotalsFromSegments(Relation parentrel, List *segmentNumList, int64 modcount_added) { - ListCell *l; - int64 *total_tupcount; + ListCell *l; + int64 *total_tupcount; Assert(RelationIsAoRows(parentrel) || RelationIsAoCols(parentrel)); Assert(Gp_role == GP_ROLE_DISPATCH); @@ -1531,11 +1542,11 @@ UpdateMasterAosegTotalsFromSegments(Relation parentrel, /* * We are interested in only the segfiles that were told to be updated. */ - foreach (l, segmentNumList) + foreach(l, segmentNumList) { - int qe_segno = lfirst_int(l); - int64 qe_tupcount = total_tupcount[qe_segno]; - int64 known_tupcount = 0; + int qe_segno = lfirst_int(l); + int64 qe_tupcount = total_tupcount[qe_segno]; + int64 known_tupcount = 0; Assert(qe_segno >= 0); @@ -1545,7 +1556,7 @@ UpdateMasterAosegTotalsFromSegments(Relation parentrel, */ if (RelationIsAoRows(parentrel)) { - FileSegInfo *fsinfo; + FileSegInfo *fsinfo; fsinfo = GetFileSegInfo(parentrel, appendOnlyMetaDataSnapshot, qe_segno); @@ -1558,13 +1569,16 @@ UpdateMasterAosegTotalsFromSegments(Relation parentrel, else { AOCSFileSegInfo *seginfo; + Assert(RelationIsAoCols(parentrel)); seginfo = GetAOCSFileSegInfo(parentrel, SnapshotNow, qe_segno); - if (seginfo != NULL) + + if (seginfo !=NULL) { known_tupcount = seginfo->total_tupcount; + pfree(seginfo); } } @@ -1575,7 +1589,7 @@ UpdateMasterAosegTotalsFromSegments(Relation parentrel, */ if (known_tupcount != qe_tupcount) { - int64 tupcount_diff = qe_tupcount - known_tupcount; + int64 tupcount_diff = qe_tupcount - known_tupcount; elog(DEBUG3, "UpdateMasterAosegTotalsFromSegments: updating " "segno %d with tupcount " INT64_FORMAT @@ -1599,33 +1613,35 @@ UpdateMasterAosegTotalsFromSegments(Relation parentrel, * track of file 'segments' and their EOF's and decide which segno to use in * future writes into the table. */ -void UpdateMasterAosegTotals(Relation parentrel, int segno, int64 tupcount, int64 modcount_added) +void +UpdateMasterAosegTotals(Relation parentrel, int segno, int64 tupcount, int64 modcount_added) { - AORelHashEntry aoHashEntry = NULL; + AORelHashEntry aoHashEntry = NULL; Assert(Gp_role == GP_ROLE_DISPATCH); Assert(segno >= 0); ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("UpdateMasterAosegTotals: Updating aoseg entry for append-only relation %d " - "with " INT64_FORMAT " new tuples for segno %d", - RelationGetRelid(parentrel), (int64)tupcount, segno))); + (errmsg("UpdateMasterAosegTotals: Updating aoseg entry for append-only relation %d " + "with " INT64_FORMAT " new tuples for segno %d", + RelationGetRelid(parentrel), (int64) tupcount, segno))); - // CONSIDER: We should probably get this lock even sooner. + /* CONSIDER: We should probably get this lock even sooner. */ LockRelationAppendOnlySegmentFile( - &parentrel->rd_node, - segno, - AccessExclusiveLock, - /* dontWait */ false); + &parentrel->rd_node, + segno, + AccessExclusiveLock, + /* dontWait */ false); - if(RelationIsAoRows(parentrel)) + if (RelationIsAoRows(parentrel)) { - FileSegInfo *fsinfo; + FileSegInfo *fsinfo; /* get the information for the file segment we inserted into */ /* - * Since we have an exclusive lock on the segment-file entry, we can use SnapshotNow. + * Since we have an exclusive lock on the segment-file entry, we can + * use SnapshotNow. */ fsinfo = GetFileSegInfo(parentrel, SnapshotNow, segno); if (fsinfo == NULL) @@ -1638,10 +1654,11 @@ void UpdateMasterAosegTotals(Relation parentrel, int segno, int64 tupcount, int6 } /* - * Update the master AO segment info table with correct tuple count total + * Update the master AO segment info table with correct tuple count + * total */ - UpdateFileSegInfo(parentrel, segno, 0, 0, tupcount, 0, - modcount_added, AOSEG_STATE_USECURRENT); + UpdateFileSegInfo(parentrel, segno, 0, 0, tupcount, 0, + modcount_added, AOSEG_STATE_USECURRENT); } else { @@ -1651,13 +1668,14 @@ void UpdateMasterAosegTotals(Relation parentrel, int segno, int64 tupcount, int6 Assert(RelationIsAoCols(parentrel)); seginfo = GetAOCSFileSegInfo( - parentrel, - SnapshotNow, - segno); + parentrel, + SnapshotNow, + segno); + if (seginfo == NULL) { - InsertInitialAOCSFileSegInfo(parentrel, segno, - RelationGetNumberOfAttributes(parentrel)); + InsertInitialAOCSFileSegInfo(parentrel, segno, + RelationGetNumberOfAttributes(parentrel)); } else { @@ -1667,10 +1685,11 @@ void UpdateMasterAosegTotals(Relation parentrel, int segno, int64 tupcount, int6 } /* - * Now, update num of tups added in the hash table. This pending count will - * get added to the total count when the transaction actually commits. - * (or will get discarded if it aborts). We don't need to do the same - * trick for the aoseg table since MVCC protects us there in case we abort. + * Now, update num of tups added in the hash table. This pending count + * will get added to the total count when the transaction actually + * commits. (or will get discarded if it aborts). We don't need to do the + * same trick for the aoseg table since MVCC protects us there in case we + * abort. */ aoHashEntry = AORelGetHashEntry(RelationGetRelid(parentrel)); aoHashEntry->relsegfiles[segno].tupsadded += tupcount; @@ -1693,8 +1712,8 @@ void AtCommit_AppendOnly(void) { HASH_SEQ_STATUS status; - AORelHashEntry aoentry; - TransactionId CurrentXid; + AORelHashEntry aoentry; + TransactionId CurrentXid; if (Gp_role != GP_ROLE_DISPATCH) return; @@ -1709,6 +1728,7 @@ AtCommit_AppendOnly(void) Assert(CurrentXid != InvalidTransactionId); LWLockAcquire(AOSegFileLock, LW_EXCLUSIVE); + /* * for each AO table hash entry */ @@ -1717,42 +1737,43 @@ AtCommit_AppendOnly(void) /* * Only look at tables that are marked in use currently */ - if(aoentry->txns_using_rel > 0) + if (aoentry->txns_using_rel > 0) { - int i; + int i; /* * Was any segfile was updated in our own transaction? */ - for (i = 0 ; i < MAX_AOREL_CONCURRENCY ; i++) + for (i = 0; i < MAX_AOREL_CONCURRENCY; i++) { AOSegfileStatus *segfilestat = &aoentry->relsegfiles[i]; - TransactionId InsertingXid = segfilestat->xid; - if(InsertingXid == CurrentXid) + TransactionId InsertingXid = segfilestat->xid; + + if (InsertingXid == CurrentXid) { /* bingo! */ Assert(segfilestat->state == INSERT_USE || - segfilestat->state == COMPACTION_USE || - segfilestat->state == DROP_USE || - segfilestat->state == PSEUDO_COMPACTION_USE || - segfilestat->state == COMPACTED_DROP_SKIPPED); + segfilestat->state == COMPACTION_USE || + segfilestat->state == DROP_USE || + segfilestat->state == PSEUDO_COMPACTION_USE || + segfilestat->state == COMPACTED_DROP_SKIPPED); ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("AtCommit_AppendOnly: found a segno that inserted in " - "our txn for table %d. Updating segno %d " - "tupcount: old count " INT64_FORMAT ", tups " - "added in this txn " INT64_FORMAT " new " - "count " INT64_FORMAT, aoentry->relid, i, - (int64)segfilestat->total_tupcount, - (int64)segfilestat->tupsadded, - (int64)segfilestat->total_tupcount + - (int64)segfilestat->tupsadded ))); + (errmsg("AtCommit_AppendOnly: found a segno that inserted in " + "our txn for table %d. Updating segno %d " + "tupcount: old count " INT64_FORMAT ", tups " + "added in this txn " INT64_FORMAT " new " + "count " INT64_FORMAT, aoentry->relid, i, + (int64) segfilestat->total_tupcount, + (int64) segfilestat->tupsadded, + (int64) segfilestat->total_tupcount + + (int64) segfilestat->tupsadded))); /* now do the in memory update */ segfilestat->total_tupcount += segfilestat->tupsadded; segfilestat->tupsadded = 0; segfilestat->isfull = - (segfilestat->total_tupcount > segfileMaxRowThreshold()); + (segfilestat->total_tupcount > segfileMaxRowThreshold()); Assert(!TransactionIdIsValid(segfilestat->latestWriteXid) || TransactionIdPrecedes(segfilestat->latestWriteXid, @@ -1783,8 +1804,8 @@ void AtAbort_AppendOnly(void) { HASH_SEQ_STATUS status; - AORelHashEntry aoentry = NULL; - TransactionId CurrentXid = GetCurrentTransactionIdIfAny(); + AORelHashEntry aoentry = NULL; + TransactionId CurrentXid = GetCurrentTransactionIdIfAny(); if (Gp_role != GP_ROLE_DISPATCH) return; @@ -1806,46 +1827,46 @@ AtAbort_AppendOnly(void) /* * Only look at tables that are marked in use currently */ - if(aoentry->txns_using_rel == 0) + if (aoentry->txns_using_rel == 0) { continue; } - int i; + int i; - /* - * Was any segfile updated in our own transaction? - */ - for (i = 0 ; i < MAX_AOREL_CONCURRENCY ; i++) + /* + * Was any segfile updated in our own transaction? + */ + for (i = 0; i < MAX_AOREL_CONCURRENCY; i++) + { + AOSegfileStatus *segfilestat = &aoentry->relsegfiles[i]; + TransactionId InsertingXid = segfilestat->xid; + + if (InsertingXid != CurrentXid) { - AOSegfileStatus *segfilestat = &aoentry->relsegfiles[i]; - TransactionId InsertingXid = segfilestat->xid; + continue; + } + /* bingo! */ - if(InsertingXid != CurrentXid) - { - continue; - } - /* bingo! */ - - Assert(segfilestat->state == INSERT_USE || - segfilestat->state == COMPACTION_USE || - segfilestat->state == DROP_USE || - segfilestat->state == PSEUDO_COMPACTION_USE || - segfilestat->state == COMPACTED_DROP_SKIPPED); - - ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("AtAbort_AppendOnly: found a segno that inserted in our txn for " - "table %d. Cleaning segno %d tupcount: old " - "count " INT64_FORMAT " tups added in this " - "txn " INT64_FORMAT ", count " - "remains " INT64_FORMAT, aoentry->relid, i, - (int64)segfilestat->total_tupcount, - (int64)segfilestat->tupsadded, - (int64)segfilestat->total_tupcount))); + Assert(segfilestat->state == INSERT_USE || + segfilestat->state == COMPACTION_USE || + segfilestat->state == DROP_USE || + segfilestat->state == PSEUDO_COMPACTION_USE || + segfilestat->state == COMPACTED_DROP_SKIPPED); + + ereportif(Debug_appendonly_print_segfile_choice, LOG, + (errmsg("AtAbort_AppendOnly: found a segno that inserted in our txn for " + "table %d. Cleaning segno %d tupcount: old " + "count " INT64_FORMAT " tups added in this " + "txn " INT64_FORMAT ", count " + "remains " INT64_FORMAT, aoentry->relid, i, + (int64) segfilestat->total_tupcount, + (int64) segfilestat->tupsadded, + (int64) segfilestat->total_tupcount))); /* now do the in memory cleanup. tupcount not touched */ segfilestat->tupsadded = 0; segfilestat->aborted = true; - /* state transitions are done by AtEOXact_AppendOnly and friends */ + /* state transitions are done by AtEOXact_AppendOnly and friends */ } } @@ -1859,18 +1880,18 @@ AtAbort_AppendOnly(void) * */ static void -AtEOXact_AppendOnly_StateTransition(AORelHashEntry aoentry, int segno, - AOSegfileStatus *segfilestat) +AtEOXact_AppendOnly_StateTransition(AORelHashEntry aoentry, int segno, + AOSegfileStatus *segfilestat) { AOSegfileState oldstate; Assert(segfilestat); Assert(segfilestat->state == INSERT_USE || - segfilestat->state == COMPACTION_USE || - segfilestat->state == DROP_USE || - segfilestat->state == PSEUDO_COMPACTION_USE || - segfilestat->state == COMPACTED_DROP_SKIPPED); - + segfilestat->state == COMPACTION_USE || + segfilestat->state == DROP_USE || + segfilestat->state == PSEUDO_COMPACTION_USE || + segfilestat->state == COMPACTED_DROP_SKIPPED); + oldstate = segfilestat->state; if (segfilestat->state == INSERT_USE) { @@ -1917,28 +1938,29 @@ AtEOXact_AppendOnly_StateTransition(AORelHashEntry aoentry, int segno, { Assert(false); } - + ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("Segment file state transition for " - "table %d, segno %d: %d -> %d", - aoentry->relid, segno, - oldstate, segfilestat->state))); + (errmsg("Segment file state transition for " + "table %d, segno %d: %d -> %d", + aoentry->relid, segno, + oldstate, segfilestat->state))); segfilestat->xid = InvalidTransactionId; segfilestat->aborted = false; } + /* * Assumes that AOSegFileLock is held. */ static void -AtEOXact_AppendOnly_Relation(AORelHashEntry aoentry, TransactionId currentXid) +AtEOXact_AppendOnly_Relation(AORelHashEntry aoentry, TransactionId currentXid) { - int i; - bool entry_updated = false; + int i; + bool entry_updated = false; /* * Only look at tables that are marked in use currently */ - if(aoentry->txns_using_rel == 0) + if (aoentry->txns_using_rel == 0) { return; } @@ -1946,12 +1968,12 @@ AtEOXact_AppendOnly_Relation(AORelHashEntry aoentry, TransactionId currentXid) /* * Was any segfile updated in our own transaction? */ - for (i = 0 ; i < MAX_AOREL_CONCURRENCY ; i++) + for (i = 0; i < MAX_AOREL_CONCURRENCY; i++) { AOSegfileStatus *segfilestat = &aoentry->relsegfiles[i]; - TransactionId InsertingXid = segfilestat->xid; + TransactionId InsertingXid = segfilestat->xid; - if(InsertingXid != currentXid) + if (InsertingXid != currentXid) { continue; } @@ -1962,20 +1984,21 @@ AtEOXact_AppendOnly_Relation(AORelHashEntry aoentry, TransactionId currentXid) } /* done with this AO table entry */ - if(entry_updated) + if (entry_updated) { aoentry->txns_using_rel--; ereportif(Debug_appendonly_print_segfile_choice, LOG, - (errmsg("AtEOXact_AppendOnly: updated txns_using_rel, it is now %d", - aoentry->txns_using_rel))); + (errmsg("AtEOXact_AppendOnly: updated txns_using_rel, it is now %d", + aoentry->txns_using_rel))); } if (test_AppendOnlyHash_eviction_vs_just_marking_not_inuse) { /* * If no transaction is using this entry, it can be removed if - * hash-table gets full. So perform the same here if the above GUC is set. + * hash-table gets full. So perform the same here if the above GUC is + * set. */ if (aoentry->txns_using_rel == 0) { @@ -1983,6 +2006,7 @@ AtEOXact_AppendOnly_Relation(AORelHashEntry aoentry, TransactionId currentXid) } } } + /* * End of txn processing for append only tables. * @@ -1998,8 +2022,8 @@ void AtEOXact_AppendOnly(void) { HASH_SEQ_STATUS status; - AORelHashEntry aoentry = NULL; - TransactionId CurrentXid = GetCurrentTransactionIdIfAny(); + AORelHashEntry aoentry = NULL; + TransactionId CurrentXid = GetCurrentTransactionIdIfAny(); if (Gp_role != GP_ROLE_DISPATCH) return; @@ -2026,8 +2050,9 @@ AtEOXact_AppendOnly(void) appendOnlyInsertXact = false; } -void ValidateAppendOnlyMetaDataSnapshot( - Snapshot *appendOnlyMetaDataSnapshot) +void +ValidateAppendOnlyMetaDataSnapshot( + Snapshot *appendOnlyMetaDataSnapshot) { - // Placeholder. + /* Placeholder. */ } diff --git a/src/backend/access/appendonly/test/aomd_test.c b/src/backend/access/appendonly/test/aomd_test.c index d8c8947468..09c0fba8d4 100644 --- a/src/backend/access/appendonly/test/aomd_test.c +++ b/src/backend/access/appendonly/test/aomd_test.c @@ -6,89 +6,89 @@ #include "../aomd.c" -void -test__AOSegmentFilePathNameLen(void **state) +void +test__AOSegmentFilePathNameLen(void **state) { RelationData reldata; - char* basepath = "base/21381/123"; + char *basepath = "base/21381/123"; expect_any(relpath, &rnode); will_return(relpath, pstrdup(basepath)); - int r = AOSegmentFilePathNameLen(&reldata); + int r = AOSegmentFilePathNameLen(&reldata); assert_in_range(r, strlen(basepath) + 3, strlen(basepath) + 10); } -void -test__FormatAOSegmentFileName(void **state) +void +test__FormatAOSegmentFileName(void **state) { - char* basepath = "base/21381/123"; - int32 fileSegNo; - char filepathname[256]; + char *basepath = "base/21381/123"; + int32 fileSegNo; + char filepathname[256]; - // seg 0, no columns + /* seg 0, no columns */ FormatAOSegmentFileName(basepath, 0, -1, &fileSegNo, filepathname); assert_string_equal(filepathname, "base/21381/123"); assert_int_equal(fileSegNo, 0); - // seg 1, no columns + /* seg 1, no columns */ FormatAOSegmentFileName(basepath, 1, -1, &fileSegNo, filepathname); assert_string_equal(filepathname, "base/21381/123.1"); assert_int_equal(fileSegNo, 1); - // seg 0, column 1 + /* seg 0, column 1 */ FormatAOSegmentFileName(basepath, 0, 1, &fileSegNo, filepathname); assert_string_equal(filepathname, "base/21381/123.128"); assert_int_equal(fileSegNo, 128); - // seg 1, column 1 + /* seg 1, column 1 */ FormatAOSegmentFileName(basepath, 1, 1, &fileSegNo, filepathname); assert_string_equal(filepathname, "base/21381/123.129"); assert_int_equal(fileSegNo, 129); - // seg 0, column 2 + /* seg 0, column 2 */ FormatAOSegmentFileName(basepath, 0, 2, &fileSegNo, filepathname); assert_string_equal(filepathname, "base/21381/123.256"); assert_int_equal(fileSegNo, 256); } -void -test__MakeAOSegmentFileName(void **state) +void +test__MakeAOSegmentFileName(void **state) { - char* basepath = "base/21381/123"; - int32 fileSegNo; - char filepathname[256]; + char *basepath = "base/21381/123"; + int32 fileSegNo; + char filepathname[256]; RelationData reldata; expect_any_count(relpath, &rnode, -1); - // seg 0, no columns + /* seg 0, no columns */ will_return(relpath, pstrdup(basepath)); MakeAOSegmentFileName(&reldata, 0, -1, &fileSegNo, filepathname); assert_string_equal(filepathname, "base/21381/123"); assert_int_equal(fileSegNo, 0); - // seg 1, no columns + /* seg 1, no columns */ will_return(relpath, pstrdup(basepath)); MakeAOSegmentFileName(&reldata, 1, -1, &fileSegNo, filepathname); assert_string_equal(filepathname, "base/21381/123.1"); assert_int_equal(fileSegNo, 1); - // seg 0, column 1 + /* seg 0, column 1 */ will_return(relpath, pstrdup(basepath)); MakeAOSegmentFileName(&reldata, 0, 1, &fileSegNo, filepathname); assert_string_equal(filepathname, "base/21381/123.128"); assert_int_equal(fileSegNo, 128); - // seg 1, column 1 + /* seg 1, column 1 */ will_return(relpath, pstrdup(basepath)); MakeAOSegmentFileName(&reldata, 1, 1, &fileSegNo, filepathname); assert_string_equal(filepathname, "base/21381/123.129"); assert_int_equal(fileSegNo, 129); - // seg 0, column 2 + /* seg 0, column 2 */ will_return(relpath, pstrdup(basepath)); MakeAOSegmentFileName(&reldata, 0, 2, &fileSegNo, filepathname); assert_string_equal(filepathname, "base/21381/123.256"); @@ -96,15 +96,15 @@ test__MakeAOSegmentFileName(void **state) } -int -main(int argc, char* argv[]) +int +main(int argc, char *argv[]) { cmockery_parse_arguments(argc, argv); - const UnitTest tests[] = { - unit_test(test__AOSegmentFilePathNameLen), - unit_test(test__FormatAOSegmentFileName), - unit_test(test__MakeAOSegmentFileName) + const UnitTest tests[] = { + unit_test(test__AOSegmentFilePathNameLen), + unit_test(test__FormatAOSegmentFileName), + unit_test(test__MakeAOSegmentFileName) }; MemoryContextInit(); diff --git a/src/backend/access/appendonly/test/appendonly_visimap_test.c b/src/backend/access/appendonly/test/appendonly_visimap_test.c index 3f1def161c..641509dd0a 100644 --- a/src/backend/access/appendonly/test/appendonly_visimap_test.c +++ b/src/backend/access/appendonly/test/appendonly_visimap_test.c @@ -17,7 +17,8 @@ test__AppendOnlyVisimapDelete_Finish_outoforder(void **state) AppendOnlyVisiMapDeleteData val; AppendOnlyVisimapDelete visiMapDelete; AppendOnlyVisimap visiMap; - bool found; + bool found; + visiMapDelete.visiMap = &visiMap; visiMap.visimapEntry.segmentFileNum = 2; visiMap.visimapEntry.firstRowNum = 32768; @@ -32,9 +33,9 @@ test__AppendOnlyVisimapDelete_Finish_outoforder(void **state) will_return(AppendOnlyVisimapEntry_HasChanged, true); #ifdef USE_ASSERT_CHECKING + /* - * AppendOnlyVisimap_Store calls - * Assert(AppendOnlyVisimapEntry_IsValid) + * AppendOnlyVisimap_Store calls Assert(AppendOnlyVisimapEntry_IsValid) */ expect_any(AppendOnlyVisimapEntry_IsValid, visiMapEntry); will_return(AppendOnlyVisimapEntry_IsValid, true); @@ -65,13 +66,13 @@ test__AppendOnlyVisimapDelete_Finish_outoforder(void **state) } -int -main(int argc, char* argv[]) +int +main(int argc, char *argv[]) { cmockery_parse_arguments(argc, argv); - const UnitTest tests[] = { - unit_test(test__AppendOnlyVisimapDelete_Finish_outoforder) + const UnitTest tests[] = { + unit_test(test__AppendOnlyVisimapDelete_Finish_outoforder) }; MemoryContextInit(); -- GitLab