printtup.c 12.3 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * printtup.c
4 5 6
 *	  Routines to print out tuples to the destination (both frontend
 *	  clients and interactive backends are supported here).
 *
7
 *
B
Bruce Momjian 已提交
8
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
9
 * Portions Copyright (c) 1994, Regents of the University of California
10 11
 *
 * IDENTIFICATION
12
 *	  $Header: /cvsroot/pgsql/src/backend/access/common/printtup.c,v 1.67 2003/04/26 20:22:58 tgl Exp $
13 14 15
 *
 *-------------------------------------------------------------------------
 */
16 17 18 19
#include "postgres.h"

#include "access/heapam.h"
#include "access/printtup.h"
20
#include "libpq/libpq.h"
21
#include "libpq/pqformat.h"
22 23
#include "utils/lsyscache.h"

M
Marc G. Fournier 已提交
24

25
static void printtup_setup(DestReceiver *self, int operation,
B
Bruce Momjian 已提交
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);
30

31
/* ----------------------------------------------------------------
32
 *		printtup / debugtup support
33 34 35
 * ----------------------------------------------------------------
 */

36 37 38 39
/* ----------------
 *		Private state for a printtup destination object
 * ----------------
 */
B
Bruce Momjian 已提交
40 41
typedef struct
{								/* Per-attribute information */
42 43
	Oid			typoutput;		/* Oid for the attribute's type output fn */
	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 */
46
} PrinttupAttrInfo;
47

B
Bruce Momjian 已提交
48 49 50 51 52 53
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 */
54
} DR_printtup;
55 56 57 58 59

/* ----------------
 *		Initialize: create a DestReceiver for printtup
 * ----------------
 */
B
Bruce Momjian 已提交
60
DestReceiver *
61
printtup_create_DR(bool isBinary)
62
{
B
Bruce Momjian 已提交
63
	DR_printtup *self = (DR_printtup *) palloc(sizeof(DR_printtup));
64

65
	self->pub.receiveTuple = isBinary ? printtup_internal : printtup;
66 67 68 69 70 71 72
	self->pub.setup = printtup_setup;
	self->pub.cleanup = printtup_cleanup;

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

B
Bruce Momjian 已提交
73
	return (DestReceiver *) self;
74 75 76
}

static void
77 78
printtup_setup(DestReceiver *self, int operation,
			   const char *portalName, TupleDesc typeinfo)
79
{
80 81 82 83 84 85 86 87 88 89 90 91
	if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
	{
		/*
		 * Send portal name to frontend (obsolete cruft, gone in proto 3.0)
		 *
		 * If portal name not specified, use "blank" portal.
		 */
		if (portalName == NULL)
			portalName = "blank";

		pq_puttextmessage('P', portalName);
	}
92 93

	/*
B
Bruce Momjian 已提交
94 95
	 * if this is a retrieve, then we send back the tuple descriptor of
	 * the tuples.
96 97 98 99 100
	 */
	if (operation == CMD_SELECT)
	{
		Form_pg_attribute *attrs = typeinfo->attrs;
		int			natts = typeinfo->natts;
101
		int			proto = PG_PROTOCOL_MAJOR(FrontendProtocol);
102 103 104
		int			i;
		StringInfoData buf;

105
		pq_beginmessage(&buf, 'T'); /* tuple descriptor message type */
B
Bruce Momjian 已提交
106
		pq_sendint(&buf, natts, 2);		/* # of attrs in tuples */
107 108 109 110

		for (i = 0; i < natts; ++i)
		{
			pq_sendstring(&buf, NameStr(attrs[i]->attname));
111 112 113 114 115 116 117
			/* column ID info appears in protocol 3.0 and up */
			if (proto >= 3)
			{
				/* XXX not yet implemented, send zeroes */
				pq_sendint(&buf, 0, 4);
				pq_sendint(&buf, 0, 2);
			}
118 119 120 121
			pq_sendint(&buf, (int) attrs[i]->atttypid,
					   sizeof(attrs[i]->atttypid));
			pq_sendint(&buf, attrs[i]->attlen,
					   sizeof(attrs[i]->attlen));
122 123
			/* typmod appears in protocol 2.0 and up */
			if (proto >= 2)
124 125 126 127 128 129
				pq_sendint(&buf, attrs[i]->atttypmod,
						   sizeof(attrs[i]->atttypmod));
		}
		pq_endmessage(&buf);
	}

130 131
	/* ----------------
	 * We could set up the derived attr info at this time, but we postpone it
132
	 * until the first call of printtup, for 2 reasons:
133
	 * 1. We don't waste time (compared to the old way) if there are no
B
Bruce Momjian 已提交
134
	 *	  tuples at all to output.
135
	 * 2. Checking in printtup allows us to handle the case that the tuples
B
Bruce Momjian 已提交
136 137
	 *	  change type midway through (although this probably can't happen in
	 *	  the current executor).
138 139 140 141 142
	 * ----------------
	 */
}

static void
143
printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
144
{
B
Bruce Momjian 已提交
145
	int			i;
146 147

	if (myState->myinfo)
B
Bruce Momjian 已提交
148
		pfree(myState->myinfo); /* get rid of any old data */
149 150 151 152 153
	myState->myinfo = NULL;
	myState->attrinfo = typeinfo;
	myState->nattrs = numAttrs;
	if (numAttrs <= 0)
		return;
B
Bruce Momjian 已提交
154
	myState->myinfo = (PrinttupAttrInfo *)
155 156 157
		palloc(numAttrs * sizeof(PrinttupAttrInfo));
	for (i = 0; i < numAttrs; i++)
	{
B
Bruce Momjian 已提交
158 159
		PrinttupAttrInfo *thisState = myState->myinfo + i;

160 161 162
		if (getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
							  &thisState->typoutput, &thisState->typelem,
							  &thisState->typisvarlena))
163
			fmgr_info(thisState->typoutput, &thisState->finfo);
164 165 166
	}
}

167
/* ----------------
168
 *		printtup
169 170
 * ----------------
 */
171
static void
172
printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
173
{
B
Bruce Momjian 已提交
174
	DR_printtup *myState = (DR_printtup *) self;
175
	StringInfoData buf;
176
	int			natts = tuple->t_data->t_natts;
177 178
	int			i,
				j,
179
				k;
180

181
	/* Set or update my derived attribute info, if needed */
182 183
	if (myState->attrinfo != typeinfo || myState->nattrs != natts)
		printtup_prepare_info(myState, typeinfo, natts);
184

185 186
	/*
	 * tell the frontend to expect new tuple data (in ASCII style)
187
	 */
188
	pq_beginmessage(&buf, 'D');
189

190 191
	/*
	 * send a bitmap of which attributes are not null
192 193 194
	 */
	j = 0;
	k = 1 << 7;
195
	for (i = 0; i < natts; ++i)
196
	{
B
Bruce Momjian 已提交
197
		if (!heap_attisnull(tuple, i + 1))
198
			j |= k;				/* set bit if not null */
199
		k >>= 1;
200
		if (k == 0)				/* end of byte? */
201
		{
202
			pq_sendint(&buf, j, 1);
203 204 205
			j = 0;
			k = 1 << 7;
		}
206
	}
207
	if (k != (1 << 7))			/* flush last partial byte */
208
		pq_sendint(&buf, j, 1);
209

210 211
	/*
	 * send the attributes of this tuple
212
	 */
213
	for (i = 0; i < natts; ++i)
214
	{
B
Bruce Momjian 已提交
215
		PrinttupAttrInfo *thisState = myState->myinfo + i;
216 217 218 219
		Datum		origattr,
					attr;
		bool		isnull;
		char	   *outputstr;
B
Bruce Momjian 已提交
220

221
		origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
M
 
Marc G. Fournier 已提交
222 223
		if (isnull)
			continue;
224
		if (OidIsValid(thisState->typoutput))
225
		{
226
			/*
B
Bruce Momjian 已提交
227 228
			 * If we have a toasted datum, forcibly detoast it here to
			 * avoid memory leakage inside the type's output routine.
229 230 231 232 233 234
			 */
			if (thisState->typisvarlena)
				attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
			else
				attr = origattr;

235
			outputstr = DatumGetCString(FunctionCall3(&thisState->finfo,
B
Bruce Momjian 已提交
236 237 238
													  attr,
									ObjectIdGetDatum(thisState->typelem),
						  Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
239

240
			pq_sendcountedtext(&buf, outputstr, strlen(outputstr));
241 242 243 244

			/* Clean up detoasted copy, if any */
			if (attr != origattr)
				pfree(DatumGetPointer(attr));
245 246
			pfree(outputstr);
		}
M
 
Marc G. Fournier 已提交
247 248 249
		else
		{
			outputstr = "<unprintable>";
250
			pq_sendcountedtext(&buf, outputstr, strlen(outputstr));
251
		}
252
	}
253 254

	pq_endmessage(&buf);
255 256
}

257 258 259 260 261
/* ----------------
 *		printtup_cleanup
 * ----------------
 */
static void
262
printtup_cleanup(DestReceiver *self)
263
{
B
Bruce Momjian 已提交
264 265
	DR_printtup *myState = (DR_printtup *) self;

266 267 268 269 270
	if (myState->myinfo)
		pfree(myState->myinfo);
	pfree(myState);
}

271
/* ----------------
272
 *		printatt
273 274 275 276
 * ----------------
 */
static void
printatt(unsigned attributeId,
277
		 Form_pg_attribute attributeP,
278
		 char *value)
279
{
B
Bruce Momjian 已提交
280
	printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
281
		   attributeId,
282
		   NameStr(attributeP->attname),
283 284 285 286 287
		   value != NULL ? " = \"" : "",
		   value != NULL ? value : "",
		   value != NULL ? "\"" : "",
		   (unsigned int) (attributeP->atttypid),
		   attributeP->attlen,
B
Bruce Momjian 已提交
288
		   attributeP->atttypmod,
289
		   attributeP->attbyval ? 't' : 'f');
290 291 292
}

/* ----------------
293
 *		showatts
294 295
 * ----------------
 */
296 297
static void
showatts(const char *name, TupleDesc tupleDesc)
298
{
299
	int			natts = tupleDesc->natts;
300
	Form_pg_attribute *attinfo = tupleDesc->attrs;
301
	int			i;
302

303 304 305 306
	puts(name);
	for (i = 0; i < natts; ++i)
		printatt((unsigned) i + 1, attinfo[i], (char *) NULL);
	printf("\t----\n");
307 308 309
}

/* ----------------
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
 *		debugSetup - prepare to print tuples for an interactive backend
 * ----------------
 */
void
debugSetup(DestReceiver *self, int operation,
		   const char *portalName, TupleDesc typeinfo)
{
	/*
	 * show the return type of the tuples
	 */
	if (portalName == NULL)
		portalName = "blank";

	showatts(portalName, typeinfo);
}

/* ----------------
 *		debugtup - print one tuple for an interactive backend
328 329 330
 * ----------------
 */
void
331
debugtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
332
{
333
	int			natts = tuple->t_data->t_natts;
334
	int			i;
335 336
	Datum		origattr,
				attr;
337
	char	   *value;
338
	bool		isnull;
339 340
	Oid			typoutput,
				typelem;
341
	bool		typisvarlena;
342

343
	for (i = 0; i < natts; ++i)
344
	{
345
		origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
346 347
		if (isnull)
			continue;
348 349
		if (getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
							  &typoutput, &typelem, &typisvarlena))
350
		{
351
			/*
B
Bruce Momjian 已提交
352 353
			 * If we have a toasted datum, forcibly detoast it here to
			 * avoid memory leakage inside the type's output routine.
354 355 356 357 358 359
			 */
			if (typisvarlena)
				attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
			else
				attr = origattr;

360
			value = DatumGetCString(OidFunctionCall3(typoutput,
B
Bruce Momjian 已提交
361 362 363
													 attr,
											   ObjectIdGetDatum(typelem),
						  Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
364

365
			printatt((unsigned) i + 1, typeinfo->attrs[i], value);
366 367 368 369

			/* Clean up detoasted copy, if any */
			if (attr != origattr)
				pfree(DatumGetPointer(attr));
370 371
			pfree(value);
		}
372
	}
373
	printf("\t----\n");
374 375 376
}

/* ----------------
377 378 379
 *		printtup_internal
 *		We use a different data prefix, e.g. 'B' instead of 'D' to
 *		indicate a tuple in internal (binary) form.
380
 *
381
 *		This is largely same as printtup, except we don't use the typout func.
382 383
 * ----------------
 */
384
static void
385
printtup_internal(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
386
{
387
	DR_printtup *myState = (DR_printtup *) self;
388
	StringInfoData buf;
389
	int			natts = tuple->t_data->t_natts;
390 391 392
	int			i,
				j,
				k;
393 394 395 396

	/* Set or update my derived attribute info, if needed */
	if (myState->attrinfo != typeinfo || myState->nattrs != natts)
		printtup_prepare_info(myState, typeinfo, natts);
397

398 399
	/*
	 * tell the frontend to expect new tuple data (in binary style)
400
	 */
401
	pq_beginmessage(&buf, 'B');
402

403 404
	/*
	 * send a bitmap of which attributes are not null
405 406 407
	 */
	j = 0;
	k = 1 << 7;
408
	for (i = 0; i < natts; ++i)
409
	{
B
Bruce Momjian 已提交
410
		if (!heap_attisnull(tuple, i + 1))
411
			j |= k;				/* set bit if not null */
412
		k >>= 1;
413
		if (k == 0)				/* end of byte? */
414
		{
415
			pq_sendint(&buf, j, 1);
416 417 418
			j = 0;
			k = 1 << 7;
		}
419
	}
420
	if (k != (1 << 7))			/* flush last partial byte */
421
		pq_sendint(&buf, j, 1);
422

423 424
	/*
	 * send the attributes of this tuple
425
	 */
426
#ifdef IPORTAL_DEBUG
427
	fprintf(stderr, "sending tuple with %d atts\n", natts);
428
#endif
429 430

	for (i = 0; i < natts; ++i)
431
	{
432 433 434 435 436
		PrinttupAttrInfo *thisState = myState->myinfo + i;
		Datum		origattr,
					attr;
		bool		isnull;
		int32		len;
437

438 439 440 441 442
		origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
		if (isnull)
			continue;
		/* send # of bytes, and opaque data */
		if (thisState->typisvarlena)
443
		{
444 445 446 447 448 449
			/*
			 * If we have a toasted datum, must detoast before sending.
			 */
			attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));

			len = VARSIZE(attr) - VARHDRSZ;
450

451 452
			pq_sendint(&buf, len, VARHDRSZ);
			pq_sendbytes(&buf, VARDATA(attr), len);
453

454
#ifdef IPORTAL_DEBUG
455 456
			{
				char	   *d = VARDATA(attr);
457

458 459
				fprintf(stderr, "length %d data %x %x %x %x\n",
						len, *d, *(d + 1), *(d + 2), *(d + 3));
460
			}
461 462 463 464 465 466 467 468
#endif

			/* Clean up detoasted copy, if any */
			if (attr != origattr)
				pfree(DatumGetPointer(attr));
		}
		else
		{
469
			/* fixed size or cstring */
470 471
			attr = origattr;
			len = typeinfo->attrs[i]->attlen;
472 473 474 475 476 477
			if (len <= 0)
			{
				/* it's a cstring */
				Assert(len == -2 && !typeinfo->attrs[i]->attbyval);
				len = strlen(DatumGetCString(attr)) + 1;
			}
478 479
			pq_sendint(&buf, len, sizeof(int32));
			if (typeinfo->attrs[i]->attbyval)
480
			{
481 482 483 484 485 486 487 488
				Datum		datumBuf;

				/*
				 * We need this horsing around because we don't know how
				 * shorter data values are aligned within a Datum.
				 */
				store_att_byval(&datumBuf, attr, len);
				pq_sendbytes(&buf, (char *) &datumBuf, len);
489
#ifdef IPORTAL_DEBUG
490 491
				fprintf(stderr, "byval length %d data %ld\n", len,
						(long) attr);
492
#endif
493 494 495 496
			}
			else
			{
				pq_sendbytes(&buf, DatumGetPointer(attr), len);
497
#ifdef IPORTAL_DEBUG
498 499
				fprintf(stderr, "byref length %d data %p\n", len,
						DatumGetPointer(attr));
500
#endif
501
			}
502 503
		}
	}
504 505

	pq_endmessage(&buf);
506
}