pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feedREPACK (CONCURRENTLY) fails when table owner lacks CONNECT
12+ messages / 5 participants
[nested] [flat]
Commits from this thread:
2026-09-08 0717ed2623 9bf3a41456 Fix REPACK (CONCURRENTLY) when the table owner lacks CONNECT.
* REPACK (CONCURRENTLY) fails when table owner lacks CONNECT
@ 2026-08-27 15:46 Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 18:16 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
0 siblings, 1 reply; 12+ messages in thread
From: Nathan Bossart @ 2026-08-27 15:46 UTC (permalink / raw)
To: pgsql-hackers; +Cc: alvherre@kurilemu.de
The REPACK decoding worker doesn't pass BGWORKER_BYPASS_ALLOWCONN like
parallel.c does, so it can fail to start. Here is a reproducer:
CREATE DATABASE d;
REVOKE CONNECT ON DATABASE d FROM PUBLIC;
\c d
CREATE ROLE r NOLOGIN;
CREATE TABLE t (a INT PRIMARY KEY);
ALTER TABLE t OWNER TO r;
REPACK t; -- works
REPACK (CONCURRENTLY) t; -- fails
That last command produces the following output:
ERROR: permission denied for database "d"
DETAIL: User does not have CONNECT privilege.
CONTEXT: REPACK decoding worker
This one seems easy enough to fix.
--
nathan
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT
2026-08-27 15:46 REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
@ 2026-08-27 18:16 ` Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 18:33 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
0 siblings, 1 reply; 12+ messages in thread
From: Matheus Alcantara @ 2026-08-27 18:16 UTC (permalink / raw)
To: Nathan Bossart <nathandbossart@gmail.com>; pgsql-hackers; +Cc: alvherre@kurilemu.de
On 27/08/26 12:46, Nathan Bossart wrote:
> The REPACK decoding worker doesn't pass BGWORKER_BYPASS_ALLOWCONN like
> parallel.c does, so it can fail to start. Here is a reproducer:
>
> CREATE DATABASE d;
> REVOKE CONNECT ON DATABASE d FROM PUBLIC;
> \c d
> CREATE ROLE r NOLOGIN;
> CREATE TABLE t (a INT PRIMARY KEY);
> ALTER TABLE t OWNER TO r;
> REPACK t; -- works
> REPACK (CONCURRENTLY) t; -- fails
>
> That last command produces the following output:
>
> ERROR: permission denied for database "d"
> DETAIL: User does not have CONNECT privilege.
> CONTEXT: REPACK decoding worker
>
> This one seems easy enough to fix.
>
I'm attaching a patch to use BGWORKER_BYPASS_ALLOWCONN on
BackgroundWorkerInitializeConnectionByOid as parallel.c.
I also added a test case for it.
--
Matheus Alcantara
EDB: https://www.enterprisedb.com
Attachments:
0001-Let-the-REPACK-decoding-worker-bypass-connection-pri.patch (text/plain, 4.4K) download
From 6df6235fc91eee8be5bac876716539bbd57ad7b5 Mon Sep 17 00:00:00 2001
From: Matheus Alcantara <mths.dev@pm.me>
Date: Thu, 27 Aug 2026 15:07:39 -0300
Subject: [PATCH] Let the REPACK decoding worker bypass connection privilege
checks
REPACK (CONCURRENTLY) launches a background worker to decode the changes
made while the table is being rewritten. Since cluster_rel() switches
the current user to the owner of the table being repacked, that is the
role the worker connects as, and the worker asked for a bypass of the
LOGIN check only. The CONNECT privilege on the database was therefore
still checked against the table owner, who has no reason to have it, so
the command could fail with
ERROR: permission denied for database "d"
DETAIL: User does not have CONNECT privilege.
CONTEXT: REPACK decoding worker
Pass BGWORKER_BYPASS_ALLOWCONN as well, as parallel.c already does for
parallel workers: the backend that launched the worker is connected to
the database already, and the worker only decodes changes on its behalf.
Reported-by: Nathan Bossart <nathandbossart@gmail.com>
Author: Matheus Alcantara <mths.dev@pm.me>
Discussion: https://www.postgresql.org/message-id/apBbzFd_EYAfHV45@nathan
---
contrib/test_decoding/expected/repack.out | 11 +++++++++++
contrib/test_decoding/sql/repack.sql | 12 ++++++++++++
src/backend/commands/repack_worker.c | 9 ++++++++-
3 files changed, 31 insertions(+), 1 deletion(-)
diff --git a/contrib/test_decoding/expected/repack.out b/contrib/test_decoding/expected/repack.out
index 5ddc63238c5..4fa945b178f 100644
--- a/contrib/test_decoding/expected/repack.out
+++ b/contrib/test_decoding/expected/repack.out
@@ -29,6 +29,17 @@ SELECT a.relname, a.relfilenode=b.relfilenode FROM pg_class a
DROP TABLE ptnowner;
DROP ROLE regress_ptnowner;
+-- The decoding worker runs as the owner of the table being repacked, but it
+-- should not need the CONNECT privilege on the database.
+CREATE ROLE regress_repack_noconn NOLOGIN;
+CREATE TABLE repack_noconn (i int PRIMARY KEY);
+ALTER TABLE repack_noconn OWNER TO regress_repack_noconn;
+SELECT current_database() AS datname \gset
+REVOKE CONNECT ON DATABASE :"datname" FROM PUBLIC;
+REPACK (CONCURRENTLY) repack_noconn;
+GRANT CONNECT ON DATABASE :"datname" TO PUBLIC;
+DROP TABLE repack_noconn;
+DROP ROLE regress_repack_noconn;
-- Verify that REPACK (CONCURRENTLY) doesn't lose "attmissingval" columns
CREATE TABLE rpk_missing (id int PRIMARY KEY);
INSERT INTO rpk_missing SELECT generate_series(1, 3);
diff --git a/contrib/test_decoding/sql/repack.sql b/contrib/test_decoding/sql/repack.sql
index f461f5479f4..c5245982bb1 100644
--- a/contrib/test_decoding/sql/repack.sql
+++ b/contrib/test_decoding/sql/repack.sql
@@ -24,6 +24,18 @@ SELECT a.relname, a.relfilenode=b.relfilenode FROM pg_class a
DROP TABLE ptnowner;
DROP ROLE regress_ptnowner;
+-- The decoding worker runs as the owner of the table being repacked, but it
+-- should not need the CONNECT privilege on the database.
+CREATE ROLE regress_repack_noconn NOLOGIN;
+CREATE TABLE repack_noconn (i int PRIMARY KEY);
+ALTER TABLE repack_noconn OWNER TO regress_repack_noconn;
+SELECT current_database() AS datname \gset
+REVOKE CONNECT ON DATABASE :"datname" FROM PUBLIC;
+REPACK (CONCURRENTLY) repack_noconn;
+GRANT CONNECT ON DATABASE :"datname" TO PUBLIC;
+DROP TABLE repack_noconn;
+DROP ROLE regress_repack_noconn;
+
-- Verify that REPACK (CONCURRENTLY) doesn't lose "attmissingval" columns
CREATE TABLE rpk_missing (id int PRIMARY KEY);
INSERT INTO rpk_missing SELECT generate_series(1, 3);
diff --git a/src/backend/commands/repack_worker.c b/src/backend/commands/repack_worker.c
index af7e2a94764..a49b681059f 100644
--- a/src/backend/commands/repack_worker.c
+++ b/src/backend/commands/repack_worker.c
@@ -101,8 +101,15 @@ RepackWorkerMain(Datum main_arg)
pq_set_parallel_leader(shared->backend_pid,
shared->backend_proc_number);
- /* Connect to the database. LOGIN is not required. */
+ /*
+ * Connect to the database. Like parallel workers do, we skip the
+ * connection authorization checks. The backend that launched us is
+ * already connected to this database, and the role we run as is the owner
+ * of the table being repacked, which needs neither the LOGIN attribute
+ * nor the CONNECT privilege for our purposes.
+ */
BackgroundWorkerInitializeConnectionByOid(shared->dbid, shared->roleid,
+ BGWORKER_BYPASS_ALLOWCONN |
BGWORKER_BYPASS_ROLELOGINCHECK);
/*
--
2.50.1 (Apple Git-155)
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT
2026-08-27 15:46 REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 18:16 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
@ 2026-08-27 18:33 ` Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-08-27 18:55 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
0 siblings, 1 reply; 12+ messages in thread
From: Bharath Rupireddy @ 2026-08-27 18:33 UTC (permalink / raw)
To: Matheus Alcantara <matheusssilv97@gmail.com>; +Cc: Nathan Bossart <nathandbossart@gmail.com>; pgsql-hackers; alvherre@kurilemu.de
Hi,
On Thu, Aug 27, 2026 at 11:16 AM Matheus Alcantara
<matheusssilv97@gmail.com> wrote:
>
> On 27/08/26 12:46, Nathan Bossart wrote:
> > The REPACK decoding worker doesn't pass BGWORKER_BYPASS_ALLOWCONN like
> > parallel.c does, so it can fail to start. Here is a reproducer:
> >
> > CREATE DATABASE d;
> > REVOKE CONNECT ON DATABASE d FROM PUBLIC;
> > \c d
> > CREATE ROLE r NOLOGIN;
> > CREATE TABLE t (a INT PRIMARY KEY);
> > ALTER TABLE t OWNER TO r;
> > REPACK t; -- works
> > REPACK (CONCURRENTLY) t; -- fails
> >
> > That last command produces the following output:
> >
> > ERROR: permission denied for database "d"
> > DETAIL: User does not have CONNECT privilege.
> > CONTEXT: REPACK decoding worker
> >
> > This one seems easy enough to fix.
> >
> I'm attaching a patch to use BGWORKER_BYPASS_ALLOWCONN on
> BackgroundWorkerInitializeConnectionByOid as parallel.c.
+1. If a user is able to connect and run REPACK, there's no reason to
block REPACK (CONCURRENTLY).
One general concern I have: is it okay for a role that has neither the
login privilege nor CONNECT on the database to perform logical
decoding for concurrent repack? It may well be okay, because the
repack worker already bypasses the login check for the role it uses to
connect to the database.
> I also added a test case for it.
I quickly checked and there's no test case for bypassing the login
check. Can we add one while here?
I think we need to backpatch this fix to PG19 to get consistent behavior.
--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT
2026-08-27 15:46 REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 18:16 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 18:33 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
@ 2026-08-27 18:55 ` Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 19:19 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
0 siblings, 1 reply; 12+ messages in thread
From: Matheus Alcantara @ 2026-08-27 18:55 UTC (permalink / raw)
To: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>; +Cc: Nathan Bossart <nathandbossart@gmail.com>; pgsql-hackers; alvherre@kurilemu.de
On Thu Aug 27, 2026 at 3:33 PM -03, Bharath Rupireddy wrote:
> On Thu, Aug 27, 2026 at 11:16 AM Matheus Alcantara
>> I'm attaching a patch to use BGWORKER_BYPASS_ALLOWCONN on
>> BackgroundWorkerInitializeConnectionByOid as parallel.c.
>
> +1. If a user is able to connect and run REPACK, there's no reason to
> block REPACK (CONCURRENTLY).
>
> One general concern I have: is it okay for a role that has neither the
> login privilege nor CONNECT on the database to perform logical
> decoding for concurrent repack? It may well be okay, because the
> repack worker already bypasses the login check for the role it uses to
> connect to the database.
>
I think so. IIUC the privilege decision is not made in the worker. The
leader checks ACL_MAINTAIN against the invoking user in
repack_is_permitted_for_relation(), and only then starts the worker. The
role the worker connects as is not an authorization decision, it's just
the identity the work runs under.
>> I also added a test case for it.
>
> I quickly checked and there's no test case for bypassing the login
> check. Can we add one while here?
>
Added this new test case on attached.
--
Matheus Alcantara
EDB: https://www.enterprisedb.com
Attachments:
v2-0001-Let-the-REPACK-decoding-worker-bypass-connection-.patch (text/plain, 5.2K) download
From 4d1cd5c49f0c4cfce919995413bea056f6e50117 Mon Sep 17 00:00:00 2001
From: Matheus Alcantara <mths.dev@pm.me>
Date: Thu, 27 Aug 2026 15:07:39 -0300
Subject: [PATCH v2] Let the REPACK decoding worker bypass connection privilege
checks
REPACK (CONCURRENTLY) launches a background worker to decode the changes
made while the table is being rewritten. Since cluster_rel() switches
the current user to the owner of the table being repacked, that is the
role the worker connects as, and the worker asked for a bypass of the
LOGIN check only. The CONNECT privilege on the database was therefore
still checked against the table owner, who has no reason to have it, so
the command could fail with
ERROR: permission denied for database "d"
DETAIL: User does not have CONNECT privilege.
CONTEXT: REPACK decoding worker
Pass BGWORKER_BYPASS_ALLOWCONN as well, as parallel.c already does for
parallel workers: the backend that launched the worker is connected to
the database already, and the worker only decodes changes on its behalf.
Reported-by: Nathan Bossart <nathandbossart@gmail.com>
Author: Matheus Alcantara <mths.dev@pm.me>
Discussion: https://www.postgresql.org/message-id/apBbzFd_EYAfHV45@nathan
---
contrib/test_decoding/expected/repack.out | 19 +++++++++++++++++++
contrib/test_decoding/sql/repack.sql | 21 +++++++++++++++++++++
src/backend/commands/repack_worker.c | 9 ++++++++-
3 files changed, 48 insertions(+), 1 deletion(-)
diff --git a/contrib/test_decoding/expected/repack.out b/contrib/test_decoding/expected/repack.out
index 5ddc63238c5..a65ef660b06 100644
--- a/contrib/test_decoding/expected/repack.out
+++ b/contrib/test_decoding/expected/repack.out
@@ -29,6 +29,25 @@ SELECT a.relname, a.relfilenode=b.relfilenode FROM pg_class a
DROP TABLE ptnowner;
DROP ROLE regress_ptnowner;
+-- The decoding worker runs as the owner of the table being repacked, but it
+-- should not need the LOGIN attribute.
+CREATE ROLE regress_repack_nologin NOLOGIN;
+CREATE TABLE repack_nologin (i int PRIMARY KEY);
+ALTER TABLE repack_nologin OWNER TO regress_repack_nologin;
+REPACK (CONCURRENTLY) repack_nologin;
+DROP TABLE repack_nologin;
+DROP ROLE regress_repack_nologin;
+-- The decoding worker runs as the owner of the table being repacked, but it
+-- should not need the CONNECT privilege on the database.
+CREATE ROLE regress_repack_noconn NOLOGIN;
+CREATE TABLE repack_noconn (i int PRIMARY KEY);
+ALTER TABLE repack_noconn OWNER TO regress_repack_noconn;
+SELECT current_database() AS datname \gset
+REVOKE CONNECT ON DATABASE :"datname" FROM PUBLIC;
+REPACK (CONCURRENTLY) repack_noconn;
+GRANT CONNECT ON DATABASE :"datname" TO PUBLIC;
+DROP TABLE repack_noconn;
+DROP ROLE regress_repack_noconn;
-- Verify that REPACK (CONCURRENTLY) doesn't lose "attmissingval" columns
CREATE TABLE rpk_missing (id int PRIMARY KEY);
INSERT INTO rpk_missing SELECT generate_series(1, 3);
diff --git a/contrib/test_decoding/sql/repack.sql b/contrib/test_decoding/sql/repack.sql
index f461f5479f4..4068b707f30 100644
--- a/contrib/test_decoding/sql/repack.sql
+++ b/contrib/test_decoding/sql/repack.sql
@@ -24,6 +24,27 @@ SELECT a.relname, a.relfilenode=b.relfilenode FROM pg_class a
DROP TABLE ptnowner;
DROP ROLE regress_ptnowner;
+-- The decoding worker runs as the owner of the table being repacked, but it
+-- should not need the LOGIN attribute.
+CREATE ROLE regress_repack_nologin NOLOGIN;
+CREATE TABLE repack_nologin (i int PRIMARY KEY);
+ALTER TABLE repack_nologin OWNER TO regress_repack_nologin;
+REPACK (CONCURRENTLY) repack_nologin;
+DROP TABLE repack_nologin;
+DROP ROLE regress_repack_nologin;
+
+-- The decoding worker runs as the owner of the table being repacked, but it
+-- should not need the CONNECT privilege on the database.
+CREATE ROLE regress_repack_noconn NOLOGIN;
+CREATE TABLE repack_noconn (i int PRIMARY KEY);
+ALTER TABLE repack_noconn OWNER TO regress_repack_noconn;
+SELECT current_database() AS datname \gset
+REVOKE CONNECT ON DATABASE :"datname" FROM PUBLIC;
+REPACK (CONCURRENTLY) repack_noconn;
+GRANT CONNECT ON DATABASE :"datname" TO PUBLIC;
+DROP TABLE repack_noconn;
+DROP ROLE regress_repack_noconn;
+
-- Verify that REPACK (CONCURRENTLY) doesn't lose "attmissingval" columns
CREATE TABLE rpk_missing (id int PRIMARY KEY);
INSERT INTO rpk_missing SELECT generate_series(1, 3);
diff --git a/src/backend/commands/repack_worker.c b/src/backend/commands/repack_worker.c
index af7e2a94764..a49b681059f 100644
--- a/src/backend/commands/repack_worker.c
+++ b/src/backend/commands/repack_worker.c
@@ -101,8 +101,15 @@ RepackWorkerMain(Datum main_arg)
pq_set_parallel_leader(shared->backend_pid,
shared->backend_proc_number);
- /* Connect to the database. LOGIN is not required. */
+ /*
+ * Connect to the database. Like parallel workers do, we skip the
+ * connection authorization checks. The backend that launched us is
+ * already connected to this database, and the role we run as is the owner
+ * of the table being repacked, which needs neither the LOGIN attribute
+ * nor the CONNECT privilege for our purposes.
+ */
BackgroundWorkerInitializeConnectionByOid(shared->dbid, shared->roleid,
+ BGWORKER_BYPASS_ALLOWCONN |
BGWORKER_BYPASS_ROLELOGINCHECK);
/*
--
2.50.1 (Apple Git-155)
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT
2026-08-27 15:46 REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 18:16 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 18:33 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-08-27 18:55 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
@ 2026-08-27 19:19 ` Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 19:40 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-08-27 19:52 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
0 siblings, 2 replies; 12+ messages in thread
From: Nathan Bossart @ 2026-08-27 19:19 UTC (permalink / raw)
To: Matheus Alcantara <matheusssilv97@gmail.com>; +Cc: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>; pgsql-hackers; alvherre@kurilemu.de
The patch looks reasonable to me.
On Thu, Aug 27, 2026 at 03:55:11PM -0300, Matheus Alcantara wrote:
> Added this new test case on attached.
I'm not sure the new tests are necessary. It seems unlikely that we'll
accidentally remove the flags down the road.
--
nathan
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT
2026-08-27 15:46 REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 18:16 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 18:33 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-08-27 18:55 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 19:19 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
@ 2026-08-27 19:40 ` Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
1 sibling, 0 replies; 12+ messages in thread
From: Bharath Rupireddy @ 2026-08-27 19:40 UTC (permalink / raw)
To: Nathan Bossart <nathandbossart@gmail.com>; +Cc: Matheus Alcantara <matheusssilv97@gmail.com>; pgsql-hackers; alvherre@kurilemu.de
Hi,
On Thu, Aug 27, 2026 at 12:19 PM Nathan Bossart
<nathandbossart@gmail.com> wrote:
>
> The patch looks reasonable to me.
+1.
> On Thu, Aug 27, 2026 at 03:55:11PM -0300, Matheus Alcantara wrote:
> > Added this new test case on attached.
>
> I'm not sure the new tests are necessary. It seems unlikely that we'll
> accidentally remove the flags down the road.
I don't fully agree with this. These tests helped identify an issue,
and removing these flags from a fairly new repack code is a behavioral
change, even if it doesn't bring in additional code coverage. Maybe
have these just for HEAD?
--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT
2026-08-27 15:46 REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 18:16 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 18:33 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-08-27 18:55 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 19:19 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
@ 2026-08-27 19:52 ` Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 20:50 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-09-04 18:40 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Masahiko Sawada <sawada.mshk@gmail.com>
1 sibling, 2 replies; 12+ messages in thread
From: Matheus Alcantara @ 2026-08-27 19:52 UTC (permalink / raw)
To: Nathan Bossart <nathandbossart@gmail.com>; +Cc: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>; pgsql-hackers; alvherre@kurilemu.de
On 27/08/26 16:19, Nathan Bossart wrote:
> The patch looks reasonable to me.
>
> On Thu, Aug 27, 2026 at 03:55:11PM -0300, Matheus Alcantara wrote:
>> Added this new test case on attached.
>
> I'm not sure the new tests are necessary. It seems unlikely that we'll
> accidentally remove the flags down the road.
>
Yeah, I also think that is unlikely that we'll remove, I just put the
test to be more conservative. I think that is good to have such tests
to ensure that these bugs are not re-introduced but given that this
seems a minor/oversight I'm not totally against not adding them.
Attached is v3 without the tests.
--
Matheus Alcantara
EDB: https://www.enterprisedb.com
Attachments:
v3-0001-Let-the-REPACK-decoding-worker-bypass-connection-.patch (text/plain, 2.2K) download
From 7f5ec60686edbc3c0555bfa5c743719405321002 Mon Sep 17 00:00:00 2001
From: Matheus Alcantara <mths.dev@pm.me>
Date: Thu, 27 Aug 2026 15:07:39 -0300
Subject: [PATCH v3] Let the REPACK decoding worker bypass connection privilege
checks
REPACK (CONCURRENTLY) launches a background worker to decode the changes
made while the table is being rewritten. Since cluster_rel() switches
the current user to the owner of the table being repacked, that is the
role the worker connects as, and the worker asked for a bypass of the
LOGIN check only. The CONNECT privilege on the database was therefore
still checked against the table owner, who has no reason to have it, so
the command could fail with
ERROR: permission denied for database "d"
DETAIL: User does not have CONNECT privilege.
CONTEXT: REPACK decoding worker
Pass BGWORKER_BYPASS_ALLOWCONN as well, as parallel.c already does for
parallel workers: the backend that launched the worker is connected to
the database already, and the worker only decodes changes on its behalf.
Author: Matheus Alcantara <mths.dev@pm.me>
Reported-by: Nathan Bossart <nathandbossart@gmail.com>
Discussion: https://www.postgresql.org/message-id/apBbzFd_EYAfHV45@nathan
---
src/backend/commands/repack_worker.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/src/backend/commands/repack_worker.c b/src/backend/commands/repack_worker.c
index af7e2a94764..a49b681059f 100644
--- a/src/backend/commands/repack_worker.c
+++ b/src/backend/commands/repack_worker.c
@@ -101,8 +101,15 @@ RepackWorkerMain(Datum main_arg)
pq_set_parallel_leader(shared->backend_pid,
shared->backend_proc_number);
- /* Connect to the database. LOGIN is not required. */
+ /*
+ * Connect to the database. Like parallel workers do, we skip the
+ * connection authorization checks. The backend that launched us is
+ * already connected to this database, and the role we run as is the owner
+ * of the table being repacked, which needs neither the LOGIN attribute
+ * nor the CONNECT privilege for our purposes.
+ */
BackgroundWorkerInitializeConnectionByOid(shared->dbid, shared->roleid,
+ BGWORKER_BYPASS_ALLOWCONN |
BGWORKER_BYPASS_ROLELOGINCHECK);
/*
--
2.50.1 (Apple Git-155)
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT
2026-08-27 15:46 REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 18:16 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 18:33 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-08-27 18:55 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 19:19 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 19:52 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
@ 2026-08-27 20:50 ` Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
1 sibling, 0 replies; 12+ messages in thread
From: Bharath Rupireddy @ 2026-08-27 20:50 UTC (permalink / raw)
To: Matheus Alcantara <matheusssilv97@gmail.com>; +Cc: Nathan Bossart <nathandbossart@gmail.com>; pgsql-hackers; alvherre@kurilemu.de
Hi,
On Thu, Aug 27, 2026 at 12:52 PM Matheus Alcantara
<matheusssilv97@gmail.com> wrote:
>
> On 27/08/26 16:19, Nathan Bossart wrote:
> > The patch looks reasonable to me.
> >
> > On Thu, Aug 27, 2026 at 03:55:11PM -0300, Matheus Alcantara wrote:
> >> Added this new test case on attached.
> >
> > I'm not sure the new tests are necessary. It seems unlikely that we'll
> > accidentally remove the flags down the road.
> >
>
> Yeah, I also think that is unlikely that we'll remove, I just put the
> test to be more conservative. I think that is good to have such tests
> to ensure that these bugs are not re-introduced but given that this
> seems a minor/oversight I'm not totally against not adding them.
> Attached is v3 without the tests.
Fair enough. If anyone feels differently later, we can always add them
to HEAD. The attached v3 patch looks good to me. pgindent and tests
are all happy.
--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT
2026-08-27 15:46 REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 18:16 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 18:33 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-08-27 18:55 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 19:19 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 19:52 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
@ 2026-09-04 18:40 ` Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-04 21:01 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-09-08 19:01 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Alvaro Herrera <alvherre@kurilemu.de>
1 sibling, 2 replies; 12+ messages in thread
From: Masahiko Sawada @ 2026-09-04 18:40 UTC (permalink / raw)
To: Matheus Alcantara <matheusssilv97@gmail.com>; +Cc: Nathan Bossart <nathandbossart@gmail.com>; Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>; pgsql-hackers; alvherre@kurilemu.de
Hi,
On Thu, Aug 27, 2026 at 12:52 PM Matheus Alcantara
<matheusssilv97@gmail.com> wrote:
>
> On 27/08/26 16:19, Nathan Bossart wrote:
> > The patch looks reasonable to me.
> >
> > On Thu, Aug 27, 2026 at 03:55:11PM -0300, Matheus Alcantara wrote:
> >> Added this new test case on attached.
> >
> > I'm not sure the new tests are necessary. It seems unlikely that we'll
> > accidentally remove the flags down the road.
> >
>
> Yeah, I also think that is unlikely that we'll remove, I just put the
> test to be more conservative. I think that is good to have such tests
> to ensure that these bugs are not re-introduced but given that this
> seems a minor/oversight I'm not totally against not adding them.
> Attached is v3 without the tests.
+1 not skipping the test.
The patch looks good to me. I've slightly rephrased the comment and
commit message. I'm going to push it barring any objections.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
Attachments:
v4-0001-Fix-REPACK-CONCURRENTLY-when-the-table-owner-lack.patch (text/x-patch, 2.0K) download
From a044532e65a7cb1da56aa761eefc9ca082c4d02c Mon Sep 17 00:00:00 2001
From: Matheus Alcantara <mths.dev@pm.me>
Date: Thu, 27 Aug 2026 15:07:39 -0300
Subject: [PATCH v4] Fix REPACK (CONCURRENTLY) when the table owner lacks
CONNECT.
REPACK (CONCURRENTLY) launches a background worker to decode changes
made while the table is being rewritten. The worker connects as the
table owner but bypassed the LOGIN check only, so CONNECT was still
checked against a role that need not have it, and the command could
fail with "permission denied for database".
Pass BGWORKER_BYPASS_ALLOWCONN as well, as we do for parallel
workers. That is safe because the leader already checked the invoking
user's privileges on the table before starting the worker.
Reported-by: Nathan Bossart <nathandbossart@gmail.com>
Author: Matheus Alcantara <mths.dev@pm.me>
Reviewed-by: Nathan Bossart <nathandbossart@gmail.com>
Reviewed-by: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Discussion: https://postgr.es/m/apBbzFd_EYAfHV45@nathan
Backpatch-through: 19
---
src/backend/commands/repack_worker.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/src/backend/commands/repack_worker.c b/src/backend/commands/repack_worker.c
index af7e2a94764..b4ba9cfc67b 100644
--- a/src/backend/commands/repack_worker.c
+++ b/src/backend/commands/repack_worker.c
@@ -101,8 +101,14 @@ RepackWorkerMain(Datum main_arg)
pq_set_parallel_leader(shared->backend_pid,
shared->backend_proc_number);
- /* Connect to the database. LOGIN is not required. */
+ /*
+ * Connect to the database, skipping the connection authorization checks
+ * as parallel workers do. Note that we run as the owner of the table
+ * being repacked, who need not be able to log in or connect; the leader
+ * checked the invoking user's privileges before starting us.
+ */
BackgroundWorkerInitializeConnectionByOid(shared->dbid, shared->roleid,
+ BGWORKER_BYPASS_ALLOWCONN |
BGWORKER_BYPASS_ROLELOGINCHECK);
/*
--
2.55.0
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT
2026-08-27 15:46 REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 18:16 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 18:33 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-08-27 18:55 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 19:19 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 19:52 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-09-04 18:40 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Masahiko Sawada <sawada.mshk@gmail.com>
@ 2026-09-04 21:01 ` Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
1 sibling, 0 replies; 12+ messages in thread
From: Bharath Rupireddy @ 2026-09-04 21:01 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: Matheus Alcantara <matheusssilv97@gmail.com>; Nathan Bossart <nathandbossart@gmail.com>; pgsql-hackers; alvherre@kurilemu.de
Hi,
On Fri, Sep 4, 2026 at 11:41 AM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
>
> +1 not skipping the test.
>
> The patch looks good to me. I've slightly rephrased the comment and
> commit message. I'm going to push it barring any objections.
Thanks for reviewing it. The v4 patch looks good to me. pgindent and
make check are happy.
--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT
2026-08-27 15:46 REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 18:16 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 18:33 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-08-27 18:55 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 19:19 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 19:52 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-09-04 18:40 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Masahiko Sawada <sawada.mshk@gmail.com>
@ 2026-09-08 19:01 ` Alvaro Herrera <alvherre@kurilemu.de>
2026-09-08 19:08 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Masahiko Sawada <sawada.mshk@gmail.com>
1 sibling, 1 reply; 12+ messages in thread
From: Alvaro Herrera @ 2026-09-08 19:01 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: Matheus Alcantara <matheusssilv97@gmail.com>; Nathan Bossart <nathandbossart@gmail.com>; Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>; pgsql-hackers
Hello,
On 2026-Sep-04, Masahiko Sawada wrote:
> The patch looks good to me. I've slightly rephrased the comment and
> commit message. I'm going to push it barring any objections.
No objections here, thanks.
--
Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT
2026-08-27 15:46 REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 18:16 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 18:33 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-08-27 18:55 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 19:19 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 19:52 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Matheus Alcantara <matheusssilv97@gmail.com>
2026-09-04 18:40 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-08 19:01 ` Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Alvaro Herrera <alvherre@kurilemu.de>
@ 2026-09-08 19:08 ` Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 0 replies; 12+ messages in thread
From: Masahiko Sawada @ 2026-09-08 19:08 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@kurilemu.de>; +Cc: Matheus Alcantara <matheusssilv97@gmail.com>; Nathan Bossart <nathandbossart@gmail.com>; Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>; pgsql-hackers
On Tue, Sep 8, 2026 at 12:01 PM Alvaro Herrera <alvherre@kurilemu.de> wrote:
>
> Hello,
>
> On 2026-Sep-04, Masahiko Sawada wrote:
>
> > The patch looks good to me. I've slightly rephrased the comment and
> > commit message. I'm going to push it barring any objections.
>
> No objections here, thanks.
Thank you for looking into it! I've just pushed the fix and marked
this open item as resolved.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
^ permalink raw reply [nested|flat] 12+ messages in thread
end of thread, other threads:[~2026-09-08 19:08 UTC | newest]
Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-27 15:46 REPACK (CONCURRENTLY) fails when table owner lacks CONNECT Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 18:16 ` Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 18:33 ` Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-08-27 18:55 ` Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 19:19 ` Nathan Bossart <nathandbossart@gmail.com>
2026-08-27 19:40 ` Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-08-27 19:52 ` Matheus Alcantara <matheusssilv97@gmail.com>
2026-08-27 20:50 ` Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-09-04 18:40 ` Masahiko Sawada <sawada.mshk@gmail.com>
2026-09-04 21:01 ` Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
2026-09-08 19:01 ` Alvaro Herrera <alvherre@kurilemu.de>
2026-09-08 19:08 ` Masahiko Sawada <sawada.mshk@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox