2

I want to use plpython in postgresql's function. I installed Python3.9 and configured the path and some other configations, and I copied the python's dll into the pg directory (my server is windows server 2019, my pg is pg13), and I created the plpython3u language.

I tried some simple script to test plpython3u, everything looks so fine. In fact, I want to connect to mssql to synchronize data between pg and sqlserver, so I installed pymssql package by pip command.

I can successfullly create the function, but cannot call it normally. Below is my script:

create or replace function public._py_sync_mspg()
  returns bigint
  language plpython3u
  cost 100
  volatile 
as $body$
import pymssql
conn = pymssql.connect('mssql host', 'ms user', 'mssql password', "database")
cursor = conn.cursor(as_dict=True)  

cursor.execute('select fitemid,fname from t_empl_detail where fname=%s', 'someone')
for row in cursor:
    print("ID=%d, Name=%s" % (row['fitemid'], row['fname']))

conn.close()
$body$;

alter function public._py_sync_mspg()
    owner to postgres;

I can create the function, But I can not call the function!

Below is the error message:

The application has lost the database connection: ⁃ If the connection was idle it may have been forcibly disconnected. ⁃ The application server or database server may have been restarted. ⁃ The user session may have timed out. Do you want to continue and establish a new session?

Below is the error captured image:

the error capture

I ever encountered the error for could not load some dll, I solved it. I ever thought that the import clause could not be used, I tried import os, that's ok. Now the current error completely stopped my hope to connect to SQLServer.

What's wrong?

Waiting for your help, thanks very much.

3
  • Look in the PostgreSQL servers log file for the error message.
    – jjanes
    Dec 4, 2020 at 21:41
  • I tried but I can not find the log for this. Is there any other reason that will cause this kind of error?
    – Jsy
    Dec 5, 2020 at 11:59
  • You can't find the database server's log file at all, or you can't find a message in it that you think is relevant?
    – jjanes
    Dec 5, 2020 at 15:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.