Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bHhwO-0002rq-NM for pgsql-zh-general@arkaria.postgresql.org; Tue, 28 Jun 2016 01:35:52 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bHhwO-0006u9-5Q for pgsql-zh-general@arkaria.postgresql.org; Tue, 28 Jun 2016 01:35:52 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bHhwG-0006mE-Gr for pgsql-zh-general@postgresql.org; Tue, 28 Jun 2016 01:35:44 +0000 Received: from [124.127.160.226] (helo=mail.freemail.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bHhw8-0005n6-BB for pgsql-zh-general@postgresql.org; Tue, 28 Jun 2016 01:35:41 +0000 Received: from [192.168.0.132] (pfSense.localdomain [192.168.0.1]) by mail.freemail.com (Postfix) with ESMTPSA id F39FD10095E for ; Tue, 28 Jun 2016 09:31:15 +0800 (CST) To: "pgsql-zh-general@postgresql.org" From: Quan Zongliang Subject: =?UTF-8?B?5Zyj5YWJ54Wn6ICA6IGU55uf4oCUUG9zdGdyZVNRTOS4tOaXtuihqA==?= =?UTF-8?B?55qE5Yib5bu65LiO5L2/55So6L+H56iL?= Message-ID: Date: Tue, 28 Jun 2016 09:34:28 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 124.127.160.226 (failed) X-Pg-Spam-Score: 3.2 (+++) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-zh-general Precedence: bulk Sender: pgsql-zh-general-owner@postgresql.org 原文发表于 http://my.oschina.net/quanzl/blog/701657 1、临时表的创建与普通表没什么区别,都是通过 DefineRelation 函数 (定义于 src/backend/commands/tablecmds.c),临时表具有属性 relation->relpersistence == RELPERSISTENCE_TEMP #define RELPERSISTENCE_PERMANENT 'p' /* regular table */ #define RELPERSISTENCE_UNLOGGED 'u' /* unlogged permanent table */ #define RELPERSISTENCE_TEMP 't' /* temporary table */ 2、首先为临时表创建临时schema,命名为 "pg_temp_%d", MyBackendId,也就是以客户端后台进程的PID为后缀,保证各进程不冲突。(消耗 OID) 函数 InitTempTableNamespace定义于 src/backend/catalog/namespace.c 3、随后的创建过程与普通表区别不大,仍然消耗 OID 4、我们直接跳到缓存的分配看: bool isLocalBuf = SmgrIsTemp(smgr); if (isLocalBuf) { bufHdr = LocalBufferAlloc(smgr, forkNum, blockNum, &found); if (found) pgBufferUsage.local_blks_hit++; else pgBufferUsage.local_blks_read++; } 这里 isLocalBuf 就是临时表的意思 #define SmgrIsTemp(smgr) \ RelFileNodeBackendIsTemp((smgr)->smgr_rnode) /* * Augmenting a relfilenode with the backend ID provides all the information * we need to locate the physical storage. The backend ID is InvalidBackendId * for regular relations (those accessible to more than one backend), or the * owning backend's ID for backend-local relations. Backend-local relations * are always transient and removed in case of a database crash; they are * never WAL-logged or fsync'd. */ typedef struct RelFileNodeBackend { RelFileNode node; BackendId backend; } RelFileNodeBackend; 5、临时表缓存 if (LocalBufHash == NULL) InitLocalBuffers(); 这里就是文档 Chapter 18. Server Configuration,18.4.1. Memory 里边关于 temp_buffers 的说明:The setting can be changed within individual sessions, but only before the first use of temporary tables within the session; subsequent attempts to change the value will have no effect on that session. 第一次使用时才会初始化,随后再修改将不起作用。 -------------------------------------------- 权宗亮 -- Sent via pgsql-zh-general mailing list (pgsql-zh-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-zh-general