Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 16d52fc

Browse files
committed
Refactor sending of DataRow messages in replication protocol
Some routines open-coded the construction of DataRow messages. Use TupOutputState struct and associated functions instead, which was already done in some places. SendTimeLineHistory() is a bit more complicated and isn't converted by this. Reviewed-by: Nathan Bossart <nathandbossart@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/7e4fdbdc-699c-4cd0-115d-fb78a957fc22@enterprisedb.com
1 parent b55f62a commit 16d52fc

File tree

2 files changed

+33
-47
lines changed

2 files changed

+33
-47
lines changed

src/backend/access/common/printsimple.c

+11
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ printsimple(TupleTableSlot *slot, DestReceiver *self)
121121
}
122122
break;
123123

124+
case OIDOID:
125+
{
126+
Oid num = ObjectIdGetDatum(value);
127+
char str[10]; /* 10 digits */
128+
int len;
129+
130+
len = pg_ultoa_n(num, str);
131+
pq_sendcountedtext(&buf, str, len, false);
132+
}
133+
break;
134+
124135
default:
125136
elog(ERROR, "unsupported type OID: %u", attr->atttypid);
126137
}

src/backend/replication/basebackup_copy.c

+22-47
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727

2828
#include "access/tupdesc.h"
2929
#include "catalog/pg_type_d.h"
30+
#include "executor/executor.h"
3031
#include "libpq/libpq.h"
3132
#include "libpq/pqformat.h"
3233
#include "replication/basebackup.h"
3334
#include "replication/basebackup_sink.h"
3435
#include "tcop/dest.h"
36+
#include "utils/builtins.h"
3537
#include "utils/timestamp.h"
3638

3739
typedef struct bbsink_copystream
@@ -86,7 +88,6 @@ static void SendCopyOutResponse(void);
8688
static void SendCopyDone(void);
8789
static void SendXlogRecPtrResult(XLogRecPtr ptr, TimeLineID tli);
8890
static void SendTablespaceList(List *tablespaces);
89-
static void send_int8_string(StringInfoData *buf, int64 intval);
9091

9192
static const bbsink_ops bbsink_copystream_ops = {
9293
.begin_backup = bbsink_copystream_begin_backup,
@@ -339,10 +340,10 @@ static void
339340
SendXlogRecPtrResult(XLogRecPtr ptr, TimeLineID tli)
340341
{
341342
DestReceiver *dest;
343+
TupOutputState *tstate;
342344
TupleDesc tupdesc;
343-
StringInfoData buf;
344-
char str[MAXFNAMELEN];
345-
Size len;
345+
Datum values[2];
346+
bool nulls[2] = {0};
346347

347348
dest = CreateDestReceiver(DestRemoteSimple);
348349

@@ -355,22 +356,14 @@ SendXlogRecPtrResult(XLogRecPtr ptr, TimeLineID tli)
355356
TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 2, "tli", INT8OID, -1, 0);
356357

357358
/* send RowDescription */
358-
dest->rStartup(dest, CMD_SELECT, tupdesc);
359+
tstate = begin_tup_output_tupdesc(dest, tupdesc, &TTSOpsVirtual);
359360

360361
/* Data row */
361-
pq_beginmessage(&buf, 'D');
362-
pq_sendint16(&buf, 2); /* number of columns */
363-
364-
len = snprintf(str, sizeof(str),
365-
"%X/%X", LSN_FORMAT_ARGS(ptr));
366-
pq_sendint32(&buf, len);
367-
pq_sendbytes(&buf, str, len);
362+
values[0]= CStringGetTextDatum(psprintf("%X/%X", LSN_FORMAT_ARGS(ptr)));
363+
values[1] = Int64GetDatum(tli);
364+
do_tup_output(tstate, values, nulls);
368365

369-
len = snprintf(str, sizeof(str), "%u", tli);
370-
pq_sendint32(&buf, len);
371-
pq_sendbytes(&buf, str, len);
372-
373-
pq_endmessage(&buf);
366+
end_tup_output(tstate);
374367

375368
/* Send a CommandComplete message */
376369
pq_puttextmessage('C', "SELECT");
@@ -383,8 +376,8 @@ static void
383376
SendTablespaceList(List *tablespaces)
384377
{
385378
DestReceiver *dest;
379+
TupOutputState *tstate;
386380
TupleDesc tupdesc;
387-
StringInfoData buf;
388381
ListCell *lc;
389382

390383
dest = CreateDestReceiver(DestRemoteSimple);
@@ -395,51 +388,33 @@ SendTablespaceList(List *tablespaces)
395388
TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 3, "size", INT8OID, -1, 0);
396389

397390
/* send RowDescription */
398-
dest->rStartup(dest, CMD_SELECT, tupdesc);
391+
tstate = begin_tup_output_tupdesc(dest, tupdesc, &TTSOpsVirtual);
399392

400393
/* Construct and send the directory information */
401394
foreach(lc, tablespaces)
402395
{
403396
tablespaceinfo *ti = lfirst(lc);
397+
Datum values[3];
398+
bool nulls[3] = {0};
404399

405400
/* Send one datarow message */
406-
pq_beginmessage(&buf, 'D');
407-
pq_sendint16(&buf, 3); /* number of columns */
408401
if (ti->path == NULL)
409402
{
410-
pq_sendint32(&buf, -1); /* Length = -1 ==> NULL */
411-
pq_sendint32(&buf, -1);
403+
nulls[0] = true;
404+
nulls[1] = true;
412405
}
413406
else
414407
{
415-
Size len;
416-
417-
len = strlen(ti->oid);
418-
pq_sendint32(&buf, len);
419-
pq_sendbytes(&buf, ti->oid, len);
420-
421-
len = strlen(ti->path);
422-
pq_sendint32(&buf, len);
423-
pq_sendbytes(&buf, ti->path, len);
408+
values[0] = ObjectIdGetDatum(strtoul(ti->oid, NULL, 10));
409+
values[1] = CStringGetTextDatum(ti->path);
424410
}
425411
if (ti->size >= 0)
426-
send_int8_string(&buf, ti->size / 1024);
412+
values[2] = Int64GetDatum(ti->size / 1024);
427413
else
428-
pq_sendint32(&buf, -1); /* NULL */
414+
nulls[2] = true;
429415

430-
pq_endmessage(&buf);
416+
do_tup_output(tstate, values, nulls);
431417
}
432-
}
433-
434-
/*
435-
* Send a 64-bit integer as a string via the wire protocol.
436-
*/
437-
static void
438-
send_int8_string(StringInfoData *buf, int64 intval)
439-
{
440-
char is[32];
441418

442-
sprintf(is, INT64_FORMAT, intval);
443-
pq_sendint32(buf, strlen(is));
444-
pq_sendbytes(buf, is, strlen(is));
419+
end_tup_output(tstate);
445420
}

0 commit comments

Comments
 (0)