Refactor insert select planner for improved identity column handling and enhance regression tests

pull/7920/head
Mehmet Yilmaz 2025-03-05 07:14:40 +00:00
parent 56547f72d1
commit 8ff4b94271
2 changed files with 78 additions and 72 deletions

View File

@ -1073,7 +1073,6 @@ ReorderInsertSelectTargetLists(Query *originalQuery, RangeTblEntry *insertRte,
oldInsertTargetEntry->resname);
/* see transformInsertRow() for the details */
if (IsA(oldInsertTargetEntry->expr, SubscriptingRef) ||
IsA(oldInsertTargetEntry->expr, FieldStore))
@ -1116,7 +1115,8 @@ ReorderInsertSelectTargetLists(Query *originalQuery, RangeTblEntry *insertRte,
* specify OVERRIDING SYSTEM VALUE. If both conditions are true, we need to consider
* generating a default sequence value.
*/
if (IsIdentityColumn(insertRelationId, oldInsertTargetEntry->resname) && originalQuery->override != OVERRIDING_SYSTEM_VALUE)
if (IsIdentityColumn(insertRelationId, oldInsertTargetEntry->resname) &&
originalQuery->override != OVERRIDING_SYSTEM_VALUE)
{
/*
* Open the target relation (table) with an AccessShareLock to safely access metadata,
@ -1124,7 +1124,8 @@ ReorderInsertSelectTargetLists(Query *originalQuery, RangeTblEntry *insertRte,
*/
Relation targetRel = table_open(insertRelationId, AccessShareLock);
AttrNumber attrNum = get_attnum(insertRelationId, oldInsertTargetEntry->resname);
AttrNumber attrNum = get_attnum(insertRelationId,
oldInsertTargetEntry->resname);
bool missingOk = false;
Oid seqOid = getIdentitySequence(targetRel, attrNum, missingOk);
@ -1233,11 +1234,10 @@ ReorderInsertSelectTargetLists(Query *originalQuery, RangeTblEntry *insertRte,
Expr *
MakeNextValExprForIdentity(Oid seq_relid)
{
Const *seq_const;
List *func_args;
Oid nextval_oid;
seq_const = makeConst(
Const *seq_const = makeConst(
REGCLASSOID, /* type for regclass */
-1, /* no specific collation */
InvalidOid, /* default collation */
@ -1251,7 +1251,7 @@ MakeNextValExprForIdentity(Oid seq_relid)
nextval_oid = LookupFuncName(
list_make1(makeString("nextval")),
1,
(Oid[]){ REGCLASSOID },
(Oid[]) { REGCLASSOID },
false
);
@ -1274,17 +1274,23 @@ IsIdentityColumn(Oid relid, const char *colName)
{
/* Check if colName is non-null (optional, if colName can be NULL) */
if (colName == NULL)
{
return false;
}
/* Get the attribute number for the given column name */
AttrNumber attrNum = get_attnum(relid, colName);
if (attrNum == InvalidAttrNumber)
{
return false;
}
/* Open the relation to access its metadata */
Relation rel = RelationIdGetRelation(relid);
if (!RelationIsValid(rel))
{
return false;
}
/* Ensure the attribute number is within the valid range */
if (attrNum <= 0 || attrNum > rel->rd_att->natts)