printtup.c 11.1 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
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
8
 * Portions Copyright (c) 1994, Regents of the University of California
9 10 11
 *
 *
 * IDENTIFICATION
12
 *	  $Header: /cvsroot/pgsql/src/backend/access/common/printtup.c,v 1.60 2001/10/25 05:49:20 momjian Exp $
13 14 15
 *
 *-------------------------------------------------------------------------
 */
16 17 18 19 20 21 22
#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 已提交
23

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

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

/* ----------------
35
 *		getTypeOutputInfo -- get info needed for printing values of a type
36 37
 * ----------------
 */
38 39 40
bool
getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem,
				  bool *typIsVarlena)
41
{
42
	HeapTuple	typeTuple;
43
	Form_pg_type pt;
44

45 46 47 48
	typeTuple = SearchSysCache(TYPEOID,
							   ObjectIdGetDatum(type),
							   0, 0, 0);
	if (!HeapTupleIsValid(typeTuple))
49
		elog(ERROR, "getTypeOutputInfo: Cache lookup of type %u failed", type);
50
	pt = (Form_pg_type) GETSTRUCT(typeTuple);
51

52 53
	*typOutput = pt->typoutput;
	*typElem = pt->typelem;
B
Bruce Momjian 已提交
54
	*typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
55 56
	ReleaseSysCache(typeTuple);
	return OidIsValid(*typOutput);
57 58
}

59 60 61 62
/* ----------------
 *		Private state for a printtup destination object
 * ----------------
 */
B
Bruce Momjian 已提交
63 64
typedef struct
{								/* Per-attribute information */
65 66
	Oid			typoutput;		/* Oid for the attribute's type output fn */
	Oid			typelem;		/* typelem value to pass to the output fn */
67
	bool		typisvarlena;	/* is it varlena (ie possibly toastable)? */
68
	FmgrInfo	finfo;			/* Precomputed call info for typoutput */
69
} PrinttupAttrInfo;
70

B
Bruce Momjian 已提交
71 72 73 74 75 76
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 */
77
} DR_printtup;
78 79 80 81 82

/* ----------------
 *		Initialize: create a DestReceiver for printtup
 * ----------------
 */
B
Bruce Momjian 已提交
83
DestReceiver *
84
printtup_create_DR(bool isBinary)
85
{
B
Bruce Momjian 已提交
86
	DR_printtup *self = (DR_printtup *) palloc(sizeof(DR_printtup));
87

88
	self->pub.receiveTuple = isBinary ? printtup_internal : printtup;
89 90 91 92 93 94 95
	self->pub.setup = printtup_setup;
	self->pub.cleanup = printtup_cleanup;

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

B
Bruce Momjian 已提交
96
	return (DestReceiver *) self;
97 98 99
}

static void
100
printtup_setup(DestReceiver *self, TupleDesc typeinfo)
101 102 103 104 105
{
	/* ----------------
	 * 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 已提交
106
	 *	  tuples at all to output.
107
	 * 2. Checking in printtup allows us to handle the case that the tuples
B
Bruce Momjian 已提交
108 109
	 *	  change type midway through (although this probably can't happen in
	 *	  the current executor).
110 111 112 113 114 115
	 * 3. Right now, ExecutorRun passes a NULL for typeinfo anyway :-(
	 * ----------------
	 */
}

static void
116
printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
117
{
B
Bruce Momjian 已提交
118
	int			i;
119 120

	if (myState->myinfo)
B
Bruce Momjian 已提交
121
		pfree(myState->myinfo); /* get rid of any old data */
122 123 124 125 126
	myState->myinfo = NULL;
	myState->attrinfo = typeinfo;
	myState->nattrs = numAttrs;
	if (numAttrs <= 0)
		return;
B
Bruce Momjian 已提交
127
	myState->myinfo = (PrinttupAttrInfo *)
128 129 130
		palloc(numAttrs * sizeof(PrinttupAttrInfo));
	for (i = 0; i < numAttrs; i++)
	{
B
Bruce Momjian 已提交
131 132
		PrinttupAttrInfo *thisState = myState->myinfo + i;

133 134 135
		if (getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
							  &thisState->typoutput, &thisState->typelem,
							  &thisState->typisvarlena))
136
			fmgr_info(thisState->typoutput, &thisState->finfo);
137 138 139
	}
}

140
/* ----------------
141
 *		printtup
142 143
 * ----------------
 */
144
static void
145
printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
146
{
B
Bruce Momjian 已提交
147
	DR_printtup *myState = (DR_printtup *) self;
148
	StringInfoData buf;
149
	int			natts = tuple->t_data->t_natts;
150 151
	int			i,
				j,
152
				k;
153

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

158 159
	/*
	 * tell the frontend to expect new tuple data (in ASCII style)
160
	 */
161 162
	pq_beginmessage(&buf);
	pq_sendbyte(&buf, 'D');
163

164 165
	/*
	 * send a bitmap of which attributes are not null
166 167 168
	 */
	j = 0;
	k = 1 << 7;
169
	for (i = 0; i < natts; ++i)
170
	{
B
Bruce Momjian 已提交
171
		if (!heap_attisnull(tuple, i + 1))
172
			j |= k;				/* set bit if not null */
173
		k >>= 1;
174
		if (k == 0)				/* end of byte? */
175
		{
176
			pq_sendint(&buf, j, 1);
177 178 179
			j = 0;
			k = 1 << 7;
		}
180
	}
181
	if (k != (1 << 7))			/* flush last partial byte */
182
		pq_sendint(&buf, j, 1);
183

184 185
	/*
	 * send the attributes of this tuple
186
	 */
187
	for (i = 0; i < natts; ++i)
188
	{
B
Bruce Momjian 已提交
189
		PrinttupAttrInfo *thisState = myState->myinfo + i;
190 191 192 193
		Datum		origattr,
					attr;
		bool		isnull;
		char	   *outputstr;
B
Bruce Momjian 已提交
194

195
		origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
M
 
Marc G. Fournier 已提交
196 197
		if (isnull)
			continue;
198
		if (OidIsValid(thisState->typoutput))
199
		{
200
			/*
B
Bruce Momjian 已提交
201 202
			 * If we have a toasted datum, forcibly detoast it here to
			 * avoid memory leakage inside the type's output routine.
203 204 205 206 207 208
			 */
			if (thisState->typisvarlena)
				attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
			else
				attr = origattr;

209
			outputstr = DatumGetCString(FunctionCall3(&thisState->finfo,
B
Bruce Momjian 已提交
210 211 212
													  attr,
									ObjectIdGetDatum(thisState->typelem),
						  Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
213

214
			pq_sendcountedtext(&buf, outputstr, strlen(outputstr));
215 216 217 218

			/* Clean up detoasted copy, if any */
			if (attr != origattr)
				pfree(DatumGetPointer(attr));
219 220
			pfree(outputstr);
		}
M
 
Marc G. Fournier 已提交
221 222 223
		else
		{
			outputstr = "<unprintable>";
224
			pq_sendcountedtext(&buf, outputstr, strlen(outputstr));
225
		}
226
	}
227 228

	pq_endmessage(&buf);
229 230
}

231 232 233 234 235
/* ----------------
 *		printtup_cleanup
 * ----------------
 */
static void
236
printtup_cleanup(DestReceiver *self)
237
{
B
Bruce Momjian 已提交
238 239
	DR_printtup *myState = (DR_printtup *) self;

240 241 242 243 244
	if (myState->myinfo)
		pfree(myState->myinfo);
	pfree(myState);
}

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

/* ----------------
267
 *		showatts
268 269 270 271 272
 * ----------------
 */
void
showatts(char *name, TupleDesc tupleDesc)
{
273 274
	int			i;
	int			natts = tupleDesc->natts;
275
	Form_pg_attribute *attinfo = tupleDesc->attrs;
276

277 278 279 280
	puts(name);
	for (i = 0; i < natts; ++i)
		printatt((unsigned) i + 1, attinfo[i], (char *) NULL);
	printf("\t----\n");
281 282 283
}

/* ----------------
284
 *		debugtup
285 286 287
 * ----------------
 */
void
288
debugtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
289
{
290
	int			natts = tuple->t_data->t_natts;
291
	int			i;
292 293
	Datum		origattr,
				attr;
294
	char	   *value;
295
	bool		isnull;
296 297
	Oid			typoutput,
				typelem;
298
	bool		typisvarlena;
299

300
	for (i = 0; i < natts; ++i)
301
	{
302
		origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
303 304
		if (isnull)
			continue;
305 306
		if (getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
							  &typoutput, &typelem, &typisvarlena))
307
		{
308
			/*
B
Bruce Momjian 已提交
309 310
			 * If we have a toasted datum, forcibly detoast it here to
			 * avoid memory leakage inside the type's output routine.
311 312 313 314 315 316
			 */
			if (typisvarlena)
				attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
			else
				attr = origattr;

317
			value = DatumGetCString(OidFunctionCall3(typoutput,
B
Bruce Momjian 已提交
318 319 320
													 attr,
											   ObjectIdGetDatum(typelem),
						  Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
321

322
			printatt((unsigned) i + 1, typeinfo->attrs[i], value);
323 324 325 326

			/* Clean up detoasted copy, if any */
			if (attr != origattr)
				pfree(DatumGetPointer(attr));
327 328
			pfree(value);
		}
329
	}
330
	printf("\t----\n");
331 332 333
}

/* ----------------
334 335 336
 *		printtup_internal
 *		We use a different data prefix, e.g. 'B' instead of 'D' to
 *		indicate a tuple in internal (binary) form.
337
 *
338
 *		This is largely same as printtup, except we don't use the typout func.
339 340
 * ----------------
 */
341
static void
342
printtup_internal(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
343
{
344
	DR_printtup *myState = (DR_printtup *) self;
345
	StringInfoData buf;
346
	int			natts = tuple->t_data->t_natts;
347 348 349
	int			i,
				j,
				k;
350 351 352 353

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

355 356
	/*
	 * tell the frontend to expect new tuple data (in binary style)
357
	 */
358 359
	pq_beginmessage(&buf);
	pq_sendbyte(&buf, 'B');
360

361 362
	/*
	 * send a bitmap of which attributes are not null
363 364 365
	 */
	j = 0;
	k = 1 << 7;
366
	for (i = 0; i < natts; ++i)
367
	{
B
Bruce Momjian 已提交
368
		if (!heap_attisnull(tuple, i + 1))
369
			j |= k;				/* set bit if not null */
370
		k >>= 1;
371
		if (k == 0)				/* end of byte? */
372
		{
373
			pq_sendint(&buf, j, 1);
374 375 376
			j = 0;
			k = 1 << 7;
		}
377
	}
378
	if (k != (1 << 7))			/* flush last partial byte */
379
		pq_sendint(&buf, j, 1);
380

381 382
	/*
	 * send the attributes of this tuple
383
	 */
384
#ifdef IPORTAL_DEBUG
385
	fprintf(stderr, "sending tuple with %d atts\n", natts);
386
#endif
387 388

	for (i = 0; i < natts; ++i)
389
	{
390 391 392 393 394
		PrinttupAttrInfo *thisState = myState->myinfo + i;
		Datum		origattr,
					attr;
		bool		isnull;
		int32		len;
395

396 397 398 399 400
		origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
		if (isnull)
			continue;
		/* send # of bytes, and opaque data */
		if (thisState->typisvarlena)
401
		{
402 403 404 405 406 407
			/*
			 * If we have a toasted datum, must detoast before sending.
			 */
			attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));

			len = VARSIZE(attr) - VARHDRSZ;
408

409 410
			pq_sendint(&buf, len, VARHDRSZ);
			pq_sendbytes(&buf, VARDATA(attr), len);
411

412
#ifdef IPORTAL_DEBUG
413 414
			{
				char	   *d = VARDATA(attr);
415

416 417
				fprintf(stderr, "length %d data %x %x %x %x\n",
						len, *d, *(d + 1), *(d + 2), *(d + 3));
418
			}
419 420 421 422 423 424 425 426 427 428 429 430 431
#endif

			/* Clean up detoasted copy, if any */
			if (attr != origattr)
				pfree(DatumGetPointer(attr));
		}
		else
		{
			/* fixed size */
			attr = origattr;
			len = typeinfo->attrs[i]->attlen;
			pq_sendint(&buf, len, sizeof(int32));
			if (typeinfo->attrs[i]->attbyval)
432
			{
433 434 435 436 437 438 439 440
				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);
441
#ifdef IPORTAL_DEBUG
442 443
				fprintf(stderr, "byval length %d data %ld\n", len,
						(long) attr);
444
#endif
445 446 447 448
			}
			else
			{
				pq_sendbytes(&buf, DatumGetPointer(attr), len);
449
#ifdef IPORTAL_DEBUG
450 451
				fprintf(stderr, "byref length %d data %p\n", len,
						DatumGetPointer(attr));
452
#endif
453
			}
454 455
		}
	}
456 457

	pq_endmessage(&buf);
458
}