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 <[email protected]>
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 <[email protected]
> > <mailto:[email protected]>> 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
> [email protected]
>


-- 

Regards,
Dinesh
manojadinesh.blogspot.com

Reply via email to