aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer/geqo/geqo_eval.c
blob: 67b967533fed06aa273496fcd3fe20a9c1840b20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/*------------------------------------------------------------------------
 *
 * geqo_eval.c--
 *	  Routines to evaluate query trees
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 * $Id: geqo_eval.c,v 1.18 1998/02/26 04:32:21 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */

/* contributed by:
   =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
   *  Martin Utesch				 * Institute of Automatic Control	   *
   =							 = University of Mining and Technology =
   *  utesch@aut.tu-freiberg.de  * Freiberg, Germany				   *
   =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
 */

#include "postgres.h"

#include <math.h>
#ifdef HAVE_LIMITS_H
#include <limits.h>
#ifndef MAXINT
#define MAXINT INT_MAX
#endif
#else
#include <values.h>
#endif

#include "nodes/pg_list.h"
#include "nodes/relation.h"
#include "nodes/primnodes.h"

#include "utils/palloc.h"
#include "utils/elog.h"

#include "optimizer/internal.h"
#include "optimizer/paths.h"
#include "optimizer/pathnode.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/tlist.h"
#include "optimizer/joininfo.h"

#include "optimizer/geqo_gene.h"
#include "optimizer/geqo.h"
#include "optimizer/geqo_paths.h"


static List *gimme_clause_joins(Query *root, Rel *outer_rel, Rel *inner_rel);
static Rel *gimme_clauseless_join(Rel *outer_rel, Rel *inner_rel);
static Rel *init_join_rel(Rel *outer_rel, Rel *inner_rel, JInfo *joininfo);
static List *new_join_tlist(List *tlist, List *other_relids, int first_resdomno);
static List *new_joininfo_list(List *joininfo_list, List *join_relids);
static void geqo_joinrel_size(Rel *joinrel, Rel *outer_rel, Rel *inner_rel);
static Rel *geqo_nth(int stop, List *rels);

/*
 * geqo_eval--
 *
 * Returns cost of a query tree as an individual of the population.
 */
Cost
geqo_eval(Query *root, Gene *tour, int num_gene)
{
	Rel		   *joinrel;
	Cost		fitness;
	List	   *temp;


/* remember root->join_relation_list_ ... */
/* because root->join_relation_list_ will be changed during the following */
	temp = listCopy(root->join_relation_list_);

/* joinrel is readily processed query tree -- left-sided ! */
	joinrel = gimme_tree(root, tour, 0, num_gene, NULL);

/* compute fitness */
	fitness = (Cost) joinrel->cheapestpath->path_cost;

	root->join_relation_list_ = listCopy(temp);

	pfree(joinrel);
	freeList(temp);

	return (fitness);

}

/*
 * gimme-tree --
 *	  this program presumes that only LEFT-SIDED TREES are considered!
 *
 * 'outer_rel' is the preceeding join
 *
 * Returns a new join relation incorporating all joins in a left-sided tree.
 */
Rel *
gimme_tree(Query *root, Gene *tour, int rel_count, int num_gene, Rel *outer_rel)
{
	Rel		   *inner_rel;		/* current relation */
	int			base_rel_index;

	List	   *new_rels = NIL;
	Rel		   *new_rel = NULL;

	if (rel_count < num_gene)
	{							/* tree not yet finished */

		/* tour[0] = 3; tour[1] = 1; tour[2] = 2 */
		base_rel_index = (int) tour[rel_count];

		inner_rel = (Rel *) geqo_nth(base_rel_index, root->base_relation_list_);

		if (rel_count == 0)
		{						/* processing first join with
								 * base_rel_index = (int) tour[0] */
			rel_count++;
			return gimme_tree(root, tour, rel_count, num_gene, inner_rel);
		}
		else
		{						/* tree main part */

			if (!(new_rels = gimme_clause_joins(root, outer_rel, inner_rel)))
			{
				if (BushyPlanFlag)
				{
					new_rels = lcons(gimme_clauseless_join(outer_rel, outer_rel), NIL); /* ??? MAU */
				}
				else
				{
					new_rels = lcons(gimme_clauseless_join(outer_rel, inner_rel), NIL);
				}
			}

			/* process new_rel->pathlist */
			find_all_join_paths(root, new_rels);

			/* prune new_rels */
			/* MAU: is this necessary? */

			/*
			 * what's the matter if more than one new rel is left till
			 * now?
			 */

			/*
			 * joinrels in newrels with different ordering of relids are
			 * not possible
			 */
			if (length(new_rels) > 1)
				new_rels = geqo_prune_rels(new_rels);

			if (length(new_rels) > 1)
			{					/* should never be reached ... */
				elog(DEBUG, "gimme_tree: still %d relations left", length(new_rels));
			}

			/* get essential new relation */
			new_rel = (Rel *) lfirst(new_rels);
			rel_count++;

			/* process new_rel->cheapestpath, new_rel->unorderedpath */
			geqo_rel_paths(new_rel);

			/* processing of other new_rel attributes */
			if (new_rel->size <= 0)
				new_rel->size = compute_rel_size(new_rel);
			new_rel->width = compute_rel_width(new_rel);

			root->join_relation_list_ = lcons(new_rel, NIL);

			return gimme_tree(root, tour, rel_count, num_gene, new_rel);
		}

	}

	return (outer_rel);			/* tree finished ... */
}

/*
 * gimme-clause-joins--
 *
 * 'outer-rel' is the relation entry for the outer relation
 * 'inner-rel' is the relation entry for the inner relation
 *
 * Returns a list of new join relations.
 */

static List *
gimme_clause_joins(Query *root, Rel *outer_rel, Rel *inner_rel)
{
	List	   *join_list = NIL;
	List	   *i = NIL;
	List	   *joininfo_list = (List *) outer_rel->joininfo;

	foreach(i, joininfo_list)
	{
		JInfo	   *joininfo = (JInfo *) lfirst(i);
		Rel		   *rel = NULL;

		if (!joininfo->inactive)
		{
			List	   *other_rels = (List *) joininfo->otherrels;

			if (other_rels != NIL)
			{
				if ((length(other_rels) == 1))
				{

					if (same(other_rels, inner_rel->relids))
					{			/* look if inner_rel is it... */
						rel = init_join_rel(outer_rel, inner_rel, joininfo);
					}
				}
				else if (BushyPlanFlag)
				{				/* ?!? MAU */
					rel = init_join_rel(outer_rel, get_join_rel(root, other_rels), joininfo);
				}
				else
				{
					rel = NULL;
				}

				if (rel != NULL)
					join_list = lappend(join_list, rel);

			}
		}
	}

	return (join_list);
}

/*
 * gimme-clauseless-join--
 *	  Given an outer relation 'outer-rel' and an inner relation
 *	  'inner-rel', create a join relation between 'outer-rel' and 'inner-rel'
 *
 * Returns a new join relation.
 */

static Rel *
gimme_clauseless_join(Rel *outer_rel, Rel *inner_rel)
{
	return (init_join_rel(outer_rel, inner_rel, (JInfo *) NULL));
}

/*
 * init-join-rel--
 *	  Creates and initializes a new join relation.
 *
 * 'outer-rel' and 'inner-rel' are relation nodes for the relations to be
 *		joined
 * 'joininfo' is the joininfo node(join clause) containing both
 *		'outer-rel' and 'inner-rel', if any exists
 *
 * Returns the new join relation node.
 */
static Rel *
init_join_rel(Rel *outer_rel, Rel *inner_rel, JInfo *joininfo)
{
	Rel		   *joinrel = makeNode(Rel);
	List	   *joinrel_joininfo_list = NIL;
	List	   *new_outer_tlist;
	List	   *new_inner_tlist;

	/*
	 * Create a new tlist by removing irrelevant elements from both tlists
	 * of the outer and inner join relations and then merging the results
	 * together.
	 */
	new_outer_tlist =
		new_join_tlist(outer_rel->targetlist,	/* XXX 1-based attnos */
					   inner_rel->relids, 1);
	new_inner_tlist =
		new_join_tlist(inner_rel->targetlist,	/* XXX 1-based attnos */
					   outer_rel->relids,
					   length(new_outer_tlist) + 1);

	joinrel->relids = NIL;
	joinrel->indexed = false;
	joinrel->pages = 0;
	joinrel->tuples = 0;
	joinrel->width = 0;
/*	  joinrel->targetlist = NIL;*/
	joinrel->pathlist = NIL;
	joinrel->unorderedpath = (Path *) NULL;
	joinrel->cheapestpath = (Path *) NULL;
	joinrel->pruneable = true;
	joinrel->classlist = NULL;
	joinrel->relam = InvalidOid;
	joinrel->ordering = NULL;
	joinrel->clauseinfo = NIL;
	joinrel->joininfo = NULL;
	joinrel->innerjoin = NIL;
	joinrel->superrels = NIL;

	joinrel->relids = lcons(outer_rel->relids, lcons(inner_rel->relids, NIL));

	new_outer_tlist = nconc(new_outer_tlist, new_inner_tlist);
	joinrel->targetlist = new_outer_tlist;

	if (joininfo)
	{
		joinrel->clauseinfo = joininfo->jinfoclauseinfo;
		if (BushyPlanFlag)
			joininfo->inactive = true;
	}

	joinrel_joininfo_list =
		new_joininfo_list(append(outer_rel->joininfo, inner_rel->joininfo),
						intAppend(outer_rel->relids, inner_rel->relids));

	joinrel->joininfo = joinrel_joininfo_list;

	geqo_joinrel_size(joinrel, outer_rel, inner_rel);

	return (joinrel);
}

/*
 * new-join-tlist--
 *	  Builds a join relations's target list by keeping those elements that
 *	  will be in the final target list and any other elements that are still
 *	  needed for future joins.	For a target list entry to still be needed
 *	  for future joins, its 'joinlist' field must not be empty after removal
 *	  of all relids in 'other-relids'.
 *
 * 'tlist' is the target list of one of the join relations
 * 'other-relids' is a list of relids contained within the other
 *				join relation
 * 'first-resdomno' is the resdom number to use for the first created
 *				target list entry
 *
 * Returns the new target list.
 */
static List *
new_join_tlist(List *tlist,
			   List *other_relids,
			   int first_resdomno)
{
	int			resdomno = first_resdomno - 1;
	TargetEntry *xtl = NULL;
	List	   *temp_node = NIL;
	List	   *t_list = NIL;
	List	   *i = NIL;
	List	   *join_list = NIL;
	bool		in_final_tlist = false;


	foreach(i, tlist)
	{
		xtl = lfirst(i);
		in_final_tlist = (join_list == NIL);
		if (in_final_tlist)
		{
			resdomno += 1;
			temp_node =
				lcons(create_tl_element(get_expr(xtl),
										resdomno),
					  NIL);
			t_list = nconc(t_list, temp_node);
		}
	}

	return (t_list);
}

/*
 * new-joininfo-list--
 *	  Builds a join relation's joininfo list by checking for join clauses
 *	  which still need to used in future joins involving this relation.  A
 *	  join clause is still needed if there are still relations in the clause
 *	  not contained in the list of relations comprising this join relation.
 *	  New joininfo nodes are only created and added to
 *	  'current-joininfo-list' if a node for a particular join hasn't already
 *	  been created.
 *
 * 'current-joininfo-list' contains a list of those joininfo nodes that
 *		have already been built
 * 'joininfo-list' is the list of join clauses involving this relation
 * 'join-relids' is a list of relids corresponding to the relations
 *		currently being joined
 *
 * Returns a list of joininfo nodes, new and old.
 */
static List *
new_joininfo_list(List *joininfo_list, List *join_relids)
{
	List	   *current_joininfo_list = NIL;
	List	   *new_otherrels = NIL;
	JInfo	   *other_joininfo = (JInfo *) NULL;
	List	   *xjoininfo = NIL;

	foreach(xjoininfo, joininfo_list)
	{
		List	   *or;
		JInfo	   *joininfo = (JInfo *) lfirst(xjoininfo);

		new_otherrels = joininfo->otherrels;
		foreach(or, new_otherrels)
		{
			if (intMember(lfirsti(or), join_relids))
				new_otherrels = lremove((void *) lfirst(or), new_otherrels);
		}
		joininfo->otherrels = new_otherrels;
		if (new_otherrels != NIL)
		{
			other_joininfo = joininfo_member(new_otherrels,
											 current_joininfo_list);
			if (other_joininfo)
			{
				other_joininfo->jinfoclauseinfo =
					(List *) LispUnion(joininfo->jinfoclauseinfo,
									   other_joininfo->jinfoclauseinfo);
			}
			else
			{
				other_joininfo = makeNode(JInfo);

				other_joininfo->otherrels =
					joininfo->otherrels;
				other_joininfo->jinfoclauseinfo =
					joininfo->jinfoclauseinfo;
				other_joininfo->mergesortable =
					joininfo->mergesortable;
				other_joininfo->hashjoinable =
					joininfo->hashjoinable;
				other_joininfo->inactive = false;

				current_joininfo_list = lcons(other_joininfo,
											  current_joininfo_list);
			}
		}
	}

	return (current_joininfo_list);
}

#ifdef	NOTUSED
/*
 * add-new-joininfos--
 *	  For each new join relation, create new joininfos that
 *	  use the join relation as inner relation, and add
 *	  the new joininfos to those rel nodes that still
 *	  have joins with the join relation.
 *
 * 'joinrels' is a list of join relations.
 *
 * Modifies the joininfo field of appropriate rel nodes.
 */
static void
geqo_add_new_joininfos(Query *root, List *joinrels, List *outerrels)
{
	List	   *xjoinrel = NIL;
	List	   *xrelid = NIL;
	List	   *xrel = NIL;
	List	   *xjoininfo = NIL;

	Rel		   *rel;
	List	   *relids;

	List	   *super_rels;
	List	   *xsuper_rel = NIL;
	JInfo	   *new_joininfo;

	foreach(xjoinrel, joinrels)
	{
		Rel		   *joinrel = (Rel *) lfirst(xjoinrel);

		foreach(xrelid, joinrel->relids)
		{

			/*
			 * length(joinrel->relids) should always be greater that 1,
			 * because of *JOIN*
			 */

			/*
			 * ! BUG BUG ! Relid relid = (Relid)lfirst(xrelid); Rel *rel =
			 * get_join_rel(root, relid);
			 */

			/*
			 * if ( (root->join_relation_list_) != NIL ) { rel =
			 * get_join_rel(root, xrelid); } else { rel =
			 * get_base_rel(root, lfirsti(xrelid)); }
			 */

			/* NOTE: STILL BUGGY FOR CLAUSE-JOINS: */

			/*
			 * relids = lconsi(lfirsti(xrelid), NIL); rel =
			 * rel_member(relids, outerrels);
			 */

			relids = lconsi(lfirsti(xrelid), NIL);
			rel = rel_member(relids, root->base_relation_list_);

			add_superrels(rel, joinrel);
		}
	}
	foreach(xjoinrel, joinrels)
	{
		Rel		   *joinrel = (Rel *) lfirst(xjoinrel);

		foreach(xjoininfo, joinrel->joininfo)
		{
			JInfo	   *joininfo = (JInfo *) lfirst(xjoininfo);
			List	   *other_rels = joininfo->otherrels;
			List	   *clause_info = joininfo->jinfoclauseinfo;
			bool		mergesortable = joininfo->mergesortable;
			bool		hashjoinable = joininfo->hashjoinable;

			foreach(xrelid, other_rels)
			{

				/*
				 * ! BUG BUG ! Relid relid = (Relid)lfirst(xrelid); Rel
				 * *rel = get_join_rel(root, relid);
				 */

				/*
				 * if ( (root->join_relation_list_) != NIL ) { rel =
				 * get_join_rel(root, xrelid); } else { rel =
				 * get_base_rel(root, lfirsti(xrelid)); }
				 */

				/* NOTE: STILL BUGGY FOR CLAUSE-JOINS: */

				/*
				 * relids = lconsi(lfirsti(xrelid), NIL); rel =
				 * rel_member(relids, outerrels);
				 */

				relids = lconsi(lfirsti(xrelid), NIL);
				rel = rel_member(relids, root->base_relation_list_);

				super_rels = rel->superrels;
				new_joininfo = makeNode(JInfo);

				new_joininfo->otherrels = joinrel->relids;
				new_joininfo->jinfoclauseinfo = clause_info;
				new_joininfo->mergesortable = mergesortable;
				new_joininfo->hashjoinable = hashjoinable;
				new_joininfo->inactive = false;
				rel->joininfo =
					lappend(rel->joininfo, new_joininfo);

				foreach(xsuper_rel, super_rels)
				{
					Rel		   *super_rel = (Rel *) lfirst(xsuper_rel);

					if (nonoverlap_rels(super_rel, joinrel))
					{
						List	   *new_relids = super_rel->relids;
						JInfo	   *other_joininfo =
						joininfo_member(new_relids,
										joinrel->joininfo);

						if (other_joininfo)
						{
							other_joininfo->jinfoclauseinfo =
								(List *) LispUnion(clause_info,
										other_joininfo->jinfoclauseinfo);
						}
						else
						{
							JInfo	   *new_joininfo = makeNode(JInfo);

							new_joininfo->otherrels = new_relids;
							new_joininfo->jinfoclauseinfo = clause_info;
							new_joininfo->mergesortable = mergesortable;
							new_joininfo->hashjoinable = hashjoinable;
							new_joininfo->inactive = false;
							joinrel->joininfo =
								lappend(joinrel->joininfo,
										new_joininfo);
						}
					}
				}
			}
		}
	}
	foreach(xrel, outerrels)
	{
		rel = (Rel *) lfirst(xrel);
		rel->superrels = NIL;
	}
}

/*
 * final-join-rels--
 *	   Find the join relation that includes all the original
 *	   relations, i.e. the final join result.
 *
 * 'join-rel-list' is a list of join relations.
 *
 * Returns the list of final join relations.
 */
static List *
geqo_final_join_rels(List *join_rel_list)
{
	List	   *xrel = NIL;
	List	   *temp = NIL;
	List	   *t_list = NIL;

	/*
	 * find the relations that has no further joins, i.e., its joininfos
	 * all have otherrels nil.
	 */
	foreach(xrel, join_rel_list)
	{
		Rel		   *rel = (Rel *) lfirst(xrel);
		List	   *xjoininfo = NIL;
		bool		final = true;

		foreach(xjoininfo, rel->joininfo)
		{
			JInfo	   *joininfo = (JInfo *) lfirst(xjoininfo);

			if (joininfo->otherrels != NIL)
			{
				final = false;
				break;
			}
		}
		if (final)
		{
			temp = lcons(rel, NIL);
			t_list = nconc(t_list, temp);
		}
	}

	return (t_list);
}

/*
 * add_superrels--
 *	  add rel to the temporary property list superrels.
 *
 * 'rel' a rel node
 * 'super-rel' rel node of a join relation that includes rel
 *
 * Modifies the superrels field of rel
 */
static void
add_superrels(Rel *rel, Rel *super_rel)
{
	rel->superrels = lappend(rel->superrels, super_rel);
}

/*
 * nonoverlap-rels--
 *	  test if two join relations overlap, i.e., includes the same
 *	  relation.
 *
 * 'rel1' and 'rel2' are two join relations
 *
 * Returns non-nil if rel1 and rel2 do not overlap.
 */
static bool
nonoverlap_rels(Rel *rel1, Rel *rel2)
{
	return (nonoverlap_sets(rel1->relids, rel2->relids));
}

static bool
nonoverlap_sets(List *s1, List *s2)
{
	List	   *x = NIL;

	foreach(x, s1)
	{
		int			e = lfirsti(x);

		if (intMember(e, s2))
			return (false);
	}
	return (true);
}

#endif							/* NOTUSED */

/*
 * geqo_joinrel_size--
 *	  compute estimate for join relation tuples, even for
 *	  long join queries; so get logarithm of size when MAXINT overflow;
 */
static void
geqo_joinrel_size(Rel *joinrel, Rel *outer_rel, Rel *inner_rel)
{
	Cost		temp;
	int			ntuples;

	temp = (Cost) inner_rel->tuples * (Cost) outer_rel->tuples; /* cartesian product */

	if (joinrel->clauseinfo)
	{
		temp = temp * product_selec(joinrel->clauseinfo);
	}

	if (temp >= (MAXINT - 1))
	{
		ntuples = ceil(geqo_log((double) temp, (double) GEQO_LOG_BASE));
	}
	else
	{
		ntuples = ceil((double) temp);
	}

	if (ntuples < 1)
		ntuples = 1;			/* make the best case 1 instead of 0 */

	joinrel->tuples = ntuples;
}

double
geqo_log(double x, double b)
{
	return (log(x) / log(b));
}

static Rel *
geqo_nth(int stop, List *rels)
{
	List	   *r;
	int			i = 1;

	foreach(r, rels)
	{
		if (i == stop)
			return lfirst(r);
		i++;
	}
	elog(ERROR, "geqo_nth: Internal error - ran off end of list");
	return NULL;				/* to keep compiler happy */
}