REPACK in a READ ONLY transaction: is it intended?

Started by dinesh kumar5 days ago10 messagesgeneral
Jump to latest
#1dinesh kumar
dineshkumar02@gmail.com

Hello Everyone,

On PostgreSQL 19.0, REPACK runs inside a READ ONLY transaction:

SELECT pg_relation_filenode('t'); -- 25599

START TRANSACTION READ ONLY;
INSERT INTO t VALUES (0, 'x');
ERROR: cannot execute INSERT in a read-only transaction
ROLLBACK;

START TRANSACTION READ ONLY;
REPACK t; -- succeeds, AccessExclusiveLock
COMMIT;

SELECT pg_relation_filenode('t'); -- 25607

I have a slight concern here, as the READ ONLY transaction is often
considered safe on a
production server, and this one takes an exclusive lock. Would anyone
suggest if we are missing anything here, please?..

Thank you.
--

Regards,
Dinesh
pgSherpa - https://cortexridge.com/

#2Laurenz Albe
laurenz.albe@cybertec.at
In reply to: dinesh kumar (#1)
Re: REPACK in a READ ONLY transaction: is it intended?

On Tue, 2026-11-24 at 13:48 +0530, dinesh kumar wrote:

On PostgreSQL 19.0, REPACK runs inside a READ ONLY transaction:

  SELECT pg_relation_filenode('t');   -- 25599

  START TRANSACTION READ ONLY;
  INSERT INTO t VALUES (0, 'x');
  ERROR:  cannot execute INSERT in a read-only transaction
  ROLLBACK;

  START TRANSACTION READ ONLY;
  REPACK t;                           -- succeeds, AccessExclusiveLock
  COMMIT;

  SELECT pg_relation_filenode('t');   -- 25607

I have a slight concern here, as the READ ONLY transaction is often considered safe on a
production server, and this one takes an exclusive lock. Would anyone suggest if we are missing anything here, please?.. 

REPACK doesn't change the logical contents of the table.

Since you are concerned about locking:

LOCK TABLE t IN ACCESS EXCLUSIVE MODE;

also works in read-only transactions.

I wouldn't consider a read-only transaction "safe" in the sense that it cannot
cause any problems on the server.

Yours,
Laurenz Albe

#3dinesh kumar
dineshkumar02@gmail.com
In reply to: Laurenz Albe (#2)
Re: REPACK in a READ ONLY transaction: is it intended?

Thank you for the clarification, Laurenz.

That makes sense regarding the logical content and locking behavior. I
appreciate the insight.

Agreed on the locks, but I have a slight concern: we changed the completed
underlying file (actually rewriting the table),
while the transaction mode is READ ONLY.

My actual question is: how does a READ ONLY TRANSACTION rewrite the
complete table?

Shouldn't we restrict this activity during READ ONLY TRANSACTION?

On Tue, Nov 24, 2026 at 7:58 PM Laurenz Albe <laurenz.albe@cybertec.at>
wrote:

On Tue, 2026-11-24 at 13:48 +0530, dinesh kumar wrote:

On PostgreSQL 19.0, REPACK runs inside a READ ONLY transaction:

SELECT pg_relation_filenode('t'); -- 25599

START TRANSACTION READ ONLY;
INSERT INTO t VALUES (0, 'x');
ERROR: cannot execute INSERT in a read-only transaction
ROLLBACK;

START TRANSACTION READ ONLY;
REPACK t; -- succeeds, AccessExclusiveLock
COMMIT;

SELECT pg_relation_filenode('t'); -- 25607

I have a slight concern here, as the READ ONLY transaction is often

considered safe on a

production server, and this one takes an exclusive lock. Would anyone

suggest if we are missing anything here, please?..

REPACK doesn't change the logical contents of the table.

Since you are concerned about locking:

LOCK TABLE t IN ACCESS EXCLUSIVE MODE;

also works in read-only transactions.

I wouldn't consider a read-only transaction "safe" in the sense that it
cannot
cause any problems on the server.

Yours,
Laurenz Albe

--

Regards,
Dinesh
pgSherpa - https://cortexridge.com/

#4Adrian Klaver
adrian.klaver@aklaver.com
In reply to: dinesh kumar (#3)
Re: REPACK in a READ ONLY transaction: is it intended?

On 11/24/26 6:54 AM, dinesh kumar wrote:

Thank you for the clarification, Laurenz.

That makes sense regarding the logical content and locking behavior. I
appreciate the insight.

Agreed on the locks, but I have a slight concern: we changed the
completed underlying file (actually rewriting the table),
while the transaction mode is READ ONLY.

My actual question is: how does a READ ONLY TRANSACTION rewrite the
complete table?

https://www.postgresql.org/docs/19/sql-repack.html

"
Notes on Concurrent Operation
Warning

REPACK with the CONCURRENTLY option is not MVCC-safe. After the
repacking transaction commits, the table will appear empty to concurrent
transactions, if they are using a snapshot taken before repacking
started. See Section 13.6 for more details.

REPACK copies the contents of the table (ignoring dead tuples) into a
new file, sorted by the specified index, and also creates a new file for
each index. It then swaps the old and new files for the table and all
the indexes, and deletes the old files. Without the CONCURRENTLY option,
an ACCESS EXCLUSIVE lock is acquired at the beginning and remains held
throughout the operation to make sure that the old files do not change
during the processing; otherwise, any concurrent changes would get lost
due to the swap.

...
"

So it creates a copy, not rewrite the the existing the table.

Shouldn't we restrict this activity during READ ONLY TRANSACTION?

On Tue, Nov 24, 2026 at 7:58 PM Laurenz Albe <laurenz.albe@cybertec.at
<mailto:laurenz.albe@cybertec.at>> wrote:

On Tue, 2026-11-24 at 13:48 +0530, dinesh kumar wrote:

On PostgreSQL 19.0, REPACK runs inside a READ ONLY transaction:

  SELECT pg_relation_filenode('t');   -- 25599

  START TRANSACTION READ ONLY;
  INSERT INTO t VALUES (0, 'x');
  ERROR:  cannot execute INSERT in a read-only transaction
  ROLLBACK;

  START TRANSACTION READ ONLY;
  REPACK t;                           -- succeeds,

AccessExclusiveLock

  COMMIT;

  SELECT pg_relation_filenode('t');   -- 25607

I have a slight concern here, as the READ ONLY transaction is

often considered safe on a

production server, and this one takes an exclusive lock. Would

anyone suggest if we are missing anything here, please?..

REPACK doesn't change the logical contents of the table.

Since you are concerned about locking:

  LOCK TABLE t IN ACCESS EXCLUSIVE MODE;

also works in read-only transactions.

I wouldn't consider a read-only transaction "safe" in the sense that
it cannot
cause any problems on the server.

Yours,
Laurenz Albe

--

Regards,
Dinesh
pgSherpa - https://cortexridge.com/ <https://cortexridge.com/>

--
Adrian Klaver
adrian.klaver@aklaver.com

#5dinesh kumar
dineshkumar02@gmail.com
In reply to: Adrian Klaver (#4)
Re: REPACK in a READ ONLY transaction: is it intended?

Hi Adrian,

Thank you for the details.

Since it creates a new file, updates the indexes, and deletes the old
files, isn't that still a write operation at the file system level?

From the documentation snippet you shared:

- "REPACK copies the contents of the table ... into a new file" (Writes)
- "...and also creates a new file for each index" (Writes)
- "It then swaps the old and new files for the table and all the indexes,
and deletes the old files." (Writes)

Given all these write operations on disk, shouldn't this activity be
restricted inside a READ ONLY transaction?

--

On Tue, Nov 24, 2026 at 9:23 PM Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

On 11/24/26 6:54 AM, dinesh kumar wrote:

Thank you for the clarification, Laurenz.

That makes sense regarding the logical content and locking behavior. I
appreciate the insight.

Agreed on the locks, but I have a slight concern: we changed the
completed underlying file (actually rewriting the table),
while the transaction mode is READ ONLY.

My actual question is: how does a READ ONLY TRANSACTION rewrite the
complete table?

https://www.postgresql.org/docs/19/sql-repack.html

"
Notes on Concurrent Operation
Warning

REPACK with the CONCURRENTLY option is not MVCC-safe. After the
repacking transaction commits, the table will appear empty to concurrent
transactions, if they are using a snapshot taken before repacking
started. See Section 13.6 for more details.

REPACK copies the contents of the table (ignoring dead tuples) into a
new file, sorted by the specified index, and also creates a new file for
each index. It then swaps the old and new files for the table and all
the indexes, and deletes the old files. Without the CONCURRENTLY option,
an ACCESS EXCLUSIVE lock is acquired at the beginning and remains held
throughout the operation to make sure that the old files do not change
during the processing; otherwise, any concurrent changes would get lost
due to the swap.

...
"

So it creates a copy, not rewrite the the existing the table.

Shouldn't we restrict this activity during READ ONLY TRANSACTION?

On Tue, Nov 24, 2026 at 7:58 PM Laurenz Albe <laurenz.albe@cybertec.at
<mailto:laurenz.albe@cybertec.at>> wrote:

On Tue, 2026-11-24 at 13:48 +0530, dinesh kumar wrote:

On PostgreSQL 19.0, REPACK runs inside a READ ONLY transaction:

SELECT pg_relation_filenode('t'); -- 25599

START TRANSACTION READ ONLY;
INSERT INTO t VALUES (0, 'x');
ERROR: cannot execute INSERT in a read-only transaction
ROLLBACK;

START TRANSACTION READ ONLY;
REPACK t; -- succeeds,

AccessExclusiveLock

COMMIT;

SELECT pg_relation_filenode('t'); -- 25607

I have a slight concern here, as the READ ONLY transaction is

often considered safe on a

production server, and this one takes an exclusive lock. Would

anyone suggest if we are missing anything here, please?..

REPACK doesn't change the logical contents of the table.

Since you are concerned about locking:

LOCK TABLE t IN ACCESS EXCLUSIVE MODE;

also works in read-only transactions.

I wouldn't consider a read-only transaction "safe" in the sense that
it cannot
cause any problems on the server.

Yours,
Laurenz Albe

--

Regards,
Dinesh
pgSherpa - https://cortexridge.com/ <https://cortexridge.com/>

--
Adrian Klaver
adrian.klaver@aklaver.com

--

Regards,
Dinesh
manojadinesh.blogspot.com

#6Adrian Klaver
adrian.klaver@aklaver.com
In reply to: dinesh kumar (#5)
Re: REPACK in a READ ONLY transaction: is it intended?

On 11/24/26 8:04 AM, dinesh kumar wrote:

Hi Adrian,

Thank you for the details.

Since it creates a new file, updates the indexes, and deletes the old
files, isn't that still a write operation at the file system level?

From the documentation snippet you shared:

  - "REPACK copies the contents of the table ... into a new file" (Writes)
  - "...and also creates a new file for each index" (Writes)
  - "It then swaps the old and new files for the table and all the
indexes, and deletes the old files." (Writes)

Given all these write operations on disk, shouldn't this activity be
restricted inside a READ ONLY transaction?

https://www.postgresql.org/docs/19/sql-set-transaction.html

"The transaction access mode determines whether the transaction is
read/write or read-only. Read/write is the default. [...] This is a
high-level notion of read-only that does not prevent all writes to disk."

--

--
Adrian Klaver
adrian.klaver@aklaver.com

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Adrian Klaver (#6)
Re: REPACK in a READ ONLY transaction: is it intended?

Adrian Klaver <adrian.klaver@aklaver.com> writes:

On 11/24/26 8:04 AM, dinesh kumar wrote:

Given all these write operations on disk, shouldn't this activity be
restricted inside a READ ONLY transaction?

https://www.postgresql.org/docs/19/sql-set-transaction.html
"The transaction access mode determines whether the transaction is
read/write or read-only. Read/write is the default. [...] This is a
high-level notion of read-only that does not prevent all writes to disk."

I agree that the fact that REPACK performs writes doesn't disqualify
it from being done inside a READ ONLY transaction. To my mind the
essential characteristic we expect of READ ONLY is that it doesn't
change the logical state of the database from the viewpoint of other
transactions. But the thing that gives me pause is

REPACK with the CONCURRENTLY option is not MVCC-safe.

It is very hard to claim that an operation that behaves like that
doesn't change the state of the database in a visible way.
So I tend to agree that REPACK ought to be rejected when
default_transaction_read_only=1. You could maybe say that we
only reject it with CONCURRENTLY, but that feels a little weird;
I'd just do it always.

regards, tom lane

#8Ron
ronljohnsonjr@gmail.com
In reply to: Adrian Klaver (#4)
Re: REPACK in a READ ONLY transaction: is it intended?

On Tue, Nov 24, 2026 at 10:54 AM Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

On 11/24/26 6:54 AM, dinesh kumar wrote:

Thank you for the clarification, Laurenz.

That makes sense regarding the logical content and locking behavior. I
appreciate the insight.

Agreed on the locks, but I have a slight concern: we changed the
completed underlying file (actually rewriting the table),
while the transaction mode is READ ONLY.

My actual question is: how does a READ ONLY TRANSACTION rewrite the
complete table?

https://www.postgresql.org/docs/19/sql-repack.html

"
Notes on Concurrent Operation
Warning

REPACK with the CONCURRENTLY option is not MVCC-safe. After the
repacking transaction commits, the table will appear empty to concurrent
transactions, if they are using a snapshot taken before repacking
started. See Section 13.6 for more details.

It's still massively counterintuitive to be able to do this. Even though
REPACK is documented to not be MVCC-safe, the REPACK command should check
to see if it's in a R/O txn, and if so, immediately throw an error.
START TRANSACTION READ ONLY;
REPACK t; -- succeeds, AccessExclusiveLock
COMMIT;

--
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!

#9Fujii Masao
masao.fujii@gmail.com
In reply to: Tom Lane (#7)
Re: REPACK in a READ ONLY transaction: is it intended?

On Wed, Nov 25, 2026 at 1:31 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:

You could maybe say that we
only reject it with CONCURRENTLY, but that feels a little weird;
I'd just do it always.

If we consider REPACK without CONCURRENTLY basically equivalent to
CLUSTER or VACUUM FULL, both of which are allowed in read-only
transactions, isn't it more consistent to allow it and reject only
REPACK with CONCURRENTLY?

Regards,

--
Fujii Masao

#10Tom Lane
tgl@sss.pgh.pa.us
In reply to: Fujii Masao (#9)
Re: REPACK in a READ ONLY transaction: is it intended?

Fujii Masao <masao.fujii@gmail.com> writes:

On Wed, Nov 25, 2026 at 1:31 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:

You could maybe say that we
only reject it with CONCURRENTLY, but that feels a little weird;
I'd just do it always.

If we consider REPACK without CONCURRENTLY basically equivalent to
CLUSTER or VACUUM FULL, both of which are allowed in read-only
transactions, isn't it more consistent to allow it and reject only
REPACK with CONCURRENTLY?

If you want one of those commands, you can use one of those commands.
ISTM REPACK is the swiss army knife of table-maintenance commands,
so it is going to have some variants that can work inside a
transaction and some that can't. Expecting psql to keep track of
which is which seems like a recipe for future bugs to me (even if
we had a 100% solution for that today, which we do not).

regards, tom lane