Skip to content

Commit

Permalink
Store number of tuples in WalRcvExecResult
Browse files Browse the repository at this point in the history
It seems to be a useful information while allocating memory for queries
that returns more than one row. It reduces memory allocation
for initial table synchronization.

While in it, since we have the number of columns, allocate only nfields
for cstrs instead of MaxTupleAttributeNumber.
  • Loading branch information
Euler Taveira committed Aug 23, 2019
1 parent 44fe998 commit 94f2e70
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ libpqrcv_processTuples(PGresult *pgres, WalRcvExecResult *walres,
errdetail("Expected %d fields, got %d fields.",
nRetTypes, nfields)));

walres->ntuples = PQntuples(pgres);
walres->tuplestore = tuplestore_begin_heap(true, false, work_mem);

/* Create tuple descriptor corresponding to expected result. */
Expand All @@ -863,7 +864,7 @@ libpqrcv_processTuples(PGresult *pgres, WalRcvExecResult *walres,
attinmeta = TupleDescGetAttInMetadata(walres->tupledesc);

/* No point in doing more here if there were no tuples returned. */
if (PQntuples(pgres) == 0)
if (walres->ntuples == 0)
return;

/* Create temporary context for local allocations. */
Expand All @@ -872,9 +873,9 @@ libpqrcv_processTuples(PGresult *pgres, WalRcvExecResult *walres,
ALLOCSET_DEFAULT_SIZES);

/* Process returned rows. */
for (tupn = 0; tupn < PQntuples(pgres); tupn++)
for (tupn = 0; tupn < walres->ntuples; tupn++)
{
char *cstrs[MaxTupleAttributeNumber];
char *cstrs[nfields];

ProcessWalRcvInterrupts();

Expand Down
5 changes: 2 additions & 3 deletions src/backend/replication/logical/tablesync.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,8 @@ fetch_remote_table_info(char *nspname, char *relname,
(errmsg("could not fetch table info for table \"%s.%s\": %s",
nspname, relname, res->err)));

/* We don't know the number of rows coming, so allocate enough space. */
lrel->attnames = palloc0(MaxTupleAttributeNumber * sizeof(char *));
lrel->atttyps = palloc0(MaxTupleAttributeNumber * sizeof(Oid));
lrel->attnames = palloc0(res->ntuples * sizeof(char *));
lrel->atttyps = palloc0(res->ntuples * sizeof(Oid));
lrel->attkeys = NULL;

natt = 0;
Expand Down
1 change: 1 addition & 0 deletions src/include/replication/walreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ typedef struct WalRcvExecResult
char *err;
Tuplestorestate *tuplestore;
TupleDesc tupledesc;
int ntuples;
} WalRcvExecResult;

/* libpqwalreceiver hooks */
Expand Down

0 comments on commit 94f2e70

Please sign in to comment.