Unlogged Crash Detection

Started by Gersnerover 8 years ago6 messagesgeneral
Jump to latest
#1Gersner
gersner@gmail.com

Is there a reliable way to distinguish between an empty unlogged table to
an unlogged table which has been truncated due to a crash?

Gersner.

#2Michael Paquier
michael@paquier.xyz
In reply to: Gersner (#1)
Re: Unlogged Crash Detection

On Tue, Aug 29, 2017 at 5:17 PM, Gersner <gersner@gmail.com> wrote:

Is there a reliable way to distinguish between an empty unlogged table to an
unlogged table which has been truncated due to a crash?

Why do you want to make such a difference? At the beginning of a crash
recovery all the, the existing relation files of unlogged tables are
all removed, and replaced by the init forknum which represents their
initial state. You can see by yourself ResetUnloggedRelations &
friends in reinit.c.
--
Michael

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3Gersner
gersner@gmail.com
In reply to: Michael Paquier (#2)
Re: Unlogged Crash Detection

I see, interesting.

We have lots of unlogged tables, upon a crash we want to create a
feedback/alert that data disappeared.

Not very familiar with the internal structure, but is it possible to
identify if the current table is the INIT_FORKNUM?

Gersner

On Tue, Aug 29, 2017 at 11:27 AM, Michael Paquier <michael.paquier@gmail.com

Show quoted text

wrote:

On Tue, Aug 29, 2017 at 5:17 PM, Gersner <gersner@gmail.com> wrote:

Is there a reliable way to distinguish between an empty unlogged table

to an

unlogged table which has been truncated due to a crash?

Why do you want to make such a difference? At the beginning of a crash
recovery all the, the existing relation files of unlogged tables are
all removed, and replaced by the init forknum which represents their
initial state. You can see by yourself ResetUnloggedRelations &
friends in reinit.c.
--
Michael

#4Michael Paquier
michael@paquier.xyz
In reply to: Gersner (#3)
Re: Unlogged Crash Detection

On Tue, Aug 29, 2017 at 6:06 PM, Gersner <gersner@gmail.com> wrote:

I see, interesting.

Please do not top-post. This is not the recommended way of dealing
with threads on this mailing list.

We have lots of unlogged tables, upon a crash we want to create a
feedback/alert that data disappeared.

Not very familiar with the internal structure, but is it possible to
identify if the current table is the INIT_FORKNUM?

Using pg_relation_filepath, you can know the path to a relation file
on disk. So a simple idea would be to use pg_read_binary_file with the
path of the file and the path of the init fork, which is suffixed with
"_init", and then a comparison between both. If the data read is the
same, the relation has been untouched. Note that you can only do such
a thing as a superuser, and that the data is read from disk, not from
shared buffers. So that's not perfect, but it give an indication.
--
Michael

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#5Andres Freund
andres@anarazel.de
In reply to: Michael Paquier (#4)
Re: Unlogged Crash Detection

On 2017-08-29 20:19:52 +0900, Michael Paquier wrote:

On Tue, Aug 29, 2017 at 6:06 PM, Gersner <gersner@gmail.com> wrote:

I see, interesting.

Please do not top-post. This is not the recommended way of dealing
with threads on this mailing list.

We have lots of unlogged tables, upon a crash we want to create a
feedback/alert that data disappeared.

Not very familiar with the internal structure, but is it possible to
identify if the current table is the INIT_FORKNUM?

Using pg_relation_filepath, you can know the path to a relation file
on disk. So a simple idea would be to use pg_read_binary_file with the
path of the file and the path of the init fork, which is suffixed with
"_init", and then a comparison between both. If the data read is the
same, the relation has been untouched.

Huh, but that's not particularly meaningful, is it? That'll just as well
be the case for a freshly created relation, no?

- Andres

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#6Michael Paquier
michael@paquier.xyz
In reply to: Andres Freund (#5)
Re: Unlogged Crash Detection

On Tue, Aug 29, 2017 at 11:09 PM, Andres Freund <andres@anarazel.de> wrote:

Huh, but that's not particularly meaningful, is it? That'll just as well
be the case for a freshly created relation, no?

I have assumed that the OP has some control on the timing of the
relations, using an event trigger for example. There is no perfect
method I am afraid.
--
Michael

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general