您的位置:首页 > 数据库

PostgreSQL在何处处理 sql查询之三

2013-05-22 10:30 483 查看
前面已经说过,在 exec_simple_query中,完成sql文的执行。

具体地说,是要构造portal,然后运行 PortalStart , PortalRun...

下面就先看看 portal如何构造:

在 exec_simple_query中,有这么一段:

/*
* Create unnamed portal to run the query or queries in. If there
* already is one, silently drop it.
*/
portal = CreatePortal("", true, true);
/* Don't display the portal in pg_cursors */
portal->visible = false;


具体的CreatePortal,是如何作的(portalmem.c):

/*
* CreatePortal
*        Returns a new portal given a name.
*
* allowDup: if true, automatically drop any pre-existing portal of the
* same name (if false, an error is raised).
*
* dupSilent: if true, don't even emit a WARNING.
*/
Portal
CreatePortal(const char *name, bool allowDup, bool dupSilent)
{
Portal        portal;

AssertArg(PointerIsValid(name));

portal = GetPortalByName(name);

if (PortalIsValid(portal))
{
if (!allowDup)
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_CURSOR),
errmsg("cursor \"%s\" already exists", name)));
if (!dupSilent)
ereport(WARNING,
(errcode(ERRCODE_DUPLICATE_CURSOR),
errmsg("closing existing cursor \"%s\"",
name)));
PortalDrop(portal, false);
}

/* make new portal structure */
portal = (Portal) MemoryContextAllocZero(PortalMemory, sizeof *portal);

/* initialize portal heap context; typically it won't store much */
portal->heap = AllocSetContextCreate(PortalMemory,
"PortalHeapMemory",
ALLOCSET_SMALL_MINSIZE,
ALLOCSET_SMALL_INITSIZE,
ALLOCSET_SMALL_MAXSIZE);

/* create a resource owner for the portal */
portal->resowner = ResourceOwnerCreate(CurTransactionResourceOwner,
"Portal");

/* initialize portal fields that don't start off zero */
portal->status = PORTAL_NEW;
portal->cleanup = PortalCleanup;
portal->createSubid = GetCurrentSubTransactionId();
portal->strategy = PORTAL_MULTI_QUERY;
portal->cursorOptions = CURSOR_OPT_NO_SCROLL;
portal->atStart = true;
portal->atEnd = true;        /* disallow fetches until query is set */
portal->visible = true;
portal->creation_time = GetCurrentStatementStartTimestamp();

/* put portal in table (sets portal->name) */
PortalHashTableInsert(portal, name);

return portal;
}


由于调用 CreatePortal 的时候,传递name是空值,所以 portal = GetPortalByName(name); 返回NULL。

通过 MemoryContextAllocZero 建立一个初始的portal,然后再赋予各种值,最后压入哈希表中。此时name依然为空。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: