-
Notifications
You must be signed in to change notification settings - Fork 704
Make db creation stateless. #3137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 4.5.1 | ||
| 4.5.2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,6 +25,7 @@ extern "C" { | |||||||||
|
|
||||||||||
| #include "column_statistics.hpp" | ||||||||||
| #include "deeplake_executor.hpp" | ||||||||||
| #include "dl_catalog.hpp" | ||||||||||
| #include "pg_deeplake.hpp" | ||||||||||
| #include "pg_version_compat.h" | ||||||||||
| #include "sync_worker.hpp" | ||||||||||
|
|
@@ -473,6 +474,10 @@ static void deeplake_shmem_request() | |||||||||
| // Request shared memory for table DDL lock | ||||||||||
| RequestAddinShmemSpace(pg::table_ddl_lock::get_shmem_size()); | ||||||||||
| RequestNamedLWLockTranche("deeplake_table_ddl", 1); | ||||||||||
|
|
||||||||||
| // Request shared memory for pending extension install queue | ||||||||||
| RequestAddinShmemSpace(pg::pending_install_queue::get_shmem_size()); | ||||||||||
| RequestNamedLWLockTranche("deeplake_install_queue", 1); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| static void deeplake_shmem_startup() | ||||||||||
|
|
@@ -483,6 +488,7 @@ static void deeplake_shmem_startup() | |||||||||
|
|
||||||||||
| pg::table_version_tracker::initialize(); | ||||||||||
| pg::table_ddl_lock::initialize(); | ||||||||||
| pg::pending_install_queue::initialize(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| static void process_utility(PlannedStmt* pstmt, | ||||||||||
|
|
@@ -675,12 +681,83 @@ static void process_utility(PlannedStmt* pstmt, | |||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Pre-hook: mark database as "dropping" in S3 catalog before PostgreSQL drops it | ||||||||||
| if (IsA(pstmt->utilityStmt, DropdbStmt) && pg::stateless_enabled) { | ||||||||||
| DropdbStmt* dbstmt = (DropdbStmt*)pstmt->utilityStmt; | ||||||||||
| try { | ||||||||||
| auto root_path = pg::session_credentials::get_root_path(); | ||||||||||
| if (root_path.empty()) { | ||||||||||
| root_path = pg::utils::get_deeplake_root_directory(); | ||||||||||
| } | ||||||||||
| if (!root_path.empty()) { | ||||||||||
| auto creds = pg::session_credentials::get_credentials(); | ||||||||||
| pg::dl_catalog::database_meta db_meta; | ||||||||||
| db_meta.db_name = dbstmt->dbname; | ||||||||||
| db_meta.state = "dropping"; | ||||||||||
| pg::dl_catalog::upsert_database(root_path, creds, db_meta); | ||||||||||
| pg::dl_catalog::bump_catalog_version(root_path, creds); | ||||||||||
| elog(LOG, "pg_deeplake: marked database '%s' as dropping in catalog", dbstmt->dbname); | ||||||||||
| } | ||||||||||
| } catch (const std::exception& e) { | ||||||||||
| elog(WARNING, "pg_deeplake: failed to mark database '%s' as dropping in catalog: %s", dbstmt->dbname, e.what()); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (prev_process_utility_hook != nullptr) { | ||||||||||
| prev_process_utility_hook(pstmt, queryString, readOnlyTree, context, params, queryEnv, dest, completionTag); | ||||||||||
| } else { | ||||||||||
| standard_ProcessUtility(pstmt, queryString, readOnlyTree, context, params, queryEnv, dest, completionTag); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Post-hook: record CREATE DATABASE in S3 catalog and install extension | ||||||||||
| if (IsA(pstmt->utilityStmt, CreatedbStmt)) { | ||||||||||
| CreatedbStmt* dbstmt = (CreatedbStmt*)pstmt->utilityStmt; | ||||||||||
|
|
||||||||||
| // Queue the database for async extension install by the sync worker. | ||||||||||
| // The inline PQconnectdb approach fails on PG15+ because CREATE DATABASE | ||||||||||
| // is WAL-logged/transactional and the pg_database row isn't committed yet. | ||||||||||
| pg::pending_install_queue::enqueue(dbstmt->dbname); | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ignoring return value from enqueue. If queue is full, extension install will be silently skipped with no warning to user.
Suggested change
|
||||||||||
|
|
||||||||||
| // Record in S3 catalog if stateless mode is enabled | ||||||||||
| if (pg::stateless_enabled) { | ||||||||||
| try { | ||||||||||
| auto root_path = pg::session_credentials::get_root_path(); | ||||||||||
| if (root_path.empty()) { | ||||||||||
| root_path = pg::utils::get_deeplake_root_directory(); | ||||||||||
| } | ||||||||||
| if (!root_path.empty()) { | ||||||||||
| auto creds = pg::session_credentials::get_credentials(); | ||||||||||
| pg::dl_catalog::database_meta db_meta; | ||||||||||
| db_meta.db_name = dbstmt->dbname; | ||||||||||
| db_meta.state = "ready"; | ||||||||||
|
|
||||||||||
| // Extract options from CREATE DATABASE statement | ||||||||||
| ListCell* lc = nullptr; | ||||||||||
| foreach (lc, dbstmt->options) { | ||||||||||
| DefElem* def = (DefElem*)lfirst(lc); | ||||||||||
| if (strcmp(def->defname, "owner") == 0) { | ||||||||||
| db_meta.owner = defGetString(def); | ||||||||||
| } else if (strcmp(def->defname, "encoding") == 0) { | ||||||||||
| db_meta.encoding = defGetString(def); | ||||||||||
| } else if (strcmp(def->defname, "lc_collate") == 0) { | ||||||||||
| db_meta.lc_collate = defGetString(def); | ||||||||||
| } else if (strcmp(def->defname, "lc_ctype") == 0) { | ||||||||||
| db_meta.lc_ctype = defGetString(def); | ||||||||||
| } else if (strcmp(def->defname, "template") == 0) { | ||||||||||
| db_meta.template_db = defGetString(def); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| pg::dl_catalog::upsert_database(root_path, creds, db_meta); | ||||||||||
| pg::dl_catalog::bump_catalog_version(root_path, creds); | ||||||||||
| elog(DEBUG1, "pg_deeplake: recorded CREATE DATABASE '%s' in catalog", dbstmt->dbname); | ||||||||||
| } | ||||||||||
| } catch (const std::exception& e) { | ||||||||||
| elog(DEBUG1, "pg_deeplake: failed to record CREATE DATABASE '%s' in catalog: %s", dbstmt->dbname, e.what()); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Post-process ALTER TABLE ADD COLUMN to add column to deeplake dataset | ||||||||||
| if (IsA(pstmt->utilityStmt, AlterTableStmt)) { | ||||||||||
| AlterTableStmt* stmt = (AlterTableStmt*)pstmt->utilityStmt; | ||||||||||
|
|
||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Race condition:
updated_at <= meta.updated_atallows ties. Two concurrent upserts with same timestamp result in non-deterministic latest selection (map insertion order dependent).