| From: | wangyang25(at)otcaix(dot)iscas(dot)ac(dot)cn |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Subject: | vacuumlo can delete large objects referenced only by FORCE RLS-hidden rows |
| Date: | 2026-11-23 09:41:14 |
| Message-ID: | 179542695016.3515577.15766019450284216645@otcaix.iscas.ac.cn |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
PostgreSQL server version: PostgreSQL 19.0, REL_19_0, commit 5c839a2a08b9b87a349be97371fac6c5a2873b10
psql version: PostgreSQL 19.0
vacuumlo version: PostgreSQL 19.0
Platform: Ubuntu 24.04 LTS, Linux x86_64, kernel 7.0.0-38-generic, glibc 2.39; GCC 13.3.0
Build options: --without-readline --without-zlib --without-icu --without-llvm
Cluster setup: Run as the postgres operating-system user. Set PGDATA to a new empty data directory and SOCKET to a local socket directory, then run:
export PGDATA=/path/to/pgdata
export SOCKET=/path/to/socket
mkdir -p "$SOCKET"
initdb -D "$PGDATA" --no-locale -E UTF8 --auth-local=trust --auth-host=reject
Start the server with:
pg_ctl -D "$PGDATA" -l "$PGDATA/server.log" -o "-c listen_addresses= -c unix_socket_directories=$SOCKET -c port=55488 -c max_connections=10 -c shared_buffers=32MB -c wal_level=logical -c max_replication_slots=10 -c max_wal_senders=10" -w start
Connect with PGHOST=$SOCKET PGPORT=55488 psql -X. Use the postgres database superuser for setup and postflight, and repro_owner for the foreign-table operations and vacuumlo invocation.
Summary
vacuumlo can delete a large object that is still referenced by a row hidden from the utility's ordinary SELECT by FORCE ROW LEVEL SECURITY. The row remains, leaving its OID without a large object.
Reproduction
Use a fresh disposable cluster. As a superuser, create a login role and database:
CREATE ROLE repro_owner LOGIN;
CREATE DATABASE repro_db OWNER repro_owner;
Connect to repro_db as repro_owner using PGHOST=$SOCKET PGPORT=55488 psql -X -U repro_owner -d repro_db, then run:
CREATE TABLE refs_force (loid oid);
CREATE TABLE refs_plain (loid oid);
SELECT lo_create(0) AS force_lo \gset
SELECT lo_create(0) AS plain_lo \gset
INSERT INTO refs_force VALUES (:'force_lo');
INSERT INTO refs_plain VALUES (:'plain_lo');
ALTER TABLE refs_force ENABLE ROW LEVEL SECURITY;
ALTER TABLE refs_force FORCE ROW LEVEL SECURITY;
CREATE POLICY hide_force ON refs_force FOR SELECT USING (false);
ALTER TABLE refs_plain ENABLE ROW LEVEL SECURITY;
CREATE POLICY hide_plain ON refs_plain FOR SELECT USING (false);
SELECT count(*) AS force_visible FROM refs_force;
SELECT count(*) AS plain_visible FROM refs_plain;
As repro_owner, run the dry run and then the actual utility:
PGHOST=$SOCKET PGPORT=55488 vacuumlo -n -v -U repro_owner repro_db
PGHOST=$SOCKET PGPORT=55488 vacuumlo -v -U repro_owner repro_db
As a superuser, connect with PGHOST=$SOCKET PGPORT=55488 psql -X -U postgres -d repro_db and check the references:
SELECT 'refs_force' AS relation, loid,
loid IN (SELECT oid FROM pg_largeobject_metadata) AS object_exists
FROM refs_force
UNION ALL
SELECT 'refs_plain', loid,
loid IN (SELECT oid FROM pg_largeobject_metadata)
FROM refs_plain
ORDER BY relation;
Observed result
As repro_owner, force_visible is 0 and plain_visible is 1. The dry run prints Would remove 1 large objects. The actual run prints Successfully removed 1 large objects. The final query shows refs_force with object_exists=false and refs_plain with object_exists=true; both reference rows remain. This result reproduced in two independent PostgreSQL 19.0 runs and on PostgreSQL 18.7 (REL_18_7, commit f5c41f6681c910b3f69951bf6fb97c6d74605d25).
Expected result
The PostgreSQL 19 vacuumlo documentation defines an orphan as an OID that does not appear in any oid or lo data column in the database. The refs_force row still contains the OID, so that object is not orphaned. FORCE ROW LEVEL SECURITY makes the table owner subject to the policy, and the ordinary SELECT used by the utility cannot see the row. A filtered reference scan should not be treated as a complete list of database references for destructive cleanup.
Documentation:
https://www.postgresql.org/docs/19/vacuumlo.html
https://www.postgresql.org/docs/19/ddl-rowsecurity.html
https://www.postgresql.org/docs/19/lo-implementation.html
Relevant source location: contrib/vacuumlo/vacuumlo.c removes references found by ordinary SELECT queries and later calls lo_unlink() for the remaining candidate OIDs. Source at the tested commit: https://github.com/postgres/postgres/blob/5c839a2a08b9b87a349be97371fac6c5a2873b10/contrib/vacuumlo/vacuumlo.c#L241-L333
Cleanup
After disconnecting from repro_db, connect to postgres as a superuser and drop the disposable database and role:
DROP DATABASE repro_db;
DROP ROLE repro_owner;
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Shlok Kyal | 2026-11-23 10:00:47 | Re: Logical replication commits duplicates in DEFERRABLE unique indexes |
| Previous Message | Raja Sai pranav | 2026-11-23 09:36:25 | Re: Stale partition constraint on sub-partitions of a partitioned DEFAULT partition after a sibling is detached/dropped/created |