PostgreSQL在何处处理 sql查询之三十四

接前面,回溯调用关系:

exec_simple_query --> PortalStart --> ExecutorStart --> StandardExecutorStart --> InitPlan

再回到 exec_simple_query 来:

事前知道,表 tst04 对应的文件名为 16393。

postgres=# select oid from pg_class where relname='tst04';
  oid  
-------
 16393
(1 row)

postgres=# 

看 exec_simple_query,加点调试信息:

static void
exec_simple_query(const char *query_string)
{
    ...
    parsetree_list = pg_parse_query(query_string);
    ...
    /*
     * Run through the raw parsetree(s) and process each one.
     */
    foreach(parsetree_item, parsetree_list)
    {
        ...
        querytree_list = pg_analyze_and_rewrite(parsetree, query_string,NULL, 0);
        plantree_list = pg_plan_queries(querytree_list, 0, NULL);
        ...
        portal = CreatePortal("", true, true);
        ...
        PortalDefineQuery(portal,
                          NULL,
                          query_string,
                          commandTag,
                          plantree_list,
                          NULL);
        ...
        PortalStart(portal, NULL, 0, snapshot_set);
        ...

        fprintf(stderr,"Before we call PortalRun,Now sleep for 120 seconds!!!\n");
        sleep(120);

        /*
         * Run the portal to completion, and then drop it (and the receiver).
         */
        (void) PortalRun(portal,
                         FETCH_ALL,
                         isTopLevel,
                         receiver,
                         receiver,
                         completionTag);

        fprintf(stderr,"After we call PortalRun!!!\n");
        ...
    }                            /* end loop over parsetrees */
    ...
}

在PortalRun之前,让它休眠120秒,在此期间,将 tst04表对应文件改名。

测试发现,改名对执行无影响、在同一客户端,再次发起对同名表的sql问,没有问题:

[postgres@lex pgsql]$ ./bin/pg_ctl -D ./data start
server starting
[postgres@lex pgsql]$ LOG:  database system was shut down at 2013-05-30 13:15:52 CST
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections
Before we call PortalRun,Now sleep for 120 seconds!!!
After we call PortalRun!!!
Before we call PortalRun,Now sleep for 120 seconds!!!
After we call PortalRun!!!
[postgres@lex 12788]$ mv 16393 16393.bak
[postgres@lex 12788]$ 
postgres=# select val from tst04;
 val 
-----
 100
 200
 300
(3 rows)

postgres=# select id from tst04;
 id 
----
  1
  2
  3
(3 rows)

postgres=# 

这说明:一旦准备好了执行计划,对文件的打开句柄将保持(除非因lru之类算法被移除)。

原文地址:https://www.cnblogs.com/gaojian/p/3108018.html