Age Owner 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-2023, 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 "utils/builtins.h"
25 :
26 : /*
27 : * At startup time, send a RowDescription message.
28 : */
29 : void
2266 rhaas 30 CBC 1707 : printsimple_startup(DestReceiver *self, int operation, TupleDesc tupdesc)
31 : {
32 : StringInfoData buf;
33 : int i;
34 :
35 1707 : pq_beginmessage(&buf, 'T'); /* RowDescription */
2006 andres 36 1707 : pq_sendint16(&buf, tupdesc->natts);
37 :
2266 rhaas 38 6523 : for (i = 0; i < tupdesc->natts; ++i)
39 : {
2058 andres 40 4816 : Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
41 :
2266 rhaas 42 4816 : pq_sendstring(&buf, NameStr(attr->attname));
1957 43 4816 : pq_sendint32(&buf, 0); /* table oid */
44 4816 : pq_sendint16(&buf, 0); /* attnum */
2006 andres 45 4816 : pq_sendint32(&buf, (int) attr->atttypid);
46 4816 : pq_sendint16(&buf, attr->attlen);
47 4816 : pq_sendint32(&buf, attr->atttypmod);
1957 rhaas 48 4816 : pq_sendint16(&buf, 0); /* format code */
49 : }
50 :
2266 51 1707 : pq_endmessage(&buf);
52 1707 : }
53 :
54 : /*
55 : * For each tuple, send a DataRow message.
56 : */
57 : bool
58 2432 : printsimple(TupleTableSlot *slot, DestReceiver *self)
59 : {
60 2432 : TupleDesc tupdesc = slot->tts_tupleDescriptor;
61 : StringInfoData buf;
62 : int i;
63 :
64 : /* Make sure the tuple is fully deconstructed */
65 2432 : slot_getallattrs(slot);
66 :
67 : /* Prepare and send message */
68 2432 : pq_beginmessage(&buf, 'D');
2006 andres 69 2432 : pq_sendint16(&buf, tupdesc->natts);
70 :
2266 rhaas 71 9436 : for (i = 0; i < tupdesc->natts; ++i)
72 : {
2058 andres 73 7004 : Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
74 : Datum value;
75 :
2266 rhaas 76 7004 : if (slot->tts_isnull[i])
77 : {
2006 andres 78 1041 : pq_sendint32(&buf, -1);
2266 rhaas 79 1041 : continue;
80 : }
81 :
82 5963 : value = slot->tts_values[i];
83 :
84 : /*
85 : * We can't call the regular type output functions here because we
86 : * might not have catalog access. Instead, we must hard-wire
87 : * knowledge of the required types.
88 : */
89 5963 : switch (attr->atttypid)
90 : {
91 5028 : case TEXTOID:
92 : {
93 5028 : text *t = DatumGetTextPP(value);
94 :
95 10056 : pq_sendcountedtext(&buf,
96 5028 : VARDATA_ANY(t),
97 5028 : VARSIZE_ANY_EXHDR(t),
98 : false);
99 : }
100 5028 : break;
101 :
2258 rhaas 102 UBC 0 : case INT4OID:
103 : {
2153 bruce 104 0 : int32 num = DatumGetInt32(value);
105 : char str[12]; /* sign, 10 digits and '\0' */
106 : int len;
107 :
1030 drowley 108 0 : len = pg_ltoa(num, str);
109 0 : pq_sendcountedtext(&buf, str, len, false);
110 : }
2258 rhaas 111 0 : break;
112 :
2258 rhaas 113 CBC 919 : case INT8OID:
114 : {
2153 bruce 115 919 : int64 num = DatumGetInt64(value);
116 : char str[MAXINT8LEN + 1];
117 : int len;
118 :
1030 drowley 119 919 : len = pg_lltoa(num, str);
120 919 : pq_sendcountedtext(&buf, str, len, false);
121 : }
2258 rhaas 122 919 : break;
123 :
277 peter 124 GNC 16 : case OIDOID:
125 : {
126 16 : Oid num = ObjectIdGetDatum(value);
127 : char str[10]; /* 10 digits */
128 : int len;
129 :
130 16 : len = pg_ultoa_n(num, str);
131 16 : pq_sendcountedtext(&buf, str, len, false);
132 : }
133 16 : break;
134 :
2266 rhaas 135 LBC 0 : default:
2266 rhaas 136 UIC 0 : elog(ERROR, "unsupported type OID: %u", attr->atttypid);
2266 rhaas 137 ECB : }
138 : }
139 :
2266 rhaas 140 GIC 2432 : pq_endmessage(&buf);
2266 rhaas 141 ECB :
2266 rhaas 142 CBC 2432 : return true;
143 : }
|