REPACK (CONCURRENTLY) fails when table owner lacks CONNECT

Started by Nathan Bossart3 months ago12 messageshackers
Jump to latest
#1Nathan Bossart
nathandbossart@gmail.com

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

#2Matheus Alcantara
matheusssilv97@gmail.com
In reply to: Nathan Bossart (#1)
Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT

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:

t253575_2
0001-Let-the-REPACK-decoding-worker-bypass-connection-pri.patchtext/plain; charset=UTF-8; name=0001-Let-the-REPACK-decoding-worker-bypass-connection-pri.patchDownload+31-2
#3Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Matheus Alcantara (#2)
Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT

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

#4Matheus Alcantara
matheusssilv97@gmail.com
In reply to: Bharath Rupireddy (#3)
Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT

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:

t253575_4
v2-0001-Let-the-REPACK-decoding-worker-bypass-connection-.patchtext/plain; charset=utf-8; name=v2-0001-Let-the-REPACK-decoding-worker-bypass-connection-.patchDownload+48-2
#5Nathan Bossart
nathandbossart@gmail.com
In reply to: Matheus Alcantara (#4)
Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT

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

#6Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Nathan Bossart (#5)
Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT

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

#7Matheus Alcantara
matheusssilv97@gmail.com
In reply to: Nathan Bossart (#5)
Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT

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:

t253575_7
v3-0001-Let-the-REPACK-decoding-worker-bypass-connection-.patchtext/plain; charset=UTF-8; name=v3-0001-Let-the-REPACK-decoding-worker-bypass-connection-.patchDownload+8-2
#8Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Matheus Alcantara (#7)
Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT

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

#9Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Matheus Alcantara (#7)
Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT

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:

t253575_9
v4-0001-Fix-REPACK-CONCURRENTLY-when-the-table-owner-lack.patchtext/x-patch; charset=US-ASCII; name=v4-0001-Fix-REPACK-CONCURRENTLY-when-the-table-owner-lack.patchDownload+7-2
#10Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Masahiko Sawada (#9)
Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT

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

#11Alvaro Herrera
alvherre@kurilemu.de
In reply to: Masahiko Sawada (#9)
Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT

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/

#12Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Alvaro Herrera (#11)
Re: REPACK (CONCURRENTLY) fails when table owner lacks CONNECT

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