postgres函数/扩展没有正确转储或恢复?

时间:2017-06-14 13:02:44

标签: postgresql postgresql-9.4

更新

自我回答,请参阅此处的第一条评论和我自己的答案。

原始问题

我正在设置一个带有一些sql文件的本地开发postgres数据库,其中一个为距离计算创建了一个自定义函数。创建扩展“cube”和“earthdistance”,该函数按预期工作。

我正在转储本地开发postgres db,sql转储包含扩展创建和自定义函数 - 看起来没问题。

我正在另一台测试机上导入转储而没有错误消息。

但是在这里我使用函数时遇到异常:

Caused by: org.postgresql.util.PSQLException: ERROR: function ll_to_earth(numeric, numeric) does not exist
  Hint: No function matches the given name and argument types. You might need to add explicit type casts.
  Where: PL/pgSQL function opengeodb_radius_selection(numeric,numeric,integer,integer) line 3 at RETURN QUERY

设置本地db时使用的完整SQL代码:

CREATE EXTENSION cube;
CREATE EXTENSION earthdistance;

CREATE FUNCTION
    opengeodb_radius_selection(
        baseLatitude numeric(9,6),
        baseLongitude numeric(9,6),
        radius_in_metres integer,
        opengeodb_level integer)
    RETURNS TABLE(
        locid integer,
        name character varying(255),
        distance float8)
AS
$func$
BEGIN
    RETURN QUERY
    SELECT subQueryAlias.locid, subQueryAlias.name, subQueryAlias.distance
FROM
(
    SELECT *, earth_distance(ll_to_earth(baseLatitude,baseLongitude), ll_to_earth(lat,lon)) as distance
    FROM opengeodb
    GROUP BY opengeodb.locid, opengeodb.lat, opengeodb.lon, opengeodb.name, distance
) subQueryAlias
WHERE plz is not null
AND plz <> ''
AND level = opengeodb_level
AND earth_box(ll_to_earth(lat,lon),radius_in_metres) @> ll_to_earth(baseLatitude,baseLongitude)
AND subQueryAlias.distance <= radius_in_metres
ORDER BY subQueryAlias.distance;
END
$func$ LANGUAGE plpgsql;

转储中的完整SQL代码:

--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;

--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: 
--

CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;


--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: 
--

COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';


--
-- Name: cube; Type: EXTENSION; Schema: -; Owner: 
--

CREATE EXTENSION IF NOT EXISTS cube WITH SCHEMA public;


--
-- Name: EXTENSION cube; Type: COMMENT; Schema: -; Owner: 
--

COMMENT ON EXTENSION cube IS 'data type for multidimensional cubes';


--
-- Name: earthdistance; Type: EXTENSION; Schema: -; Owner: 
--

CREATE EXTENSION IF NOT EXISTS earthdistance WITH SCHEMA public;


--
-- Name: EXTENSION earthdistance; Type: COMMENT; Schema: -; Owner: 
--

COMMENT ON EXTENSION earthdistance IS 'calculate great-circle distances on the surface of the Earth';


SET search_path = public, pg_catalog;

--
-- Name: opengeodb_radius_selection(numeric, numeric, integer, integer); Type: FUNCTION; Schema: public; Owner: postgres
--

CREATE FUNCTION opengeodb_radius_selection(baselatitude numeric, baselongitude numeric, radius_in_metres integer, opengeodb_level integer) RETURNS TABLE(locid integer, name character varying, distance double precision)
    LANGUAGE plpgsql
    AS $$
BEGIN
    RETURN QUERY
    SELECT subQueryAlias.locid, subQueryAlias.name, subQueryAlias.distance
    FROM
    (
        SELECT *, earth_distance(ll_to_earth(baseLatitude,baseLongitude), ll_to_earth(lat,lon)) as distance
        FROM opengeodb
        GROUP BY opengeodb.locid, opengeodb.lat, opengeodb.lon, opengeodb.name, distance
    ) subQueryAlias
    WHERE plz is not null
    AND plz <> ''
    AND level = opengeodb_level
    AND earth_box(ll_to_earth(lat,lon),radius_in_metres) @> ll_to_earth(baseLatitude,baseLongitude)
    AND subQueryAlias.distance <= radius_in_metres
    ORDER BY subQueryAlias.distance;
END
$$;


ALTER FUNCTION public.opengeodb_radius_selection(baselatitude numeric, baselongitude numeric, radius_in_metres integer, opengeodb_level integer) OWNER TO postgres;

SET default_tablespace = '';

SET default_with_oids = false;

...(followed by table creation and so on)

这是我的错吗或错误行为?

2 个答案:

答案 0 :(得分:0)

ll_to_earth被定义为接受参数(float8, float8),但您正在传递numeric, numeric。所以你需要为你的函数添加一些强制转换。

目前尚不清楚为何之前有效。也许你是从旧版本升级的?

答案 1 :(得分:0)

自我回答(参见第一个问题评论)

在debian系统上安装了9.4.10,没有postgresql-contrib for 9.4.10 for debian(但在我的开发windows postgres包中,也是9.4.10)

我们已经将debian系统更新到9.4.12并且现在包含了contrib,该函数正常工作。