From 0ebb3e626d865c5cf64e4acc6f6090afc0498e9b Mon Sep 17 00:00:00 2001 From: anju15bharti Date: Mon, 1 Jun 2026 15:06:23 +0000 Subject: [PATCH 1/3] Fix: ENR temp table creation fails for long table names --- src/backend/commands/tablecmds.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 4d66c4d2524..2ea7bc8bf2a 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -797,6 +797,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, LOCKMODE parentLockmode; Oid accessMethodId = InvalidOid; ObjectAddress tsql_tabletype_address; + const char *effective_relname; /* * Truncate relname to appropriate length (probably a waste of time, as @@ -804,6 +805,18 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, */ strlcpy(relname, stmt->relation->relname, NAMEDATALEN); + /* + * For T-SQL #temp tables, ENR stores names in heap-allocated memory + * without NAMEDATALEN limit. Use the full original name so that + * subsequent lookups via the scanner match the registered ENR entry. + */ + if (sql_dialect == SQL_DIALECT_TSQL && + stmt->relation->relpersistence == RELPERSISTENCE_TEMP && + stmt->relation->relname[0] == '#') + effective_relname = stmt->relation->relname; + else + effective_relname = relname; + /* * Check consistency of arguments */ @@ -1078,7 +1091,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, * passed in for immediate handling --- since they don't need parsing, * they can be stored immediately. */ - relationId = heap_create_with_catalog(relname, + relationId = heap_create_with_catalog(effective_relname, namespaceId, tablespaceId, InvalidOid, @@ -5821,6 +5834,23 @@ ATParseTransformCmd(List **wqueue, AlteredTableInfo *tab, Relation rel, makeRangeVar(get_namespace_name(RelationGetNamespace(rel)), pstrdup(RelationGetRelationName(rel)), -1); + + /* + * For T-SQL #temp tables with long names (>= NAMEDATALEN), the relation + * descriptor stores the name clipped to NAMEDATALEN-1 (63 chars) due to the + * Name type limitation. However, ENR stores the full untruncated name which + * is what subsequent lookups via RangeVarGetRelidExtended need to match. + * Replace the clipped relname with the full name from ENR. + */ + if (sql_dialect == SQL_DIALECT_TSQL && + RelationGetRelationName(rel)[0] == '#') + { + EphemeralNamedRelation enr = get_ENR_withoid(currentQueryEnv, + RelationGetRelid(rel), + ENR_TSQL_TEMP, true); + if (enr) + atstmt->relation->relname = pstrdup(enr->md.name); + } atstmt->relation->inh = recurse; atstmt->cmds = list_make1(cmd); atstmt->objtype = OBJECT_TABLE; /* needn't be picky here */ From 6368e3748c71247bb28a743f6b11302416ec4478 Mon Sep 17 00:00:00 2001 From: anju15bharti Date: Tue, 9 Jun 2026 13:35:46 +0000 Subject: [PATCH 2/3] Rename effective_relname variable --- src/backend/commands/tablecmds.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 2ea7bc8bf2a..21215180a12 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -797,7 +797,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, LOCKMODE parentLockmode; Oid accessMethodId = InvalidOid; ObjectAddress tsql_tabletype_address; - const char *effective_relname; + const char *enr_relname; /* * Truncate relname to appropriate length (probably a waste of time, as @@ -813,9 +813,9 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, if (sql_dialect == SQL_DIALECT_TSQL && stmt->relation->relpersistence == RELPERSISTENCE_TEMP && stmt->relation->relname[0] == '#') - effective_relname = stmt->relation->relname; + enr_relname = stmt->relation->relname; else - effective_relname = relname; + enr_relname = relname; /* * Check consistency of arguments @@ -1091,7 +1091,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, * passed in for immediate handling --- since they don't need parsing, * they can be stored immediately. */ - relationId = heap_create_with_catalog(effective_relname, + relationId = heap_create_with_catalog(enr_relname, namespaceId, tablespaceId, InvalidOid, From 6c45a53cd944b24a9924d3cfd27d9dfebb6b372e Mon Sep 17 00:00:00 2001 From: anju15bharti Date: Wed, 10 Jun 2026 13:58:37 +0000 Subject: [PATCH 3/3] Revert: ENR temp table creation for long table names Reverts commits 6368e3748c and 0ebb3e626d. The approach of storing full name directly in ENR is replaced by a new design using a separate orig_name field. --- src/backend/commands/tablecmds.c | 32 +------------------------------- 1 file changed, 1 insertion(+), 31 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 21215180a12..4d66c4d2524 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -797,7 +797,6 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, LOCKMODE parentLockmode; Oid accessMethodId = InvalidOid; ObjectAddress tsql_tabletype_address; - const char *enr_relname; /* * Truncate relname to appropriate length (probably a waste of time, as @@ -805,18 +804,6 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, */ strlcpy(relname, stmt->relation->relname, NAMEDATALEN); - /* - * For T-SQL #temp tables, ENR stores names in heap-allocated memory - * without NAMEDATALEN limit. Use the full original name so that - * subsequent lookups via the scanner match the registered ENR entry. - */ - if (sql_dialect == SQL_DIALECT_TSQL && - stmt->relation->relpersistence == RELPERSISTENCE_TEMP && - stmt->relation->relname[0] == '#') - enr_relname = stmt->relation->relname; - else - enr_relname = relname; - /* * Check consistency of arguments */ @@ -1091,7 +1078,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, * passed in for immediate handling --- since they don't need parsing, * they can be stored immediately. */ - relationId = heap_create_with_catalog(enr_relname, + relationId = heap_create_with_catalog(relname, namespaceId, tablespaceId, InvalidOid, @@ -5834,23 +5821,6 @@ ATParseTransformCmd(List **wqueue, AlteredTableInfo *tab, Relation rel, makeRangeVar(get_namespace_name(RelationGetNamespace(rel)), pstrdup(RelationGetRelationName(rel)), -1); - - /* - * For T-SQL #temp tables with long names (>= NAMEDATALEN), the relation - * descriptor stores the name clipped to NAMEDATALEN-1 (63 chars) due to the - * Name type limitation. However, ENR stores the full untruncated name which - * is what subsequent lookups via RangeVarGetRelidExtended need to match. - * Replace the clipped relname with the full name from ENR. - */ - if (sql_dialect == SQL_DIALECT_TSQL && - RelationGetRelationName(rel)[0] == '#') - { - EphemeralNamedRelation enr = get_ENR_withoid(currentQueryEnv, - RelationGetRelid(rel), - ENR_TSQL_TEMP, true); - if (enr) - atstmt->relation->relname = pstrdup(enr->md.name); - } atstmt->relation->inh = recurse; atstmt->cmds = list_make1(cmd); atstmt->objtype = OBJECT_TABLE; /* needn't be picky here */