圣光照耀联盟—PostgreSQL临时表的创建与使用过程
2016-06-28 09:30
519 查看
1、临时表的创建与普通表没什么区别,都是通过 DefineRelation 函数 (定义于 src/backend/commands/tablecmds.c),临时表具有属性 relation->relpersistence == RELPERSISTENCE_TEMP
2、首先为临时表创建临时schema,命名为 "pg_temp_%d", MyBackendId,也就是以客户端后台进程的PID为后缀,保证各进程不冲突。(消耗 OID)
函数 InitTempTableNamespace定义于 src/backend/catalog/namespace.c
3、随后的创建过程与普通表区别不大,仍然消耗 OID
4、我们直接跳到缓存的分配看:
这里 isLocalBuf 就是临时表的意思
5、临时表缓存
这里就是文档 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.
第一次使用时才会初始化,随后再修改将不起作用。
--------------------------------------------
权宗亮
神州飞象(北京)数据科技有限公司
我们的力量源自最先进的开源数据库PostgreSQL
zongliang.quan@postgresdata.com
#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.
第一次使用时才会初始化,随后再修改将不起作用。
--------------------------------------------
权宗亮
神州飞象(北京)数据科技有限公司
我们的力量源自最先进的开源数据库PostgreSQL
zongliang.quan@postgresdata.com
相关文章推荐
- (转)SQL Server中使用convert进行日期转换
- Hibernate的七种映射关系之七种关联映射
- PostgreSQL 锁监控
- mysql 行列转换方式
- oracle 导入txt
- [转]MySQL主从复制入门
- mysql常用的增删改操作
- ORACLE存储过程while循环的使用
- oracle 按自定义的序列排序结果集
- session入mysql
- SQLite更新数据列,给数据加一个前(后)缀
- Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
- 数据库事务
- Sql的行列(纵横表)转换
- mysql xtrabackup 备份恢复实现分享
- MYSQL + MHA +keepalive + VIP安装配置(一)--MYSQL安装配置
- MYSQL + MHA +keepalive + VIP安装配置(二)--MHA的配置
- mysql数据库sql优化原则
- 数据库分词查询的优缺点以及英文和中文各自的分词方法(二)
- MySQL添加用户、删除用户与授权