From e45bdefc87cdc328ff55bccbc20e5f8ed7563c6d Mon Sep 17 00:00:00 2001 From: Kris Wehner Date: Wed, 9 Nov 2016 10:47:06 -0800 Subject: [PATCH] Add comment and dry up the prepare write calls --- pgoutput/output_proto.c | 7 ++++++- pgoutput/transicator_output.c | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pgoutput/output_proto.c b/pgoutput/output_proto.c index 7a60562..ff68fbf 100644 --- a/pgoutput/output_proto.c +++ b/pgoutput/output_proto.c @@ -88,6 +88,12 @@ static void tuple_to_proto( col = (Common__ColumnPb*)palloc(sizeof(Common__ColumnPb)); common__column_pb__init(col); + // NOTE: You cannot use natt here as the array index, as it's counting + // the number of entries in tupdesc, but we've previously computed and + // allocated the cols array based on a count that doesn't include the + // attr's that are dropped or have a negative attnum. If you use + // natt to index into cols, you risk jumping off the end of the array + // and causing a pg crash. cols[colnum++] = col; typ = attr->atttypid; @@ -210,6 +216,5 @@ void transicatorOutputChangeProto( pack = (uint8_t*)palloc(sizeof(uint8_t) * packSize); common__change_pb__pack(&pb, pack); - OutputPluginPrepareWrite(ctx, true); appendBinaryStringInfo(ctx->out, (char*)pack, packSize); } diff --git a/pgoutput/transicator_output.c b/pgoutput/transicator_output.c index 19d75f8..bdc7aca 100644 --- a/pgoutput/transicator_output.c +++ b/pgoutput/transicator_output.c @@ -107,6 +107,7 @@ static void outputChange( /* Switch to our private memory context so that we will not leak. */ oldMemCtx = MemoryContextSwitchTo(state->memCtx); + OutputPluginPrepareWrite(ctx, true); if (state->isBinary) { transicatorOutputChangeProto(ctx, txn, relation, change, state); } else {