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
3739typedef struct bbsink_copystream
@@ -86,7 +88,6 @@ static void SendCopyOutResponse(void);
8688static void SendCopyDone (void );
8789static void SendXlogRecPtrResult (XLogRecPtr ptr , TimeLineID tli );
8890static void SendTablespaceList (List * tablespaces );
89- static void send_int8_string (StringInfoData * buf , int64 intval );
9091
9192static const bbsink_ops bbsink_copystream_ops = {
9293 .begin_backup = bbsink_copystream_begin_backup ,
@@ -339,10 +340,10 @@ static void
339340SendXlogRecPtrResult (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
383376SendTablespaceList (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