Orphaned Files in PostgreSQL

Поиск
Список
Период
Сортировка
Искать

Orphaned Files in PostgreSQL

От:
Ashutosh Sharma <ashu.coek88@gmail.com>
Дата:
Hi All,

While investigating one of our customer issues, we discovered several
orphaned data files on the disk that do not have corresponding entries
in the pg_class table. Upon further analysis, we identified specific
scenarios in PostgreSQL where this issue can occur. One such scenario
is as follows:

Consider a situation where a table is being created within a
transaction, and data is being loaded into it. If PostgreSQL
unexpectedly crashes while the transaction is still in progress, an
orphaned file may be left behind on the disk. In cases where multiple
such transactions occur, this can lead to the accumulation of numerous
orphaned files, resulting in significant disk space consumption.
Unfortunately, these files are not cleared during PostgreSQL's restart
process.

We have discussed this issue internally, and one proposed solution
involves adding a marker file to the disk for any table created within
a transaction, immediately upon its creation. This marker file would
then be removed during the commit process. If the transaction is
aborted due to a server crash, the marker file and the corresponding
disk file would be cleared at the end of the recovery process during
server startup.

I would appreciate your thoughts on this solution. Should you have any
suggestions or alternative approaches, I would be grateful to hear
them.

Additionally, I am unsure if this issue has already been reported or
if it is currently being addressed. If that is the case, I would be
grateful if you could point me to the relevant discussion thread so I
can follow the progress and contribute if needed.

Thank you for your time and assistance.

--
With Regards,
Ashutosh Sharma.


Re: Orphaned Files in PostgreSQL

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Tue, Feb 18, 2025 at 04:16:02PM +0530, Ashutosh Sharma wrote:
> Additionally, I am unsure if this issue has already been reported or
> if it is currently being addressed. If that is the case, I would be
> grateful if you could point me to the relevant discussion thread so I
> can follow the progress and contribute if needed.

I think that it is a known issue (see [1]). The thread links to a discussion that
could provide a fix (If I understood correctly) and introduces a way/extension to
clean those orphaned files in a clean way (using a dirty snapshot). Idea was to
gauge if there is interest to add this extension in contrib (would certainly
need some polishing and code work to meet the "contrib" expectations though).

[1]: https://www.postgresql.org/message-id/flat/7ff08868-843a-c39c-c96d-7e7f77fe5f5c%40amazon.com

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


Re: Orphaned Files in PostgreSQL

От:
Ashutosh Sharma <ashu.coek88@gmail.com>
Дата:
Вложения:
Hi,

On Fri, Aug 21, 2026 at 12:25 PM Bertrand Drouvot
 wrote:
>
> Hi,
>
> On Thu, Aug 20, 2026 at 02:40:25PM +0530, Ashutosh Sharma wrote:
> > Hi All,
> >
> > I am revisiting this thread to propose a possible fix for this known issue.
>
> Thanks for working on this!
>
> > This ordering guarantees that relation storage cannot become durable
> > without either a durable marker or WAL capable of reconstructing that
> > marker.
>
> I wonder if logging XLOG_SMGR_CREATE before smgrcreate() could interact badly
> with a concurrent checkpoint? I looked at [1] and it looks like it used a separate
> PRECREATE record and kept the usual CREATE record after physical creation.
>

This has been addressed in the attached patch.

The patch also replaces the earlier design of creating one marker file
per relation file with a manifest file per transaction XID. Each
manifest records the durable relations created by that transaction.

On transaction completion, the corresponding manifests are retired and
removed. If PostgreSQL crashes before transaction cleanup completes,
recovery examines the remaining manifests after WAL replay. Manifests
belonging to committed or prepared transactions are preserved or
cleaned up appropriately, while relation files recorded for aborted or
incomplete transactions are removed. The processed manifests are then
removed.

The patch also handles subtransactions, prepared transactions, standby
replay, truncated manifests, and failures that leave retired manifests
behind.

Please take a look and let me know.

--
With Regards,
Ashutosh Sharma.

Re: Orphaned Files in PostgreSQL

От:
Andrey Borodin <x4mmm@yandex-team.ru>
Дата:
Hi Ashutosh,

On 23 Sep 2026, Ashutosh Sharma wrote:
> Please take a look and let me know.

At the design level, one manifest per XID still means a pg_fsync() for
each appended record, including during redo. Have you considered WAL
plus delayed manifest synchronization, along the lines Andres
suggested [0]? It would be useful to compare small-DDL and replay
costs before settling on synchronous per-record writes.

From reading v2, I am concerned about mapped catalog rewrites.
write_relmap_file() flushes XLOG_RELMAP_UPDATE before calling
RelationPreserveStorage(), where the patch now records PRESERVE.
A crash between those steps leaves the new mapping durable, but the
creating transaction uncommitted and its manifest without PRESERVE.
relmap_redo() does not preserve the storage either. Wouldn't the new
end-of-recovery cleanup then remove files needed by the mapped catalog?

Could preservation be part of the relmap update's recovery semantics?
A crash test in that window during VACUUM FULL of a mapped catalog
seems particularly important. I haven't run that reproducer yet.

The truncated-manifest test expects startup to fail. Can a crash during
a normal append leave that state without replay repairing it? If so,
could we retain the uncertain files rather than refuse startup?

Also, Greg recently mentioned renewed UNDO/FILEOPS work [1]. It may
be worth coordinating the scope with him. Preventing new orphans and
handling existing ones, as needed for online checksums, are separate
parts of the problem.

Thank you!


Best regards, Andrey Borodin.

[0] https://postgr.es/m/20170814185632.zodm5qykgss7ud32@alap3.anarazel.de
[1] https://postgr.es/m/5d89549c-117e-45ae-b934-a2bb71c82a79@app.fastmail.com



Re: Orphaned Files in PostgreSQL

От:
Greg Burd <greg@burd.me>
Дата:

On Wednesday, September 23rd, 2026 at 8:39 AM, Andrey Borodin  wrote:

> Hi Ashutosh,
> 
> On 23 Sep 2026, Ashutosh Sharma wrote:
> > Please take a look and let me know.

Hey Ashutosh!

Thanks for the excellent email kicking off this thread, I couldn't agree
more about the issue.  I started in a different place, asking myself if
I could resurrect UNDO from ZHEAP without modifying HEAP at all (because
I tried and it didn't help, other different table AMs might find benefit
but HEAP is rather solid as it is) and if I could how would I demonstrate
and justify it without adding a new table AM or modifying HEAP?

I used to work on Berkeley DB and one of the features was its ability to
WAL log UNDO/REDO records for "filesystem operations" and then during
recovery tidy up and make things consistent.  That struck me as a solid
first application of UNDO in Postgres, so I created that and called it
FILEOPS (because I'm not creative at all).

> At the design level, one manifest per XID still means a pg_fsync() for
> each appended record, including during redo. Have you considered WAL
> plus delayed manifest synchronization, along the lines Andres
> suggested [0]? It would be useful to compare small-DDL and replay
> costs before settling on synchronous per-record writes.
> 
> From reading v2, I am concerned about mapped catalog rewrites.
> write_relmap_file() flushes XLOG_RELMAP_UPDATE before calling
> RelationPreserveStorage(), where the patch now records PRESERVE.
> A crash between those steps leaves the new mapping durable, but the
> creating transaction uncommitted and its manifest without PRESERVE.
> relmap_redo() does not preserve the storage either. Wouldn't the new
> end-of-recovery cleanup then remove files needed by the mapped catalog?
> 
> Could preservation be part of the relmap update's recovery semantics?
> A crash test in that window during VACUUM FULL of a mapped catalog
> seems particularly important. I haven't run that reproducer yet.
> 
> The truncated-manifest test expects startup to fail. Can a crash during
> a normal append leave that state without replay repairing it? If so,
> could we retain the uncertain files rather than refuse startup?
> 
> Also, Greg recently mentioned renewed UNDO/FILEOPS work [1]. It may
> be worth coordinating the scope with him. Preventing new orphans and
> handling existing ones, as needed for online checksums, are separate
> parts of the problem.

I'm spending a lot of time this week cleaning up the UNDO and FILEOPS
patches in hopes of posting them soon as either an RFC or a proposed
patch set.  I do have new table AMs that use it, but I don't think
they are ready for prime time yet.  There are other use cases for UNDO
also like BLOB/CLOBs etc. that I think might be interesting and if I
can get the integration with nbtree and hash correct there are also
benefits for indexes.

That said, it's a large change and one that has philosophical and
technical challenges before the community could even consider merging
it in.  I have hope, but it'll be a long road.

Your approach has less overhead/history to deal with. I'll need to
dig into it more to appreciate the direction you've taken but I do
agree that it needs to happen somehow and so I don't see this as a
competing idea at all.

best.

-greg

> Thank you!
> 
> 
> Best regards, Andrey Borodin.
> 
> [0] https://postgr.es/m/20170814185632.zodm5qykgss7ud32@alap3.anarazel.de
> [1] https://postgr.es/m/5d89549c-117e-45ae-b934-a2bb71c82a79@app.fastmail.com



Re: Orphaned Files in PostgreSQL

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Hello!

This issue is also related to a recent discussion about online
checksums[1] and I tried to look into possible solutions into it when
investigating that, and I agree that it should be improved.

But I think the proposed patch has some issues.

On Wed, 23 Sep 2026, Andrey Borodin  wrote:
> From reading v2, I am concerned about mapped catalog rewrites.
> write_relmap_file() flushes XLOG_RELMAP_UPDATE before calling
> RelationPreserveStorage(), where the patch now records PRESERVE.
> A crash between those steps leaves the new mapping durable, but the
> creating transaction uncommitted and its manifest without PRESERVE.

It doesn't need a random crash, PITR to a VACUUM FULL
pg_class/pg_database/... with recovery_target_action = 'promote' can
crash the server / completely brick the datadir.

For example if waldump shows:

Storage      0/030380B0 PRECREATE base/5/16387
RelMap       0/03040E48 UPDATE database ...
Storage      0/03041080 PRESERVE base/5/16384 xid 664
Storage      0/030410B0 PRESERVE base/5/16387 xid 664
Transaction  0/03041140 COMMIT

Then PITR to that RelMap entry reproduces the issue.

Another recovery issue is that end of recovery reconciliation runs
after the cluster already left recovery, and it will unlink the
storage of live transactions.
For example retrying BEGIN; CREATE TABLE ... ; loops accross a pg_ctl
promote pulls the storage out from the new tables, the transactions
can still COMMIT, and then access to that table fails because there's
no storage for it.

@@ -262,6 +697,15 @@ RelationPreserveStorage(RelFileLocator rlocator,
bool atCommit)
 		if (RelFileLocatorEquals(rlocator, pending->rlocator)
 			&& pending->atCommit == atCommit)
 		{
+			if (!atCommit && TransactionIdIsValid(pending->createXid))
+			{


This performs IO inside a critical section and can panic the server if
that IO fails. And if that panic happens with a catalog table, the
database can't start up again, because recovery unlinks the new file.


> The patch also handles subtransactions, prepared transactions, standby
> replay, truncated manifests, and failures that leave retired manifests
> behind.

It seems to me that the standby keeps crash aborted orphan files, only
the primary deletes them properly.

> It would be useful to compare small-DDL and replay
> costs before settling on synchronous per-record writes.

Other than costs, currently postgres honors synchronous_commit = off
for storage creating DDL. With the patch, it no longer does so. That
at least needs proper documentation, but I am not so sure that this is
an actual requirement for preventing orphan files.

[1] : https://www.postgresql.org/message-id/CA%2BTgmoaOCdjAjr240e_%2BxoqQCRmLC9MFn3kZOu-7j8w8HBrd8g%40mail.gmail.com


Re: Orphaned Files in PostgreSQL

От:
Ashutosh Sharma <ashu.coek88@gmail.com>
Дата:
Hi,

Thank you for taking a quick look at the proposed changes and sharing
your feedback.

On Wed, Sep 23, 2026 at 6:09 PM Andrey Borodin  wrote:
>
> Hi Ashutosh,
>
> On 23 Sep 2026, Ashutosh Sharma wrote:
> > Please take a look and let me know.
>
> At the design level, one manifest per XID still means a pg_fsync() for
> each appended record, including during redo. Have you considered WAL
> plus delayed manifest synchronization, along the lines Andres
> suggested [0]? It would be useful to compare small-DDL and replay
> costs before settling on synchronous per-record writes.
>
> From reading v2, I am concerned about mapped catalog rewrites.
> write_relmap_file() flushes XLOG_RELMAP_UPDATE before calling
> RelationPreserveStorage(), where the patch now records PRESERVE.
> A crash between those steps leaves the new mapping durable, but the
> creating transaction uncommitted and its manifest without PRESERVE.
> relmap_redo() does not preserve the storage either. Wouldn't the new
> end-of-recovery cleanup then remove files needed by the mapped catalog?
>
> Could preservation be part of the relmap update's recovery semantics?
> A crash test in that window during VACUUM FULL of a mapped catalog
> seems particularly important. I haven't run that reproducer yet.
>
> The truncated-manifest test expects startup to fail. Can a crash during
> a normal append leave that state without replay repairing it? If so,
> could we retain the uncertain files rather than refuse startup?
>
> Also, Greg recently mentioned renewed UNDO/FILEOPS work [1]. It may
> be worth coordinating the scope with him. Preventing new orphans and
> handling existing ones, as needed for online checksums, are separate
> parts of the problem.
>
> Thank you!
>
>
> Best regards, Andrey Borodin.
>
> [0] https://postgr.es/m/20170814185632.zodm5qykgss7ud32@alap3.anarazel.de
> [1] https://postgr.es/m/5d89549c-117e-45ae-b934-a2bb71c82a79@app.fastmail.com
>

I have not yet reviewed the earlier discussions in these threads in
enough detail. It appears that the approach I am proposing overlaps
with Chris Travers earlier work, but I do not yet understand why that
work did not proceed, whether because of unresolved technical issues
or other considerations. I will review those discussions before
deciding how to revise this proposal.

The concern raised about mapped relation files looks valid and
requires additional handling. However, before working out that fix, I
would first like to understand the earlier proposals and reconsider
the overall strategy in that context.

--
With Regards,
Ashutosh Sharma.


Re: Orphaned Files in PostgreSQL

От:
Ashutosh Sharma <ashu.coek88@gmail.com>
Дата:
Hi,

On Wed, Sep 23, 2026 at 11:55 PM Greg Burd  wrote:
> Hey Ashutosh!
>
> Thanks for the excellent email kicking off this thread, I couldn't agree
> more about the issue.

Thanks to you too for working on this project. I am glad to know that
I am not alone on this path of finding a solution to this problem.

I started in a different place, asking myself if
> I could resurrect UNDO from ZHEAP without modifying HEAP at all (because
> I tried and it didn't help, other different table AMs might find benefit
> but HEAP is rather solid as it is) and if I could how would I demonstrate
> and justify it without adding a new table AM or modifying HEAP?
>
> I used to work on Berkeley DB and one of the features was its ability to
> WAL log UNDO/REDO records for "filesystem operations" and then during
> recovery tidy up and make things consistent.  That struck me as a solid
> first application of UNDO in Postgres, so I created that and called it
> FILEOPS (because I'm not creative at all).
>

Thanks for sharing all of this. I will surely go through it once you
make it public.

>
> I'm spending a lot of time this week cleaning up the UNDO and FILEOPS
> patches in hopes of posting them soon as either an RFC or a proposed
> patch set.  I do have new table AMs that use it, but I don't think
> they are ready for prime time yet.  There are other use cases for UNDO
> also like BLOB/CLOBs etc. that I think might be interesting and if I
> can get the integration with nbtree and hash correct there are also
> benefits for indexes.
>

Sure, please share it, and I will be happy to contribute in whatever
way I can. I see that you shared this information earlier here [0],
but I somehow missed it, perhaps because I was not part of that
discussion or did not realize that it was related to the work being
discussed here.

> That said, it's a large change and one that has philosophical and
> technical challenges before the community could even consider merging
> it in.  I have hope, but it'll be a long road.
>

Yes, that seems to be pretty obvious considering the complexity involved here.

> Your approach has less overhead/history to deal with. I'll need to
> dig into it more to appreciate the direction you've taken but I do
> agree that it needs to happen somehow and so I don't see this as a
> competing idea at all.
>

Well, it seems like this approach has already been discussed/proposed
earlier here [1] by Chris but somehow it didn't move forward. I still
need to understand the details and any blockers that prevent it from
progressing.

[0] - https://www.postgresql.org/message-id/5d89549c-117e-45ae-b934-a2bb71c82a79%40app.fastmail.com
[1] - https://www.postgresql.org/message-id/CAN-RpxDBA7HbTsJPq4t4VznmRFJkssP2SNEMuG%3DoNJ%2B%3DsxLQew%40mail.gmail.com

--
With Regards,
Ashutosh Sharma.


Re: Orphaned Files in PostgreSQL

От:
Ashutosh Sharma <ashu.coek88@gmail.com>
Дата:
Hi,

Thanks for taking a quick look at the proposed changes and sharing
your review comments.

On Thu, Sep 24, 2026 at 3:40 AM Zsolt Parragi  wrote:
>
> Hello!
>
> This issue is also related to a recent discussion about online
> checksums[1] and I tried to look into possible solutions into it when
> investigating that, and I agree that it should be improved.
>

Yes, there is some connection. I haven't had a chance to go through
the previous discussions yet. Let me first go through all the past
discussions related to this and then reconsider the approach.

I am also glad to learn from this discussion that Greg, as mentioned
in his earlier response in this thread, is working on a solution to
this problem as well. We can wait for his solution to become available
and see how things progress from there.

--
With Regards,
Ashutosh Sharma.

>
> [1] : https://www.postgresql.org/message-id/CA%2BTgmoaOCdjAjr240e_%2BxoqQCRmLC9MFn3kZOu-7j8w8HBrd8g%40mail.gmail.com


Re: Orphaned Files in PostgreSQL

От:
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Дата:
Hi,

On Wed, Sep 23, 2026 at 03:10:27PM -0700, Zsolt Parragi wrote:
> Hello!
> 
> This issue is also related to a recent discussion about online
> checksums[1] and I tried to look into possible solutions into it when
> investigating that, and I agree that it should be improved.

Indeed. The proposed approach could prevent new orphan files, but it would not
address existing ones or "relation looking" files "manually" added to the data
directory. While the latter can be considered unsupported, they can trigger the
same base backup verification failure.

FWIW, pg_orphaned [1], already provides functions to list, quarantine, restore
and remove such unreferenced files. I wonder if it would be worth considering
adding pg_orphaned, or part of its functionality, to contrib, independently of
this patch?

[1]: https://github.com/bdrouvot/pg_orphaned

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com


FAQ