Commit 71d5d953 authored by Tom Lane's avatar Tom Lane

Update hash and join routines to use fd.c's new temp-file

code, instead of not-very-bulletproof stuff they had before.
parent c1167a08
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Id: nbtsort.c,v 1.37 1999/02/21 03:48:27 scrappy Exp $ * $Id: nbtsort.c,v 1.38 1999/05/09 00:53:19 tgl Exp $
* *
* NOTES * NOTES
* *
...@@ -85,11 +85,9 @@ static void _bt_uppershutdown(Relation index, BTPageState *state); ...@@ -85,11 +85,9 @@ static void _bt_uppershutdown(Relation index, BTPageState *state);
#define MAXTAPES (7) #define MAXTAPES (7)
#define TAPEBLCKSZ (BLCKSZ << 2) #define TAPEBLCKSZ (BLCKSZ << 2)
#define TAPETEMP "pg_btsortXXXXXXX"
extern int NDirectFileRead; extern int NDirectFileRead;
extern int NDirectFileWrite; extern int NDirectFileWrite;
extern char *mktemp(char *template);
/* /*
* this is what we use to shovel BTItems in and out of memory. it's * this is what we use to shovel BTItems in and out of memory. it's
...@@ -107,7 +105,7 @@ extern char *mktemp(char *template); ...@@ -107,7 +105,7 @@ extern char *mktemp(char *template);
typedef struct typedef struct
{ {
int bttb_magic; /* magic number */ int bttb_magic; /* magic number */
int bttb_fd; /* file descriptor */ File bttb_fd; /* file descriptor */
int bttb_top; /* top of free space within bttb_data */ int bttb_top; /* top of free space within bttb_data */
short bttb_ntup; /* number of tuples in this block */ short bttb_ntup; /* number of tuples in this block */
short bttb_eor; /* End-Of-Run marker */ short bttb_eor; /* End-Of-Run marker */
...@@ -380,7 +378,7 @@ _bt_tapereset(BTTapeBlock *tape) ...@@ -380,7 +378,7 @@ _bt_tapereset(BTTapeBlock *tape)
static void static void
_bt_taperewind(BTTapeBlock *tape) _bt_taperewind(BTTapeBlock *tape)
{ {
FileSeek(tape->bttb_fd, 0, SEEK_SET); FileSeek(tape->bttb_fd, 0L, SEEK_SET);
} }
/* /*
...@@ -411,7 +409,7 @@ _bt_tapeclear(BTTapeBlock *tape) ...@@ -411,7 +409,7 @@ _bt_tapeclear(BTTapeBlock *tape)
* as well as opening a physical tape file. * as well as opening a physical tape file.
*/ */
static BTTapeBlock * static BTTapeBlock *
_bt_tapecreate(char *fname) _bt_tapecreate(void)
{ {
BTTapeBlock *tape = (BTTapeBlock *) palloc(sizeof(BTTapeBlock)); BTTapeBlock *tape = (BTTapeBlock *) palloc(sizeof(BTTapeBlock));
...@@ -420,11 +418,7 @@ _bt_tapecreate(char *fname) ...@@ -420,11 +418,7 @@ _bt_tapecreate(char *fname)
tape->bttb_magic = BTTAPEMAGIC; tape->bttb_magic = BTTAPEMAGIC;
#ifndef __CYGWIN32__ tape->bttb_fd = OpenTemporaryFile();
tape->bttb_fd = FileNameOpenFile(fname, O_RDWR | O_CREAT | O_TRUNC, 0600);
#else
tape->bttb_fd = FileNameOpenFile(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0600);
#endif
Assert(tape->bttb_fd >= 0); Assert(tape->bttb_fd >= 0);
/* initialize the buffer */ /* initialize the buffer */
...@@ -467,7 +461,7 @@ _bt_tapewrite(BTTapeBlock *tape, int eor) ...@@ -467,7 +461,7 @@ _bt_tapewrite(BTTapeBlock *tape, int eor)
static int static int
_bt_taperead(BTTapeBlock *tape) _bt_taperead(BTTapeBlock *tape)
{ {
int fd; File fd;
int nread; int nread;
if (tape->bttb_eor) if (tape->bttb_eor)
...@@ -550,9 +544,8 @@ _bt_spoolinit(Relation index, int ntapes, bool isunique) ...@@ -550,9 +544,8 @@ _bt_spoolinit(Relation index, int ntapes, bool isunique)
{ {
BTSpool *btspool = (BTSpool *) palloc(sizeof(BTSpool)); BTSpool *btspool = (BTSpool *) palloc(sizeof(BTSpool));
int i; int i;
char *fname = (char *) palloc(sizeof(TAPETEMP) + 1);
if (btspool == (BTSpool *) NULL || fname == (char *) NULL) if (btspool == (BTSpool *) NULL)
elog(ERROR, "_bt_spoolinit: out of memory"); elog(ERROR, "_bt_spoolinit: out of memory");
MemSet((char *) btspool, 0, sizeof(BTSpool)); MemSet((char *) btspool, 0, sizeof(BTSpool));
btspool->bts_ntapes = ntapes; btspool->bts_ntapes = ntapes;
...@@ -567,10 +560,9 @@ _bt_spoolinit(Relation index, int ntapes, bool isunique) ...@@ -567,10 +560,9 @@ _bt_spoolinit(Relation index, int ntapes, bool isunique)
for (i = 0; i < ntapes; ++i) for (i = 0; i < ntapes; ++i)
{ {
btspool->bts_itape[i] = _bt_tapecreate(mktemp(strcpy(fname, TAPETEMP))); btspool->bts_itape[i] = _bt_tapecreate();
btspool->bts_otape[i] = _bt_tapecreate(mktemp(strcpy(fname, TAPETEMP))); btspool->bts_otape[i] = _bt_tapecreate();
} }
pfree((void *) fname);
_bt_isortcmpinit(index, btspool); _bt_isortcmpinit(index, btspool);
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* *
* $Id: nodeHash.c,v 1.33 1999/05/06 00:30:46 tgl Exp $ * $Id: nodeHash.c,v 1.34 1999/05/09 00:53:20 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -39,9 +39,6 @@ ...@@ -39,9 +39,6 @@
extern int NBuffers; extern int NBuffers;
#define HJ_TEMP_NAMELEN 16 /* max length for mk_hj_temp file names */
static void mk_hj_temp(char *tempname);
static int hashFunc(Datum key, int len, bool byVal); static int hashFunc(Datum key, int len, bool byVal);
static RelativeAddr hashTableAlloc(int size, HashJoinTable hashtable); static RelativeAddr hashTableAlloc(int size, HashJoinTable hashtable);
static void * absHashTableAlloc(int size, HashJoinTable hashtable); static void * absHashTableAlloc(int size, HashJoinTable hashtable);
...@@ -72,7 +69,6 @@ ExecHash(Hash *node) ...@@ -72,7 +69,6 @@ ExecHash(Hash *node)
RelativeAddr *batchPos; RelativeAddr *batchPos;
int *batchSizes; int *batchSizes;
int i; int i;
RelativeAddr *innerbatchNames;
/* ---------------- /* ----------------
* get state info from node * get state info from node
...@@ -91,8 +87,6 @@ ExecHash(Hash *node) ...@@ -91,8 +87,6 @@ ExecHash(Hash *node)
if (nbatch > 0) if (nbatch > 0)
{ /* if needs hash partition */ { /* if needs hash partition */
innerbatchNames = (RelativeAddr *) ABSADDR(hashtable->innerbatchNames);
/* -------------- /* --------------
* allocate space for the file descriptors of batch files * allocate space for the file descriptors of batch files
* then open the batch files in the current processes. * then open the batch files in the current processes.
...@@ -101,13 +95,7 @@ ExecHash(Hash *node) ...@@ -101,13 +95,7 @@ ExecHash(Hash *node)
batches = (File *) palloc(nbatch * sizeof(File)); batches = (File *) palloc(nbatch * sizeof(File));
for (i = 0; i < nbatch; i++) for (i = 0; i < nbatch; i++)
{ {
#ifndef __CYGWIN32__ batches[i] = OpenTemporaryFile();
batches[i] = FileNameOpenFile(ABSADDR(innerbatchNames[i]),
O_CREAT | O_RDWR, 0600);
#else
batches[i] = FileNameOpenFile(ABSADDR(innerbatchNames[i]),
O_CREAT | O_RDWR | O_BINARY, 0600);
#endif
} }
hashstate->hashBatches = batches; hashstate->hashBatches = batches;
batchPos = (RelativeAddr *) ABSADDR(hashtable->innerbatchPos); batchPos = (RelativeAddr *) ABSADDR(hashtable->innerbatchPos);
...@@ -291,7 +279,7 @@ absHashTableAlloc(int size, HashJoinTable hashtable) ...@@ -291,7 +279,7 @@ absHashTableAlloc(int size, HashJoinTable hashtable)
* ---------------------------------------------------------------- * ----------------------------------------------------------------
*/ */
#define NTUP_PER_BUCKET 10 #define NTUP_PER_BUCKET 10
#define FUDGE_FAC 1.5 #define FUDGE_FAC 2.0
HashJoinTable HashJoinTable
ExecHashTableCreate(Hash *node) ExecHashTableCreate(Hash *node)
...@@ -310,12 +298,9 @@ ExecHashTableCreate(Hash *node) ...@@ -310,12 +298,9 @@ ExecHashTableCreate(Hash *node)
int totalbuckets; int totalbuckets;
int bucketsize; int bucketsize;
int i; int i;
RelativeAddr *outerbatchNames;
RelativeAddr *outerbatchPos; RelativeAddr *outerbatchPos;
RelativeAddr *innerbatchNames;
RelativeAddr *innerbatchPos; RelativeAddr *innerbatchPos;
int *innerbatchSizes; int *innerbatchSizes;
RelativeAddr tempname;
/* ---------------- /* ----------------
* Get information about the size of the relation to be hashed * Get information about the size of the relation to be hashed
...@@ -425,46 +410,32 @@ ExecHashTableCreate(Hash *node) ...@@ -425,46 +410,32 @@ ExecHashTableCreate(Hash *node)
* allocate and initialize the outer batches * allocate and initialize the outer batches
* --------------- * ---------------
*/ */
outerbatchNames = (RelativeAddr *)
absHashTableAlloc(nbatch * sizeof(RelativeAddr), hashtable);
outerbatchPos = (RelativeAddr *) outerbatchPos = (RelativeAddr *)
absHashTableAlloc(nbatch * sizeof(RelativeAddr), hashtable); absHashTableAlloc(nbatch * sizeof(RelativeAddr), hashtable);
for (i = 0; i < nbatch; i++) for (i = 0; i < nbatch; i++)
{ {
tempname = hashTableAlloc(HJ_TEMP_NAMELEN, hashtable);
mk_hj_temp(ABSADDR(tempname));
outerbatchNames[i] = tempname;
outerbatchPos[i] = -1; outerbatchPos[i] = -1;
} }
hashtable->outerbatchNames = RELADDR(outerbatchNames);
hashtable->outerbatchPos = RELADDR(outerbatchPos); hashtable->outerbatchPos = RELADDR(outerbatchPos);
/* --------------- /* ---------------
* allocate and initialize the inner batches * allocate and initialize the inner batches
* --------------- * ---------------
*/ */
innerbatchNames = (RelativeAddr *)
absHashTableAlloc(nbatch * sizeof(RelativeAddr), hashtable);
innerbatchPos = (RelativeAddr *) innerbatchPos = (RelativeAddr *)
absHashTableAlloc(nbatch * sizeof(RelativeAddr), hashtable); absHashTableAlloc(nbatch * sizeof(RelativeAddr), hashtable);
innerbatchSizes = (int *) innerbatchSizes = (int *)
absHashTableAlloc(nbatch * sizeof(int), hashtable); absHashTableAlloc(nbatch * sizeof(int), hashtable);
for (i = 0; i < nbatch; i++) for (i = 0; i < nbatch; i++)
{ {
tempname = hashTableAlloc(HJ_TEMP_NAMELEN, hashtable);
mk_hj_temp(ABSADDR(tempname));
innerbatchNames[i] = tempname;
innerbatchPos[i] = -1; innerbatchPos[i] = -1;
innerbatchSizes[i] = 0; innerbatchSizes[i] = 0;
} }
hashtable->innerbatchNames = RELADDR(innerbatchNames);
hashtable->innerbatchPos = RELADDR(innerbatchPos); hashtable->innerbatchPos = RELADDR(innerbatchPos);
hashtable->innerbatchSizes = RELADDR(innerbatchSizes); hashtable->innerbatchSizes = RELADDR(innerbatchSizes);
} }
else else
{ {
hashtable->outerbatchNames = (RelativeAddr) NULL;
hashtable->outerbatchPos = (RelativeAddr) NULL; hashtable->outerbatchPos = (RelativeAddr) NULL;
hashtable->innerbatchNames = (RelativeAddr) NULL;
hashtable->innerbatchPos = (RelativeAddr) NULL; hashtable->innerbatchPos = (RelativeAddr) NULL;
hashtable->innerbatchSizes = (RelativeAddr) NULL; hashtable->innerbatchSizes = (RelativeAddr) NULL;
} }
...@@ -886,15 +857,6 @@ ExecHashTableReset(HashJoinTable hashtable, int ntuples) ...@@ -886,15 +857,6 @@ ExecHashTableReset(HashJoinTable hashtable, int ntuples)
hashtable->pcount = hashtable->nprocess; hashtable->pcount = hashtable->nprocess;
} }
static void
mk_hj_temp(char *tempname)
{
static int hjtmpcnt = 0;
snprintf(tempname, HJ_TEMP_NAMELEN, "HJ%d.%d", (int) MyProcPid, hjtmpcnt);
hjtmpcnt = (hjtmpcnt + 1) % 1000;
}
void void
ExecReScanHash(Hash *node, ExprContext *exprCtxt, Plan *parent) ExecReScanHash(Hash *node, ExprContext *exprCtxt, Plan *parent)
{ {
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/nodeHashjoin.c,v 1.18 1999/05/06 00:30:47 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/executor/nodeHashjoin.c,v 1.19 1999/05/09 00:53:21 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -74,7 +74,6 @@ ExecHashJoin(HashJoin *node) ...@@ -74,7 +74,6 @@ ExecHashJoin(HashJoin *node)
int nbatch; int nbatch;
int curbatch; int curbatch;
File *outerbatches; File *outerbatches;
RelativeAddr *outerbatchNames;
RelativeAddr *outerbatchPos; RelativeAddr *outerbatchPos;
Var *innerhashkey; Var *innerhashkey;
int batch; int batch;
...@@ -166,21 +165,10 @@ ExecHashJoin(HashJoin *node) ...@@ -166,21 +165,10 @@ ExecHashJoin(HashJoin *node)
*/ */
innerhashkey = hashNode->hashkey; innerhashkey = hashNode->hashkey;
hjstate->hj_InnerHashKey = innerhashkey; hjstate->hj_InnerHashKey = innerhashkey;
outerbatchNames = (RelativeAddr *) outerbatches = (File *) palloc(nbatch * sizeof(File));
ABSADDR(hashtable->outerbatchNames);
outerbatches = (File *)
palloc(nbatch * sizeof(File));
for (i = 0; i < nbatch; i++) for (i = 0; i < nbatch; i++)
{ {
#ifndef __CYGWIN32__ outerbatches[i] = OpenTemporaryFile();
outerbatches[i] = FileNameOpenFile(
ABSADDR(outerbatchNames[i]),
O_CREAT | O_RDWR, 0600);
#else
outerbatches[i] = FileNameOpenFile(
ABSADDR(outerbatchNames[i]),
O_CREAT | O_RDWR | O_BINARY, 0600);
#endif
} }
hjstate->hj_OuterBatches = outerbatches; hjstate->hj_OuterBatches = outerbatches;
...@@ -193,7 +181,6 @@ ExecHashJoin(HashJoin *node) ...@@ -193,7 +181,6 @@ ExecHashJoin(HashJoin *node)
} }
outerbatchPos = (RelativeAddr *) ABSADDR(hashtable->outerbatchPos); outerbatchPos = (RelativeAddr *) ABSADDR(hashtable->outerbatchPos);
curbatch = hashtable->curbatch; curbatch = hashtable->curbatch;
outerbatchNames = (RelativeAddr *) ABSADDR(hashtable->outerbatchNames);
/* ---------------- /* ----------------
* Now get an outer tuple and probe into the hash table for matches * Now get an outer tuple and probe into the hash table for matches
......
This diff is collapsed.
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* *
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* $Id: hashjoin.h,v 1.9 1999/05/06 00:30:45 tgl Exp $ * $Id: hashjoin.h,v 1.10 1999/05/09 00:53:18 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -54,9 +54,7 @@ typedef struct HashTableData ...@@ -54,9 +54,7 @@ typedef struct HashTableData
RelativeAddr batch; /* char* */ RelativeAddr batch; /* char* */
RelativeAddr readbuf; /* char* */ RelativeAddr readbuf; /* char* */
int nbatch; int nbatch;
RelativeAddr outerbatchNames; /* RelativeAddr* */
RelativeAddr outerbatchPos; /* RelativeAddr* */ RelativeAddr outerbatchPos; /* RelativeAddr* */
RelativeAddr innerbatchNames; /* RelativeAddr* */
RelativeAddr innerbatchPos; /* RelativeAddr* */ RelativeAddr innerbatchPos; /* RelativeAddr* */
RelativeAddr innerbatchSizes; /* int* */ RelativeAddr innerbatchSizes; /* int* */
int curbatch; int curbatch;
......
...@@ -6,27 +6,25 @@ ...@@ -6,27 +6,25 @@
* *
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* $Id: psort.h,v 1.17 1999/02/13 23:22:29 momjian Exp $ * $Id: psort.h,v 1.18 1999/05/09 00:53:18 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#ifndef PSORT_H #ifndef PSORT_H
#define PSORT_H #define PSORT_H
#include <stdio.h> #include "storage/fd.h"
#include "access/relscan.h" #include "access/relscan.h"
#include "utils/lselect.h" #include "utils/lselect.h"
#include "nodes/plannodes.h" #include "nodes/plannodes.h"
#define MAXTAPES 7 /* 7--See Fig. 70, p273 */ #define MAXTAPES 7 /* See Knuth Fig. 70, p273 */
#define TAPEEXTLEN strlen("pg_psort.xxxxx.xxx") /* TEMPDIR/TAPEEXT */
#define FREE(x) pfree((char *) x)
struct tape struct tape
{ {
int tp_dummy; /* (D) */ int tp_dummy; /* (D) */
int tp_fib; /* (A) */ int tp_fib; /* (A) */
FILE *tp_file; /* (TAPE) */ BufFile *tp_file; /* (TAPE) */
struct tape *tp_prev; struct tape *tp_prev;
}; };
...@@ -58,7 +56,7 @@ typedef struct Psortstate ...@@ -58,7 +56,7 @@ typedef struct Psortstate
struct leftist *Tuples; struct leftist *Tuples;
FILE *psort_grab_file; BufFile *psort_grab_file;
long psort_current; /* could be file offset, or array index */ long psort_current; /* could be file offset, or array index */
long psort_saved; /* could be file offset, or array index */ long psort_saved; /* could be file offset, or array index */
bool using_tape_files; bool using_tape_files;
...@@ -68,7 +66,6 @@ typedef struct Psortstate ...@@ -68,7 +66,6 @@ typedef struct Psortstate
} Psortstate; } Psortstate;
#ifdef EBUG #ifdef EBUG
#include <stdio.h>
#include "utils/elog.h" #include "utils/elog.h"
#include "storage/buf.h" #include "storage/buf.h"
#include "storage/bufmgr.h" #include "storage/bufmgr.h"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment