-
Notifications
You must be signed in to change notification settings - Fork 710
Fixed parallel ingestion. #3135
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
Changes from all commits
Commits
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
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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,7 @@ extern "C" { | |||||||
| #include <access/heapam.h> | ||||||||
| #include <access/htup_details.h> | ||||||||
| #include <access/parallel.h> | ||||||||
| #include <access/xact.h> | ||||||||
| #include <catalog/namespace.h> | ||||||||
| #include <catalog/pg_type.h> | ||||||||
| #include <executor/spi.h> | ||||||||
|
|
@@ -344,28 +345,15 @@ void table_storage::load_table_metadata() | |||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| // Not in DDL context (e.g., SET root_path) - safe to auto-create. | ||||||||
| // Use catalog_only_guard to skip S3 dataset operations in create_table() — | ||||||||
| // the dataset already exists on S3, we just need the pg_class entry. | ||||||||
| catalog_only_guard co_guard; | ||||||||
| pg::utils::memory_context_switcher context_switcher; | ||||||||
| pg::utils::spi_connector connector; | ||||||||
| bool pushed_snapshot = false; | ||||||||
| if (!ActiveSnapshotSet()) { | ||||||||
| PushActiveSnapshot(GetTransactionSnapshot()); | ||||||||
| pushed_snapshot = true; | ||||||||
| } | ||||||||
| // Build CREATE TABLE IF NOT EXISTS from catalog metadata. | ||||||||
| // Wrap in a subtransaction so that if another backend concurrently | ||||||||
| // creates the same table (race on composite type), the error is | ||||||||
| // caught and we continue instead of aborting the session. | ||||||||
| const char* qschema = quote_identifier(meta.schema_name.c_str()); | ||||||||
| const char* qtable = quote_identifier(meta.table_name.c_str()); | ||||||||
|
|
||||||||
| StringInfoData buf; | ||||||||
| initStringInfo(&buf); | ||||||||
| appendStringInfo(&buf, "CREATE SCHEMA IF NOT EXISTS %s", qschema); | ||||||||
| SPI_execute(buf.data, false, 0); | ||||||||
|
|
||||||||
| // Build CREATE TABLE statement directly from catalog metadata | ||||||||
| // This avoids calling the SQL function create_deeplake_table which may not exist | ||||||||
| resetStringInfo(&buf); | ||||||||
| const char* qtable = quote_identifier(meta.table_name.c_str()); | ||||||||
| appendStringInfo(&buf, "CREATE TABLE IF NOT EXISTS %s.%s (", qschema, qtable); | ||||||||
|
|
||||||||
| bool first = true; | ||||||||
|
|
@@ -376,19 +364,49 @@ void table_storage::load_table_metadata() | |||||||
| first = false; | ||||||||
| appendStringInfo(&buf, "%s %s", quote_identifier(col.column_name.c_str()), col.pg_type.c_str()); | ||||||||
| } | ||||||||
|
|
||||||||
| // Table path is now derived from deeplake.root_path GUC set at database level | ||||||||
| // Path: {root_path}/{schema}/{table_name} | ||||||||
| appendStringInfo(&buf, ") USING deeplake"); | ||||||||
|
|
||||||||
| if (SPI_execute(buf.data, false, 0) != SPI_OK_UTILITY) { | ||||||||
| elog(WARNING, "Failed to auto-create deeplake table %s from catalog", qualified_name.c_str()); | ||||||||
| } | ||||||||
| pfree(buf.data); | ||||||||
| MemoryContext saved_context = CurrentMemoryContext; | ||||||||
| ResourceOwner saved_owner = CurrentResourceOwner; | ||||||||
|
|
||||||||
| BeginInternalSubTransaction(NULL); | ||||||||
| PG_TRY(); | ||||||||
| { | ||||||||
| catalog_only_guard co_guard; | ||||||||
| pg::utils::spi_connector connector; | ||||||||
| bool pushed_snapshot = false; | ||||||||
| if (!ActiveSnapshotSet()) { | ||||||||
| PushActiveSnapshot(GetTransactionSnapshot()); | ||||||||
| pushed_snapshot = true; | ||||||||
| } | ||||||||
|
|
||||||||
| if (pushed_snapshot) { | ||||||||
| PopActiveSnapshot(); | ||||||||
| // Create schema if needed | ||||||||
| StringInfoData schema_buf; | ||||||||
| initStringInfo(&schema_buf); | ||||||||
| appendStringInfo(&schema_buf, "CREATE SCHEMA IF NOT EXISTS %s", qschema); | ||||||||
| SPI_execute(schema_buf.data, false, 0); | ||||||||
| pfree(schema_buf.data); | ||||||||
|
|
||||||||
| SPI_execute(buf.data, false, 0); | ||||||||
|
|
||||||||
| if (pushed_snapshot) { | ||||||||
| PopActiveSnapshot(); | ||||||||
| } | ||||||||
|
|
||||||||
| ReleaseCurrentSubTransaction(); | ||||||||
|
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. Missing
Suggested change
|
||||||||
| } | ||||||||
| PG_CATCH(); | ||||||||
| { | ||||||||
| // Another backend created this table concurrently — not an error. | ||||||||
| MemoryContextSwitchTo(saved_context); | ||||||||
| CurrentResourceOwner = saved_owner; | ||||||||
| RollbackAndReleaseCurrentSubTransaction(); | ||||||||
| FlushErrorState(); | ||||||||
| elog(DEBUG1, "Concurrent table creation for %s, skipping", qualified_name.c_str()); | ||||||||
| } | ||||||||
| PG_END_TRY(); | ||||||||
|
|
||||||||
| pfree(buf.data); | ||||||||
|
|
||||||||
| relid = RangeVarGetRelid(rel, NoLock, true); | ||||||||
| } | ||||||||
|
|
||||||||
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.
Missing
MemoryContextSwitchTo(saved_context)beforeReleaseCurrentSubTransaction(). PostgreSQL subtransaction cleanup requires restoring the memory context before releasing, or you risk context corruption.