I think I found a couple of logical decoding issues while writing tests forfailover slots.

Despite the docs' claim that a logical slot will replay data "exactlyonce", a slot's confirmed_lsn can go backwards and the SQL functions canreplay the same data more than once.We don't mark a slot as dirty if onlyits confirmed_lsn is advanced, so it isn't flushed to disk. For failoverslots this means it also doesn't get replicated via WAL. After a mastercrash, or for failover slots after a promote event, the confirmed_lsn willgo backwards. Users of the SQL interface must keep track of the safelylocally flushed slot position themselves and throw the repeated data away.Unlike with the walsender protocol it has no way to ask the server to skipthat data.

Worse, because we don't dirty the slot even a *clean shutdown* causes slotconfirmed_lsn to go backwards. That's a bug IMO. We should force a flush ofall slots at the shutdown checkpoint, whether dirty or not, to address it.

Barring objections I intend to submit a fix to:

- Document that slots can replay data more than once- Force flush of all slots on a shutdown checkpoint

Also, pg_logical_slot_get_changes and its _peek_ variant should have aparam specifying the starting LSN to read and return. If this is lower thanthe restart_lsn but non-null it should ERROR; if it's greater than or equalit should use this position instead of starting at the confirmed_lsn.

Time permitting I'd like to add a pg_logical_slot_confirm function, so youcan aternate _peek_changes and _confirm, making it possible to getwalsender-interface-like behaviour via the SQL interface. Right now youcan't reliably use the SQL interface because _get_changes can succeedlocally and advance the slot but the client can fail to receive all thechanges due to network issues etc. Sure, the SQL interface is meant mainlyfor testing, but especially for !postgresql downstreams I strongly suspectpeople will want to use it for "real" work rather than have to modify eachclient driver to support replication protocol extensions.