SELECT 表名 FROM 表名

再次看到这段代码,想起来以前随口在QQ群里提过,突然感觉还是记录一下比较好,所以专门开辟新博客分类,我们来看看 PostgreSQL 代码中都隐藏着什么秘密


假如我们发起一条语句:

SELECT rolname FROM pg_authid;

会发生什么事呢?其中一个步骤是确定表中是否有rolname

/* Try to identify as an unqualified column */
node = colNameToVar(pstate, colname, false, cref->location);

if (node == NULL)
{
...
	/*
	 * Try to find the name as a relation.  Note that only
	 * relations already entered into the rangetable will be
	 * recognized.
	 *
	 * This is a hack for backwards compatibility with
	 * PostQUEL-inspired syntax.  The preferred form now is
	 * "rel.*".
	 */
	rte = refnameRangeTblEntry(pstate, NULL, colname,
							   cref->location,
							   &levels_up);
	if (rte)
		node = transformWholeRowRef(pstate, rte,
									cref->location);

最后一段很奇怪,好在有说明,测试一下确实可用,但是这是我以前从来不知道的用法。

postgres=# SELECT pg_authid FROM pg_authid;
          pg_authid          
-----------------------------
 (quanzl,t,t,t,t,t,t,t,-1,,)
(1 row)

postgres=#

粗略查看了一下 9.4.5 文档,似乎没有关于这个的说明,应该是一个废弃已久的用法。

PostgreSQL代码中隐藏着很多秘密,我要继续挖掘。

你可能感兴趣的:(PostgreSQL)