From 1a802a17d5f7fd23ebd972d7559b35acf27b2786 Mon Sep 17 00:00:00 2001 From: kooooootb Date: Sun, 12 Nov 2023 20:12:11 +0300 Subject: [PATCH] Checking ao table while looking for column to prevent finding invalid system columns add check for ao table while looking for column add tests add comments --- src/backend/parser/parse_relation.c | 20 ++++++++++++++++++++ src/test/regress/input/appendonly.source | 5 +++++ src/test/regress/output/appendonly.source | 9 +++++++++ 3 files changed, 34 insertions(+) diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c index f114504d835b..3144cc3d2d4f 100644 --- a/src/backend/parser/parse_relation.c +++ b/src/backend/parser/parse_relation.c @@ -531,6 +531,20 @@ GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte, int rtelevelsup) return NULL; /* keep compiler quiet */ } +static bool +IsAppendOptimizedByOid(Oid relid) +{ + Relation rel; + bool result; + + rel = heap_open(relid, NoLock); + + result = RelationIsAppendOptimized(rel); + heap_close(rel, NoLock); + + return result; +} + /* * scanRTEForColumn * Search the column names of a single RTE for the given name. @@ -603,6 +617,12 @@ scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, char *colname, Gp_role != GP_ROLE_UTILITY) return result; + /* In GPDB tables that have append-optimized storage system columns are not + * stored in tuples, so we check it here + */ + if (IsAppendOptimizedByOid(rte->relid)) + return result; + /* quick check to see if name could be a system column */ attnum = specialAttNum(colname); diff --git a/src/test/regress/input/appendonly.source b/src/test/regress/input/appendonly.source index 8e1b5e58515f..d285fc84f306 100644 --- a/src/test/regress/input/appendonly.source +++ b/src/test/regress/input/appendonly.source @@ -804,3 +804,8 @@ insert into fix_ao_truncate_last_sequence select 1, 1 from generate_series(1, 5) select count(*) from fix_ao_truncate_last_sequence; abort; +-- Check if system columns in ao table can be selected +DROP TABLE IF EXISTS ttt_ao; +CREATE TABLE ttt (id int) WITH (appendonly=true) DISTRIBUTED BY (id); +INSERT INTO ttt VALUES(1); +SELECT xmin FROM ttt; diff --git a/src/test/regress/output/appendonly.source b/src/test/regress/output/appendonly.source index fe566fd24abd..a74bd6c75441 100644 --- a/src/test/regress/output/appendonly.source +++ b/src/test/regress/output/appendonly.source @@ -1706,3 +1706,12 @@ select count(*) from fix_ao_truncate_last_sequence; (1 row) abort; + +-- Check if system columns in ao table can be selected +DROP TABLE IF EXISTS ttt_ao; +CREATE TABLE ttt (id int) WITH (appendonly=true) DISTRIBUTED BY (id); +INSERT INTO ttt VALUES(1); +SELECT xmin FROM ttt; +ERROR: column "xmin" does not exist +LINE 1: select xmin from ttt; + ^