博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PostgreSQL在VFD管理上是一套还是多套
阅读量:5761 次
发布时间:2019-06-18

本文共 5286 字,大约阅读时间需要 17 分钟。

VFD是为了解决文件句柄的限制,防止把OS级别的文件句柄用光。

原来我认为VFD是各个进程间共有的。但是根据观察,发现每一个进程都拥有自己的VFD数组指针。

看看下面两段加了调试信息后的代码:

InitFileAccess:

从 VfdCache = (Vfd *) malloc(sizeof(Vfd)) 基本可以断定,没有使用共享内存方式

/* * InitFileAccess --- initialize this module during backend startup * * This is called during either normal or standalone backend start. * It is *not* called in the postmaster. */voidInitFileAccess(void){    fprintf(stderr,"In %s ...by Process %d\n", __FUNCTION__,getpid());    fprintf(stderr,"----------------------------------------------------\n\n");    Assert(SizeVfdCache == 0);    /* call me only once */    /* initialize cache header entry */    VfdCache = (Vfd *) malloc(sizeof(Vfd));    if (VfdCache == NULL)        ereport(FATAL,                (errcode(ERRCODE_OUT_OF_MEMORY),                 errmsg("out of memory")));    MemSet((char *) &(VfdCache[0]), 0, sizeof(Vfd));    VfdCache->fd = VFD_CLOSED;    SizeVfdCache = 1;    /* register proc-exit hook to ensure temp files are dropped at exit */    on_proc_exit(AtProcExit_Files, 0);}

AllocateVfd:

会因为扩充内存结构数组的需要,进行 realloc,这导致 VfdCache指针的值会在进程的范畴里发生变化。

static FileAllocateVfd(void){    Index        i;    File        file;    DO_DB(elog(LOG, "AllocateVfd. Size %lu", SizeVfdCache));    Assert(SizeVfdCache > 0);    /* InitFileAccess not called? */    if (VfdCache[0].nextFree == 0)    {        /*         * The free list is empty so it is time to increase the size of the         * array.  We choose to double it each time this happens. However,         * there's not much point in starting *real* small.         */        Size        newCacheSize = SizeVfdCache * 2;        Vfd           *newVfdCache;        if (newCacheSize < 32)            newCacheSize = 32;        /*         * Be careful not to clobber VfdCache ptr if realloc fails.         */        newVfdCache = (Vfd *) realloc(VfdCache, sizeof(Vfd) * newCacheSize);        if (newVfdCache == NULL)            ereport(ERROR,                    (errcode(ERRCODE_OUT_OF_MEMORY),                     errmsg("out of memory")));        VfdCache = newVfdCache;    ...    file = VfdCache[0].nextFree;    VfdCache[0].nextFree = VfdCache[file].nextFree;    return file;}

PathNameOpenFile:

/* * 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). */FilePathNameOpenFile(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")));    //VfdCache    fprintf(stderr,"In %s ...by Process %d...", __FUNCTION__,getpid());    fprintf(stderr,"VfdCache Address is: %p \n\n +++++++++++++++++++++++++++++++++++\n",VfdCache);    file = AllocateVfd();    vfdP = &VfdCache[file];    ...    return file;}

实际运行的结果如下(开两个psql客户端,一个pid为 6518,另一个为6584):

+In BaseInit ...by Process 6518In InitFileAccess ...by Process 6518----------------------------------------------------In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10dfff50  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6518...VfdCache Address is: 0x10e9c450 In BaseInit ...by Process 6584In InitFileAccess ...by Process 6584----------------------------------------------------In PathNameOpenFile ...by Process 6584...VfdCache Address is: 0x10e04e00  +++++++++++++++++++++++++++++++++++In PathNameOpenFile ...by Process 6584...VfdCache Address is: 0x10ecaad0

两个进程里面的 VfdCache 地址是不同的。

我想,可能是出于效率的原因,各进程各自保持VFD指针。

转载地址:http://yvlkx.baihongyu.com/

你可能感兴趣的文章
endWith is not a function
查看>>
你敢说自己了解单例模式?
查看>>
TCP和流
查看>>
Docker入门之六端口映射与容器互联
查看>>
simpleDateFormat的 学习
查看>>
MSSQL无法启动-原来电脑登录密码改了,重启后要设置
查看>>
Python split() 方法
查看>>
pyspark import 可以通过 --py-files
查看>>
ios发布以后关键信息确认与nslog
查看>>
Android--从零开始开发一款文章阅读APP
查看>>
Win8 Metro(C#)数字图像处理--2.64图像高斯滤波算法
查看>>
Html5 js FileReader接口
查看>>
面试笔记【自己总结】
查看>>
Java虚拟机16:Metaspace
查看>>
Spark 底层网络模块
查看>>
New UWP Community Toolkit - RangeSelector
查看>>
51单片机开发板(W25Q16学习)
查看>>
TeamViewer远程协作软件支持哪些系统?
查看>>
php数组增加元素
查看>>
AOP AspectJ 切面 Android简单示例
查看>>