diff --git a/src/backend/replication/logical/logicalfuncs.c b/src/backend/replication/logical/logicalfuncs.c
index 3be5263..039bcb1 100644
--- a/src/backend/replication/logical/logicalfuncs.c
+++ b/src/backend/replication/logical/logicalfuncs.c
@@ -72,6 +72,7 @@ LogicalOutputWrite(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xi
 	Datum		values[3];
 	bool		nulls[3];
 	DecodingOutputState *p;
+	text	   *output;
 
 	/* SQL Datums can only be of a limited length... */
 	if (ctx->out->len > MaxAllocSize - VARHDRSZ)
@@ -93,10 +94,11 @@ LogicalOutputWrite(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xi
 							   false));
 
 	/* ick, but cstring_to_text_with_len works for bytea perfectly fine */
-	values[2] = PointerGetDatum(
-					cstring_to_text_with_len(ctx->out->data, ctx->out->len));
+	output = cstring_to_text_with_len(ctx->out->data, ctx->out->len);
+	values[2] = PointerGetDatum(output);
 
 	tuplestore_putvalues(p->tupstore, p->tupdesc, values, nulls);
+	pfree(output);
 	p->returned_rows++;
 }
 
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index 20bb3b7..a004041 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -1936,7 +1936,7 @@ ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
 		 * store in segment in which it belongs by start lsn, don't split over
 		 * multiple segments tho
 		 */
-		if (fd == -1 || XLByteInSeg(change->lsn, curOpenSegNo))
+		if (fd == -1 || !XLByteInSeg(change->lsn, curOpenSegNo))
 		{
 			XLogRecPtr	recptr;
 
