@@ -164,16 +164,6 @@ static void pg_decode_shutdown(LogicalDecodingContext *ctx) {
164
164
MemoryContextDelete (data -> context );
165
165
}
166
166
167
- /* BEGIN callback */
168
- static void pg_decode_begin_txn (LogicalDecodingContext * ctx ,
169
- ReorderBufferTXN * txn ) {
170
- }
171
-
172
- /* COMMIT callback */
173
- static void pg_decode_commit_txn (LogicalDecodingContext * ctx ,
174
- ReorderBufferTXN * txn , XLogRecPtr commit_lsn ) {
175
- }
176
-
177
167
/* print tuple datums (only used for debug-mode) */
178
168
static void print_tuple_datums (StringInfo out , Decoderbufs__DatumMessage * * tup ,
179
169
size_t n ) {
@@ -491,6 +481,82 @@ static void add_metadata_to_msg(Decoderbufs__TypeInfo **tmsg,
491
481
}
492
482
}
493
483
484
+ /* BEGIN callback */
485
+ static void pg_decode_begin_txn (LogicalDecodingContext * ctx ,
486
+ ReorderBufferTXN * txn ) {
487
+
488
+ DecoderData * data ;
489
+ MemoryContext old ;
490
+ Decoderbufs__RowMessage rmsg = DECODERBUFS__ROW_MESSAGE__INIT ;
491
+ elog (DEBUG1 , "Entering begin callback" );
492
+
493
+
494
+ /* Avoid leaking memory by using and resetting our own context */
495
+ data = ctx -> output_plugin_private ;
496
+ old = MemoryContextSwitchTo (data -> context );
497
+
498
+ rmsg .op = DECODERBUFS__OP__BEGIN ;
499
+ rmsg .has_op = true;
500
+ rmsg .transaction_id = txn -> xid ;
501
+ rmsg .has_transaction_id = true;
502
+ rmsg .commit_time = TIMESTAMPTZ_TO_USEC_SINCE_EPOCH (txn -> commit_time );
503
+ rmsg .has_commit_time = true;
504
+
505
+ /* write msg */
506
+ OutputPluginPrepareWrite (ctx , true);
507
+ if (data -> debug_mode ) {
508
+ print_row_msg (ctx -> out , & rmsg );
509
+ } else {
510
+ size_t psize = decoderbufs__row_message__get_packed_size (& rmsg );
511
+ void * packed = palloc (psize );
512
+ size_t ssize = decoderbufs__row_message__pack (& rmsg , packed );
513
+ appendBinaryStringInfo (ctx -> out , packed , ssize );
514
+ }
515
+ OutputPluginWrite (ctx , true);
516
+
517
+ /* Cleanup, freeing memory */
518
+ MemoryContextSwitchTo (old );
519
+ MemoryContextReset (data -> context );
520
+ }
521
+
522
+ /* COMMIT callback */
523
+ static void pg_decode_commit_txn (LogicalDecodingContext * ctx ,
524
+ ReorderBufferTXN * txn , XLogRecPtr commit_lsn ) {
525
+
526
+ DecoderData * data ;
527
+ MemoryContext old ;
528
+ Decoderbufs__RowMessage rmsg = DECODERBUFS__ROW_MESSAGE__INIT ;
529
+ elog (DEBUG1 , "Entering commit callback" );
530
+
531
+
532
+ /* Avoid leaking memory by using and resetting our own context */
533
+ data = ctx -> output_plugin_private ;
534
+ old = MemoryContextSwitchTo (data -> context );
535
+
536
+ rmsg .op = DECODERBUFS__OP__COMMIT ;
537
+ rmsg .has_op = true;
538
+ rmsg .transaction_id = txn -> xid ;
539
+ rmsg .has_transaction_id = true;
540
+ rmsg .commit_time = TIMESTAMPTZ_TO_USEC_SINCE_EPOCH (txn -> commit_time );
541
+ rmsg .has_commit_time = true;
542
+
543
+ /* write msg */
544
+ OutputPluginPrepareWrite (ctx , true);
545
+ if (data -> debug_mode ) {
546
+ print_row_msg (ctx -> out , & rmsg );
547
+ } else {
548
+ size_t psize = decoderbufs__row_message__get_packed_size (& rmsg );
549
+ void * packed = palloc (psize );
550
+ size_t ssize = decoderbufs__row_message__pack (& rmsg , packed );
551
+ appendBinaryStringInfo (ctx -> out , packed , ssize );
552
+ }
553
+ OutputPluginWrite (ctx , true);
554
+
555
+ /* Cleanup, freeing memory */
556
+ MemoryContextSwitchTo (old );
557
+ MemoryContextReset (data -> context );
558
+ }
559
+
494
560
/* callback for individual changed tuples */
495
561
static void pg_decode_change (LogicalDecodingContext * ctx , ReorderBufferTXN * txn ,
496
562
Relation relation , ReorderBufferChange * change ) {
0 commit comments