Skip to content
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

Quit postgres if we cannot open connection to motherduck #622

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/pgduckdb_background_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static BackgoundWorkerShmemStruct *BgwShmemStruct;

extern "C" {
PGDLLEXPORT void
pgduckdb_background_worker_main(Datum /* main_arg */) {
pgduckdb_background_worker_main(Datum arg) {
elog(LOG, "started pg_duckdb background worker");
// Set up a signal handler for SIGTERM
pqsignal(SIGTERM, die);
Expand All @@ -94,7 +94,10 @@ pgduckdb_background_worker_main(Datum /* main_arg */) {

duckdb::unique_ptr<duckdb::Connection> connection;

while (true) {
int backend_pid = (int)arg;
bool motherduck_connection_error = false;

while (!motherduck_connection_error) {
// Initialize SPI (Server Programming Interface) and connect to the database
SetCurrentStatementStartTimestamp();
StartTransactionCommand();
Expand All @@ -103,7 +106,15 @@ pgduckdb_background_worker_main(Datum /* main_arg */) {

if (pgduckdb::IsExtensionRegistered()) {
if (!connection) {
connection = pgduckdb::DuckDBManager::Get().CreateConnection();
try {
connection = pgduckdb::DuckDBManager::Get().CreateConnection();
} catch (...) {
// cosmetic, error thrown will not end with new line delimiter
fprintf(stdout, "\n");
elog(WARNING, "Error making connection to motherduck service. Re-check parameters.");
motherduck_connection_error = true;
continue;
}
}
SpinLockAcquire(&pgduckdb::BgwShmemStruct->lock);
int64 new_activity_count = pgduckdb::BgwShmemStruct->activity_count;
Expand Down Expand Up @@ -134,7 +145,10 @@ pgduckdb_background_worker_main(Datum /* main_arg */) {
ResetLatch(MyLatch);
}

// Unreachable
// We have motherduck connecton error so shut down so we don't retry
if (motherduck_connection_error) {
kill(backend_pid, SIGQUIT);
}
}

PG_FUNCTION_INFO_V1(force_motherduck_sync);
Expand Down Expand Up @@ -235,7 +249,7 @@ InitBackgroundWorker(void) {
snprintf(worker.bgw_function_name, BGW_MAXLEN, "pgduckdb_background_worker_main");
snprintf(worker.bgw_name, BGW_MAXLEN, "pg_duckdb sync worker");
worker.bgw_restart_time = 1;
worker.bgw_main_arg = (Datum)0;
worker.bgw_main_arg = (Datum)MyProcPid;

// Register the worker
RegisterBackgroundWorker(&worker);
Expand Down