printtup.c 10.0 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * printtup.c
4 5
 *	  Routines to print out tuples to the destination (binary or non-binary
 *	  portals, frontend/interactive backend, etc.).
6 7 8 9 10
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
11
 *	  $Header: /cvsroot/pgsql/src/backend/access/common/printtup.c,v 1.51 1999/11/22 17:55:52 momjian Exp $
12 13 14 15
 *
 *-------------------------------------------------------------------------
 */

16

17 18 19 20 21 22 23
#include "postgres.h"

#include "access/heapam.h"
#include "access/printtup.h"
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
#include "utils/syscache.h"
M
Marc G. Fournier 已提交
24

25 26 27
static void printtup_setup(DestReceiver *self, TupleDesc typeinfo);
static void printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self);
static void printtup_cleanup(DestReceiver *self);
28

29
/* ----------------------------------------------------------------
30
 *		printtup / debugtup support
31 32 33 34
 * ----------------------------------------------------------------
 */

/* ----------------
35 36 37 38 39
 *		getTypeOutAndElem -- get both typoutput and typelem for a type
 *
 * We used to fetch these with two separate function calls,
 * typtoout() and gettypelem(), which each called SearchSysCacheTuple.
 * This way takes half the time.
40 41
 * ----------------
 */
42
int
B
Bruce Momjian 已提交
43
getTypeOutAndElem(Oid type, Oid *typOutput, Oid *typElem)
44
{
45
	HeapTuple	typeTuple;
46

47
	typeTuple = SearchSysCacheTuple(TYPEOID,
48 49 50 51
									ObjectIdGetDatum(type),
									0, 0, 0);

	if (HeapTupleIsValid(typeTuple))
52 53
	{
		Form_pg_type pt = (Form_pg_type) GETSTRUCT(typeTuple);
B
Bruce Momjian 已提交
54

55 56 57 58
		*typOutput = (Oid) pt->typoutput;
		*typElem = (Oid) pt->typelem;
		return OidIsValid(*typOutput);
	}
59

60
	elog(ERROR, "getTypeOutAndElem: Cache lookup of type %u failed", type);
61

62 63 64
	*typOutput = InvalidOid;
	*typElem = InvalidOid;
	return 0;
65 66
}

67 68 69 70
/* ----------------
 *		Private state for a printtup destination object
 * ----------------
 */
B
Bruce Momjian 已提交
71 72
typedef struct
{								/* Per-attribute information */
73 74
	Oid			typoutput;		/* Oid for the attribute's type output fn */
	Oid			typelem;		/* typelem value to pass to the output fn */
75
	FmgrInfo	finfo;			/* Precomputed call info for typoutput */
76
} PrinttupAttrInfo;
77

B
Bruce Momjian 已提交
78 79 80 81 82 83
typedef struct
{
	DestReceiver pub;			/* publicly-known function pointers */
	TupleDesc	attrinfo;		/* The attr info we are set up for */
	int			nattrs;
	PrinttupAttrInfo *myinfo;	/* Cached info about each attr */
84
} DR_printtup;
85 86 87 88 89

/* ----------------
 *		Initialize: create a DestReceiver for printtup
 * ----------------
 */
B
Bruce Momjian 已提交
90
DestReceiver *
91 92
printtup_create_DR()
{
B
Bruce Momjian 已提交
93
	DR_printtup *self = (DR_printtup *) palloc(sizeof(DR_printtup));
94 95 96 97 98 99 100 101 102

	self->pub.receiveTuple = printtup;
	self->pub.setup = printtup_setup;
	self->pub.cleanup = printtup_cleanup;

	self->attrinfo = NULL;
	self->nattrs = 0;
	self->myinfo = NULL;

B
Bruce Momjian 已提交
103
	return (DestReceiver *) self;
104 105 106
}

static void
107
printtup_setup(DestReceiver *self, TupleDesc typeinfo)
108 109 110 111 112
{
	/* ----------------
	 * We could set up the derived attr info at this time, but we postpone it
	 * until the first call of printtup, for 3 reasons:
	 * 1. We don't waste time (compared to the old way) if there are no
B
Bruce Momjian 已提交
113
	 *	  tuples at all to output.
114
	 * 2. Checking in printtup allows us to handle the case that the tuples
B
Bruce Momjian 已提交
115 116
	 *	  change type midway through (although this probably can't happen in
	 *	  the current executor).
117 118 119 120 121 122
	 * 3. Right now, ExecutorRun passes a NULL for typeinfo anyway :-(
	 * ----------------
	 */
}

static void
123
printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
124
{
B
Bruce Momjian 已提交
125
	int			i;
126 127

	if (myState->myinfo)
B
Bruce Momjian 已提交
128
		pfree(myState->myinfo); /* get rid of any old data */
129 130 131 132 133
	myState->myinfo = NULL;
	myState->attrinfo = typeinfo;
	myState->nattrs = numAttrs;
	if (numAttrs <= 0)
		return;
B
Bruce Momjian 已提交
134
	myState->myinfo = (PrinttupAttrInfo *)
135 136 137
		palloc(numAttrs * sizeof(PrinttupAttrInfo));
	for (i = 0; i < numAttrs; i++)
	{
B
Bruce Momjian 已提交
138 139
		PrinttupAttrInfo *thisState = myState->myinfo + i;

140 141 142
		if (getTypeOutAndElem((Oid) typeinfo->attrs[i]->atttypid,
							  &thisState->typoutput, &thisState->typelem))
			fmgr_info(thisState->typoutput, &thisState->finfo);
143 144 145
	}
}

146
/* ----------------
147
 *		printtup
148 149
 * ----------------
 */
150
static void
151
printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
152
{
B
Bruce Momjian 已提交
153
	DR_printtup *myState = (DR_printtup *) self;
154
	StringInfoData buf;
155 156
	int			i,
				j,
157
				k;
158 159
	char	   *outputstr;
	Datum		attr;
160
	bool		isnull;
161

162 163 164 165 166
	/* Set or update my derived attribute info, if needed */
	if (myState->attrinfo != typeinfo ||
		myState->nattrs != tuple->t_data->t_natts)
		printtup_prepare_info(myState, typeinfo, tuple->t_data->t_natts);

167
	/* ----------------
168
	 *	tell the frontend to expect new tuple data (in ASCII style)
169 170
	 * ----------------
	 */
171 172
	pq_beginmessage(&buf);
	pq_sendbyte(&buf, 'D');
173 174

	/* ----------------
175
	 *	send a bitmap of which attributes are not null
176 177 178 179
	 * ----------------
	 */
	j = 0;
	k = 1 << 7;
180
	for (i = 0; i < tuple->t_data->t_natts; ++i)
181
	{
B
Bruce Momjian 已提交
182
		if (!heap_attisnull(tuple, i + 1))
183
			j |= k;				/* set bit if not null */
184
		k >>= 1;
185
		if (k == 0)				/* end of byte? */
186
		{
187
			pq_sendint(&buf, j, 1);
188 189 190
			j = 0;
			k = 1 << 7;
		}
191
	}
192
	if (k != (1 << 7))			/* flush last partial byte */
193
		pq_sendint(&buf, j, 1);
194 195 196 197 198

	/* ----------------
	 *	send the attributes of this tuple
	 * ----------------
	 */
199
	for (i = 0; i < tuple->t_data->t_natts; ++i)
200
	{
B
Bruce Momjian 已提交
201 202
		PrinttupAttrInfo *thisState = myState->myinfo + i;

203
		attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
M
 
Marc G. Fournier 已提交
204 205
		if (isnull)
			continue;
206
		if (OidIsValid(thisState->typoutput))
207
		{
208 209
			outputstr = (char *) (*fmgr_faddr(&thisState->finfo))
				(attr, thisState->typelem, typeinfo->attrs[i]->atttypmod);
210
			pq_sendcountedtext(&buf, outputstr, strlen(outputstr));
211 212
			pfree(outputstr);
		}
M
 
Marc G. Fournier 已提交
213 214 215
		else
		{
			outputstr = "<unprintable>";
216
			pq_sendcountedtext(&buf, outputstr, strlen(outputstr));
217
		}
218
	}
219 220

	pq_endmessage(&buf);
221 222
}

223 224 225 226 227
/* ----------------
 *		printtup_cleanup
 * ----------------
 */
static void
228
printtup_cleanup(DestReceiver *self)
229
{
B
Bruce Momjian 已提交
230 231
	DR_printtup *myState = (DR_printtup *) self;

232 233 234 235 236
	if (myState->myinfo)
		pfree(myState->myinfo);
	pfree(myState);
}

237
/* ----------------
238
 *		printatt
239 240 241 242
 * ----------------
 */
static void
printatt(unsigned attributeId,
243
		 Form_pg_attribute attributeP,
244
		 char *value)
245
{
B
Bruce Momjian 已提交
246
	printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
247
		   attributeId,
248
		   NameStr(attributeP->attname),
249 250 251 252 253
		   value != NULL ? " = \"" : "",
		   value != NULL ? value : "",
		   value != NULL ? "\"" : "",
		   (unsigned int) (attributeP->atttypid),
		   attributeP->attlen,
B
Bruce Momjian 已提交
254
		   attributeP->atttypmod,
255
		   attributeP->attbyval ? 't' : 'f');
256 257 258
}

/* ----------------
259
 *		showatts
260 261 262 263 264
 * ----------------
 */
void
showatts(char *name, TupleDesc tupleDesc)
{
265 266
	int			i;
	int			natts = tupleDesc->natts;
267
	Form_pg_attribute *attinfo = tupleDesc->attrs;
268

269 270 271 272
	puts(name);
	for (i = 0; i < natts; ++i)
		printatt((unsigned) i + 1, attinfo[i], (char *) NULL);
	printf("\t----\n");
273 274 275
}

/* ----------------
276
 *		debugtup
277 278 279
 * ----------------
 */
void
280
debugtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
281
{
282 283
	int			i;
	Datum		attr;
284
	char	   *value;
285
	bool		isnull;
286 287
	Oid			typoutput,
				typelem;
288

289
	for (i = 0; i < tuple->t_data->t_natts; ++i)
290
	{
291
		attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
292 293 294 295
		if (isnull)
			continue;
		if (getTypeOutAndElem((Oid) typeinfo->attrs[i]->atttypid,
							  &typoutput, &typelem))
296
		{
297
			value = fmgr(typoutput, attr, typelem,
298
						 typeinfo->attrs[i]->atttypmod);
299 300 301
			printatt((unsigned) i + 1, typeinfo->attrs[i], value);
			pfree(value);
		}
302
	}
303
	printf("\t----\n");
304 305 306
}

/* ----------------
307 308 309
 *		printtup_internal
 *		We use a different data prefix, e.g. 'B' instead of 'D' to
 *		indicate a tuple in internal (binary) form.
310
 *
311 312
 *		This is same as printtup, except we don't use the typout func,
 *		and therefore have no need for persistent state.
313 314 315
 * ----------------
 */
void
316
printtup_internal(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
317
{
318
	StringInfoData buf;
319 320 321
	int			i,
				j,
				k;
322
	Datum		attr;
323
	bool		isnull;
324 325

	/* ----------------
326
	 *	tell the frontend to expect new tuple data (in binary style)
327 328
	 * ----------------
	 */
329 330
	pq_beginmessage(&buf);
	pq_sendbyte(&buf, 'B');
331 332

	/* ----------------
333
	 *	send a bitmap of which attributes are not null
334 335 336 337
	 * ----------------
	 */
	j = 0;
	k = 1 << 7;
338
	for (i = 0; i < tuple->t_data->t_natts; ++i)
339
	{
B
Bruce Momjian 已提交
340
		if (!heap_attisnull(tuple, i + 1))
341
			j |= k;				/* set bit if not null */
342
		k >>= 1;
343
		if (k == 0)				/* end of byte? */
344
		{
345
			pq_sendint(&buf, j, 1);
346 347 348
			j = 0;
			k = 1 << 7;
		}
349
	}
350
	if (k != (1 << 7))			/* flush last partial byte */
351
		pq_sendint(&buf, j, 1);
352 353 354 355 356

	/* ----------------
	 *	send the attributes of this tuple
	 * ----------------
	 */
357
#ifdef IPORTAL_DEBUG
358
	fprintf(stderr, "sending tuple with %d atts\n", tuple->t_data->t_natts);
359
#endif
360
	for (i = 0; i < tuple->t_data->t_natts; ++i)
361
	{
362
		int32		len = typeinfo->attrs[i]->attlen;
363

364
		attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
365
		if (!isnull)
366
		{
367 368 369 370 371 372
			/* # of bytes, and opaque data */
			if (len == -1)
			{
				/* variable length, assume a varlena structure */
				len = VARSIZE(attr) - VARHDRSZ;

373 374
				pq_sendint(&buf, len, VARHDRSZ);
				pq_sendbytes(&buf, VARDATA(attr), len);
375

376 377
#ifdef IPORTAL_DEBUG
				{
378
					char	   *d = VARDATA(attr);
379 380 381 382

					fprintf(stderr, "length %d data %x%x%x%x\n",
							len, *d, *(d + 1), *(d + 2), *(d + 3));
				}
383
#endif
384 385 386 387 388 389
			}
			else
			{
				/* fixed size */
				if (typeinfo->attrs[i]->attbyval)
				{
390 391 392
					int8		i8;
					int16		i16;
					int32		i32;
393

394
					pq_sendint(&buf, len, sizeof(int32));
395 396
					switch (len)
					{
397 398
						case sizeof(int8):
							i8 = DatumGetChar(attr);
399
							pq_sendbytes(&buf, (char *) &i8, len);
400 401 402
							break;
						case sizeof(int16):
							i16 = DatumGetInt16(attr);
403
							pq_sendbytes(&buf, (char *) &i16, len);
404 405 406
							break;
						case sizeof(int32):
							i32 = DatumGetInt32(attr);
407
							pq_sendbytes(&buf, (char *) &i32, len);
408
							break;
409
					}
410
#ifdef IPORTAL_DEBUG
411
					fprintf(stderr, "byval length %d data %d\n", len, attr);
412
#endif
413 414 415
				}
				else
				{
416 417
					pq_sendint(&buf, len, sizeof(int32));
					pq_sendbytes(&buf, DatumGetPointer(attr), len);
418
#ifdef IPORTAL_DEBUG
419
					fprintf(stderr, "byref length %d data %x\n", len,
420
							DatumGetPointer(attr));
421
#endif
422 423
				}
			}
424 425
		}
	}
426 427

	pq_endmessage(&buf);
428
}