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 : : #include "varatt.h"
27 : :
28 : : /*
29 : : * At startup time, send a RowDescription message.
30 : : */
31 : : void
3198 rhaas@postgresql.org 32 :CBC 2142 : printsimple_startup(DestReceiver *self, int operation, TupleDesc tupdesc)
33 : : {
34 : : StringInfoData buf;
35 : : int i;
36 : :
797 nathan@postgresql.or 37 : 2142 : pq_beginmessage(&buf, PqMsg_RowDescription);
2938 andres@anarazel.de 38 : 2142 : pq_sendint16(&buf, tupdesc->natts);
39 : :
3198 rhaas@postgresql.org 40 [ + + ]: 8279 : for (i = 0; i < tupdesc->natts; ++i)
41 : : {
2990 andres@anarazel.de 42 : 6137 : Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
43 : :
3198 rhaas@postgresql.org 44 : 6137 : pq_sendstring(&buf, NameStr(attr->attname));
2889 45 : 6137 : pq_sendint32(&buf, 0); /* table oid */
46 : 6137 : pq_sendint16(&buf, 0); /* attnum */
2938 andres@anarazel.de 47 : 6137 : pq_sendint32(&buf, (int) attr->atttypid);
48 : 6137 : pq_sendint16(&buf, attr->attlen);
49 : 6137 : pq_sendint32(&buf, attr->atttypmod);
2889 rhaas@postgresql.org 50 : 6137 : pq_sendint16(&buf, 0); /* format code */
51 : : }
52 : :
3198 53 : 2142 : pq_endmessage(&buf);
54 : 2142 : }
55 : :
56 : : /*
57 : : * For each tuple, send a DataRow message.
58 : : */
59 : : bool
60 : 2979 : printsimple(TupleTableSlot *slot, DestReceiver *self)
61 : : {
62 : 2979 : TupleDesc tupdesc = slot->tts_tupleDescriptor;
63 : : StringInfoData buf;
64 : : int i;
65 : :
66 : : /* Make sure the tuple is fully deconstructed */
67 : 2979 : slot_getallattrs(slot);
68 : :
69 : : /* Prepare and send message */
797 nathan@postgresql.or 70 : 2979 : pq_beginmessage(&buf, PqMsg_DataRow);
2938 andres@anarazel.de 71 : 2979 : pq_sendint16(&buf, tupdesc->natts);
72 : :
3198 rhaas@postgresql.org 73 [ + + ]: 11640 : for (i = 0; i < tupdesc->natts; ++i)
74 : : {
2990 andres@anarazel.de 75 : 8661 : Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
76 : : Datum value;
77 : :
3198 rhaas@postgresql.org 78 [ + + ]: 8661 : if (slot->tts_isnull[i])
79 : : {
2938 andres@anarazel.de 80 : 1349 : pq_sendint32(&buf, -1);
3198 rhaas@postgresql.org 81 : 1349 : continue;
82 : : }
83 : :
84 : 7312 : value = slot->tts_values[i];
85 : :
86 : : /*
87 : : * We can't call the regular type output functions here because we
88 : : * might not have catalog access. Instead, we must hard-wire
89 : : * knowledge of the required types.
90 : : */
91 [ + - + + : 7312 : switch (attr->atttypid)
- ]
92 : : {
93 : 6075 : case TEXTOID:
94 : : {
95 : 6075 : text *t = DatumGetTextPP(value);
96 : :
97 : 6075 : pq_sendcountedtext(&buf,
98 [ - + ]: 6075 : VARDATA_ANY(t),
602 heikki.linnakangas@i 99 [ - + - - : 6075 : VARSIZE_ANY_EXHDR(t));
- - - - -
+ ]
100 : : }
3198 rhaas@postgresql.org 101 : 6075 : break;
102 : :
3190 rhaas@postgresql.org 103 :UBC 0 : case INT4OID:
104 : : {
3085 bruce@momjian.us 105 : 0 : int32 num = DatumGetInt32(value);
106 : : char str[12]; /* sign, 10 digits and '\0' */
107 : : int len;
108 : :
1962 drowley@postgresql.o 109 : 0 : len = pg_ltoa(num, str);
602 heikki.linnakangas@i 110 : 0 : pq_sendcountedtext(&buf, str, len);
111 : : }
3190 rhaas@postgresql.org 112 : 0 : break;
113 : :
3190 rhaas@postgresql.org 114 :CBC 1198 : case INT8OID:
115 : : {
3085 bruce@momjian.us 116 : 1198 : int64 num = DatumGetInt64(value);
117 : : char str[MAXINT8LEN + 1];
118 : : int len;
119 : :
1962 drowley@postgresql.o 120 : 1198 : len = pg_lltoa(num, str);
602 heikki.linnakangas@i 121 : 1198 : pq_sendcountedtext(&buf, str, len);
122 : : }
3190 rhaas@postgresql.org 123 : 1198 : break;
124 : :
1209 peter@eisentraut.org 125 : 39 : case OIDOID:
126 : : {
83 peter@eisentraut.org 127 :GNC 39 : Oid num = DatumGetObjectId(value);
128 : : char str[10]; /* 10 digits */
129 : : int len;
130 : :
1209 peter@eisentraut.org 131 :CBC 39 : len = pg_ultoa_n(num, str);
602 heikki.linnakangas@i 132 : 39 : pq_sendcountedtext(&buf, str, len);
133 : : }
1209 peter@eisentraut.org 134 : 39 : break;
135 : :
3198 rhaas@postgresql.org 136 :UBC 0 : default:
137 [ # # ]: 0 : elog(ERROR, "unsupported type OID: %u", attr->atttypid);
138 : : }
139 : : }
140 : :
3198 rhaas@postgresql.org 141 :CBC 2979 : pq_endmessage(&buf);
142 : :
143 : 2979 : return true;
144 : : }
|