Age Owner Branch data TLA 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-2025, 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
3147 rhaas@postgresql.org 31 :CBC 2138 : printsimple_startup(DestReceiver *self, int operation, TupleDesc tupdesc)
32 : : {
33 : : StringInfoData buf;
34 : : int i;
35 : :
746 nathan@postgresql.or 36 : 2138 : pq_beginmessage(&buf, PqMsg_RowDescription);
2887 andres@anarazel.de 37 : 2138 : pq_sendint16(&buf, tupdesc->natts);
38 : :
3147 rhaas@postgresql.org 39 [ + + ]: 8248 : for (i = 0; i < tupdesc->natts; ++i)
40 : : {
2939 andres@anarazel.de 41 : 6110 : Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
42 : :
3147 rhaas@postgresql.org 43 : 6110 : pq_sendstring(&buf, NameStr(attr->attname));
2838 44 : 6110 : pq_sendint32(&buf, 0); /* table oid */
45 : 6110 : pq_sendint16(&buf, 0); /* attnum */
2887 andres@anarazel.de 46 : 6110 : pq_sendint32(&buf, (int) attr->atttypid);
47 : 6110 : pq_sendint16(&buf, attr->attlen);
48 : 6110 : pq_sendint32(&buf, attr->atttypmod);
2838 rhaas@postgresql.org 49 : 6110 : pq_sendint16(&buf, 0); /* format code */
50 : : }
51 : :
3147 52 : 2138 : pq_endmessage(&buf);
53 : 2138 : }
54 : :
55 : : /*
56 : : * For each tuple, send a DataRow message.
57 : : */
58 : : bool
59 : 2969 : printsimple(TupleTableSlot *slot, DestReceiver *self)
60 : : {
61 : 2969 : TupleDesc tupdesc = slot->tts_tupleDescriptor;
62 : : StringInfoData buf;
63 : : int i;
64 : :
65 : : /* Make sure the tuple is fully deconstructed */
66 : 2969 : slot_getallattrs(slot);
67 : :
68 : : /* Prepare and send message */
746 nathan@postgresql.or 69 : 2969 : pq_beginmessage(&buf, PqMsg_DataRow);
2887 andres@anarazel.de 70 : 2969 : pq_sendint16(&buf, tupdesc->natts);
71 : :
3147 rhaas@postgresql.org 72 [ + + ]: 11585 : for (i = 0; i < tupdesc->natts; ++i)
73 : : {
2939 andres@anarazel.de 74 : 8616 : Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
75 : : Datum value;
76 : :
3147 rhaas@postgresql.org 77 [ + + ]: 8616 : if (slot->tts_isnull[i])
78 : : {
2887 andres@anarazel.de 79 : 1347 : pq_sendint32(&buf, -1);
3147 rhaas@postgresql.org 80 : 1347 : continue;
81 : : }
82 : :
83 : 7269 : 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 [ + - + + : 7269 : switch (attr->atttypid)
- ]
91 : : {
92 : 6033 : case TEXTOID:
93 : : {
94 : 6033 : text *t = DatumGetTextPP(value);
95 : :
96 : 6033 : pq_sendcountedtext(&buf,
97 [ - + ]: 6033 : VARDATA_ANY(t),
551 heikki.linnakangas@i 98 [ - + - - : 6033 : VARSIZE_ANY_EXHDR(t));
- - - - -
+ ]
99 : : }
3147 rhaas@postgresql.org 100 : 6033 : break;
101 : :
3139 rhaas@postgresql.org 102 :UBC 0 : case INT4OID:
103 : : {
3034 bruce@momjian.us 104 : 0 : int32 num = DatumGetInt32(value);
105 : : char str[12]; /* sign, 10 digits and '\0' */
106 : : int len;
107 : :
1911 drowley@postgresql.o 108 : 0 : len = pg_ltoa(num, str);
551 heikki.linnakangas@i 109 : 0 : pq_sendcountedtext(&buf, str, len);
110 : : }
3139 rhaas@postgresql.org 111 : 0 : break;
112 : :
3139 rhaas@postgresql.org 113 :CBC 1197 : case INT8OID:
114 : : {
3034 bruce@momjian.us 115 : 1197 : int64 num = DatumGetInt64(value);
116 : : char str[MAXINT8LEN + 1];
117 : : int len;
118 : :
1911 drowley@postgresql.o 119 : 1197 : len = pg_lltoa(num, str);
551 heikki.linnakangas@i 120 : 1197 : pq_sendcountedtext(&buf, str, len);
121 : : }
3139 rhaas@postgresql.org 122 : 1197 : break;
123 : :
1158 peter@eisentraut.org 124 : 39 : case OIDOID:
125 : : {
32 peter@eisentraut.org 126 :GNC 39 : Oid num = DatumGetObjectId(value);
127 : : char str[10]; /* 10 digits */
128 : : int len;
129 : :
1158 peter@eisentraut.org 130 :CBC 39 : len = pg_ultoa_n(num, str);
551 heikki.linnakangas@i 131 : 39 : pq_sendcountedtext(&buf, str, len);
132 : : }
1158 peter@eisentraut.org 133 : 39 : break;
134 : :
3147 rhaas@postgresql.org 135 :UBC 0 : default:
136 [ # # ]: 0 : elog(ERROR, "unsupported type OID: %u", attr->atttypid);
137 : : }
138 : : }
139 : :
3147 rhaas@postgresql.org 140 :CBC 2969 : pq_endmessage(&buf);
141 : :
142 : 2969 : return true;
143 : : }
|