LCOV - code coverage report
Current view: top level - src/backend/access/common - printsimple.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 44 51 86.3 %
Date: 2024-04-25 23:11:32 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * printsimple.c
       4             :  *    Routines to print out tuples containing only a limited range of
       5             :  *    builtin types without catalog access.  This is intended for
       6             :  *    backends that don't have catalog access because they are not bound
       7             :  *    to a specific database, such as some walsender processes.  It
       8             :  *    doesn't handle standalone backends or protocol versions other than
       9             :  *    3.0, because we don't need such handling for current applications.
      10             :  *
      11             :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
      12             :  * Portions Copyright (c) 1994, Regents of the University of California
      13             :  *
      14             :  * IDENTIFICATION
      15             :  *    src/backend/access/common/printsimple.c
      16             :  *
      17             :  *-------------------------------------------------------------------------
      18             :  */
      19             : #include "postgres.h"
      20             : 
      21             : #include "access/printsimple.h"
      22             : #include "catalog/pg_type.h"
      23             : #include "libpq/pqformat.h"
      24             : #include "libpq/protocol.h"
      25             : #include "utils/builtins.h"
      26             : 
      27             : /*
      28             :  * At startup time, send a RowDescription message.
      29             :  */
      30             : void
      31        3892 : printsimple_startup(DestReceiver *self, int operation, TupleDesc tupdesc)
      32             : {
      33             :     StringInfoData buf;
      34             :     int         i;
      35             : 
      36        3892 :     pq_beginmessage(&buf, PqMsg_RowDescription);
      37        3892 :     pq_sendint16(&buf, tupdesc->natts);
      38             : 
      39       14994 :     for (i = 0; i < tupdesc->natts; ++i)
      40             :     {
      41       11102 :         Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
      42             : 
      43       11102 :         pq_sendstring(&buf, NameStr(attr->attname));
      44       11102 :         pq_sendint32(&buf, 0);  /* table oid */
      45       11102 :         pq_sendint16(&buf, 0);  /* attnum */
      46       11102 :         pq_sendint32(&buf, (int) attr->atttypid);
      47       11102 :         pq_sendint16(&buf, attr->attlen);
      48       11102 :         pq_sendint32(&buf, attr->atttypmod);
      49       11102 :         pq_sendint16(&buf, 0);  /* format code */
      50             :     }
      51             : 
      52        3892 :     pq_endmessage(&buf);
      53        3892 : }
      54             : 
      55             : /*
      56             :  * For each tuple, send a DataRow message.
      57             :  */
      58             : bool
      59        5450 : printsimple(TupleTableSlot *slot, DestReceiver *self)
      60             : {
      61        5450 :     TupleDesc   tupdesc = slot->tts_tupleDescriptor;
      62             :     StringInfoData buf;
      63             :     int         i;
      64             : 
      65             :     /* Make sure the tuple is fully deconstructed */
      66        5450 :     slot_getallattrs(slot);
      67             : 
      68             :     /* Prepare and send message */
      69        5450 :     pq_beginmessage(&buf, PqMsg_DataRow);
      70        5450 :     pq_sendint16(&buf, tupdesc->natts);
      71             : 
      72       21252 :     for (i = 0; i < tupdesc->natts; ++i)
      73             :     {
      74       15802 :         Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
      75             :         Datum       value;
      76             : 
      77       15802 :         if (slot->tts_isnull[i])
      78             :         {
      79        2432 :             pq_sendint32(&buf, -1);
      80        2432 :             continue;
      81             :         }
      82             : 
      83       13370 :         value = slot->tts_values[i];
      84             : 
      85             :         /*
      86             :          * We can't call the regular type output functions here because we
      87             :          * might not have catalog access.  Instead, we must hard-wire
      88             :          * knowledge of the required types.
      89             :          */
      90       13370 :         switch (attr->atttypid)
      91             :         {
      92       11184 :             case TEXTOID:
      93             :                 {
      94       11184 :                     text       *t = DatumGetTextPP(value);
      95             : 
      96       22368 :                     pq_sendcountedtext(&buf,
      97       11184 :                                        VARDATA_ANY(t),
      98       11184 :                                        VARSIZE_ANY_EXHDR(t));
      99             :                 }
     100       11184 :                 break;
     101             : 
     102           0 :             case INT4OID:
     103             :                 {
     104           0 :                     int32       num = DatumGetInt32(value);
     105             :                     char        str[12];    /* sign, 10 digits and '\0' */
     106             :                     int         len;
     107             : 
     108           0 :                     len = pg_ltoa(num, str);
     109           0 :                     pq_sendcountedtext(&buf, str, len);
     110             :                 }
     111           0 :                 break;
     112             : 
     113        2120 :             case INT8OID:
     114             :                 {
     115        2120 :                     int64       num = DatumGetInt64(value);
     116             :                     char        str[MAXINT8LEN + 1];
     117             :                     int         len;
     118             : 
     119        2120 :                     len = pg_lltoa(num, str);
     120        2120 :                     pq_sendcountedtext(&buf, str, len);
     121             :                 }
     122        2120 :                 break;
     123             : 
     124          66 :             case OIDOID:
     125             :                 {
     126          66 :                     Oid         num = ObjectIdGetDatum(value);
     127             :                     char        str[10];    /* 10 digits */
     128             :                     int         len;
     129             : 
     130          66 :                     len = pg_ultoa_n(num, str);
     131          66 :                     pq_sendcountedtext(&buf, str, len);
     132             :                 }
     133          66 :                 break;
     134             : 
     135           0 :             default:
     136           0 :                 elog(ERROR, "unsupported type OID: %u", attr->atttypid);
     137             :         }
     138             :     }
     139             : 
     140        5450 :     pq_endmessage(&buf);
     141             : 
     142        5450 :     return true;
     143             : }

Generated by: LCOV version 1.14