0

我的系统上安装了 Postgresql 12.8.1 版本和 python 3.7.6 版本。我想使用 plpython 创建一个触发函数,所以我使用CREATE EXTENSION plpython3u. 在尝试编译触发函数时,我遇到以下错误 -

错误:服务器意外关闭连接这可能意味着服务器在处理请求之前或期间异常终止

触发功能码:

CREATE OR REPLACE FUNCTION getTemperature() RETURNS trigger as $pgsqlTrigger$
city = TD["new"]["City"]
Station  = TD["new"]["Station"]
dtime = TD["new"]["dtime"]

import requests, json, time, pandas as pd, numpy as np
from datetime import datetime
from calendar import monthrange
from copy import deepcopy
import sys

template_request = "https://api.weather.com/v1/location/{station}/observations/historical.json?apiKey=apikey&units=m&startDate={start_date}&endDate={end_date}"
df_header = ["City", "Year", "Month", "Day", "Hour", "Temperature(C)", "Condition"]
def get_weather_data(city, year, month, day, station):
    start_date = "%d%02d%02d" % (year, month, day)
    end_date = "%d%02d%02d" % (year, month, day)
    request = template_request.format(station=station, start_date=start_date, end_date=end_date)
    request_data = json.loads(requests.get(request).content)
    weather_data = []
    last_dt = None
    for observation in request_data["observations"]:
        dt = datetime.fromtimestamp(observation["valid_time_gmt"]+3600)
        if last_dt and dt.hour > (last_dt.hour + 1):
            last_row = deepcopy(weather_data[-1])
            last_row[4] = last_row[4]+1
            weather_data.append(last_row)
        weather_data.append([city, year, month, dt.day, dt.hour, observation["temp"], observation["wx_phrase"]])
        last_dt = dt
    return weather_data
dtime = datetime.strptime(dtime, '%Y-%m-%d %H:%M:%S%z')
data = get_weather_data(city, dtime.year, dtime.month, dtime.day, station)
weather_df = pd.DataFrame(data, columns=df_header).drop_duplicates(subset=["City", "Year", "Month", "Day", "Hour"])
avg = (weather_df["Temperature(C)"].values).mean()
weather_df = pd.DataFrame()
TD["new"]["temp"] = avg;
return NEW;
$pgsqlTrigger$ LANGUAGE plpython3u;



CREATE TRIGGER pgsqlTrigger
BEFORE INSERT ON tweets
FOR EACH ROW EXECUTE FUNCTION getTemperature();

我还没有找到解决方案。关于我应该做什么的任何想法?

更新:

  1. 我正在使用 Windows 10 作为我的操作系统。
  2. 关于我如何安装我的 plpython3u 扩展。我遇到了错误“找不到名为 plpython3u 的此类模块”,我在网上找到的修复方法是将 python37.dll 从本地 python 文件夹复制并粘贴到我的 C:/windows/System32 文件夹。之后,我能够使用命令创建 plpython3u 扩展 - CREATE EXTENSION plpython3u。
  3. 我正在使用具有超级用户权限的默认用户“postgres”。

下面是服务器日志的输出: 日志文件输出:

2021-09-22 11:46:28.518 IST [10620] LOG:  server process (PID 2316) was terminated by exception 0xC0000409
2021-09-22 11:46:28.518 IST [10620] DETAIL:  Failed process was running: CREATE FUNCTION public.proc1()
2021-09-22 11:46:28.518 IST [10620] HINT:  See C include file "ntstatus.h" for a description of the hexadecimal value.
2021-09-22 11:46:28.522 IST [10620] LOG:  terminating any other active server processes
2021-09-22 11:46:28.547 IST [13168] WARNING:  terminating connection because of crash of another server process
2021-09-22 11:46:28.547 IST [13168] DETAIL:  The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
2021-09-22 11:46:28.547 IST [13168] HINT:  In a moment you should be able to reconnect to the database and repeat your command.
2021-09-22 11:46:28.566 IST [7776] WARNING:  terminating connection because of crash of another server process
2021-09-22 11:46:28.566 IST [7776] DETAIL:  The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
2021-09-22 11:46:28.566 IST [7776] HINT:  In a moment you should be able to reconnect to the database and repeat your command.
2021-09-22 11:46:28.586 IST [10620] LOG:  all server processes terminated; reinitializing
2021-09-22 11:46:28.668 IST [9204] LOG:  database system was interrupted; last known up at 2021-09-21 19:23:24 IST
2021-09-22 11:46:29.103 IST [14208] FATAL:  the database system is in recovery mode
2021-09-22 11:46:29.549 IST [9204] LOG:  database system was not properly shut down; automatic recovery in progress
2021-09-22 11:46:29.555 IST [9204] LOG:  redo starts at 0/1D19AB8
2021-09-22 11:46:29.562 IST [9204] LOG:  invalid record length at 0/1D21978: wanted 24, got 0
2021-09-22 11:46:29.563 IST [9204] LOG:  redo done at 0/1D21940
2021-09-22 11:46:29.618 IST [10620] LOG:  database system is ready to accept connections
4

0 回答 0