Skip to content

Commit 8baab62

Browse files
DuncanSandsgunnarmorling
authored andcommitted
DBZ-496 The call to MemoryContextReset frees all allocated memory, which is both more
efficient and more reliable than individually freeing each allocated object.
1 parent e375003 commit 8baab62

File tree

1 file changed

+6
-83
lines changed

1 file changed

+6
-83
lines changed

src/decoderbufs.c

Lines changed: 6 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -195,76 +195,6 @@ static void pg_decode_commit_txn(LogicalDecodingContext *ctx,
195195
ReorderBufferTXN *txn, XLogRecPtr commit_lsn) {
196196
}
197197

198-
/* convenience method to free up sub-messages */
199-
static void row_message_destroy(Decoderbufs__RowMessage *msg) {
200-
if (!msg) {
201-
return;
202-
}
203-
204-
if (msg->table) {
205-
pfree(msg->table);
206-
}
207-
208-
if (msg->n_new_tuple > 0) {
209-
for (int i = 0; i < msg->n_new_tuple; i++) {
210-
if (msg->new_tuple[i]) {
211-
switch (msg->new_tuple[i]->datum_case) {
212-
case DECODERBUFS__DATUM_MESSAGE__DATUM_DATUM_STRING:
213-
if (msg->new_tuple[i]->datum_string) {
214-
pfree(msg->new_tuple[i]->datum_string);
215-
}
216-
break;
217-
case DECODERBUFS__DATUM_MESSAGE__DATUM_DATUM_BYTES:
218-
if (msg->new_tuple[i]->datum_bytes.data) {
219-
pfree(msg->new_tuple[i]->datum_bytes.data);
220-
msg->new_tuple[i]->datum_bytes.data = NULL;
221-
msg->new_tuple[i]->datum_bytes.len = 0;
222-
}
223-
break;
224-
case DECODERBUFS__DATUM_MESSAGE__DATUM_DATUM_POINT:
225-
if (msg->new_tuple[i]->datum_point) {
226-
pfree(msg->new_tuple[i]->datum_point);
227-
}
228-
break;
229-
default:
230-
break;
231-
}
232-
pfree(msg->new_tuple[i]);
233-
}
234-
}
235-
pfree(msg->new_tuple);
236-
}
237-
if (msg->n_old_tuple > 0) {
238-
for (int i = 0; i < msg->n_old_tuple; i++) {
239-
if (msg->old_tuple[i]) {
240-
switch (msg->old_tuple[i]->datum_case) {
241-
case DECODERBUFS__DATUM_MESSAGE__DATUM_DATUM_STRING:
242-
if (msg->old_tuple[i]->datum_string) {
243-
pfree(msg->old_tuple[i]->datum_string);
244-
}
245-
break;
246-
case DECODERBUFS__DATUM_MESSAGE__DATUM_DATUM_BYTES:
247-
if (msg->old_tuple[i]->datum_bytes.data) {
248-
pfree(msg->old_tuple[i]->datum_bytes.data);
249-
msg->old_tuple[i]->datum_bytes.data = NULL;
250-
msg->old_tuple[i]->datum_bytes.len = 0;
251-
}
252-
break;
253-
case DECODERBUFS__DATUM_MESSAGE__DATUM_DATUM_POINT:
254-
if (msg->old_tuple[i]->datum_point) {
255-
pfree(msg->old_tuple[i]->datum_point);
256-
}
257-
break;
258-
default:
259-
break;
260-
}
261-
pfree(msg->old_tuple[i]);
262-
}
263-
}
264-
pfree(msg->old_tuple);
265-
}
266-
}
267-
268198
/* print tuple datums (only used for debug-mode) */
269199
static void print_tuple_datums(StringInfo out, Decoderbufs__DatumMessage **tup,
270200
size_t n) {
@@ -363,8 +293,6 @@ static double numeric_to_double_no_overflow(Numeric num) {
363293
tmp)));
364294
}
365295

366-
pfree(tmp);
367-
368296
return val;
369297
}
370298

@@ -676,17 +604,16 @@ static void pg_decode_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
676604
TupleDesc tupdesc;
677605
Decoderbufs__RowMessage rmsg = DECODERBUFS__ROW_MESSAGE__INIT;
678606

679-
elog(DEBUG1, "Entering decode_change callback");
607+
elog(DEBUG1, "Entering decode_change callback");
608+
609+
/* Avoid leaking memory by using and resetting our own context */
610+
data = ctx->output_plugin_private;
611+
old = MemoryContextSwitchTo(data->context);
680612

681613
replident = relation->rd_rel->relreplident;
682614

683615
class_form = RelationGetForm(relation);
684616

685-
data = ctx->output_plugin_private;
686-
687-
/* Avoid leaking memory by using and resetting our own context */
688-
old = MemoryContextSwitchTo(data->context);
689-
690617
RelationGetIndexList(relation);
691618
is_rel_non_selective = (replident == REPLICA_IDENTITY_NOTHING ||
692619
(replident == REPLICA_IDENTITY_DEFAULT &&
@@ -790,14 +717,10 @@ static void pg_decode_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
790717
void *packed = palloc(psize);
791718
size_t ssize = decoderbufs__row_message__pack(&rmsg, packed);
792719
appendBinaryStringInfo(ctx->out, packed, ssize);
793-
/* free packed buffer */
794-
pfree(packed);
795720
}
796721
OutputPluginWrite(ctx, true);
797722

798-
/* cleanup msg */
799-
row_message_destroy(&rmsg);
800-
723+
/* Cleanup, freeing memory */
801724
MemoryContextSwitchTo(old);
802725
MemoryContextReset(data->context);
803726
}

0 commit comments

Comments
 (0)