- postgres=#select*from(select*frompg_class);
- ERROR:subqueryinFROMmusthaveanalias
- LINE1:select*from(select*frompg_class);
- ^
- HINT:Forexample,FROM(SELECT...)[AS]foo.
- postgres=#
而带子查询的是这样:
- postgres=#selectamnamefrom(select*frompg_am)asalimit3;
- amname
- --------
- btree
- hash
- gist
- (3rows)
- postgres=#
Oracle下,有无别名均可:
没什么大的影响,但如果遇到Oracle程序向PG迁移,改起来就有点罗嗦,这时候我们不妨让PG变化一下。
这个目标是非常明确,直接去源代码里边:grep -r "subquery in FROM must have an alias",发现它在src/backend/parser/gram.y
沿着代码往下看,可以看到子查询别名为空时报错,办法就很直截了当了,未指定别名时给他产生一个。
出错代码替换为:
- Alias*a=makeNode(Alias);
- a->aliasname="alias_xxxx";
- n->alias=a;
编译之后运行:
- template1=#selectamnamefrom(select*frompg_am)limit3;
- amname
- --------
- btree
- hash
- gist
- (3rows)
- template1=#
小打小闹修改PG就是如此简单,当然这里还有更多工作要做,比如别名应该随机生成而不是像这样写死。
- template1=#selectamnamefrom(select*frompg_am),(select*frompg_database);
- ERROR:tablename"alias_xxxx"specifiedmorethanonce
- template1=#
这肯定不是我们想要的结果
作为一个演示的例子,到此已经足够。
神州飞象(北京)数据科技有限公司,专业Postgresql产品与服务提供商