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
/*-------------------------------------------------------------------------
*
* psort.c--
* Polyphase merge sort.
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/sort/Attic/psort.c,v 1.3 1996/11/06 10:32:10 scrappy Exp $
*
* NOTES
* Sorts the first relation into the second relation. The sort may
* not be called twice simultaneously.
*
* Use the tape-splitting method (Knuth, Vol. III, pp281-86) in the future.
*
* Arguments? Variables?
* MAXMERGE, MAXTAPES
*-------------------------------------------------------------------------
*/
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include "postgres.h"
#include "executor/execdebug.h"
#include "access/heapam.h"
#include "access/htup.h"
#include "access/relscan.h"
#include "access/skey.h"
#include "utils/tqual.h" /* for NowTimeQual */
#include "storage/buf.h"
#include "storage/bufmgr.h" /* for BLCKSZ */
#include "utils/portal.h" /* for {Start,End}PortalAllocMode */
#include "utils/rel.h"
#include "utils/psort.h"
#include "utils/lselect.h"
#include "storage/fd.h"
#ifndef HAVE_MEMMOVE
# include <regex/utils.h>
#else
# include <string.h>
#endif
#define TEMPDIR "./"
int Nkeys;
ScanKey Key;
int SortMemory;
static int TapeRange; /* number of tapes - 1 (T) */
static int Level; /* (l) */
static int TotalDummy; /* summation of tp_dummy */
static struct tape Tape[MAXTAPES];
static long shortzero = 0; /* used to delimit runs */
static struct tuple *LastTuple = NULL; /* last output */
static int BytesRead; /* to keep track of # of IO */
static int BytesWritten;
Relation SortRdesc; /* current tuples in memory */
struct leftist *Tuples; /* current tuples in memory */
/*
* psort - polyphase merge sort entry point
*/
void
psort(Relation oldrel, Relation newrel, int nkeys, ScanKey key)
{
AssertArg(nkeys >= 1);
AssertArg(key[0].sk_attno != 0);
AssertArg(key[0].sk_procedure != 0);
Nkeys = nkeys;
Key = key;
SortMemory = 0;
SortRdesc = oldrel;
BytesRead = 0;
BytesWritten = 0;
/*
* may not be the best place.
*
* Pass 0 for the "limit" as the argument is currently ignored.
* Previously, only one arg was passed. -mer 12 Nov. 1991
*/
StartPortalAllocMode(StaticAllocMode, (Size)0);
initpsort();
initialrun(oldrel);
/* call finalrun(newrel, mergerun()) instead */
endpsort(newrel, mergeruns());
EndPortalAllocMode();
NDirectFileRead += (int)ceil((double)BytesRead / BLCKSZ);
NDirectFileWrite += (int)ceil((double)BytesWritten / BLCKSZ);
}
/*
* TAPENO - number of tape in Tape
*/
#define TAPENO(NODE) (NODE - Tape)
#define TUPLENO(TUP) ((TUP == NULL) ? -1 : (int) TUP->t_iid)
/*
* initpsort - initializes the tapes
* - (polyphase merge Alg.D(D1)--Knuth, Vol.3, p.270)
* Returns:
* number of allocated tapes
*/
void
initpsort()
{
register int i;
register struct tape *tp;
/*
ASSERT(ntapes >= 3 && ntapes <= MAXTAPES,
"initpsort: Invalid number of tapes to initialize.\n");
*/
tp = Tape;
for (i = 0; i < MAXTAPES && (tp->tp_file = gettape()) != NULL; i++) {
tp->tp_dummy = 1;
tp->tp_fib = 1;
tp->tp_prev = tp - 1;
tp++;
}
TapeRange = --tp - Tape;
tp->tp_dummy = 0;
tp->tp_fib = 0;
Tape[0].tp_prev = tp;
if (TapeRange <= 1)
elog(WARN, "initpsort: Could only allocate %d < 3 tapes\n",
TapeRange + 1);
Level = 1;
TotalDummy = TapeRange;
SortMemory = SORTMEM;
LastTuple = NULL;
Tuples = NULL;
}
/*
* resetpsort - resets (frees) malloc'd memory for an aborted Xaction
*
* Not implemented yet.
*/
void
resetpsort()
{
;
}
/*
* PUTTUP - writes the next tuple
* ENDRUN - mark end of run
* GETLEN - reads the length of the next tuple
* ALLOCTUP - returns space for the new tuple
* SETTUPLEN - stores the length into the tuple
* GETTUP - reads the tuple
*
* Note:
* LEN field must be a short; FP is a stream
*/
#define PUTTUP(TUP, FP)\
BytesWritten += (TUP)->t_len; \
fwrite((char *)TUP, (TUP)->t_len, 1, FP)
#define ENDRUN(FP) fwrite((char *)&shortzero, sizeof (shortzero), 1, FP)
#define GETLEN(LEN, FP) fread((char *)&(LEN), sizeof (shortzero), 1, FP)
#define ALLOCTUP(LEN) ((HeapTuple)malloc((unsigned)LEN))
#define GETTUP(TUP, LEN, FP)\
IncrProcessed(); \
BytesRead += (LEN) - sizeof (shortzero); \
fread((char *)(TUP) + sizeof (shortzero), (LEN) - sizeof (shortzero), 1, FP)
#define SETTUPLEN(TUP, LEN) (TUP)->t_len = LEN
/*
* USEMEM - record use of memory
* FREEMEM - record freeing of memory
* FULLMEM - 1 iff a tuple will fit
*/
#define USEMEM(AMT) SortMemory -= (AMT)
#define FREEMEM(AMT) SortMemory += (AMT)
#define LACKMEM() (SortMemory <= BLCKSZ) /* not accurate */
#define TRACEMEM(FUNC)
#define TRACEOUT(FUNC, TUP)
/*
* initialrun - distributes tuples from the relation
* - (replacement selection(R2-R3)--Knuth, Vol.3, p.257)
* - (polyphase merge Alg.D(D2-D4)--Knuth, Vol.3, p.271)
*
* Explaination:
* Tuples are distributed to the tapes as in Algorithm D.
* A "tuple" with t_size == 0 is used to mark the end of a run.
*
* Note:
* The replacement selection algorithm has been modified
* to go from R1 directly to R3 skipping R2 the first time.
*
* Maybe should use closer(rdesc) before return
* Perhaps should adjust the number of tapes if less than n.
* used--v. likely to have problems in mergeruns().
* Must know if should open/close files before each
* call to psort()? If should--messy??
*
* Possible optimization:
* put the first xxx runs in quickly--problem here since
* I (perhaps prematurely) combined the 2 algorithms.
* Also, perhaps allocate tapes when needed. Split into 2 funcs.
*/
void
initialrun(Relation rdesc)
{
/* register struct tuple *tup; */
register struct tape *tp;
HeapScanDesc sdesc;
int baseruns; /* D:(a) */
int morepasses; /* EOF */
sdesc = heap_beginscan(rdesc, 0, NowTimeQual, 0,
(ScanKey)NULL);
tp = Tape;
if ((bool)createrun(sdesc, tp->tp_file) != false)
morepasses = 0;
else
morepasses = 1 + (Tuples != NULL); /* (T != N) ? 2 : 1 */
for ( ; ; ) {
tp->tp_dummy--;
TotalDummy--;
if (tp->tp_dummy < (tp + 1)->tp_dummy)
tp++;
else if (tp->tp_dummy != 0)
tp = Tape;
else {
Level++;
baseruns = Tape[0].tp_fib;
for (tp = Tape; tp - Tape < TapeRange; tp++) {
TotalDummy +=
(tp->tp_dummy = baseruns
+ (tp + 1)->tp_fib
- tp->tp_fib);
tp->tp_fib = baseruns
+ (tp + 1)->tp_fib;
}
tp = Tape; /* D4 */
} /* D3 */
if (morepasses)
if (--morepasses) {
dumptuples(tp->tp_file);
ENDRUN(tp->tp_file);
continue;
} else
break;
if ((bool)createrun(sdesc, tp->tp_file) == false)
morepasses = 1 + (Tuples != NULL);
/* D2 */
}
for (tp = Tape + TapeRange; tp >= Tape; tp--)
rewind(tp->tp_file); /* D. */
heap_endscan(sdesc);
}
/*
* createrun - places the next run on file
*
* Uses:
* Tuples, which should contain any tuples for this run
*
* Returns:
* FALSE iff process through end of relation
* Tuples contains the tuples for the following run upon exit
*/
bool
createrun(HeapScanDesc sdesc, FILE *file)
{
register HeapTuple lasttuple;
register HeapTuple btup, tup;
struct leftist *nextrun;
Buffer b;
bool foundeor;
short junk;
lasttuple = NULL;
nextrun = NULL;
foundeor = false;
for ( ; ; ) {
while (LACKMEM() && Tuples != NULL) {
if (lasttuple != NULL) {
FREEMEM(lasttuple->t_len);
FREE(lasttuple);
TRACEMEM(createrun);
}
lasttuple = tup = gettuple(&Tuples, &junk);
PUTTUP(tup, file);
TRACEOUT(createrun, tup);
}
if (LACKMEM())
break;
btup = heap_getnext(sdesc, 0, &b);
if (!HeapTupleIsValid(btup)) {
foundeor = true;
break;
}
IncrProcessed();
tup = tuplecopy(btup, sdesc->rs_rd, b);
USEMEM(tup->t_len);
TRACEMEM(createrun);
if (lasttuple != NULL && tuplecmp(tup, lasttuple))
puttuple(&nextrun, tup, 0);
else
puttuple(&Tuples, tup, 0);
ReleaseBuffer(b);
}
if (lasttuple != NULL) {
FREEMEM(lasttuple->t_len);
FREE(lasttuple);
TRACEMEM(createrun);
}
dumptuples(file);
ENDRUN(file);
/* delimit the end of the run */
Tuples = nextrun;
return((bool)! foundeor); /* XXX - works iff bool is {0,1} */
}
/*
* tuplecopy - see also tuple.c:palloctup()
*
* This should eventually go there under that name? And this will
* then use malloc directly (see version -r1.2).
*/
HeapTuple
tuplecopy(HeapTuple tup, Relation rdesc, Buffer b)
{
HeapTuple rettup;
if (!HeapTupleIsValid(tup)) {
return(NULL); /* just in case */
}
rettup = (HeapTuple)malloc(tup->t_len);
memmove((char *)rettup, (char *)tup, tup->t_len); /* XXX */
return(rettup);
}
/*
* mergeruns - merges all runs from input tapes
* (polyphase merge Alg.D(D6)--Knuth, Vol.3, p271)
*
* Returns:
* file of tuples in order
*/
FILE *
mergeruns()
{
register struct tape *tp;
tp = Tape + TapeRange;
merge(tp);
rewind(tp->tp_file);
while (--Level != 0) {
tp = tp->tp_prev;
rewind(tp->tp_file);
/* resettape(tp->tp_file); -not sufficient */
merge(tp);
rewind(tp->tp_file);
}
return(tp->tp_file);
}
/*
* merge - handles a single merge of the tape
* (polyphase merge Alg.D(D5)--Knuth, Vol.3, p271)
*/
void
merge(struct tape *dest)
{
register HeapTuple tup;
register struct tape *lasttp; /* (TAPE[P]) */
register struct tape *tp;
struct leftist *tuples;
FILE *destfile;
int times; /* runs left to merge */
int outdummy; /* complete dummy runs */
short fromtape;
long tuplen;
lasttp = dest->tp_prev;
times = lasttp->tp_fib;
for (tp = lasttp ; tp != dest; tp = tp->tp_prev)
tp->tp_fib -= times;
tp->tp_fib += times;
/* Tape[].tp_fib (A[]) is set to proper exit values */
if (TotalDummy < TapeRange) /* no complete dummy runs */
outdummy = 0;
else {
outdummy = TotalDummy; /* a large positive number */
for (tp = lasttp; tp != dest; tp = tp->tp_prev)
if (outdummy > tp->tp_dummy)
outdummy = tp->tp_dummy;
for (tp = lasttp; tp != dest; tp = tp->tp_prev)
tp->tp_dummy -= outdummy;
tp->tp_dummy += outdummy;
TotalDummy -= outdummy * TapeRange;
/* do not add the outdummy runs yet */
times -= outdummy;
}
destfile = dest->tp_file;
while (times-- != 0) { /* merge one run */
tuples = NULL;
if (TotalDummy == 0)
for (tp = dest->tp_prev; tp != dest; tp = tp->tp_prev) {
GETLEN(tuplen, tp->tp_file);
tup = ALLOCTUP(tuplen);
USEMEM(tuplen);
TRACEMEM(merge);
SETTUPLEN(tup, tuplen);
GETTUP(tup, tuplen, tp->tp_file);
puttuple(&tuples, tup, TAPENO(tp));
}
else {
for (tp = dest->tp_prev; tp != dest; tp = tp->tp_prev) {
if (tp->tp_dummy != 0) {
tp->tp_dummy--;
TotalDummy--;
} else {
GETLEN(tuplen, tp->tp_file);
tup = ALLOCTUP(tuplen);
USEMEM(tuplen);
TRACEMEM(merge);
SETTUPLEN(tup, tuplen);
GETTUP(tup, tuplen, tp->tp_file);
puttuple(&tuples, tup, TAPENO(tp));
}
}
}
while (tuples != NULL) {
/* possible optimization by using count in tuples */
tup = gettuple(&tuples, &fromtape);
PUTTUP(tup, destfile);
FREEMEM(tup->t_len);
FREE(tup);
TRACEMEM(merge);
GETLEN(tuplen, Tape[fromtape].tp_file);
if (tuplen == 0)
;
else {
tup = ALLOCTUP(tuplen);
USEMEM(tuplen);
TRACEMEM(merge);
SETTUPLEN(tup, tuplen);
GETTUP(tup, tuplen, Tape[fromtape].tp_file);
puttuple(&tuples, tup, fromtape);
}
}
ENDRUN(destfile);
}
TotalDummy += outdummy;
}
/*
* endpsort - creates the new relation and unlinks the tape files
*/
void
endpsort(Relation rdesc, FILE *file)
{
register struct tape *tp;
register HeapTuple tup;
long tuplen;
if (! feof(file))
while (GETLEN(tuplen, file) && tuplen != 0) {
tup = ALLOCTUP(tuplen);
SortMemory += tuplen;
SETTUPLEN(tup, tuplen);
GETTUP(tup, tuplen, file);
heap_insert(rdesc, tup);
FREE(tup);
SortMemory -= tuplen;
}
for (tp = Tape + TapeRange; tp >= Tape; tp--)
destroytape(tp->tp_file);
}
/*
* gettape - handles access temporary files in polyphase merging
*
* Optimizations:
* If guarenteed that only one sort running/process,
* can simplify the file generation--and need not store the
* name for later unlink.
*/
struct tapelst {
char *tl_name;
int tl_fd;
struct tapelst *tl_next;
};
static struct tapelst *Tapes = NULL;
static char Tempfile[MAXPGPATH] = TEMPDIR;
/*
* gettape - returns an open stream for writing/reading
*
* Returns:
* Open stream for writing/reading.
* NULL if unable to open temporary file.
*/
FILE *
gettape()
{
register struct tapelst *tp;
FILE *file;
static int tapeinit = 0;
char *mktemp();
tp = (struct tapelst *)malloc((unsigned)sizeof (struct tapelst));
if (!tapeinit) {
Tempfile[sizeof (TEMPDIR) - 1] = '/';
memmove(Tempfile + sizeof(TEMPDIR), TAPEEXT, sizeof (TAPEEXT));
tapeinit = 1;
}
tp->tl_name = malloc((unsigned)sizeof(Tempfile));
/*
* now, copy template with final null into malloc'd space
*/
memmove(tp->tl_name, Tempfile, sizeof (TEMPDIR) + sizeof (TAPEEXT));
mktemp(tp->tl_name);
AllocateFile();
file = fopen(tp->tl_name, "w+");
if (file == NULL) {
/* XXX this should not happen */
FreeFile();
FREE(tp->tl_name);
FREE(tp);
return(NULL);
}
tp->tl_fd = fileno(file);
tp->tl_next = Tapes;
Tapes = tp;
return(file);
}
/*
* resettape - resets the tape to size 0
*/
void
resettape(FILE *file)
{
register struct tapelst *tp;
register int fd;
Assert(PointerIsValid(file));
fd = fileno(file);
for (tp = Tapes; tp != NULL && tp->tl_fd != fd; tp = tp->tl_next)
;
if (tp == NULL)
elog(WARN, "resettape: tape not found");
file = freopen(tp->tl_name, "w+", file);
if (file == NULL) {
elog(FATAL, "could not freopen temporary file");
}
}
/*
* distroytape - unlinks the tape
*
* Efficiency note:
* More efficient to destroy more recently allocated tapes first.
*
* Possible bugs:
* Exits instead of returning status, if given invalid tape.
*/
void
destroytape(FILE *file)
{
register struct tapelst *tp, *tq;
register int fd;
if ((tp = Tapes) == NULL)
elog(FATAL, "destroytape: tape not found");
if ((fd = fileno(file)) == tp->tl_fd) {
Tapes = tp->tl_next;
fclose(file);
FreeFile();
unlink(tp->tl_name);
FREE(tp->tl_name);
FREE(tp);
} else
for ( ; ; ) {
if (tp->tl_next == NULL)
elog(FATAL, "destroytape: tape not found");
if (tp->tl_next->tl_fd == fd) {
fclose(file);
FreeFile();
tq = tp->tl_next;
tp->tl_next = tq->tl_next;
unlink(tq->tl_name);
FREE((tq->tl_name));
FREE(tq);
break;
}
tp = tp->tl_next;
}
}