Skip to content

Commit

Permalink
Fail startup if clientauth BGWs fail to register
Browse files Browse the repository at this point in the history
  • Loading branch information
adamguo0 committed Dec 18, 2023
1 parent 6443eb4 commit 4fb243c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/clientauth.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "executor/spi.h"
#include "libpq/auth.h"
#include "nodes/pg_list.h"
#include "postmaster/bgworker_internals.h"
#include "utils/builtins.h"
#include "utils/elog.h"
#include "utils/errcodes.h"
Expand Down Expand Up @@ -239,6 +240,8 @@ void
clientauth_init(void)
{
BackgroundWorker worker;
slist_iter siter;
int num_registered_workers = 0;

/* Define our GUC parameters */
DefineCustomEnumVariable(
Expand Down Expand Up @@ -335,6 +338,24 @@ clientauth_init(void)
worker.bgw_main_arg = Int32GetDatum(i);
RegisterBackgroundWorker(&worker);
}

slist_foreach(siter, &BackgroundWorkerList)
{
RegisteredBgWorker *rw;

rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
if (strncmp(rw->rw_worker.bgw_type, "pg_tle_clientauth worker", BGW_MAXLEN) == 0)
{
num_registered_workers++;
}
}

if (num_registered_workers < clientauth_num_parallel_workers)
{
ereport(ERROR,
errmsg("\"%s.clientauth\" feature was not able to create background workers", PG_TLE_NSPNAME),
errhint("Consider increasing max_worker_processes or reducing other background workers."));
}
}

void
Expand Down
9 changes: 9 additions & 0 deletions test/t/004_pg_tle_clientauth.pl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
### 11. Users cannot log in when pgtle.enable_clientauth = 'require' and no functions are registered to clientauth
### 12. Users cannot log in when pgtle.enable_clientauth = 'require' and pg_tle is not installed on pgtle.clientauth_database_name
### 13. Rejects connections when no schema qualified function is found
### 14. Database does not come up if clientauth workers fail to start

use strict;
use warnings;
Expand Down Expand Up @@ -231,5 +232,13 @@
['psql', '-c', 'select;'],
"can still connect if database is in pgtle.clientauth_databases_to_skip");

### 14. Database does not come up if clientauth workers fail to start
$node->append_conf('postgresql.conf', qq(pgtle.clientauth_num_parallel_workers = 64));
$node->append_conf('postgresql.conf', qq(max_worker_processes = 63));
like($node->restart(fail_ok => 1), qr/^0$/); # $node->restart returns 0 on failure
$node->append_conf('postgresql.conf', qq(pgtle.clientauth_num_parallel_workers = 60));
$node->append_conf('postgresql.conf', qq(max_worker_processes = 63));
$node->restart;

$node->stop;
done_testing();

0 comments on commit 4fb243c

Please sign in to comment.