Skip to content

Commit

Permalink
Allow writes to columnstore and Postgres tables in the same transaction
Browse files Browse the repository at this point in the history
Remove the lockdowns introduced in duckdb/pg_duckdb#433 since the same restriction doesn't apply to us. In our case, Postgres remains the source of truth for columnstore tables, and DuckDB serves purely as the execution engine for reading and writing to those tables.
  • Loading branch information
dpxcc committed Dec 17, 2024
1 parent 94030bb commit c94c78e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
20 changes: 10 additions & 10 deletions src/pgduckdb/pgduckdb_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ IsAllowedStatement(Query *query, bool throw_error = false) {
elog(elevel, "DuckDB does not support modififying Postgres tables");
return false;
}
if (pgduckdb::pg::IsInTransactionBlock(true)) {
if (pgduckdb::pg::DidWalWrites()) {
elog(elevel, "Writing to DuckDB and Postgres tables in the same transaction block is not supported");
return false;
}
}
// if (pgduckdb::pg::IsInTransactionBlock(true)) {
// if (pgduckdb::pg::DidWalWrites()) {
// elog(elevel, "Writing to DuckDB and Postgres tables in the same transaction block is not supported");
// return false;
// }
// }
}

/*
Expand Down Expand Up @@ -242,10 +242,10 @@ DuckdbPlannerHook_Cpp(Query *parse, const char *query_string, int cursor_options
}
/* If we can't create a plan, we'll fall back to Postgres */
}
if (parse->commandType != CMD_SELECT && pgduckdb::ddb::DidWrites() &&
pgduckdb::pg::IsInTransactionBlock(true)) {
elog(ERROR, "Writing to DuckDB and Postgres tables in the same transaction block is not supported");
}
// if (parse->commandType != CMD_SELECT && pgduckdb::ddb::DidWrites() &&
// pgduckdb::pg::IsInTransactionBlock(true)) {
// elog(ERROR, "Writing to DuckDB and Postgres tables in the same transaction block is not supported");
// }
}

/*
Expand Down
22 changes: 11 additions & 11 deletions src/pgduckdb/pgduckdb_xact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ PreventInTransactionBlock(const char *statement_type) {
* IMPORTANT: This function should only be called at trasaction commit. At
* other points in the transaction lifecycle its return value is not reliable.
*/
static bool
[[maybe_unused]] static bool
DidWritesAtTransactionEnd() {
return pg::DidWalWrites() || pg::GetCurrentCommandId() > duckdb_command_id + 1;
}
Expand Down Expand Up @@ -87,10 +87,10 @@ ClaimCurrentCommandId() {
return;
}

if (new_command_id != duckdb_command_id + 1) {
throw duckdb::NotImplementedException(
"Writing to DuckDB and Postgres tables in the same transaction block is not supported");
}
// if (new_command_id != duckdb_command_id + 1) {
// throw duckdb::NotImplementedException(
// "Writing to DuckDB and Postgres tables in the same transaction block is not supported");
// }

duckdb_command_id = new_command_id;
}
Expand Down Expand Up @@ -157,12 +157,12 @@ DuckdbXactCallback_Cpp(XactEvent event) {
switch (event) {
case XACT_EVENT_PRE_COMMIT:
case XACT_EVENT_PARALLEL_PRE_COMMIT:
if (pg::IsInTransactionBlock(top_level_statement)) {
if (pg::DidWritesAtTransactionEnd() && ddb::DidWrites(context)) {
throw duckdb::NotImplementedException(
"Writing to DuckDB and Postgres tables in the same transaction block is not supported");
}
}
// if (pg::IsInTransactionBlock(top_level_statement)) {
// if (pg::DidWritesAtTransactionEnd() && ddb::DidWrites(context)) {
// throw duckdb::NotImplementedException(
// "Writing to DuckDB and Postgres tables in the same transaction block is not supported");
// }
// }
top_level_statement = true;
duckdb_command_id = -1;
// Commit the DuckDB transaction too
Expand Down
19 changes: 19 additions & 0 deletions test/expected/transaction.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE TABLE s (a int);
CREATE TABLE t (b int) USING columnstore;
BEGIN;
INSERT INTO s VALUES (1);
INSERT INTO t VALUES (2);
COMMIT;
SELECT * FROM s;
a
---
1
(1 row)

SELECT * FROM t;
b
---
2
(1 row)

DROP TABLE s, t;
12 changes: 12 additions & 0 deletions test/sql/transaction.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE s (a int);
CREATE TABLE t (b int) USING columnstore;

BEGIN;
INSERT INTO s VALUES (1);
INSERT INTO t VALUES (2);
COMMIT;

SELECT * FROM s;
SELECT * FROM t;

DROP TABLE s, t;

0 comments on commit c94c78e

Please sign in to comment.