Skip to content

Commit

Permalink
Fix memory leak while using add-tables and filter-tables
Browse files Browse the repository at this point in the history
This issue was reported in PR #147 by @haizz. The proposed fix was
incomplete (as I pointed out) and I fixed add-tables option and also
format 2 (add-tables and filter-tables).
  • Loading branch information
Euler Taveira committed Feb 8, 2020
1 parent ba214ca commit fbf9505
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions wal2json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,7 @@ pg_decode_change_v1(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
if (list_length(data->filter_tables) > 0)
{
ListCell *lc;
bool skip = false;

foreach(lc, data->filter_tables)
{
Expand All @@ -1169,10 +1170,18 @@ pg_decode_change_v1(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
elog(DEBUG2, "\"%s\".\"%s\" was filtered out",
((t->allschemas) ? "*" : t->schemaname),
((t->alltables) ? "*" : t->tablename));
return;
skip = true;
}
}
}

/* table was found */
if (skip)
{
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
return;
}
}

/* Add tables */
Expand Down Expand Up @@ -1200,7 +1209,11 @@ pg_decode_change_v1(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,

/* table was not found */
if (skip)
{
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
return;
}
}

/* Sanity checks */
Expand Down Expand Up @@ -1790,6 +1803,7 @@ pg_decode_change_v2(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
if (list_length(data->filter_tables) > 0)
{
ListCell *lc;
bool skip = false;

foreach(lc, data->filter_tables)
{
Expand All @@ -1802,10 +1816,18 @@ pg_decode_change_v2(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
elog(DEBUG2, "\"%s\".\"%s\" was filtered out",
((t->allschemas) ? "*" : t->schemaname),
((t->alltables) ? "*" : t->tablename));
return;
skip = true;
}
}
}

/* table was found */
if (skip)
{
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
return;
}
}

/* Add tables */
Expand Down Expand Up @@ -1833,7 +1855,11 @@ pg_decode_change_v2(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,

/* table was not found */
if (skip)
{
MemoryContextSwitchTo(old);
MemoryContextReset(data->context);
return;
}
}

pg_decode_write_change(ctx, txn, relation, change);
Expand Down

0 comments on commit fbf9505

Please sign in to comment.