@@ -175,7 +175,7 @@ extern PLtsql_function *find_cached_batch(int handle);
175175extern void apply_post_compile_actions (PLtsql_function * func , InlineCodeBlockArgs * args );
176176Datum sp_prepare (PG_FUNCTION_ARGS );
177177Datum sp_unprepare (PG_FUNCTION_ARGS );
178- static List * transformSelectIntoStmt (CreateTableAsStmt * stmt );
178+ static List * transformSelectIntoStmt (CreateTableAsStmt * stmt , const char * queryString );
179179static char * get_oid_type_string (int type_oid );
180180static int64 get_identity_into_args (Node * node );
181181extern char * construct_unique_index_name (char * index_name , char * relation_name );
@@ -239,6 +239,7 @@ static TargetEntry* buildJsonEntry(int nestLevel, char* tableAlias, TargetEntry*
239239static char * string_to_fixed_hash (const char * input );
240240static void processAutoColumns (Query * wrapperQuery , Query * origQuery , Alias * wrapperRteAlias , ForAutoContext * ctx , ForAutoMode mode );
241241extern const char * ATTOPTION_BBF_ORIGINAL_NAME ;
242+ extern const char * ATTOPTION_BBF_ORIGINAL_TABLE_NAME ;
242243extern bool pltsql_ansi_defaults ;
243244extern bool pltsql_quoted_identifier ;
244245extern bool pltsql_concat_null_yields_null ;
@@ -5386,6 +5387,34 @@ bbf_ProcessUtility(PlannedStmt *pstmt,
53865387 List * partition_schemes = stmt -> excludeOpNames ;
53875388
53885389 stmt -> excludeOpNames = NIL ;
5390+
5391+ /*
5392+ * For indexes on temp tables, extract the full original
5393+ * name from the query string using name_location stored
5394+ * by the parser. Remove name_location from options as
5395+ * it is for internal use only.
5396+ */
5397+ {
5398+ ListCell * lc ;
5399+ foreach (lc , stmt -> options )
5400+ {
5401+ DefElem * opt = (DefElem * ) lfirst (lc );
5402+ if (strcmp (opt -> defname , "name_location" ) == 0 )
5403+ {
5404+ if (stmt -> relation -> relpersistence == RELPERSISTENCE_TEMP &&
5405+ stmt -> relation -> relname [0 ] == '#' && original_name )
5406+ {
5407+ int loc = intVal (opt -> arg );
5408+ char * full_name = extract_identifier (queryString + loc , NULL );
5409+ if (full_name )
5410+ original_name = full_name ;
5411+ }
5412+ stmt -> options = foreach_delete_current (stmt -> options , lc );
5413+ break ;
5414+ }
5415+ }
5416+ }
5417+
53895418 if (stmt -> idxname && !stmt -> isconstraint )
53905419 stmt -> idxname = construct_unique_index_name (stmt -> idxname , stmt -> relation -> relname );
53915420 /*
@@ -7998,7 +8027,8 @@ get_identity_into_args(Node *node)
79988027}
79998028
80008029static List *
8001- transformSelectIntoStmt (CreateTableAsStmt * stmt )
8030+ transformSelectIntoStmt
8031+ (CreateTableAsStmt * stmt , const char * queryString )
80028032{
80038033 List * result ;
80048034 ListCell * elements ;
@@ -8239,6 +8269,31 @@ transformSelectIntoStmt(CreateTableAsStmt *stmt)
82398269 }
82408270
82418271 result = lappend (result , stmt );
8272+
8273+ /* Store original name in reloption for SELECT INTO #temp with long names */
8274+ if (into && into -> rel &&
8275+ into -> rel -> relpersistence == RELPERSISTENCE_TEMP &&
8276+ into -> rel -> relname [0 ] == '#' &&
8277+ into -> rel -> location >= 0 && queryString != NULL )
8278+ {
8279+ char * original_name = extract_identifier (queryString + into -> rel -> location , NULL );
8280+
8281+ if (original_name && strlen (original_name ) >= NAMEDATALEN )
8282+ {
8283+ if (!altstmt )
8284+ {
8285+ altstmt = makeNode (AlterTableStmt );
8286+ altstmt -> relation = into -> rel ;
8287+ altstmt -> objtype = OBJECT_TABLE ;
8288+ altstmt -> cmds = NIL ;
8289+ }
8290+ altstmt -> cmds = lappend (altstmt -> cmds , make_original_rel_name_cmd (original_name ));
8291+ pfree (original_name );
8292+ }
8293+ else if (original_name )
8294+ pfree (original_name );
8295+ }
8296+
82428297 if (altstmt && list_length (altstmt -> cmds ) > 0 )
82438298 result = lappend (result , altstmt );
82448299
@@ -8251,7 +8306,7 @@ void pltsql_bbfSelectIntoUtility(ParseState *pstate, PlannedStmt *pstmt, const c
82518306
82528307 Node * parsetree = pstmt -> utilityStmt ;
82538308 List * stmts ;
8254- stmts = transformSelectIntoStmt ((CreateTableAsStmt * )parsetree );
8309+ stmts = transformSelectIntoStmt ((CreateTableAsStmt * )parsetree , queryString );
82558310 while (stmts != NIL )
82568311 {
82578312 Node * stmt = (Node * )linitial (stmts );
0 commit comments