zoukankan      html  css  js  c++  java
  • PostgreSQL在何处处理 sql查询之十九

    再回过头来看 

    /*
     * open a file in an arbitrary directory
     *
     * NB: if the passed pathname is relative (which it usually is),
     * it will be interpreted relative to the process' working directory
     * (which should always be $PGDATA when this code is running).
     */
    File
    PathNameOpenFile(FileName fileName, int fileFlags, int fileMode)
    {
        char       *fnamecopy;
        File        file;
        Vfd           *vfdP;
    
        DO_DB(elog(LOG, "PathNameOpenFile: %s %x %o",
                   fileName, fileFlags, fileMode));
        /*
         * We need a malloc'd copy of the file name; fail cleanly if no room.
         */
        fnamecopy = strdup(fileName);
        if (fnamecopy == NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_OUT_OF_MEMORY),
                     errmsg("out of memory")));
    
        file = AllocateVfd();
        vfdP = &VfdCache[file];
    
        while (nfile + numAllocatedDescs >= max_safe_fds)
        {
            if (!ReleaseLruFile())
                break;
        }
    
        vfdP->fd = BasicOpenFile(fileName, fileFlags, fileMode);
    
        if (vfdP->fd < 0)
        {
            FreeVfd(file);
            free(fnamecopy);
            return -1;
        }
        ++nfile;
        DO_DB(elog(LOG, "PathNameOpenFile: success %d",
                   vfdP->fd));
    
        Insert(file);
        /**
        fprintf(stderr,"(O_CREAT) is:%x \n",O_CREAT);
        fprintf(stderr,"(O_TRUNC) is:%x \n",O_TRUNC);
        fprintf(stderr,"(O_EXCL) is:%x \n",O_EXCL);
        fprintf(stderr,"(O_CREAT | O_TRUNC | O_EXCL) is: %x\n", (O_CREAT | O_TRUNC | O_EXCL));
        fprintf(stderr,"~(O_CREAT | O_TRUNC | O_EXCL) is: %x\n", ~(O_CREAT | O_TRUNC | O_EXCL));
        fprintf(stderr,"fileFlags is %x\n", fileFlags);
        fprintf(stderr,"fileFlags & ~(O_CREAT | O_TRUNC | O_EXCL) is %x\n", fileFlags & ~(O_CREAT | O_TRUNC | O_EXCL));
        */
    
        vfdP->fileName = fnamecopy;
        /* Saved flags are adjusted to be OK for re-opening file */
        vfdP->fileFlags = fileFlags & ~(O_CREAT | O_TRUNC | O_EXCL);
        vfdP->fileMode = fileMode;
        vfdP->seekPos = 0;
        vfdP->fileSize = 0;
        vfdP->fdstate = 0x0;
        vfdP->resowner = NULL;
    
        //fprintf(stderr,"file is: %d by proces %d \n\n",file,getpid());
    
        return file;
    }

    这两句,其实是干了共同的一件事:

    file = AllocateVfd();
    vfdP = &VfdCache[file];

    AllocateVfd,是找到一个空闲的VFD描述符,其实就是定位了VFDCache内存结构数组的下标。
    vfdP = &VfdCache[file],就是用下标来获得第 file+1个内存结构(VFD)的指针。

    整个 PathNameOpenFile的做法也是挺怪异的,它处理的是 ,拿到一个空闲的VFD描述符。
    然后对此VFD结构体进行设置,最后return的,实际是一个VfdCache内存结构数组的下标。

    在PostgreSQL中,
    在一个服务客户的进程的范围内(例如,启动psql客户端而导致后台建立的一个进程),
    第一次访问某一个表的时候,会访问 
    PathNameOpenFile。第二次再访问同一表的时候,不会再次访问此PathNameOpenFile函数了(严格来说,应当是遵循某种LRU算法)。

  • 相关阅读:
    针对Python基本数据类型的操作
    Could not transfer artifact org.apache.maven.plugins:maven-clean-plugin:pom:2.5 from 这类问题的解决方法
    校招有感:计算机专业毕业生如何找工作(Java方向)
    我用了半年的时间,把python学到了能出书的程度
    Java面试官经验谈:如何甄别候选人真实的能力,候选人如何展示值钱技能
    Java字节码与反射机制
    以我的亲身经历,聊聊学python的流程,同时推荐学python的书
    面试时通过volatile关键字,全面展示线程内存模型的能力
    如果很好说出finalize用法,面试官会认为你很资深
    C# post json和接收json
  • 原文地址:https://www.cnblogs.com/gaojian/p/3101393.html
Copyright © 2011-2022 走看看