1 /*-------------------------------------------------------------------------
4 * Routines to print out tuples to the destination (both frontend
5 * clients and interactive backends are supported here).
8 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Header: /cvsroot/pgsql/src/backend/access/common/printtup.c,v 1.63 2002/08/22 00:01:41 tgl Exp $
14 *-------------------------------------------------------------------------
18 #include "access/heapam.h"
19 #include "access/printtup.h"
20 #include "libpq/libpq.h"
21 #include "libpq/pqformat.h"
22 #include "utils/lsyscache.h"
25 static void printtup_setup(DestReceiver *self, int operation,
26 const char *portalName, TupleDesc typeinfo);
27 static void printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self);
28 static void printtup_internal(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self);
29 static void printtup_cleanup(DestReceiver *self);
31 /* ----------------------------------------------------------------
32 * printtup / debugtup support
33 * ----------------------------------------------------------------
37 * Private state for a printtup destination object
41 { /* Per-attribute information */
42 Oid typoutput; /* Oid for the attribute's type output fn */
43 Oid typelem; /* typelem value to pass to the output fn */
44 bool typisvarlena; /* is it varlena (ie possibly toastable)? */
45 FmgrInfo finfo; /* Precomputed call info for typoutput */
50 DestReceiver pub; /* publicly-known function pointers */
51 TupleDesc attrinfo; /* The attr info we are set up for */
53 PrinttupAttrInfo *myinfo; /* Cached info about each attr */
57 * Initialize: create a DestReceiver for printtup
61 printtup_create_DR(bool isBinary)
63 DR_printtup *self = (DR_printtup *) palloc(sizeof(DR_printtup));
65 self->pub.receiveTuple = isBinary ? printtup_internal : printtup;
66 self->pub.setup = printtup_setup;
67 self->pub.cleanup = printtup_cleanup;
69 self->attrinfo = NULL;
73 return (DestReceiver *) self;
77 printtup_setup(DestReceiver *self, int operation,
78 const char *portalName, TupleDesc typeinfo)
81 * Send portal name to frontend.
83 * If portal name not specified, use "blank" portal.
85 if (portalName == NULL)
88 pq_puttextmessage('P', portalName);
91 * if this is a retrieve, then we send back the tuple
92 * descriptor of the tuples.
94 if (operation == CMD_SELECT)
96 Form_pg_attribute *attrs = typeinfo->attrs;
97 int natts = typeinfo->natts;
101 pq_beginmessage(&buf);
102 pq_sendbyte(&buf, 'T'); /* tuple descriptor message type */
103 pq_sendint(&buf, natts, 2); /* # of attrs in tuples */
105 for (i = 0; i < natts; ++i)
107 pq_sendstring(&buf, NameStr(attrs[i]->attname));
108 pq_sendint(&buf, (int) attrs[i]->atttypid,
109 sizeof(attrs[i]->atttypid));
110 pq_sendint(&buf, attrs[i]->attlen,
111 sizeof(attrs[i]->attlen));
112 if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
113 pq_sendint(&buf, attrs[i]->atttypmod,
114 sizeof(attrs[i]->atttypmod));
120 * We could set up the derived attr info at this time, but we postpone it
121 * until the first call of printtup, for 2 reasons:
122 * 1. We don't waste time (compared to the old way) if there are no
123 * tuples at all to output.
124 * 2. Checking in printtup allows us to handle the case that the tuples
125 * change type midway through (although this probably can't happen in
126 * the current executor).
132 printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
137 pfree(myState->myinfo); /* get rid of any old data */
138 myState->myinfo = NULL;
139 myState->attrinfo = typeinfo;
140 myState->nattrs = numAttrs;
143 myState->myinfo = (PrinttupAttrInfo *)
144 palloc(numAttrs * sizeof(PrinttupAttrInfo));
145 for (i = 0; i < numAttrs; i++)
147 PrinttupAttrInfo *thisState = myState->myinfo + i;
149 if (getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
150 &thisState->typoutput, &thisState->typelem,
151 &thisState->typisvarlena))
152 fmgr_info(thisState->typoutput, &thisState->finfo);
161 printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
163 DR_printtup *myState = (DR_printtup *) self;
165 int natts = tuple->t_data->t_natts;
170 /* Set or update my derived attribute info, if needed */
171 if (myState->attrinfo != typeinfo || myState->nattrs != natts)
172 printtup_prepare_info(myState, typeinfo, natts);
175 * tell the frontend to expect new tuple data (in ASCII style)
177 pq_beginmessage(&buf);
178 pq_sendbyte(&buf, 'D');
181 * send a bitmap of which attributes are not null
185 for (i = 0; i < natts; ++i)
187 if (!heap_attisnull(tuple, i + 1))
188 j |= k; /* set bit if not null */
190 if (k == 0) /* end of byte? */
192 pq_sendint(&buf, j, 1);
197 if (k != (1 << 7)) /* flush last partial byte */
198 pq_sendint(&buf, j, 1);
201 * send the attributes of this tuple
203 for (i = 0; i < natts; ++i)
205 PrinttupAttrInfo *thisState = myState->myinfo + i;
211 origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
214 if (OidIsValid(thisState->typoutput))
217 * If we have a toasted datum, forcibly detoast it here to
218 * avoid memory leakage inside the type's output routine.
220 if (thisState->typisvarlena)
221 attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
225 outputstr = DatumGetCString(FunctionCall3(&thisState->finfo,
227 ObjectIdGetDatum(thisState->typelem),
228 Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
230 pq_sendcountedtext(&buf, outputstr, strlen(outputstr));
232 /* Clean up detoasted copy, if any */
233 if (attr != origattr)
234 pfree(DatumGetPointer(attr));
239 outputstr = "<unprintable>";
240 pq_sendcountedtext(&buf, outputstr, strlen(outputstr));
252 printtup_cleanup(DestReceiver *self)
254 DR_printtup *myState = (DR_printtup *) self;
257 pfree(myState->myinfo);
266 printatt(unsigned attributeId,
267 Form_pg_attribute attributeP,
270 printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
272 NameStr(attributeP->attname),
273 value != NULL ? " = \"" : "",
274 value != NULL ? value : "",
275 value != NULL ? "\"" : "",
276 (unsigned int) (attributeP->atttypid),
278 attributeP->atttypmod,
279 attributeP->attbyval ? 't' : 'f');
287 showatts(const char *name, TupleDesc tupleDesc)
289 int natts = tupleDesc->natts;
290 Form_pg_attribute *attinfo = tupleDesc->attrs;
294 for (i = 0; i < natts; ++i)
295 printatt((unsigned) i + 1, attinfo[i], (char *) NULL);
300 * debugSetup - prepare to print tuples for an interactive backend
304 debugSetup(DestReceiver *self, int operation,
305 const char *portalName, TupleDesc typeinfo)
308 * show the return type of the tuples
310 if (portalName == NULL)
311 portalName = "blank";
313 showatts(portalName, typeinfo);
317 * debugtup - print one tuple for an interactive backend
321 debugtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
323 int natts = tuple->t_data->t_natts;
333 for (i = 0; i < natts; ++i)
335 origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
338 if (getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
339 &typoutput, &typelem, &typisvarlena))
342 * If we have a toasted datum, forcibly detoast it here to
343 * avoid memory leakage inside the type's output routine.
346 attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
350 value = DatumGetCString(OidFunctionCall3(typoutput,
352 ObjectIdGetDatum(typelem),
353 Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
355 printatt((unsigned) i + 1, typeinfo->attrs[i], value);
357 /* Clean up detoasted copy, if any */
358 if (attr != origattr)
359 pfree(DatumGetPointer(attr));
368 * We use a different data prefix, e.g. 'B' instead of 'D' to
369 * indicate a tuple in internal (binary) form.
371 * This is largely same as printtup, except we don't use the typout func.
375 printtup_internal(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
377 DR_printtup *myState = (DR_printtup *) self;
379 int natts = tuple->t_data->t_natts;
384 /* Set or update my derived attribute info, if needed */
385 if (myState->attrinfo != typeinfo || myState->nattrs != natts)
386 printtup_prepare_info(myState, typeinfo, natts);
389 * tell the frontend to expect new tuple data (in binary style)
391 pq_beginmessage(&buf);
392 pq_sendbyte(&buf, 'B');
395 * send a bitmap of which attributes are not null
399 for (i = 0; i < natts; ++i)
401 if (!heap_attisnull(tuple, i + 1))
402 j |= k; /* set bit if not null */
404 if (k == 0) /* end of byte? */
406 pq_sendint(&buf, j, 1);
411 if (k != (1 << 7)) /* flush last partial byte */
412 pq_sendint(&buf, j, 1);
415 * send the attributes of this tuple
418 fprintf(stderr, "sending tuple with %d atts\n", natts);
421 for (i = 0; i < natts; ++i)
423 PrinttupAttrInfo *thisState = myState->myinfo + i;
429 origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
432 /* send # of bytes, and opaque data */
433 if (thisState->typisvarlena)
436 * If we have a toasted datum, must detoast before sending.
438 attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
440 len = VARSIZE(attr) - VARHDRSZ;
442 pq_sendint(&buf, len, VARHDRSZ);
443 pq_sendbytes(&buf, VARDATA(attr), len);
447 char *d = VARDATA(attr);
449 fprintf(stderr, "length %d data %x %x %x %x\n",
450 len, *d, *(d + 1), *(d + 2), *(d + 3));
454 /* Clean up detoasted copy, if any */
455 if (attr != origattr)
456 pfree(DatumGetPointer(attr));
462 len = typeinfo->attrs[i]->attlen;
463 pq_sendint(&buf, len, sizeof(int32));
464 if (typeinfo->attrs[i]->attbyval)
469 * We need this horsing around because we don't know how
470 * shorter data values are aligned within a Datum.
472 store_att_byval(&datumBuf, attr, len);
473 pq_sendbytes(&buf, (char *) &datumBuf, len);
475 fprintf(stderr, "byval length %d data %ld\n", len,
481 pq_sendbytes(&buf, DatumGetPointer(attr), len);
483 fprintf(stderr, "byref length %d data %p\n", len,
484 DatumGetPointer(attr));