/* * ExecWorkFile_Create * create a new work file with the specified name, the file type, * and the compression type. * * If this fails, NULL is returned. */ ExecWorkFile * ExecWorkFile_Create(const char *fileName, ExecWorkFileType fileType, bool delOnClose, int compressType) { ExecWorkFile *workfile = NULL; void *file = NULL; /* Before creating a new file, let's check the limit on number of workfile created */ if (!WorkfileQueryspace_AddWorkfile()) { /* Failed to reserve additional disk space, notify caller */ workfile_mgr_report_error(); } /* * Create ExecWorkFile in the TopMemoryContext since this memory context * is still available when calling the transaction callback at the * time when the transaction aborts. */ MemoryContext oldContext = MemoryContextSwitchTo(TopMemoryContext); switch(fileType) { case BUFFILE: file = (void *) BufFileCreateFile(fileName, delOnClose, false /* interXact */ ); BufFileSetWorkfile(file); break; case BFZ: file = (void *)bfz_create(fileName, delOnClose, compressType); break; default: ereport(LOG, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("invalid work file type: %d", fileType))); Assert(false); } workfile = palloc0(sizeof(ExecWorkFile)); workfile->fileType = fileType; workfile->compressType = compressType; workfile->file = file; workfile->fileName = pstrdup(fileName); workfile->size = 0; ExecWorkFile_SetFlags(workfile, delOnClose, true /* created */); MemoryContextSwitchTo(oldContext); return workfile; }
/* * Open many bfz files to simulate running out of file handles. * file_type values: * 0 - bfz, no compression * 1 - bfz, zlib compression * 2 - buffile */ static void open_many_files(int file_type) { char file_name[MAXPGPATH]; int iter = 0; while (true) { CHECK_FOR_INTERRUPTS(); snprintf(file_name, MAXPGPATH, "fake_file_%d", iter); switch(file_type) { case 0: case 1: ; #if USE_ASSERT_CHECKING bfz_t *bfz_file = #endif bfz_create(file_name, true /* delOnClose */, file_type); Assert(NULL != bfz_file); break; case 2: ; #if USE_ASSERT_CHECKING BufFile *buf_file = #endif BufFileCreateTemp(file_name, false /* interXact */ ); Assert(NULL != buf_file); break; default: Assert(false && "argument for fault type not supported"); } iter++; } return; }