PostgreSQL Source Code git master
printsimple.c
Go to the documentation of this file.
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 */
31void
32printsimple_startup(DestReceiver *self, int operation, TupleDesc tupdesc)
33{
35 int i;
36
38 pq_sendint16(&buf, tupdesc->natts);
39
40 for (i = 0; i < tupdesc->natts; ++i)
41 {
42 Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
43
44 pq_sendstring(&buf, NameStr(attr->attname));
45 pq_sendint32(&buf, 0); /* table oid */
46 pq_sendint16(&buf, 0); /* attnum */
47 pq_sendint32(&buf, (int) attr->atttypid);
48 pq_sendint16(&buf, attr->attlen);
49 pq_sendint32(&buf, attr->atttypmod);
50 pq_sendint16(&buf, 0); /* format code */
51 }
52
54}
55
56/*
57 * For each tuple, send a DataRow message.
58 */
59bool
61{
62 TupleDesc tupdesc = slot->tts_tupleDescriptor;
64 int i;
65
66 /* Make sure the tuple is fully deconstructed */
67 slot_getallattrs(slot);
68
69 /* Prepare and send message */
71 pq_sendint16(&buf, tupdesc->natts);
72
73 for (i = 0; i < tupdesc->natts; ++i)
74 {
75 Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
77
78 if (slot->tts_isnull[i])
79 {
80 pq_sendint32(&buf, -1);
81 continue;
82 }
83
84 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 switch (attr->atttypid)
92 {
93 case TEXTOID:
94 {
96
98 VARDATA_ANY(t),
100 }
101 break;
102
103 case INT4OID:
104 {
106 char str[12]; /* sign, 10 digits and '\0' */
107 int len;
108
109 len = pg_ltoa(num, str);
111 }
112 break;
113
114 case INT8OID:
115 {
117 char str[MAXINT8LEN + 1];
118 int len;
119
120 len = pg_lltoa(num, str);
122 }
123 break;
124
125 case OIDOID:
126 {
128 char str[10]; /* 10 digits */
129 int len;
130
131 len = pg_ultoa_n(num, str);
133 }
134 break;
135
136 default:
137 elog(ERROR, "unsupported type OID: %u", attr->atttypid);
138 }
139 }
140
142
143 return true;
144}
#define MAXINT8LEN
Definition: builtins.h:22
#define NameStr(name)
Definition: c.h:756
int64_t int64
Definition: c.h:540
int32_t int32
Definition: c.h:539
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define DatumGetTextPP(X)
Definition: fmgr.h:292
const char * str
static struct @171 value
int i
Definition: isn.c:77
int pg_ltoa(int32 value, char *a)
Definition: numutils.c:1120
int pg_lltoa(int64 value, char *a)
Definition: numutils.c:1227
int pg_ultoa_n(uint32 value, char *a)
Definition: numutils.c:1055
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
const void size_t len
static char * buf
Definition: pg_test_fsync.c:72
static int64 DatumGetInt64(Datum X)
Definition: postgres.h:393
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:252
uint64_t Datum
Definition: postgres.h:70
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:212
unsigned int Oid
Definition: postgres_ext.h:32
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:195
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:296
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
void pq_sendcountedtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:142
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:144
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:136
void printsimple_startup(DestReceiver *self, int operation, TupleDesc tupdesc)
Definition: printsimple.c:32
bool printsimple(TupleTableSlot *slot, DestReceiver *self)
Definition: printsimple.c:60
#define PqMsg_RowDescription
Definition: protocol.h:52
#define PqMsg_DataRow
Definition: protocol.h:43
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:122
bool * tts_isnull
Definition: tuptable.h:126
Datum * tts_values
Definition: tuptable.h:124
Definition: c.h:697
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:371
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition: varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition: varatt.h:486