printtup.c 11.3 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
B
Bruce Momjian 已提交
12
 *	  $Header: /cvsroot/pgsql/src/backend/access/common/printtup.c,v 1.58 2001/03/22 03:59:11 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 163
	pq_beginmessage(&buf);
	pq_sendbyte(&buf, 'D');
164 165

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

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

198
		origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
M
 
Marc G. Fournier 已提交
199 200
		if (isnull)
			continue;
201
		if (OidIsValid(thisState->typoutput))
202
		{
B
Bruce Momjian 已提交
203

204
			/*
B
Bruce Momjian 已提交
205 206
			 * If we have a toasted datum, forcibly detoast it here to
			 * avoid memory leakage inside the type's output routine.
207 208 209 210 211 212
			 */
			if (thisState->typisvarlena)
				attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
			else
				attr = origattr;

213
			outputstr = DatumGetCString(FunctionCall3(&thisState->finfo,
B
Bruce Momjian 已提交
214 215 216
													  attr,
									ObjectIdGetDatum(thisState->typelem),
						  Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
217

218
			pq_sendcountedtext(&buf, outputstr, strlen(outputstr));
219 220 221 222

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

	pq_endmessage(&buf);
233 234
}

235 236 237 238 239
/* ----------------
 *		printtup_cleanup
 * ----------------
 */
static void
240
printtup_cleanup(DestReceiver *self)
241
{
B
Bruce Momjian 已提交
242 243
	DR_printtup *myState = (DR_printtup *) self;

244 245 246 247 248
	if (myState->myinfo)
		pfree(myState->myinfo);
	pfree(myState);
}

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

/* ----------------
271
 *		showatts
272 273 274 275 276
 * ----------------
 */
void
showatts(char *name, TupleDesc tupleDesc)
{
277 278
	int			i;
	int			natts = tupleDesc->natts;
279
	Form_pg_attribute *attinfo = tupleDesc->attrs;
280

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

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

304
	for (i = 0; i < natts; ++i)
305
	{
306
		origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
307 308
		if (isnull)
			continue;
309 310
		if (getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
							  &typoutput, &typelem, &typisvarlena))
311
		{
B
Bruce Momjian 已提交
312

313
			/*
B
Bruce Momjian 已提交
314 315
			 * If we have a toasted datum, forcibly detoast it here to
			 * avoid memory leakage inside the type's output routine.
316 317 318 319 320 321
			 */
			if (typisvarlena)
				attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
			else
				attr = origattr;

322
			value = DatumGetCString(OidFunctionCall3(typoutput,
B
Bruce Momjian 已提交
323 324 325
													 attr,
											   ObjectIdGetDatum(typelem),
						  Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
326

327
			printatt((unsigned) i + 1, typeinfo->attrs[i], value);
328 329 330 331

			/* Clean up detoasted copy, if any */
			if (attr != origattr)
				pfree(DatumGetPointer(attr));
332 333
			pfree(value);
		}
334
	}
335
	printf("\t----\n");
336 337 338
}

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

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

	/* ----------------
361
	 *	tell the frontend to expect new tuple data (in binary style)
362 363
	 * ----------------
	 */
364 365
	pq_beginmessage(&buf);
	pq_sendbyte(&buf, 'B');
366 367

	/* ----------------
368
	 *	send a bitmap of which attributes are not null
369 370 371 372
	 * ----------------
	 */
	j = 0;
	k = 1 << 7;
373
	for (i = 0; i < natts; ++i)
374
	{
B
Bruce Momjian 已提交
375
		if (!heap_attisnull(tuple, i + 1))
376
			j |= k;				/* set bit if not null */
377
		k >>= 1;
378
		if (k == 0)				/* end of byte? */
379
		{
380
			pq_sendint(&buf, j, 1);
381 382 383
			j = 0;
			k = 1 << 7;
		}
384
	}
385
	if (k != (1 << 7))			/* flush last partial byte */
386
		pq_sendint(&buf, j, 1);
387 388 389 390 391

	/* ----------------
	 *	send the attributes of this tuple
	 * ----------------
	 */
392
#ifdef IPORTAL_DEBUG
393
	fprintf(stderr, "sending tuple with %d atts\n", natts);
394
#endif
395 396

	for (i = 0; i < natts; ++i)
397
	{
398 399 400 401 402
		PrinttupAttrInfo *thisState = myState->myinfo + i;
		Datum		origattr,
					attr;
		bool		isnull;
		int32		len;
403

404 405 406 407 408
		origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
		if (isnull)
			continue;
		/* send # of bytes, and opaque data */
		if (thisState->typisvarlena)
409
		{
B
Bruce Momjian 已提交
410

411 412 413 414 415 416
			/*
			 * If we have a toasted datum, must detoast before sending.
			 */
			attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));

			len = VARSIZE(attr) - VARHDRSZ;
417

418 419
			pq_sendint(&buf, len, VARHDRSZ);
			pq_sendbytes(&buf, VARDATA(attr), len);
420

421
#ifdef IPORTAL_DEBUG
422 423
			{
				char	   *d = VARDATA(attr);
424

425 426
				fprintf(stderr, "length %d data %x %x %x %x\n",
						len, *d, *(d + 1), *(d + 2), *(d + 3));
427
			}
428 429 430 431 432 433 434 435 436 437 438 439 440
#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)
441
			{
442 443 444 445 446 447 448 449
				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);
450
#ifdef IPORTAL_DEBUG
451 452
				fprintf(stderr, "byval length %d data %ld\n", len,
						(long) attr);
453
#endif
454 455 456 457
			}
			else
			{
				pq_sendbytes(&buf, DatumGetPointer(attr), len);
458
#ifdef IPORTAL_DEBUG
459 460
				fprintf(stderr, "byref length %d data %p\n", len,
						DatumGetPointer(attr));
461
#endif
462
			}
463 464
		}
	}
465 466

	pq_endmessage(&buf);
467
}