@@ -9,7 +9,9 @@ extern "C" {
99#include < postgres.h>
1010
1111#include < catalog/namespace.h>
12+ #include < commands/dbcommands.h>
1213#include < commands/defrem.h>
14+ #include < miscadmin.h>
1315#include < commands/vacuum.h>
1416#include < nodes/nodeFuncs.h>
1517#include < optimizer/planner.h>
@@ -586,6 +588,35 @@ static void process_utility(PlannedStmt* pstmt,
586588 }
587589 }
588590 }
591+
592+ // Mark schema as "dropping" in the S3 catalog
593+ if (pg::stateless_enabled) {
594+ try {
595+ auto root_path = pg::session_credentials::get_root_path ();
596+ if (root_path.empty ()) {
597+ root_path = pg::utils::get_deeplake_root_directory ();
598+ }
599+ if (!root_path.empty ()) {
600+ auto creds = pg::session_credentials::get_credentials ();
601+ const char * dbname = get_database_name (MyDatabaseId);
602+ std::string db_name = dbname ? dbname : " postgres" ;
603+ if (dbname) pfree (const_cast <char *>(dbname));
604+
605+ pg::dl_catalog::ensure_catalog (root_path, creds);
606+ pg::dl_catalog::ensure_db_catalog (root_path, db_name, creds);
607+
608+ pg::dl_catalog::schema_meta s_meta;
609+ s_meta.schema_name = schema_name;
610+ s_meta.state = " dropping" ;
611+ pg::dl_catalog::upsert_schema (root_path, db_name, creds, s_meta);
612+
613+ pg::dl_catalog::bump_db_catalog_version (root_path, db_name, pg::session_credentials::get_credentials ());
614+ pg::dl_catalog::bump_catalog_version (root_path, pg::session_credentials::get_credentials ());
615+ }
616+ } catch (const std::exception& e) {
617+ elog (WARNING, " pg_deeplake: failed to mark schema '%s' as dropping in catalog: %s" , schema_name, e.what ());
618+ }
619+ }
589620 }
590621 } else if (stmt->removeType == OBJECT_DATABASE) {
591622 const char * query = " SELECT nspname, relname "
@@ -691,6 +722,7 @@ static void process_utility(PlannedStmt* pstmt,
691722 }
692723 if (!root_path.empty ()) {
693724 auto creds = pg::session_credentials::get_credentials ();
725+ pg::dl_catalog::ensure_catalog (root_path, creds);
694726 pg::dl_catalog::database_meta db_meta;
695727 db_meta.db_name = dbstmt->dbname ;
696728 db_meta.state = " dropping" ;
@@ -727,6 +759,7 @@ static void process_utility(PlannedStmt* pstmt,
727759 }
728760 if (!root_path.empty ()) {
729761 auto creds = pg::session_credentials::get_credentials ();
762+ pg::dl_catalog::ensure_catalog (root_path, creds);
730763 pg::dl_catalog::database_meta db_meta;
731764 db_meta.db_name = dbstmt->dbname ;
732765 db_meta.state = " ready" ;
@@ -758,6 +791,40 @@ static void process_utility(PlannedStmt* pstmt,
758791 }
759792 }
760793
794+ // Post-hook: record CREATE SCHEMA in S3 catalog for multi-instance sync
795+ if (IsA (pstmt->utilityStmt , CreateSchemaStmt) && pg::stateless_enabled) {
796+ CreateSchemaStmt* schemastmt = (CreateSchemaStmt*)pstmt->utilityStmt ;
797+ try {
798+ auto root_path = pg::session_credentials::get_root_path ();
799+ if (root_path.empty ()) {
800+ root_path = pg::utils::get_deeplake_root_directory ();
801+ }
802+ if (!root_path.empty () && schemastmt->schemaname != nullptr ) {
803+ auto creds = pg::session_credentials::get_credentials ();
804+ const char * dbname = get_database_name (MyDatabaseId);
805+ std::string db_name = dbname ? dbname : " postgres" ;
806+ if (dbname) pfree (const_cast <char *>(dbname));
807+
808+ pg::dl_catalog::ensure_catalog (root_path, creds);
809+ pg::dl_catalog::ensure_db_catalog (root_path, db_name, creds);
810+
811+ pg::dl_catalog::schema_meta s_meta;
812+ s_meta.schema_name = schemastmt->schemaname ;
813+ s_meta.state = " ready" ;
814+ if (schemastmt->authrole != nullptr ) {
815+ s_meta.owner = schemastmt->authrole ->rolename ;
816+ }
817+ pg::dl_catalog::upsert_schema (root_path, db_name, creds, s_meta);
818+
819+ pg::dl_catalog::bump_db_catalog_version (root_path, db_name, pg::session_credentials::get_credentials ());
820+ pg::dl_catalog::bump_catalog_version (root_path, pg::session_credentials::get_credentials ());
821+ elog (DEBUG1, " pg_deeplake: recorded CREATE SCHEMA '%s' in catalog" , schemastmt->schemaname );
822+ }
823+ } catch (const std::exception& e) {
824+ elog (DEBUG1, " pg_deeplake: failed to record CREATE SCHEMA in catalog: %s" , e.what ());
825+ }
826+ }
827+
761828 // Post-process ALTER TABLE ADD COLUMN to add column to deeplake dataset
762829 if (IsA (pstmt->utilityStmt , AlterTableStmt)) {
763830 AlterTableStmt* stmt = (AlterTableStmt*)pstmt->utilityStmt ;
0 commit comments