printtup.c 10.2 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
 *
B
Add:  
Bruce Momjian 已提交
7 8
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * 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.54 2000/11/16 22:30:15 tgl Exp $
13 14 15 16
 *
 *-------------------------------------------------------------------------
 */

17

18 19 20 21 22 23 24
#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 已提交
25

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

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

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

49 50 51 52 53 54
	typeTuple = SearchSysCache(TYPEOID,
							   ObjectIdGetDatum(type),
							   0, 0, 0);
	if (!HeapTupleIsValid(typeTuple))
		elog(ERROR, "getTypeOutAndElem: Cache lookup of type %u failed", type);
	pt = (Form_pg_type) GETSTRUCT(typeTuple);
55

56 57 58 59
	*typOutput = pt->typoutput;
	*typElem = pt->typelem;
	ReleaseSysCache(typeTuple);
	return OidIsValid(*typOutput);
60 61
}

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

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

/* ----------------
 *		Initialize: create a DestReceiver for printtup
 * ----------------
 */
B
Bruce Momjian 已提交
85
DestReceiver *
86 87
printtup_create_DR()
{
B
Bruce Momjian 已提交
88
	DR_printtup *self = (DR_printtup *) palloc(sizeof(DR_printtup));
89 90 91 92 93 94 95 96 97

	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 已提交
98
	return (DestReceiver *) self;
99 100 101
}

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

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

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

135 136 137
		if (getTypeOutAndElem((Oid) typeinfo->attrs[i]->atttypid,
							  &thisState->typoutput, &thisState->typelem))
			fmgr_info(thisState->typoutput, &thisState->finfo);
138 139 140
	}
}

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

157 158 159 160 161
	/* 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);

162
	/* ----------------
163
	 *	tell the frontend to expect new tuple data (in ASCII style)
164 165
	 * ----------------
	 */
166 167
	pq_beginmessage(&buf);
	pq_sendbyte(&buf, 'D');
168 169

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

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

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

	pq_endmessage(&buf);
218 219
}

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

229 230 231 232 233
	if (myState->myinfo)
		pfree(myState->myinfo);
	pfree(myState);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	pq_endmessage(&buf);
427
}