Skip to content

Commit 2ce648f

Browse files
committed
Refactor sending of RowDescription messages in replication protocol
Some routines open-coded the construction of RowDescription messages. Instead, we have support for doing this using tuple descriptors and DestRemoteSimple, so use that instead. Reviewed-by: Nathan Bossart <nathandbossart@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/7e4fdbdc-699c-4cd0-115d-fb78a957fc22@enterprisedb.com
1 parent f10a025 commit 2ce648f

File tree

3 files changed

+40
-72
lines changed

3 files changed

+40
-72
lines changed

src/backend/access/common/tupdesc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,15 @@ TupleDescInitBuiltinEntry(TupleDesc desc,
739739
att->attcollation = InvalidOid;
740740
break;
741741

742+
case OIDOID:
743+
att->attlen = 4;
744+
att->attbyval = true;
745+
att->attalign = TYPALIGN_INT;
746+
att->attstorage = TYPSTORAGE_PLAIN;
747+
att->attcompression = InvalidCompressionMethod;
748+
att->attcollation = InvalidOid;
749+
break;
750+
742751
default:
743752
elog(ERROR, "unsupported type %u", oidtypeid);
744753
}

src/backend/replication/basebackup_copy.c

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
*/
2626
#include "postgres.h"
2727

28+
#include "access/tupdesc.h"
2829
#include "catalog/pg_type_d.h"
2930
#include "libpq/libpq.h"
3031
#include "libpq/pqformat.h"
3132
#include "replication/basebackup.h"
3233
#include "replication/basebackup_sink.h"
34+
#include "tcop/dest.h"
3335
#include "utils/timestamp.h"
3436

3537
typedef struct bbsink_copystream
@@ -336,35 +338,24 @@ SendCopyDone(void)
336338
static void
337339
SendXlogRecPtrResult(XLogRecPtr ptr, TimeLineID tli)
338340
{
341+
DestReceiver *dest;
342+
TupleDesc tupdesc;
339343
StringInfoData buf;
340344
char str[MAXFNAMELEN];
341345
Size len;
342346

343-
pq_beginmessage(&buf, 'T'); /* RowDescription */
344-
pq_sendint16(&buf, 2); /* 2 fields */
345-
346-
/* Field headers */
347-
pq_sendstring(&buf, "recptr");
348-
pq_sendint32(&buf, 0); /* table oid */
349-
pq_sendint16(&buf, 0); /* attnum */
350-
pq_sendint32(&buf, TEXTOID); /* type oid */
351-
pq_sendint16(&buf, -1);
352-
pq_sendint32(&buf, 0);
353-
pq_sendint16(&buf, 0);
354-
355-
pq_sendstring(&buf, "tli");
356-
pq_sendint32(&buf, 0); /* table oid */
357-
pq_sendint16(&buf, 0); /* attnum */
347+
dest = CreateDestReceiver(DestRemoteSimple);
358348

349+
tupdesc = CreateTemplateTupleDesc(2);
350+
TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 1, "recptr", TEXTOID, -1, 0);
359351
/*
360352
* int8 may seem like a surprising data type for this, but in theory int4
361353
* would not be wide enough for this, as TimeLineID is unsigned.
362354
*/
363-
pq_sendint32(&buf, INT8OID); /* type oid */
364-
pq_sendint16(&buf, 8);
365-
pq_sendint32(&buf, 0);
366-
pq_sendint16(&buf, 0);
367-
pq_endmessage(&buf);
355+
TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 2, "tli", INT8OID, -1, 0);
356+
357+
/* send RowDescription */
358+
dest->rStartup(dest, CMD_SELECT, tupdesc);
368359

369360
/* Data row */
370361
pq_beginmessage(&buf, 'D');
@@ -391,41 +382,22 @@ SendXlogRecPtrResult(XLogRecPtr ptr, TimeLineID tli)
391382
static void
392383
SendTablespaceList(List *tablespaces)
393384
{
385+
DestReceiver *dest;
386+
TupleDesc tupdesc;
394387
StringInfoData buf;
395388
ListCell *lc;
396389

397-
/* Construct and send the directory information */
398-
pq_beginmessage(&buf, 'T'); /* RowDescription */
399-
pq_sendint16(&buf, 3); /* 3 fields */
400-
401-
/* First field - spcoid */
402-
pq_sendstring(&buf, "spcoid");
403-
pq_sendint32(&buf, 0); /* table oid */
404-
pq_sendint16(&buf, 0); /* attnum */
405-
pq_sendint32(&buf, OIDOID); /* type oid */
406-
pq_sendint16(&buf, 4); /* typlen */
407-
pq_sendint32(&buf, 0); /* typmod */
408-
pq_sendint16(&buf, 0); /* format code */
409-
410-
/* Second field - spclocation */
411-
pq_sendstring(&buf, "spclocation");
412-
pq_sendint32(&buf, 0);
413-
pq_sendint16(&buf, 0);
414-
pq_sendint32(&buf, TEXTOID);
415-
pq_sendint16(&buf, -1);
416-
pq_sendint32(&buf, 0);
417-
pq_sendint16(&buf, 0);
418-
419-
/* Third field - size */
420-
pq_sendstring(&buf, "size");
421-
pq_sendint32(&buf, 0);
422-
pq_sendint16(&buf, 0);
423-
pq_sendint32(&buf, INT8OID);
424-
pq_sendint16(&buf, 8);
425-
pq_sendint32(&buf, 0);
426-
pq_sendint16(&buf, 0);
427-
pq_endmessage(&buf);
390+
dest = CreateDestReceiver(DestRemoteSimple);
391+
392+
tupdesc = CreateTemplateTupleDesc(3);
393+
TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 1, "spcoid", OIDOID, -1, 0);
394+
TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 2, "spclocation", TEXTOID, -1, 0);
395+
TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 3, "size", INT8OID, -1, 0);
428396

397+
/* send RowDescription */
398+
dest->rStartup(dest, CMD_SELECT, tupdesc);
399+
400+
/* Construct and send the directory information */
429401
foreach(lc, tablespaces)
430402
{
431403
tablespaceinfo *ti = lfirst(lc);

src/backend/replication/walsender.c

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,8 @@ ReadReplicationSlot(ReadReplicationSlotCmd *cmd)
579579
static void
580580
SendTimeLineHistory(TimeLineHistoryCmd *cmd)
581581
{
582+
DestReceiver *dest;
583+
TupleDesc tupdesc;
582584
StringInfoData buf;
583585
char histfname[MAXFNAMELEN];
584586
char path[MAXPGPATH];
@@ -587,36 +589,21 @@ SendTimeLineHistory(TimeLineHistoryCmd *cmd)
587589
off_t bytesleft;
588590
Size len;
589591

592+
dest = CreateDestReceiver(DestRemoteSimple);
593+
590594
/*
591595
* Reply with a result set with one row, and two columns. The first col is
592596
* the name of the history file, 2nd is the contents.
593597
*/
598+
tupdesc = CreateTemplateTupleDesc(2);
599+
TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 1, "filename", TEXTOID, -1, 0);
600+
TupleDescInitBuiltinEntry(tupdesc, (AttrNumber) 2, "content", TEXTOID, -1, 0);
594601

595602
TLHistoryFileName(histfname, cmd->timeline);
596603
TLHistoryFilePath(path, cmd->timeline);
597604

598605
/* Send a RowDescription message */
599-
pq_beginmessage(&buf, 'T');
600-
pq_sendint16(&buf, 2); /* 2 fields */
601-
602-
/* first field */
603-
pq_sendstring(&buf, "filename"); /* col name */
604-
pq_sendint32(&buf, 0); /* table oid */
605-
pq_sendint16(&buf, 0); /* attnum */
606-
pq_sendint32(&buf, TEXTOID); /* type oid */
607-
pq_sendint16(&buf, -1); /* typlen */
608-
pq_sendint32(&buf, 0); /* typmod */
609-
pq_sendint16(&buf, 0); /* format code */
610-
611-
/* second field */
612-
pq_sendstring(&buf, "content"); /* col name */
613-
pq_sendint32(&buf, 0); /* table oid */
614-
pq_sendint16(&buf, 0); /* attnum */
615-
pq_sendint32(&buf, TEXTOID); /* type oid */
616-
pq_sendint16(&buf, -1); /* typlen */
617-
pq_sendint32(&buf, 0); /* typmod */
618-
pq_sendint16(&buf, 0); /* format code */
619-
pq_endmessage(&buf);
606+
dest->rStartup(dest, CMD_SELECT, tupdesc);
620607

621608
/* Send a DataRow message */
622609
pq_beginmessage(&buf, 'D');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy