另一种获取描述而不是实际列名的方法是创建视图(每个表一个)。这可以通过自动生成视图来自动化。这看起来相当笨拙,但它有一个巨大的优势,即对于“复杂*”查询,生成的查询计划将与原始列名完全相同。(加入复杂查询的函数将表现不佳:优化器无法将它们分开,所以产生的行为将等同于“一次一行”)示例:
-- tmp schema is only for testing
DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;
CREATE TABLE thedata
( a00600 varchar
, a00900 varchar
);
INSERT INTO thedata(a00600 , a00900) VALUES
('key1', 'data1')
,('key2', 'data2');
CREATE TABLE thedict
( variable varchar
, description varchar
);
INSERT INTO thedict(variable , description) VALUES
('a00600' , 'Total population')
,('a00900' , 'Zipcode' );
CREATE OR REPLACE FUNCTION create_view_definition(zname varchar)
RETURNS varchar AS
$BODY$
DECLARE
thestring varchar;
therecord RECORD;
iter INTEGER ;
thecurs cursor for
SELECT co.attname AS zname, d.description AS zdesc
FROM pg_class ct
JOIN pg_namespace cs ON cs.oid=ct.relnamespace
JOIN pg_attribute co ON co.attrelid = ct.oid AND co.attnum > 0
LEFT JOIN thedict d ON d.variable = co.attname
WHERE ct.relname = 'thedata'
AND cs.nspname = 'tmp'
;
BEGIN
thestring = '' ;
iter = 0;
FOR therecord IN thecurs LOOP
IF (iter = 0) THEN
thestring = 'CREATE VIEW ' || quote_ident('v'||zname) || ' AS ( SELECT ' ;
ELSE
thestring = thestring || ', ';
END IF;
iter=iter+1;
thestring = thestring || quote_ident(therecord.zname);
IF (therecord.zdesc IS NOT NULL) THEN
thestring = thestring || ' AS ' || quote_ident(therecord.zdesc);
END IF;
END LOOP;
IF (iter > 0) THEN
thestring = thestring || ' FROM ' || quote_ident(zname) || ' )' ;
END IF;
RETURN thestring;
END;
$BODY$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION execute_view_definition(zname varchar)
RETURNS INTEGER AS
$BODY$
DECLARE
meat varchar;
BEGIN
meat = create_view_definition(zname);
EXECUTE meat;
RETURN 0;
END;
$BODY$ LANGUAGE plpgsql;
SELECT create_view_definition('thedata');
SELECT execute_view_definition('thedata');
SELECT * FROM vthedata;
结果:
CREATE FUNCTION
CREATE FUNCTION
create_view_definition
---------------------------------------------------------------------------------------------------
CREATE VIEW vthedata AS ( SELECT a00600 AS "Total population", a00900 AS "Zipcode" FROM thedata )
(1 row)
execute_view_definition
-------------------------
0
(1 row)
Total population | Zipcode
------------------+---------
key1 | data1
key2 | data2
(2 rows)
请注意,这只是一个示例。如果是真的,我至少会将生成的视图放入单独的模式中,以避免名称冲突和原始模式的污染。