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

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,企业版 4核16GB
推荐场景:
HTAP混合负载
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介:

回到上一个层面,继续看 PortalStart的处理:

复制代码
void
PortalStart(Portal portal, ParamListInfo params,
            int eflags, bool use_active_snapshot)
{
    ...
    PG_TRY();
    {
        ...

        /*
         * Determine the portal execution strategy
         */
        portal->strategy = ChoosePortalStrategy(portal->stmts);

        /*
         * Fire her up according to the strategy
         */
        switch (portal->strategy)
        {
            case PORTAL_ONE_SELECT:

                /* Must set snapshot before starting executor. */
                if (use_active_snapshot)
                    PushActiveSnapshot(GetActiveSnapshot());
                else
                    PushActiveSnapshot(GetTransactionSnapshot());

                /*
                 * Create QueryDesc in portal's context; for the moment, set
                 * the destination to DestNone.
                 */
               queryDesc = CreateQueryDesc((PlannedStmt *) linitial(portal->stmts,
                                            portal->sourceText,
                                            GetActiveSnapshot(),
                                            InvalidSnapshot,
                                            None_Receiver,
                                            params,
                                            0);

                /*
                 * If it's a scrollable cursor, executor needs to support
                 * REWIND and backwards scan, as well as whatever the caller
                 * might've asked for.
                 */
                if (portal->cursorOptions & CURSOR_OPT_SCROLL)
                    myeflags = eflags | EXEC_FLAG_REWIND | EXEC_FLAG_BACKWARD;
                else
                    myeflags = eflags;

                /*
                 * Call ExecutorStart to prepare the plan for execution
                 */
                ExecutorStart(queryDesc, myeflags);

                /*
                 * This tells PortalCleanup to shut down the executor
                 */
                portal->queryDesc = queryDesc;

                /*
                 * Remember tuple descriptor (computed by ExecutorStart)
                 */
                portal->tupDesc = queryDesc->tupDesc;

                /*
                 * Reset cursor position data to "start of query"
                 */
                portal->atStart = true;
                portal->atEnd = false;    /* allow fetches */
                portal->portalPos = 0;
                portal->posOverflow = false;

                PopActiveSnapshot();
                break;

            case PORTAL_ONE_RETURNING:
            case PORTAL_ONE_MOD_WITH:

                ...
                break;

            case PORTAL_UTIL_SELECT:
                ...
                break;

            case PORTAL_MULTI_QUERY:
                /* Need do nothing now */
                portal->tupDesc = NULL;
                break;
        }
    }
    PG_CATCH();
    {
        ...
        PG_RE_THROW();
    }
    PG_END_TRY();
    ...
}
复制代码

由之前的分析可以知道,满足  case PORTAL_ONE_SELECT 的条件,下面再看

use_active_snapshot,回溯上层:

复制代码
static void
exec_simple_query(const char *query_string)
{
    ...
    foreach(parsetree_item, parsetree_list)
    {
        ...
        /*
         * Set up a snapshot if parse analysis/planning will need one.
         */
        if (analyze_requires_snapshot(parsetree))
        {
            PushActiveSnapshot(GetTransactionSnapshot());
            snapshot_set = true;
        }
        ...
        PortalStart(portal, NULL, 0, snapshot_set);
    }
    ...
}
复制代码

可见,snapshot 还是要搞的。简言之,snapshot 是为了MVCC控制:

复制代码
typedef struct SnapshotData
{
    SnapshotSatisfiesFunc satisfies;    /* tuple test function */

    /*
     * The remaining fields are used only for MVCC snapshots, and are normally
     * just zeroes in special snapshots.  (But xmin and xmax are used
     * specially by HeapTupleSatisfiesDirty.)
     *
     * An MVCC snapshot can never see the effects of XIDs >= xmax. It can see
     * the effects of all older XIDs except those listed in the snapshot. xmin
     * is stored as an optimization to avoid needing to search the XID arrays
     * for most tuples.
     */
    TransactionId xmin;            /* all XID < xmin are visible to me */
    TransactionId xmax;            /* all XID >= xmax are invisible to me */
    TransactionId *xip;            /* array of xact IDs in progress */
    uint32        xcnt;            /* # of xact ids in xip[] */
    /* note: all ids in xip[] satisfy xmin <= xip[i] < xmax */
    int32        subxcnt;        /* # of xact ids in subxip[] */
    TransactionId *subxip;        /* array of subxact IDs in progress */
    bool        suboverflowed;    /* has the subxip array overflowed? */
    bool        takenDuringRecovery;    /* recovery-shaped snapshot? */
    bool        copied;            /* false if it's a static snapshot */

    /*
     * note: all ids in subxip[] are >= xmin, but we don't bother filtering
     * out any that are >= xmax
     */
    CommandId    curcid;            /* in my xact, CID < curcid are visible */
    uint32        active_count;    /* refcount on ActiveSnapshot stack */
    uint32        regd_count;        /* refcount on RegisteredSnapshotList */
} SnapshotData;
复制代码





相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
3天前
|
SQL 自然语言处理 关系型数据库
PolarDB上实现一个自然语言查询系统
PolarDB上实现一个自然语言查询系统
|
2天前
|
SQL 自然语言处理 网络协议
【Linux开发实战指南】基于TCP、进程数据结构与SQL数据库:构建在线云词典系统(含注册、登录、查询、历史记录管理功能及源码分享)
TCP(Transmission Control Protocol)连接是互联网上最常用的一种面向连接、可靠的、基于字节流的传输层通信协议。建立TCP连接需要经过著名的“三次握手”过程: 1. SYN(同步序列编号):客户端发送一个SYN包给服务器,并进入SYN_SEND状态,等待服务器确认。 2. SYN-ACK:服务器收到SYN包后,回应一个SYN-ACK(SYN+ACKnowledgment)包,告诉客户端其接收到了请求,并同意建立连接,此时服务器进入SYN_RECV状态。 3. ACK(确认字符):客户端收到服务器的SYN-ACK包后,发送一个ACK包给服务器,确认收到了服务器的确
|
6天前
|
SQL DataWorks 关系型数据库
DataWorks产品使用合集之如何将硬编码的配置值(例如SQL查询中的固定值)更改为调度参数
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
33 7
|
3天前
|
SQL 存储 关系型数据库
关系型数据库SQL Server学习
【7月更文挑战第4天】
11 2
|
3天前
|
SQL 自然语言处理 关系型数据库
PolarDB自然语言到SQL语言转义
PolarDB自然语言到SQL语言转义
|
4天前
|
SQL 搜索推荐 Java
什么是笛卡尔积及其在SQL查询中的应用
什么是笛卡尔积及其在SQL查询中的应用
|
8天前
|
SQL 运维 安全
数据管理DMS产品使用合集之执行SQL时,如何添加Hint来改变查询的执行计划
阿里云数据管理DMS提供了全面的数据管理、数据库运维、数据安全、数据迁移与同步等功能,助力企业高效、安全地进行数据库管理和运维工作。以下是DMS产品使用合集的详细介绍。
20 1
|
1天前
|
SQL 人工智能 自然语言处理
一款利用人工智能将自然语言查询转换为 SQL 代码的互译工具 - SQL Translator
一款利用人工智能将自然语言查询转换为 SQL 代码的互译工具 - SQL Translator
|
2天前
|
SQL 机器学习/深度学习 自然语言处理
大数据SQL助手:告别繁琐数据处理,轻松搞定SQL查询!
大数据SQL助手:告别繁琐数据处理,轻松搞定SQL查询!
8 0
|
9天前
|
SQL 搜索推荐 Java
什么是笛卡尔积及其在SQL查询中的应用
什么是笛卡尔积及其在SQL查询中的应用