Vacuumlo improvements
Thread notes
1 note#feature#moderate#enhancement
hackorum-bot#feature#moderate#enhancement
@global #feature #moderate #enhancement
I'd like to discuss a behavior in the vacuumlo utility that can lead
to silent data loss when large object references are stored in columns
whose type is a domain over `oid` or `lo`. While fully stated in the
docs, we have observed users getting some surprises when they are
trying to do routine maintenance. I'll attach a very simple repro
that displays the behavior using
a fairly routine use of a domain. [1]attached vacuumlo_repro.txt
DESCRIPTION
---------------------
The vacuumlo documentation [2]https://www.postgresql.org/docs/current/vacuumlo.html states:
"Only types with these names are considered; in particular, domains
over them are not considered."
While the behavior is documented, in the field, the consequence is
severe: if a user creates a domain over `oid` (e.g., for semantic
clarity or to add constraints) and uses that domain-typed column to
store large object OIDs, vacuumlo will treat those LOs as orphaned and
delete them. The referenced data is silently destroyed.
This is particularly dangerous because:
- Domains over base types are a popular PostgreSQL practice
- There is no warning or diagnostic output from vacuumlo when it skips
domain-typed columns
- The --dry-run / -n flag will show these LOs as "would be removed"
but nothing indicates *why* they appear orphaned
- The data loss is irreversible
SUGGESTION
---------------------
Ideally, vacuumlo could be improved to:
- Resolve domain types back to their base types when scanning columns
(using pg_type.typbasetype), or
- At least emit a WARNING when it encounters columns with domains over
oid/lo that it is skipping, so the user is aware.
I don't currently have a patch attached, but wanted to shine light on
the issue given the silent data-loss risk. I know LO's are a sensitive
topic with discussions that wander towards deprecation, but they seem
to be here to stay and are very commonly used in the field.
At minimum, I can submit a documentation improvement to make the
data-loss risk more prominent. The current parenthetical note is easy
to miss.
[1]: attached vacuumlo_repro.txt
[2]: https://www.postgresql.org/docs/current/vacuumlo.html
Thanks,
Shawn
Attachments:
vacuumlo_repro.txttext/plain; charset=US-ASCII; name=vacuumlo_repro.txtDownload
On Tue, May 12, 2026 at 11:34:10AM -0600, Shawn McCoy wrote:
Ideally, vacuumlo could be improved to:
- Resolve domain types back to their base types when scanning columns
(using pg_type.typbasetype), or
- At least emit a WARNING when it encounters columns with domains over
oid/lo that it is skipping, so the user is aware.
Commit 64c604898e added the note about domains to the docs. Unfortunately,
neither that nor the corresponding thread [0]/messages/by-id/BAY164-W265A089BD32F8901A686C9FF430@phx.gbl offer any clues as to why
vacuumlo doesn't resolve domains. The commit history for vacuumlo has been
pretty quiet for a long time, so maybe it's just been overlooked.
At minimum, I can submit a documentation improvement to make the
data-loss risk more prominent. The current parenthetical note is easy
to miss.
Improving the documentation seems reasonable, too. Another thing we could
explore is allowing users to specify which tables/columns refer to LOs,
perhaps with a user-provided query. One wrinkle is that dblink allows
specifying multiple databases, and presumably each database will be a
little different.
Separately, do you know whether users are using lo_manage() at all? And if
not, why?
[0]: /messages/by-id/BAY164-W265A089BD32F8901A686C9FF430@phx.gbl
--
nathan
Ideally, vacuumlo could be improved to:
- Resolve domain types back to their base types when scanning columns
(using pg_type.typbasetype), or
- At least emit a WARNING when it encounters columns with domains over
oid/lo that it is skipping, so the user is aware.Commit 64c604898e added the note about domains to the docs. Unfortunately,
neither that nor the corresponding thread [0] offer any clues as to why
vacuumlo doesn't resolve domains. The commit history for vacuumlo has been
pretty quiet for a long time, so maybe it's just been overlooked.At minimum, I can submit a documentation improvement to make the
data-loss risk more prominent. The current parenthetical note is easy
to miss.Improving the documentation seems reasonable, too.
+1 to documentation that calls out the risk of data-loss.
Another thing we could explore is allowing users to specify which
tables/columns refer to LOs,
perhaps with a user-provided query. One wrinkle is that dblink allows
specifying multiple databases, and presumably each database will be a
little different.
Separately, do you know whether users are using lo_manage() at all? And if
not, why?
I think recommending the use of the LO extension [1]https://www.postgresql.org/docs/current/lo.html in the core large object
documentation is a good start. Ideally, a user should not have to run vacuumlo.
Using the LO extension, a user can use lo_manage for simple types ( or domain
over simple types ) or if they have a more complex situation, like a composite
type holding an LO, they can use a custom trigger.
In the case of TRUNCATE, since per-row triggers don't fire. But even
that can be handled with a statement level BEFORE TRUNCATE trigger that scans
and unlinks. vacuumlo then becomes a cleanup tool for legacy schemas, not a
routine requirement.
All to say, we should be steering the users towards this extension with more
recommendations, perhaps.
[1]: https://www.postgresql.org/docs/current/lo.html
--
Sami Imseih
Amazon Web Services (AWS)
On Tue, May 12, 2026 at 10:00:15PM -0500, Nathan Bossart wrote:
Commit 64c604898e added the note about domains to the docs. Unfortunately,
neither that nor the corresponding thread [0] offer any clues as to why
vacuumlo doesn't resolve domains. The commit history for vacuumlo has been
pretty quiet for a long time, so maybe it's just been overlooked.
It seems to be relatively easy to teach vacuumlo to handle domains over
oid. Note that you need a recursive query because you can have domains
over domains. Please test it out. I noticed that vacuumlo's tests are
pretty sad, so this might be a good opportunity to change that.
--
nathan
Attachments:
v1-0001-teach-vacuumlo-to-handle-domains-over-oid.patchtext/plain; charset=us-asciiDownload+13-2
Commit 64c604898e added the note about domains to the docs. Unfortunately,
neither that nor the corresponding thread [0] offer any clues as to why
vacuumlo doesn't resolve domains. The commit history for vacuumlo has been
pretty quiet for a long time, so maybe it's just been overlooked.It seems to be relatively easy to teach vacuumlo to handle domains over
oid. Note that you need a recursive query because you can have domains
over domains. Please test it out.
I think there is value in expanding the vacuumlo search capability for
LOs and OIDs.
We can also detect LOs and OIDs stored in composite types, or OID[] and LO[].
All these are detectable from the catalog.
The one complexity will be we will need vacuumlo to generate more complex
expressions for deleting the data.
DELETE FROM t WHERE lo IN (SELECT ("data")."lo_ref" FROM t_lo);
But, this will be more comprehensive and can cover all potential ways
an OID or LO can be used.
What do you think?
Please test it out. I noticed that vacuumlo's tests are
pretty sad, so this might be a good opportunity to change that.
More tests will be needed for sure.
But with all this done, I am not sure how much this moves the needle. It may
somewhat, but it's hard to tell how much.
I know I have seen users store LO references in text or other types,
so I think we still need the documentation enhancement to call out
the "data loss" potential.
I also think it will be good for the LO documentation [1]https://www.postgresql.org/docs/current/lo.html to nudge the users
to think about using the LO extension, as is done with the vacuumlo [2]https://www.postgresql.org/docs/current/vacuumlo.html
documentation.
[1]: https://www.postgresql.org/docs/current/lo.html
[2]: https://www.postgresql.org/docs/current/vacuumlo.html
--
Sami
On Fri, May 15, 2026 at 02:36:22PM -0500, Sami Imseih wrote:
I think there is value in expanding the vacuumlo search capability for
LOs and OIDs. We can also detect LOs and OIDs stored in composite types,
or OID[] and LO[]. All these are detectable from the catalog.The one complexity will be we will need vacuumlo to generate more complex
expressions for deleting the data.DELETE FROM t WHERE lo IN (SELECT ("data")."lo_ref" FROM t_lo);
But, this will be more comprehensive and can cover all potential ways
an OID or LO can be used.What do you think?
It seems worth exploring.
But with all this done, I am not sure how much this moves the needle. It may
somewhat, but it's hard to tell how much. I know I have seen users store
LO references in text or other types, so I think we still need the
documentation enhancement to call out the "data loss" potential.I also think it will be good for the LO documentation [1] to nudge the users
to think about using the LO extension, as is done with the vacuumlo [2]
documentation.
Yeah, I think we'll have to do some combination of 1) improving vacuumlo,
2) improving the documentation to warn users about things vacuumlo doesn't
catch, and 3) improving the documentation to nudge users toward the lo
extension..
--
nathan
Hi,
sorry for the late reply.
DELETE FROM t WHERE lo IN (SELECT ("data")."lo_ref" FROM t_lo);
But, this will be more comprehensive and can cover all potential ways
an OID or LO can be used.What do you think?
It seems worth exploring.
But with all this done, I am not sure how much this moves the needle. It may
somewhat, but it's hard to tell how much. I know I have seen users store
LO references in text or other types, so I think we still need the
documentation enhancement to call out the "data loss" potential.I also think it will be good for the LO documentation [1] to nudge the users
to think about using the LO extension, as is done with the vacuumlo [2]
documentation.Yeah, I think we'll have to do some combination of 1) improving vacuumlo,
2) improving the documentation to warn users about things vacuumlo doesn't
catch, and 3) improving the documentation to nudge users toward the lo
extension..
See the attached patches.
0001 - vacuumlo now can recursively search for OID or LO types inside
more complex
data types. It does so by having the query return the search
expression depending on the type
to the delete statement.
0002 - adds documentation mentioning the "data loss" and to nudge
users to use vacuumlo.
--
Sami Imseih
Amazon Web Services (AWS)
Attachments:
t139622_7v2-0001-vacuumlo-Find-OID-references-inside-domains-composit.patchapplication/octet-stream; name=v2-0001-vacuumlo-Find-OID-references-inside-domains-composit.patchDownload+179-43
v2-0002-vacuumlo-Document-data-loss-risk-for-unrecognized-co.patchapplication/octet-stream; name=v2-0002-vacuumlo-Document-data-loss-risk-for-unrecognized-co.patchDownload+13-1
Hi,
On Tue, Jun 2, 2026 at 2:05 PM Sami Imseih <samimseih@gmail.com> wrote:
Thanks for the patches. I started reading the code and docs around the
LOs for my learning before this patch review.
But with all this done, I am not sure how much this moves the needle. It may
somewhat, but it's hard to tell how much. I know I have seen users store
LO references in text or other types, so I think we still need the
documentation enhancement to call out the "data loss" potential.I also think it will be good for the LO documentation [1] to nudge the users
to think about using the LO extension, as is done with the vacuumlo [2]
documentation.Yeah, I think we'll have to do some combination of 1) improving vacuumlo,
2) improving the documentation to warn users about things vacuumlo doesn't
catch, and 3) improving the documentation to nudge users toward the lo
extension..
I agree to do something here. Improving the docs seems the first thing
to do. Also improving vacuumlo to detect the *possible* ways users can
store LO OIDs.
See the attached patches.
0001 - vacuumlo now can recursively search for OID or LO types inside
more complex
data types. It does so by having the query return the search
expression depending on the type
to the delete statement.
1/ While I'm, in general, okay to add more tests, I think the added
ones look like *some* patterns users can follow to store the LO refs.
Can we just simplify the 0001 with the one reported case of domains
and shorten it and split the others to 0003?
2/ Why TAP test and start a new database instance? Why not add a sql
test with the one reported domain case and be done with it?
3/ Looks like client-side encoding is not moved to the server-side
encoding? Does this have any impact on the different eoncodings or
such? Maybe I'm over cautious here. Can we avoid this in this patch
and if it's useful on its own, split it to a separate patch?
- schema = PQescapeIdentifier(conn, schema, strlen(schema));
- table = PQescapeIdentifier(conn, table, strlen(table));
- field = PQescapeIdentifier(conn, field, strlen(field));
4/ We now seem to be constructing the expression in the CTE, can this
ever be more than 4K? Or should we use PQExpBuffer or such?
-#define BUFSIZE 1024
+#define BUFSIZE 4096
5/
+ strcat(buf, "WITH RECURSIVE expressions(schema, tbl, expr, typid) AS ("
I'm not an expert in writing complex SQL queries, so I asked Claude
Code to optimize and it came up with the suggestions [1]- Pre-filter the non-recursive part of the CTE so only columns whose type can begin a path to `oid` enter the recursion: join `pg_type` and add `AND (t.oid = 'pg_catalog.oid'::regtype OR t.typtype IN ('d','c') OR (t.typelem <> 0 AND t.typlen = -1))`. Otherwise every `text` or `int` column is seeded and probes `pg_type` three times before terminating. On a 6040-column catalog this took the query from 105 ms to 22 ms, same output. - Drop `OR typid IN (SELECT oid FROM pg_type WHERE typname = 'lo')`: `lo` is a domain over `oid`, so the domain branch already finds it, and the clause makes the run fail on any unrelated type named `lo`. It also duplicates every `lo` and `lo[]` column, so each such table is scanned twice. - Use `SELECT DISTINCT`: drops the remaining duplicate rows, saving one scan of the table per duplicate. - Write `'pg_catalog.oid'::regtype` instead of `26`: for readability. - Drop `AND t.typrelid != 0`: implied by `typtype = 'c'`. - Write `pg_catalog.quote_ident(...)` instead of relying on the `search_path` set earlier.. Could you
check once?
6/ Just thinking if we also need to emit a warning in dryrun mode to
close the gaps. Something like: pg_log_warning("Verify that the OIDs
of the large objects to be removed are not stored in other data
columns or outside the database."); after:Would remove X large objects
from database "xxxx".
0002 - adds documentation mentioning the "data loss" and to nudge
users to use vacuumlo.
1/ Nit: Use "Large object OIDs" instead of references for consistency?
+ Large object references stored in columns of other types, such as
2/ Suggesting to create an extension might be too much here. Can we
instead use oid type or lo like that? Here's my wording [2]<caution> <para> Only <type>oid</type> and <type>lo</type> data columns are considered. An LO whose OID is stored in a data column of some other type, such as <type>text</type> or <type>bigint</type>, is considered orphaned and will be removed, resulting in data loss. Store LO OIDs in an <type>oid</type> or <type>lo</type> data column. The <type>lo</type> type comes from the <xref linkend="lo"/> module, which also provides the <function>lo_manage</function> trigger to unlink an LO when the row referencing it is deleted or updated. </para> </caution> in case it
helps.
+ resulting in data loss. Using the <type>lo</type> type
+ from the <xref linkend="lo"/> extension is recommended for columns that
+ hold large object references, as it allows
[1]: - Pre-filter the non-recursive part of the CTE so only columns whose type can begin a path to `oid` enter the recursion: join `pg_type` and add `AND (t.oid = 'pg_catalog.oid'::regtype OR t.typtype IN ('d','c') OR (t.typelem <> 0 AND t.typlen = -1))`. Otherwise every `text` or `int` column is seeded and probes `pg_type` three times before terminating. On a 6040-column catalog this took the query from 105 ms to 22 ms, same output. - Drop `OR typid IN (SELECT oid FROM pg_type WHERE typname = 'lo')`: `lo` is a domain over `oid`, so the domain branch already finds it, and the clause makes the run fail on any unrelated type named `lo`. It also duplicates every `lo` and `lo[]` column, so each such table is scanned twice. - Use `SELECT DISTINCT`: drops the remaining duplicate rows, saving one scan of the table per duplicate. - Write `'pg_catalog.oid'::regtype` instead of `26`: for readability. - Drop `AND t.typrelid != 0`: implied by `typtype = 'c'`. - Write `pg_catalog.quote_ident(...)` instead of relying on the `search_path` set earlier.
- Pre-filter the non-recursive part of the CTE so only columns whose
type can begin a path to `oid` enter the recursion: join `pg_type` and
add `AND (t.oid = 'pg_catalog.oid'::regtype OR t.typtype IN ('d','c')
OR (t.typelem <> 0 AND t.typlen = -1))`. Otherwise every `text` or
`int` column is seeded and probes `pg_type` three times before
terminating. On a 6040-column catalog this took the query from 105 ms
to 22 ms, same output.
- Drop `OR typid IN (SELECT oid FROM pg_type WHERE typname = 'lo')`:
`lo` is a domain over `oid`, so the domain branch already finds it,
and the clause makes the run fail on any unrelated type named `lo`. It
also duplicates every `lo` and `lo[]` column, so each such table is
scanned twice.
- Use `SELECT DISTINCT`: drops the remaining duplicate rows, saving
one scan of the table per duplicate.
- Write `'pg_catalog.oid'::regtype` instead of `26`: for readability.
- Drop `AND t.typrelid != 0`: implied by `typtype = 'c'`.
- Write `pg_catalog.quote_ident(...)` instead of relying on the
`search_path` set earlier.
[2]: <caution> <para> Only <type>oid</type> and <type>lo</type> data columns are considered. An LO whose OID is stored in a data column of some other type, such as <type>text</type> or <type>bigint</type>, is considered orphaned and will be removed, resulting in data loss. Store LO OIDs in an <type>oid</type> or <type>lo</type> data column. The <type>lo</type> type comes from the <xref linkend="lo"/> module, which also provides the <function>lo_manage</function> trigger to unlink an LO when the row referencing it is deleted or updated. </para> </caution>
<caution>
<para>
Only <type>oid</type> and <type>lo</type> data columns are considered. An LO
whose OID is stored in a data column of some other type, such as
<type>text</type> or <type>bigint</type>, is considered orphaned and will be
removed, resulting in data loss. Store LO OIDs in an <type>oid</type> or
<type>lo</type> data column. The <type>lo</type> type comes from the
<xref linkend="lo"/> module, which also provides the
<function>lo_manage</function> trigger to unlink an LO when the row
referencing it is deleted or updated.
</para>
</caution>
--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com