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

m3hm3t/issue_7887
Mehmet Yilmaz 2025-03-05 07:14:40 +00:00
parent 789bfbbf54
commit c865d6323c
2 changed files with 78 additions and 72 deletions

View File

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