Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/backend/parser/parse_utilcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ typedef struct
List *ckconstraints; /* CHECK constraints */
List *fkconstraints; /* FOREIGN KEY constraints */
List *ixconstraints; /* index-creating constraints */
List *inh_indexes; /* cloned indexes from INCLUDING INDEXES
* GPDB: used by transformDistributedBy */
List *attr_encodings; /* List of ColumnReferenceStorageDirectives */
List *extstats; /* cloned extended statistics */
List *blist; /* "before list" of things to do before
Expand Down Expand Up @@ -291,6 +293,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
cxt.ckconstraints = NIL;
cxt.fkconstraints = NIL;
cxt.ixconstraints = NIL;
cxt.inh_indexes = NIL; /* GPDB: used by transformDistributedBy */
cxt.extstats = NIL;
cxt.attr_encodings = stmt->attr_encodings;
cxt.blist = NIL;
Expand Down Expand Up @@ -1092,6 +1095,13 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla

tupleDesc = RelationGetDescr(relation);

/*
* Initialize column number map for map_variable_attnos(). We need this
* since dropped columns in the source table aren't copied, so the new
* table can have different column numbers.
*/
AttrMap *attmap = make_attrmap(tupleDesc->natts);

/*
* Insert the copied attributes into the cxt for the new table definition.
* We must do this now so that they appear in the table in the relative
Expand Down Expand Up @@ -1137,6 +1147,7 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
* Add to column list
*/
cxt->columns = lappend(cxt->columns, def);
attmap->attnums[parent_attno - 1] = list_length(cxt->columns);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this approach really valid? The issue solved by the original commit was that inheritance isn't handled at the point when transformTableLikeClause is called, so the true attribute numbers can't be determined at all (since INHERITS columns should go before columns added with LIKE). I think if we need valid attribute numbers for the new table, we will have to defer this until expandTableLikeClause

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will have to defer this until expandTableLikeClause

we need indexes in transformDistributedBy() which is before expandTableLikeClause()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, so I suggest adding something like expandDistributedBy, to handle this case after transformDistributedBy, since we can't handle it here.
P.S. We can also leave this approach for now until we can run the tests, and then rework it if there are issues.

@bimboterminator1 bimboterminator1 Jun 2, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are issues. create_table_like.sql

CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, y text); /* copies indexes */
+ERROR:  oids were assigned, but not dispatched to QEs (oid_dispatch.c:1490)
+WARNING:  OID assignment not dispatched: catalog 1259, namespace: 2200, name: "inhg_pkey"
+WARNING:  OID assignment not dispatched: catalog 2606, namespace: 0, name: "inhg_pkey"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are issues. create_table_like.sql

CREATE TABLE inhg (x text, LIKE inhx INCLUDING INDEXES, y text); /* copies indexes */
+ERROR:  oids were assigned, but not dispatched to QEs (oid_dispatch.c:1490)
+WARNING:  OID assignment not dispatched: catalog 1259, namespace: 2200, name: "inhg_pkey"
+WARNING:  OID assignment not dispatched: catalog 2606, namespace: 0, name: "inhg_pkey"

fixed by #2639


/*
* Although we don't transfer the column's default/generation
Expand Down Expand Up @@ -1297,6 +1308,53 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
list_free(parent_extstats);
}

/*
* Copy indexes for Greengage choosing distributed-by keys.
* PostgreSQL processes index statements after here in expandTableLikeClause(),
* but we need indexes in transformDistributedBy() which is before expandTableLikeClause(),
* So we both retain the index statements processing here and expandTableLikeClause.
* the process here is just used by transformDistributedBy().
*/
if ((table_like_clause->options & CREATE_TABLE_LIKE_INDEXES) &&
relation->rd_rel->relhasindex)
{
List *parent_indexes;
ListCell *l;

parent_indexes = RelationGetIndexList(relation);

foreach(l, parent_indexes)
{
Oid parent_index_oid = lfirst_oid(l);
Relation parent_index;
IndexStmt *index_stmt;

parent_index = index_open(parent_index_oid, AccessShareLock);

/* Build CREATE INDEX statement to recreate the parent_index */
index_stmt = generateClonedIndexStmt(stmt->relation,
parent_index,
attmap,
NULL);

/* Copy comment on index, if requested */
if (table_like_clause->options & CREATE_TABLE_LIKE_COMMENTS)
{
comment = GetComment(parent_index_oid, RelationRelationId, 0);

/*
* We make use of IndexStmt's idxcomment option, so as not to
* need to know now what name the index will have.
*/
index_stmt->idxcomment = comment;
}

/* Save it in the inh_indexes list for the time being */
cxt->inh_indexes = lappend(cxt->inh_indexes, index_stmt);

index_close(parent_index, AccessShareLock);
}
}
/*
* Close the parent rel, but keep our AccessShareLock on it until xact
* commit. That will prevent someone else from deleting or ALTERing the
Expand Down Expand Up @@ -2176,6 +2234,7 @@ transformCreateExternalStmt(CreateExternalStmt *stmt, const char *queryString)
cxt.ckconstraints = NIL;
cxt.fkconstraints = NIL;
cxt.ixconstraints = NIL;
cxt.inh_indexes = NIL; /* GPDB: used by transformDistributedBy */
cxt.attr_encodings = NIL;
cxt.pkey = NULL;
cxt.rel = NULL;
Expand Down Expand Up @@ -4306,6 +4365,7 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
cxt.ckconstraints = NIL;
cxt.fkconstraints = NIL;
cxt.ixconstraints = NIL;
cxt.inh_indexes = NIL; /* GPDB: used by transformDistributedBy */
cxt.attr_encodings = NIL;
cxt.extstats = NIL;
cxt.blist = NIL;
Expand Down