@@ -611,8 +611,26 @@ static void process_utility(PlannedStmt* pstmt,
611611
612612 // / Extract table name
613613 RangeVar* rel = stmt->relation ;
614- const std::string schema_name = (rel->schemaname != nullptr ? rel->schemaname : " public" );
615- const std::string table_name = schema_name + " ." + rel->relname ;
614+
615+ // Get the actual relation OID to resolve the proper schema name (handles custom schemas via search_path)
616+ Oid rel_oid = RangeVarGetRelid (rel, NoLock, false );
617+ Relation temp_rel = RelationIdGetRelation (rel_oid);
618+ std::string table_name;
619+
620+ if (RelationIsValid (temp_rel)) {
621+ Oid nspid = RelationGetNamespace (temp_rel);
622+ char * nspname = get_namespace_name (nspid);
623+ table_name = std::string (nspname ? nspname : " public" ) + " ." + RelationGetRelationName (temp_rel);
624+ if (nspname) {
625+ pfree (nspname);
626+ }
627+ RelationClose (temp_rel);
628+ } else {
629+ // Fallback to RangeVar if relation is invalid (shouldn't happen)
630+ const std::string schema_name = (rel->schemaname != nullptr ? rel->schemaname : " public" );
631+ table_name = schema_name + " ." + rel->relname ;
632+ }
633+
616634 auto * td = pg::table_storage::instance ().get_table_data_if_exists (table_name);
617635 if (td != nullptr ) {
618636 ListCell* lc = nullptr ;
@@ -732,8 +750,26 @@ static void process_utility(PlannedStmt* pstmt,
732750 if (IsA (pstmt->utilityStmt , AlterTableStmt)) {
733751 AlterTableStmt* stmt = (AlterTableStmt*)pstmt->utilityStmt ;
734752 RangeVar* rel = stmt->relation ;
735- const std::string schema_name = (rel->schemaname != nullptr ? rel->schemaname : " public" );
736- const std::string table_name = schema_name + " ." + rel->relname ;
753+
754+ // Get the actual relation OID to resolve the proper schema name (handles custom schemas via search_path)
755+ Oid rel_oid = RangeVarGetRelid (rel, NoLock, false );
756+ Relation temp_rel = RelationIdGetRelation (rel_oid);
757+ std::string table_name;
758+
759+ if (RelationIsValid (temp_rel)) {
760+ Oid nspid = RelationGetNamespace (temp_rel);
761+ char * nspname = get_namespace_name (nspid);
762+ table_name = std::string (nspname ? nspname : " public" ) + " ." + RelationGetRelationName (temp_rel);
763+ if (nspname) {
764+ pfree (nspname);
765+ }
766+ RelationClose (temp_rel);
767+ } else {
768+ // Fallback to RangeVar if relation is invalid (shouldn't happen)
769+ const std::string schema_name = (rel->schemaname != nullptr ? rel->schemaname : " public" );
770+ table_name = schema_name + " ." + rel->relname ;
771+ }
772+
737773 auto * td = pg::table_storage::instance ().get_table_data_if_exists (table_name);
738774
739775 if (td != nullptr ) {
@@ -746,7 +782,6 @@ static void process_utility(PlannedStmt* pstmt,
746782 const char * column_name = coldef->colname ;
747783
748784 // Get the relation to query the new column's type from catalog
749- Oid rel_oid = RangeVarGetRelid (rel, NoLock, false );
750785 Relation relation = RelationIdGetRelation (rel_oid);
751786 if (RelationIsValid (relation)) {
752787 TupleDesc tupdesc = RelationGetDescr (relation);
@@ -981,8 +1016,26 @@ static void process_utility(PlannedStmt* pstmt,
9811016 RenameStmt* stmt = (RenameStmt*)pstmt->utilityStmt ;
9821017 if (stmt->renameType == OBJECT_COLUMN && stmt->relation != nullptr ) {
9831018 RangeVar* rel = stmt->relation ;
984- const std::string schema_name = (rel->schemaname != nullptr ? rel->schemaname : " public" );
985- const std::string table_name = schema_name + " ." + rel->relname ;
1019+
1020+ // Get the actual relation OID to resolve the proper schema name (handles custom schemas via search_path)
1021+ Oid rel_oid = RangeVarGetRelid (rel, NoLock, false );
1022+ Relation temp_rel = RelationIdGetRelation (rel_oid);
1023+ std::string table_name;
1024+
1025+ if (RelationIsValid (temp_rel)) {
1026+ Oid nspid = RelationGetNamespace (temp_rel);
1027+ char * nspname = get_namespace_name (nspid);
1028+ table_name = std::string (nspname ? nspname : " public" ) + " ." + RelationGetRelationName (temp_rel);
1029+ if (nspname) {
1030+ pfree (nspname);
1031+ }
1032+ RelationClose (temp_rel);
1033+ } else {
1034+ // Fallback to RangeVar if relation is invalid (shouldn't happen)
1035+ const std::string schema_name = (rel->schemaname != nullptr ? rel->schemaname : " public" );
1036+ table_name = schema_name + " ." + rel->relname ;
1037+ }
1038+
9861039 auto * td = pg::table_storage::instance ().get_table_data_if_exists (table_name);
9871040
9881041 if (td != nullptr && stmt->subname != nullptr && stmt->newname != nullptr ) {
@@ -1018,9 +1071,26 @@ static void process_utility(PlannedStmt* pstmt,
10181071 CopyStmt* copy_stmt = (CopyStmt*)pstmt->utilityStmt ;
10191072 if (copy_stmt->relation ) {
10201073 // Build the qualified table name
1021- const char * schema = copy_stmt->relation ->schemaname ? copy_stmt->relation ->schemaname : " public" ;
1022- const char * table = copy_stmt->relation ->relname ;
1023- std::string table_name = std::string (schema) + " ." + table;
1074+ // Get the actual relation OID to resolve the proper schema name (handles custom schemas via search_path)
1075+ Oid rel_oid = RangeVarGetRelid (copy_stmt->relation , NoLock, false );
1076+ Relation temp_rel = RelationIdGetRelation (rel_oid);
1077+ std::string table_name;
1078+
1079+ if (RelationIsValid (temp_rel)) {
1080+ Oid nspid = RelationGetNamespace (temp_rel);
1081+ char * nspname = get_namespace_name (nspid);
1082+ table_name = std::string (nspname ? nspname : " public" ) + " ." + RelationGetRelationName (temp_rel);
1083+ if (nspname) {
1084+ pfree (nspname);
1085+ }
1086+ RelationClose (temp_rel);
1087+ } else {
1088+ // Fallback to RangeVar if relation is invalid (shouldn't happen)
1089+ const char * schema = copy_stmt->relation ->schemaname ? copy_stmt->relation ->schemaname : " public" ;
1090+ const char * table = copy_stmt->relation ->relname ;
1091+ table_name = std::string (schema) + " ." + table;
1092+ }
1093+
10241094 // If this is a deeplake table, flush/commit
10251095 auto * td = pg::table_storage::instance ().get_table_data_if_exists (table_name);
10261096 if (td) {
0 commit comments