LCOV - code coverage report
Current view: top level - src/backend/tcop - dest.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 57 68 83.8 %
Date: 2020-05-25 05:06:35 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * dest.c
       4             :  *    support for communication destinations
       5             :  *
       6             :  *
       7             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/tcop/dest.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : /*
      16             :  *   INTERFACE ROUTINES
      17             :  *      BeginCommand - initialize the destination at start of command
      18             :  *      CreateDestReceiver - create tuple receiver object for destination
      19             :  *      EndCommand - clean up the destination at end of command
      20             :  *      NullCommand - tell dest that an empty query string was recognized
      21             :  *      ReadyForQuery - tell dest that we are ready for a new query
      22             :  *
      23             :  *   NOTES
      24             :  *      These routines do the appropriate work before and after
      25             :  *      tuples are returned by a query to keep the backend and the
      26             :  *      "destination" portals synchronized.
      27             :  */
      28             : 
      29             : #include "postgres.h"
      30             : 
      31             : #include "access/printsimple.h"
      32             : #include "access/printtup.h"
      33             : #include "access/xact.h"
      34             : #include "commands/copy.h"
      35             : #include "commands/createas.h"
      36             : #include "commands/matview.h"
      37             : #include "executor/functions.h"
      38             : #include "executor/tqueue.h"
      39             : #include "executor/tstoreReceiver.h"
      40             : #include "libpq/libpq.h"
      41             : #include "libpq/pqformat.h"
      42             : #include "utils/portal.h"
      43             : 
      44             : 
      45             : /* ----------------
      46             :  *      dummy DestReceiver functions
      47             :  * ----------------
      48             :  */
      49             : static bool
      50      803556 : donothingReceive(TupleTableSlot *slot, DestReceiver *self)
      51             : {
      52      803556 :     return true;
      53             : }
      54             : 
      55             : static void
      56       41304 : donothingStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
      57             : {
      58       41304 : }
      59             : 
      60             : static void
      61      317270 : donothingCleanup(DestReceiver *self)
      62             : {
      63             :     /* this is used for both shutdown and destroy methods */
      64      317270 : }
      65             : 
      66             : /* ----------------
      67             :  *      static DestReceiver structs for dest types needing no local state
      68             :  * ----------------
      69             :  */
      70             : static const DestReceiver donothingDR = {
      71             :     donothingReceive, donothingStartup, donothingCleanup, donothingCleanup,
      72             :     DestNone
      73             : };
      74             : 
      75             : static const DestReceiver debugtupDR = {
      76             :     debugtup, debugStartup, donothingCleanup, donothingCleanup,
      77             :     DestDebug
      78             : };
      79             : 
      80             : static const DestReceiver printsimpleDR = {
      81             :     printsimple, printsimple_startup, donothingCleanup, donothingCleanup,
      82             :     DestRemoteSimple
      83             : };
      84             : 
      85             : static const DestReceiver spi_printtupDR = {
      86             :     spi_printtup, spi_dest_startup, donothingCleanup, donothingCleanup,
      87             :     DestSPI
      88             : };
      89             : 
      90             : /*
      91             :  * Globally available receiver for DestNone.
      92             :  *
      93             :  * It's ok to cast the constness away as any modification of the none receiver
      94             :  * would be a bug (which gets easier to catch this way).
      95             :  */
      96             : DestReceiver *None_Receiver = (DestReceiver *) &donothingDR;
      97             : 
      98             : /* ----------------
      99             :  *      BeginCommand - initialize the destination at start of command
     100             :  * ----------------
     101             :  */
     102             : void
     103      538656 : BeginCommand(CommandTag commandTag, CommandDest dest)
     104             : {
     105             :     /* Nothing to do at present */
     106      538656 : }
     107             : 
     108             : /* ----------------
     109             :  *      CreateDestReceiver - return appropriate receiver function set for dest
     110             :  * ----------------
     111             :  */
     112             : DestReceiver *
     113      667218 : CreateDestReceiver(CommandDest dest)
     114             : {
     115             :     /*
     116             :      * It's ok to cast the constness away as any modification of the none
     117             :      * receiver would be a bug (which gets easier to catch this way).
     118             :      */
     119             : 
     120      667218 :     switch (dest)
     121             :     {
     122      331392 :         case DestRemote:
     123             :         case DestRemoteExecute:
     124      331392 :             return printtup_create_DR(dest);
     125             : 
     126        1200 :         case DestRemoteSimple:
     127        1200 :             return unconstify(DestReceiver *, &printsimpleDR);
     128             : 
     129         702 :         case DestNone:
     130         702 :             return unconstify(DestReceiver *, &donothingDR);
     131             : 
     132      201912 :         case DestDebug:
     133      201912 :             return unconstify(DestReceiver *, &debugtupDR);
     134             : 
     135       54026 :         case DestSPI:
     136       54026 :             return unconstify(DestReceiver *, &spi_printtupDR);
     137             : 
     138       17326 :         case DestTuplestore:
     139       17326 :             return CreateTuplestoreDestReceiver();
     140             : 
     141           0 :         case DestIntoRel:
     142           0 :             return CreateIntoRelDestReceiver(NULL);
     143             : 
     144         144 :         case DestCopyOut:
     145         144 :             return CreateCopyDestReceiver();
     146             : 
     147       60516 :         case DestSQLFunction:
     148       60516 :             return CreateSQLFunctionDestReceiver();
     149             : 
     150           0 :         case DestTransientRel:
     151           0 :             return CreateTransientRelDestReceiver(InvalidOid);
     152             : 
     153           0 :         case DestTupleQueue:
     154           0 :             return CreateTupleQueueDestReceiver(NULL);
     155             :     }
     156             : 
     157             :     /* should never get here */
     158           0 :     pg_unreachable();
     159             : }
     160             : 
     161             : /* ----------------
     162             :  *      EndCommand - clean up the destination at end of command
     163             :  * ----------------
     164             :  */
     165             : void
     166      521890 : EndCommand(const QueryCompletion *qc, CommandDest dest, bool force_undecorated_output)
     167             : {
     168             :     char        completionTag[COMPLETION_TAG_BUFSIZE];
     169             :     CommandTag  tag;
     170             :     const char *tagname;
     171             : 
     172      521890 :     switch (dest)
     173             :     {
     174      319978 :         case DestRemote:
     175             :         case DestRemoteExecute:
     176             :         case DestRemoteSimple:
     177             : 
     178             :             /*
     179             :              * We assume the tagname is plain ASCII and therefore requires no
     180             :              * encoding conversion.
     181             :              *
     182             :              * We no longer display LastOid, but to preserve the wire
     183             :              * protocol, we write InvalidOid where the LastOid used to be
     184             :              * written.
     185             :              *
     186             :              * All cases where LastOid was written also write nprocessed
     187             :              * count, so just Assert that rather than having an extra test.
     188             :              */
     189      319978 :             tag = qc->commandTag;
     190      319978 :             tagname = GetCommandTagName(tag);
     191             : 
     192      319978 :             if (command_tag_display_rowcount(tag) && !force_undecorated_output)
     193      188936 :                 snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
     194             :                          tag == CMDTAG_INSERT ?
     195             :                          "%s 0 " UINT64_FORMAT : "%s " UINT64_FORMAT,
     196             :                          tagname, qc->nprocessed);
     197             :             else
     198      131042 :                 snprintf(completionTag, COMPLETION_TAG_BUFSIZE, "%s", tagname);
     199      319978 :             pq_putmessage('C', completionTag, strlen(completionTag) + 1);
     200             : 
     201      521890 :         case DestNone:
     202             :         case DestDebug:
     203             :         case DestSPI:
     204             :         case DestTuplestore:
     205             :         case DestIntoRel:
     206             :         case DestCopyOut:
     207             :         case DestSQLFunction:
     208             :         case DestTransientRel:
     209             :         case DestTupleQueue:
     210      521890 :             break;
     211             :     }
     212      521890 : }
     213             : 
     214             : /* ----------------
     215             :  *      NullCommand - tell dest that an empty query string was recognized
     216             :  *
     217             :  *      In FE/BE protocol version 1.0, this hack is necessary to support
     218             :  *      libpq's crufty way of determining whether a multiple-command
     219             :  *      query string is done.  In protocol 2.0 it's probably not really
     220             :  *      necessary to distinguish empty queries anymore, but we still do it
     221             :  *      for backwards compatibility with 1.0.  In protocol 3.0 it has some
     222             :  *      use again, since it ensures that there will be a recognizable end
     223             :  *      to the response to an Execute message.
     224             :  * ----------------
     225             :  */
     226             : void
     227         486 : NullCommand(CommandDest dest)
     228             : {
     229         486 :     switch (dest)
     230             :     {
     231         486 :         case DestRemote:
     232             :         case DestRemoteExecute:
     233             :         case DestRemoteSimple:
     234             : 
     235             :             /*
     236             :              * tell the fe that we saw an empty query string.  In protocols
     237             :              * before 3.0 this has a useless empty-string message body.
     238             :              */
     239         486 :             if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
     240         486 :                 pq_putemptymessage('I');
     241             :             else
     242           0 :                 pq_putmessage('I', "", 1);
     243         486 :             break;
     244             : 
     245           0 :         case DestNone:
     246             :         case DestDebug:
     247             :         case DestSPI:
     248             :         case DestTuplestore:
     249             :         case DestIntoRel:
     250             :         case DestCopyOut:
     251             :         case DestSQLFunction:
     252             :         case DestTransientRel:
     253             :         case DestTupleQueue:
     254           0 :             break;
     255             :     }
     256         486 : }
     257             : 
     258             : /* ----------------
     259             :  *      ReadyForQuery - tell dest that we are ready for a new query
     260             :  *
     261             :  *      The ReadyForQuery message is sent so that the FE can tell when
     262             :  *      we are done processing a query string.
     263             :  *      In versions 3.0 and up, it also carries a transaction state indicator.
     264             :  *
     265             :  *      Note that by flushing the stdio buffer here, we can avoid doing it
     266             :  *      most other places and thus reduce the number of separate packets sent.
     267             :  * ----------------
     268             :  */
     269             : void
     270      493844 : ReadyForQuery(CommandDest dest)
     271             : {
     272      493844 :     switch (dest)
     273             :     {
     274      328788 :         case DestRemote:
     275             :         case DestRemoteExecute:
     276             :         case DestRemoteSimple:
     277      328788 :             if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
     278             :             {
     279             :                 StringInfoData buf;
     280             : 
     281      328788 :                 pq_beginmessage(&buf, 'Z');
     282      328788 :                 pq_sendbyte(&buf, TransactionBlockStatusCode());
     283      328788 :                 pq_endmessage(&buf);
     284             :             }
     285             :             else
     286           0 :                 pq_putemptymessage('Z');
     287             :             /* Flush output at end of cycle in any case. */
     288      328788 :             pq_flush();
     289      328788 :             break;
     290             : 
     291      165056 :         case DestNone:
     292             :         case DestDebug:
     293             :         case DestSPI:
     294             :         case DestTuplestore:
     295             :         case DestIntoRel:
     296             :         case DestCopyOut:
     297             :         case DestSQLFunction:
     298             :         case DestTransientRel:
     299             :         case DestTupleQueue:
     300      165056 :             break;
     301             :     }
     302      493844 : }

Generated by: LCOV version 1.13