@@ -797,13 +797,26 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
797797 LOCKMODE parentLockmode;
798798 Oid accessMethodId = InvalidOid;
799799 ObjectAddress tsql_tabletype_address;
800+ const char *effective_relname;
800801
801802 /*
802803 * Truncate relname to appropriate length (probably a waste of time, as
803804 * parser should have done this already).
804805 */
805806 strlcpy(relname, stmt->relation->relname, NAMEDATALEN);
806807
808+ /*
809+ * For T-SQL #temp tables, ENR stores names in heap-allocated memory
810+ * without NAMEDATALEN limit. Use the full original name so that
811+ * subsequent lookups via the scanner match the registered ENR entry.
812+ */
813+ if (sql_dialect == SQL_DIALECT_TSQL &&
814+ stmt->relation->relpersistence == RELPERSISTENCE_TEMP &&
815+ stmt->relation->relname[0] == '#')
816+ effective_relname = stmt->relation->relname;
817+ else
818+ effective_relname = relname;
819+
807820 /*
808821 * Check consistency of arguments
809822 */
@@ -1078,7 +1091,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
10781091 * passed in for immediate handling --- since they don't need parsing,
10791092 * they can be stored immediately.
10801093 */
1081- relationId = heap_create_with_catalog(relname ,
1094+ relationId = heap_create_with_catalog(effective_relname ,
10821095 namespaceId,
10831096 tablespaceId,
10841097 InvalidOid,
0 commit comments