You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudberry.apache.org by ma...@apache.org on 2025/01/07 06:21:46 UTC
(cloudberry) 05/11: If QEs hit errors in explain analyze, rethrow the error before ExplainPrintPlan (#14588)
This is an automated email from the ASF dual-hosted git repository.
maxyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git
commit 5d26f07ae2840e18823fdaab92ac089cf81faec4
Author: Divyesh Vanjare <va...@vmware.com>
AuthorDate: Tue Jan 24 11:18:15 2023 -0500
If QEs hit errors in explain analyze, rethrow the error before ExplainPrintPlan (#14588)
The execution sequence of explain analyze is as follows:
ExecutorStart
ExecutorRun -->dispatch plan to QEs
cdbdisp_checkDispatchResult -->wait all QEs finished
ExplainPrintXXX -->print explain infos
ExecutorEnd -->reThrow QE's error, etc.;
When we execute explain analyze, we might not receive stats from QEs, If some QEs hit errors.
But, In ExplainPrintXXX we will use these stats and print explain info, if we forget to do null
judgment, this may hit a null pointer exception. Finally, in ExecutorEn
d we reThrow the error from QE.
If some QE throws errors, we reThrow the error before executing ExplainPrintXXX.
---
src/backend/access/aocs/aocssegfiles.c | 123 +++++++++++++++----------
src/include/access/aocssegfiles.h | 1 -
src/test/regress/expected/alter_table_aocs.out | 90 ++++++++++++++++++
src/test/regress/sql/alter_table_aocs.sql | 15 +++
4 files changed, 177 insertions(+), 52 deletions(-)
diff --git a/src/backend/access/aocs/aocssegfiles.c b/src/backend/access/aocs/aocssegfiles.c
index e9ea524a48..b0ca80b114 100644
--- a/src/backend/access/aocs/aocssegfiles.c
+++ b/src/backend/access/aocs/aocssegfiles.c
@@ -63,8 +63,7 @@
static AOCSFileSegInfo **GetAllAOCSFileSegInfo_pg_aocsseg_rel(
- int numOfColumsn,
- char *relationName,
+ Relation rel,
Relation pg_aocsseg_rel,
Snapshot appendOnlyMetaDataSnapshot,
int32 *totalseg);
@@ -126,6 +125,68 @@ InsertInitialAOCSFileSegInfo(Relation prel, int32 segno, int32 nvp, Oid segrelid
pfree(values);
}
+/*
+ * This is a routine to extract the vpinfo underlying the untoasted datum from
+ * the pg_aocsseg relation row, given the aocs relation's relnatts, into the supplied
+ * AOCSFileSegInfo structure.
+ *
+ * Sometimes the number of columns represented in the vpinfo inside pg_aocsseg
+ * the row may not match pg_class.relnatts. For instance, when we do an ADD
+ * COLUMN operation, we will have lesser number of columns in the table row
+ * than pg_class.relnatts.
+ * On the other hand, following an aborted ADD COLUMN operation,
+ * the number of columns in the table row will be
+ * greater than pg_class.relnatts.
+ */
+static void
+deformAOCSVPInfo(Relation rel, struct varlena *v, AOCSFileSegInfo *aocs_seginfo)
+{
+ int16 relnatts = RelationGetNumberOfAttributes(rel);
+ struct varlena *dv = pg_detoast_datum(v);
+ int source_size = VARSIZE(dv);
+ int target_size = aocs_vpinfo_size(relnatts);
+
+
+ if (source_size <= target_size)
+ {
+ /* The source fits into the target, simply memcpy. */
+ memcpy(&aocs_seginfo->vpinfo, dv, source_size);
+ Assert(aocs_seginfo->vpinfo.nEntry <= relnatts);
+ }
+ else
+ {
+ /*
+ * We have more columns represented in the vpinfo recorded inside the
+ * pg_aocsseg row, than pg_class.relnatts. Perform additional validation
+ * on these extra column entries and then simply copy over relnatts
+ * worth of entries from within the datum.
+ */
+ AOCSVPInfo *vpInfo = (AOCSVPInfo *) dv;
+
+ for (int i = relnatts; i < vpInfo->nEntry; ++i)
+ {
+ /*
+ * These extra entries must have be the initial frozen inserts
+ * from when InsertInitialAOCSFileSegInfo() was called during
+ * an aborted ADD COLUMN operation. Such entries should have eofs = 0,
+ * indicating that there is no data. If not, there is something
+ * seriously wrong. Yell appropriately.
+ */
+ if(vpInfo->entry[i].eof > 0 || vpInfo->entry[i].eof_uncompressed > 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_INTERNAL_ERROR),
+ errmsg("For relation \"%s\" aborted column %d has non-zero eof %d or non-zero uncompressed eof %d",
+ RelationGetRelationName(rel), i, (int) vpInfo->entry[i].eof, (int) vpInfo->entry[i].eof_uncompressed)));
+ }
+
+ memcpy(&aocs_seginfo->vpinfo, dv, aocs_vpinfo_size(relnatts));
+ aocs_seginfo->vpinfo.nEntry = relnatts;
+ }
+
+ if (dv != v)
+ pfree(dv);
+}
+
/*
* GetAOCSFileSegInfo.
*
@@ -240,15 +301,9 @@ GetAOCSFileSegInfo(Relation prel,
seginfo ->state = DatumGetInt16(d[Anum_pg_aocs_state - 1]);
Assert(!null[Anum_pg_aocs_vpinfo - 1]);
- {
- struct varlena *v = (struct varlena *) DatumGetPointer(d[Anum_pg_aocs_vpinfo - 1]);
- struct varlena *dv = pg_detoast_datum(v);
-
- Assert(VARSIZE(dv) <= aocs_vpinfo_size(nvp));
- memcpy(&seginfo->vpinfo, dv, aocs_vpinfo_size(nvp));
- if (dv != v)
- pfree(dv);
- }
+ deformAOCSVPInfo(prel,
+ (struct varlena *) DatumGetPointer(d[Anum_pg_aocs_vpinfo - 1]),
+ seginfo);
pfree(d);
pfree(null);
@@ -285,9 +340,7 @@ GetAllAOCSFileSegInfo(Relation prel,
pg_aocsseg_rel = relation_open(segrelid, AccessShareLock);
- results = GetAllAOCSFileSegInfo_pg_aocsseg_rel(
- RelationGetNumberOfAttributes(prel),
- RelationGetRelationName(prel),
+ results = GetAllAOCSFileSegInfo_pg_aocsseg_rel(prel,
pg_aocsseg_rel,
appendOnlyMetaDataSnapshot,
totalseg);
@@ -315,17 +368,13 @@ aocsFileSegInfoCmp(const void *left, const void *right)
return 0;
}
-
static AOCSFileSegInfo **
-GetAllAOCSFileSegInfo_pg_aocsseg_rel(int numOfColumns,
- char *relationName,
+GetAllAOCSFileSegInfo_pg_aocsseg_rel(Relation rel,
Relation pg_aocsseg_rel,
Snapshot snapshot,
int32 *totalseg)
{
- int32 nvp = numOfColumns;
-
SysScanDesc scan;
HeapTuple tup;
@@ -357,7 +406,7 @@ GetAllAOCSFileSegInfo_pg_aocsseg_rel(int numOfColumns,
allseg = (AOCSFileSegInfo **) repalloc(allseg, sizeof(AOCSFileSegInfo *) * seginfo_slot_no);
}
- aocs_seginfo = (AOCSFileSegInfo *) palloc0(aocsfileseginfo_size(nvp));
+ aocs_seginfo = (AOCSFileSegInfo *) palloc0(aocsfileseginfo_size(RelationGetNumberOfAttributes(rel)));
allseg[cur_seg] = aocs_seginfo;
@@ -389,33 +438,7 @@ GetAllAOCSFileSegInfo_pg_aocsseg_rel(int numOfColumns,
aocs_seginfo->state = DatumGetInt16(d[Anum_pg_aocs_state - 1]);
Assert(!null[Anum_pg_aocs_vpinfo - 1]);
- {
- uint32 len;
- struct varlena *v = (struct varlena *) DatumGetPointer(d[Anum_pg_aocs_vpinfo - 1]);
- struct varlena *dv = pg_detoast_datum(v);
-
- /*
- * VARSIZE(dv) may be less than aocs_vpinfo_size, in case of add
- * column, we try to do a ctas from old table to new table.
- *
- * Also, VARSIZE(dv) may be greater than aocs_vpinfo_size, in case
- * of begin transaction, add column and assign a new segno for insert,
- * and then rollback transaction. In this case, we must be using
- * RESERVED_SEGNO (check ChooseSegnoForWrite() for more detail).
- */
- Assert(VARSIZE(dv) <= aocs_vpinfo_size(nvp) || aocs_seginfo->segno == RESERVED_SEGNO);
-
- len = (VARSIZE(dv) <= aocs_vpinfo_size(nvp)) ? VARSIZE(dv) : aocs_vpinfo_size(nvp);
- memcpy(&aocs_seginfo->vpinfo, dv, len);
- if (len != VARSIZE(dv))
- {
- /* truncate VPInfoEntry of useless columns */
- SET_VARSIZE(&aocs_seginfo->vpinfo, len);
- aocs_seginfo->vpinfo.nEntry = nvp;
- }
- if (dv != v)
- pfree(dv);
- }
+ deformAOCSVPInfo(rel, (struct varlena *) DatumGetPointer(d[Anum_pg_aocs_vpinfo - 1]), aocs_seginfo);
++cur_seg;
}
@@ -1255,8 +1278,7 @@ gp_aocsseg_internal(PG_FUNCTION_ARGS, Oid aocsRelOid)
pg_aocsseg_rel = heap_open(segrelid, AccessShareLock);
context->aocsSegfileArray = GetAllAOCSFileSegInfo_pg_aocsseg_rel(
- aocsRel->rd_rel->relnatts,
- RelationGetRelationName(aocsRel),
+ aocsRel,
pg_aocsseg_rel,
appendOnlyMetaDataSnapshot,
&context->totalAocsSegFiles);
@@ -1468,8 +1490,7 @@ gp_aocsseg_history(PG_FUNCTION_ARGS)
pg_aocsseg_rel = heap_open(segrelid, AccessShareLock);
context->aocsSegfileArray = GetAllAOCSFileSegInfo_pg_aocsseg_rel(
- RelationGetNumberOfAttributes(aocsRel),
- RelationGetRelationName(aocsRel),
+ aocsRel,
pg_aocsseg_rel,
SnapshotAny, //Get ALL tuples from pg_aocsseg_ % including aborted and in - progress ones.
& context->totalAocsSegFiles);
diff --git a/src/include/access/aocssegfiles.h b/src/include/access/aocssegfiles.h
index fc0ec00529..18d67e6df2 100644
--- a/src/include/access/aocssegfiles.h
+++ b/src/include/access/aocssegfiles.h
@@ -135,7 +135,6 @@ extern AOCSFileSegInfo *GetAOCSFileSegInfo(Relation prel,
Snapshot appendOnlyMetaDataSnapshot,
int32 segno, bool locked);
-
extern AOCSFileSegInfo **GetAllAOCSFileSegInfo(Relation prel,
Snapshot appendOnlyMetaDataSnapshot,
int *totalseg,
diff --git a/src/test/regress/expected/alter_table_aocs.out b/src/test/regress/expected/alter_table_aocs.out
index b8fc977c6f..c6806a0d0a 100644
--- a/src/test/regress/expected/alter_table_aocs.out
+++ b/src/test/regress/expected/alter_table_aocs.out
@@ -836,3 +836,93 @@ Distributed by: (a)
Options: compresstype=rle_type, compresslevel=4, blocksize=65536
DROP TABLE aocs_alter_add_col_reorganize;
+-- test case: Ensure that reads don't fail after aborting an add column + insert operation and we don't project the aborted column
+CREATE TABLE aocs_addcol_abort(a int, b int) USING ao_column;
+INSERT INTO aocs_addcol_abort SELECT i,i FROM generate_series(1,10)i;
+BEGIN;
+ALTER TABLE aocs_addcol_abort ADD COLUMN c int;
+INSERT INTO aocs_addcol_abort SELECT i,i,i FROM generate_series(1,10)i;
+-- check state of aocsseg for entries of add column + insert
+SELECT * FROM gp_toolkit.__gp_aocsseg('aocs_addcol_abort') ORDER BY segment_id, column_num;
+ segment_id | segno | column_num | physical_segno | tupcount | eof | eof_uncompressed | modcount | formatversion | state
+------------+-------+------------+----------------+----------+-----+------------------+----------+---------------+-------
+ 0 | 0 | 0 | 0 | 5 | 64 | 64 | 1 | 3 | 1
+ 0 | 1 | 0 | 1 | 5 | 64 | 64 | 2 | 3 | 1
+ 0 | 0 | 1 | 128 | 5 | 64 | 64 | 1 | 3 | 1
+ 0 | 1 | 1 | 129 | 5 | 64 | 64 | 2 | 3 | 1
+ 0 | 0 | 2 | 256 | 5 | 64 | 64 | 1 | 3 | 1
+ 0 | 1 | 2 | 257 | 5 | 48 | 48 | 2 | 3 | 1
+ 1 | 0 | 0 | 0 | 1 | 48 | 48 | 1 | 3 | 1
+ 1 | 1 | 0 | 1 | 1 | 48 | 48 | 2 | 3 | 1
+ 1 | 0 | 1 | 128 | 1 | 48 | 48 | 1 | 3 | 1
+ 1 | 1 | 1 | 129 | 1 | 48 | 48 | 2 | 3 | 1
+ 1 | 0 | 2 | 256 | 1 | 48 | 48 | 1 | 3 | 1
+ 1 | 1 | 2 | 257 | 1 | 48 | 48 | 2 | 3 | 1
+ 2 | 0 | 0 | 0 | 4 | 56 | 56 | 1 | 3 | 1
+ 2 | 1 | 0 | 1 | 4 | 56 | 56 | 2 | 3 | 1
+ 2 | 0 | 1 | 128 | 4 | 56 | 56 | 1 | 3 | 1
+ 2 | 1 | 1 | 129 | 4 | 56 | 56 | 2 | 3 | 1
+ 2 | 0 | 2 | 256 | 4 | 56 | 56 | 1 | 3 | 1
+ 2 | 1 | 2 | 257 | 4 | 48 | 48 | 2 | 3 | 1
+(18 rows)
+
+SELECT * FROM aocs_addcol_abort;
+ a | b | c
+----+----+----
+ 5 | 5 | 5
+ 6 | 6 | 6
+ 9 | 9 | 9
+ 10 | 10 | 10
+ 5 | 5 |
+ 6 | 6 |
+ 9 | 9 |
+ 10 | 10 |
+ 1 | 1 | 1
+ 1 | 1 |
+ 2 | 2 | 2
+ 3 | 3 | 3
+ 4 | 4 | 4
+ 7 | 7 | 7
+ 8 | 8 | 8
+ 2 | 2 |
+ 3 | 3 |
+ 4 | 4 |
+ 7 | 7 |
+ 8 | 8 |
+(20 rows)
+
+ABORT;
+-- check state of aocsseg if entries for new column are rolled back correctly
+SELECT * FROM gp_toolkit.__gp_aocsseg('aocs_addcol_abort') ORDER BY segment_id, column_num;
+ segment_id | segno | column_num | physical_segno | tupcount | eof | eof_uncompressed | modcount | formatversion | state
+------------+-------+------------+----------------+----------+-----+------------------+----------+---------------+-------
+ 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3 | 1
+ 0 | 1 | 0 | 1 | 5 | 64 | 64 | 1 | 3 | 1
+ 0 | 0 | 1 | 128 | 0 | 0 | 0 | 0 | 3 | 1
+ 0 | 1 | 1 | 129 | 5 | 64 | 64 | 1 | 3 | 1
+ 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3 | 1
+ 1 | 1 | 0 | 1 | 1 | 48 | 48 | 1 | 3 | 1
+ 1 | 0 | 1 | 128 | 0 | 0 | 0 | 0 | 3 | 1
+ 1 | 1 | 1 | 129 | 1 | 48 | 48 | 1 | 3 | 1
+ 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3 | 1
+ 2 | 1 | 0 | 1 | 4 | 56 | 56 | 1 | 3 | 1
+ 2 | 0 | 1 | 128 | 0 | 0 | 0 | 0 | 3 | 1
+ 2 | 1 | 1 | 129 | 4 | 56 | 56 | 1 | 3 | 1
+(12 rows)
+
+SELECT * FROM aocs_addcol_abort;
+ a | b
+----+----
+ 5 | 5
+ 6 | 6
+ 9 | 9
+ 10 | 10
+ 1 | 1
+ 2 | 2
+ 3 | 3
+ 4 | 4
+ 7 | 7
+ 8 | 8
+(10 rows)
+
+DROP TABLE aocs_addcol_abort;
diff --git a/src/test/regress/sql/alter_table_aocs.sql b/src/test/regress/sql/alter_table_aocs.sql
index 46d2804906..d6e542c1c7 100644
--- a/src/test/regress/sql/alter_table_aocs.sql
+++ b/src/test/regress/sql/alter_table_aocs.sql
@@ -480,3 +480,18 @@ RESET gp_default_storage_options;
ALTER TABLE aocs_alter_add_col_reorganize ADD COLUMN d int;
\d+ aocs_alter_add_col_reorganize
DROP TABLE aocs_alter_add_col_reorganize;
+
+-- test case: Ensure that reads don't fail after aborting an add column + insert operation and we don't project the aborted column
+CREATE TABLE aocs_addcol_abort(a int, b int) USING ao_column;
+INSERT INTO aocs_addcol_abort SELECT i,i FROM generate_series(1,10)i;
+BEGIN;
+ALTER TABLE aocs_addcol_abort ADD COLUMN c int;
+INSERT INTO aocs_addcol_abort SELECT i,i,i FROM generate_series(1,10)i;
+-- check state of aocsseg for entries of add column + insert
+SELECT * FROM gp_toolkit.__gp_aocsseg('aocs_addcol_abort') ORDER BY segment_id, column_num;
+SELECT * FROM aocs_addcol_abort;
+ABORT;
+-- check state of aocsseg if entries for new column are rolled back correctly
+SELECT * FROM gp_toolkit.__gp_aocsseg('aocs_addcol_abort') ORDER BY segment_id, column_num;
+SELECT * FROM aocs_addcol_abort;
+DROP TABLE aocs_addcol_abort;
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org