@@ -109,7 +109,7 @@ typedef struct ReorderBufferIterTXNEntry
109109XLogRecPtr lsn ;
110110ReorderBufferChange * change ;
111111ReorderBufferTXN * txn ;
112- int fd ;
112+ File fd ;
113113XLogSegNo segno ;
114114}ReorderBufferIterTXNEntry ;
115115
@@ -178,7 +178,8 @@ static void AssertTXNLsnOrder(ReorderBuffer *rb);
178178 * subtransactions
179179 * ---------------------------------------
180180 */
181- static ReorderBufferIterTXNState * ReorderBufferIterTXNInit (ReorderBuffer * rb ,ReorderBufferTXN * txn );
181+ static void ReorderBufferIterTXNInit (ReorderBuffer * rb ,ReorderBufferTXN * txn ,
182+ ReorderBufferIterTXNState * volatile * iter_state );
182183static ReorderBufferChange * ReorderBufferIterTXNNext (ReorderBuffer * rb ,ReorderBufferIterTXNState * state );
183184static void ReorderBufferIterTXNFinish (ReorderBuffer * rb ,
184185ReorderBufferIterTXNState * state );
@@ -194,7 +195,7 @@ static void ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
194195static void ReorderBufferSerializeChange (ReorderBuffer * rb ,ReorderBufferTXN * txn ,
195196int fd ,ReorderBufferChange * change );
196197static Size ReorderBufferRestoreChanges (ReorderBuffer * rb ,ReorderBufferTXN * txn ,
197- int * fd ,XLogSegNo * segno );
198+ File * fd ,XLogSegNo * segno );
198199static void ReorderBufferRestoreChange (ReorderBuffer * rb ,ReorderBufferTXN * txn ,
199200char * change );
200201static void ReorderBufferRestoreCleanup (ReorderBuffer * rb ,ReorderBufferTXN * txn );
@@ -945,15 +946,23 @@ ReorderBufferIterCompare(Datum a, Datum b, void *arg)
945946/*
946947 * Allocate & initialize an iterator which iterates in lsn order over a
947948 * transaction and all its subtransactions.
949+ *
950+ * Note: The iterator state is returned through iter_state parameter rather
951+ * than the function's return value. This is because the state gets cleaned up
952+ * in a PG_CATCH block in the caller, so we want to make sure the caller gets
953+ * back the state even if this function throws an exception.
948954 */
949- static ReorderBufferIterTXNState *
950- ReorderBufferIterTXNInit (ReorderBuffer * rb ,ReorderBufferTXN * txn )
955+ static void
956+ ReorderBufferIterTXNInit (ReorderBuffer * rb ,ReorderBufferTXN * txn ,
957+ ReorderBufferIterTXNState * volatile * iter_state )
951958{
952959Size nr_txns = 0 ;
953960ReorderBufferIterTXNState * state ;
954961dlist_iter cur_txn_i ;
955962int32 off ;
956963
964+ * iter_state = NULL ;
965+
957966/*
958967 * Calculate the size of our heap: one element for every transaction that
959968 * contains changes. (Besides the transactions already in the reorder
@@ -997,6 +1006,9 @@ ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn)
9971006ReorderBufferIterCompare ,
9981007state );
9991008
1009+ /* Now that the state fields are initialized, it is safe to return it. */
1010+ * iter_state = state ;
1011+
10001012/*
10011013 * Now insert items into the binary heap, in an unordered fashion. (We
10021014 * will run a heap assembly step at the end; this is more efficient.)
@@ -1059,8 +1071,6 @@ ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn)
10591071
10601072/* assemble a valid binary heap */
10611073binaryheap_build (state -> heap );
1062-
1063- return state ;
10641074}
10651075
10661076/*
@@ -1164,7 +1174,7 @@ ReorderBufferIterTXNFinish(ReorderBuffer *rb,
11641174for (off = 0 ;off < state -> nr_txns ;off ++ )
11651175{
11661176if (state -> entries [off ].fd != -1 )
1167- CloseTransientFile (state -> entries [off ].fd );
1177+ FileClose (state -> entries [off ].fd );
11681178}
11691179
11701180/* free memory we might have "leaked" in the last *Next call */
@@ -1500,7 +1510,7 @@ ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid,
15001510
15011511rb -> begin (rb ,txn );
15021512
1503- iterstate = ReorderBufferIterTXNInit (rb ,txn );
1513+ ReorderBufferIterTXNInit (rb ,txn , & iterstate );
15041514while ((change = ReorderBufferIterTXNNext (rb ,iterstate ))!= NULL )
15051515{
15061516Relation relation = NULL ;
@@ -2517,7 +2527,7 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
25172527 */
25182528static Size
25192529ReorderBufferRestoreChanges (ReorderBuffer * rb ,ReorderBufferTXN * txn ,
2520- int * fd ,XLogSegNo * segno )
2530+ File * fd ,XLogSegNo * segno )
25212531{
25222532Size restored = 0 ;
25232533XLogSegNo last_segno ;
@@ -2562,7 +2572,7 @@ ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
25622572ReorderBufferSerializedPath (path ,MyReplicationSlot ,txn -> xid ,
25632573* segno );
25642574
2565- * fd = OpenTransientFile (path ,O_RDONLY |PG_BINARY );
2575+ * fd = PathNameOpenFile (path ,O_RDONLY |PG_BINARY );
25662576if (* fd < 0 && errno == ENOENT )
25672577{
25682578* fd = -1 ;
@@ -2582,14 +2592,13 @@ ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
25822592 * end of this file.
25832593 */
25842594ReorderBufferSerializeReserve (rb ,sizeof (ReorderBufferDiskChange ));
2585- pgstat_report_wait_start (WAIT_EVENT_REORDER_BUFFER_READ );
2586- readBytes = read (* fd ,rb -> outbuf ,sizeof (ReorderBufferDiskChange ));
2587- pgstat_report_wait_end ();
2595+ readBytes = FileRead (* fd ,rb -> outbuf ,sizeof (ReorderBufferDiskChange ),
2596+ WAIT_EVENT_REORDER_BUFFER_READ );
25882597
25892598/* eof */
25902599if (readBytes == 0 )
25912600{
2592- CloseTransientFile (* fd );
2601+ FileClose (* fd );
25932602* fd = -1 ;
25942603(* segno )++ ;
25952604continue ;
@@ -2611,10 +2620,10 @@ ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
26112620sizeof (ReorderBufferDiskChange )+ ondisk -> size );
26122621ondisk = (ReorderBufferDiskChange * )rb -> outbuf ;
26132622
2614- pgstat_report_wait_start ( WAIT_EVENT_REORDER_BUFFER_READ );
2615- readBytes = read ( * fd , rb -> outbuf + sizeof (ReorderBufferDiskChange ),
2616- ondisk -> size - sizeof (ReorderBufferDiskChange ));
2617- pgstat_report_wait_end ( );
2623+ readBytes = FileRead ( * fd ,
2624+ rb -> outbuf + sizeof (ReorderBufferDiskChange ),
2625+ ondisk -> size - sizeof (ReorderBufferDiskChange ),
2626+ WAIT_EVENT_REORDER_BUFFER_READ );
26182627
26192628if (readBytes < 0 )
26202629ereport (ERROR ,