Using psycopg2 package with python 2.7 I keep getting the titled error: psycopg2.DatabaseError: SSL SYSCALL error: EOF detected
It only occurs when I add a WHERE column LIKE ''%X%'' clause to my pgrouting query. An example:
SELECT id1 as node, cost FROM PGR_Driving_Distance(
'SELECT id, source, target, cost
FROM edge_table
WHERE cost IS NOT NULL and column LIKE ''%x%'' ',
1, 10, false, false)
Threads on the internet suggest it is an issue with SSL intuitively, but whenever I comment out the pattern matching side of things the query and connection to the database works fine.
This is on a local database running Xubuntu 13.10.
After further investigation: It looks like this may be cause by the pgrouting extension crashing the database because it is a bad query and their are not links which have this pattern.
Will post an answer soon ...
The error: psycopg2.operationalerror: SSL SYSCALL error: EOF detected
The setup: Airflow + Redshift + psycopg2
When: Queries take a long time to execute (more than 300 seconds).
A socket timeout occurs in this instance. What solves this specific variant of the error is adding keepalive arguments to the connection string.
keepalive_kwargs = {
"keepalives": 1,
"keepalives_idle": 30,
"keepalives_interval": 5,
"keepalives_count": 5,
}
conection = psycopg2.connect(connection_string, **keepalive_kwargs)
Redshift requires a keepalives_idle of less than 300. A value of 30 worked for me, your mileage may vary. It is also possible that the keepalives_idle argument is the only one you need to set - but ensure keepalives is set to 1.
Link to docs on postgres keepalives.
Link to airflow doc advising on 300 timeout.
I ran into this problem when running a slow query in a Droplet on a Digital Ocean instance. All other SQL would run fine and it worked on my laptop. After scaling up to a 1 GB RAM instance instead of 512 MB it works fine so it seems that this error could occur if the process is running out of memory.
Very similar answer to what #FoxMulder900 did, except I could not get his first select to work. This works, though:
WITH long_running AS (
SELECT pid, now() - pg_stat_activity.query_start AS duration, query, state
FROM pg_stat_activity
WHERE (now() - pg_stat_activity.query_start) > interval '1 minutes'
and state = 'active'
)
SELECT * from long_running;
If you want to kill the processes from long_running just comment out the last line and insert SELECT pg_cancel_backend(long_running.pid) from long_running ;
This issue occurred for me when I had some rogue queries running causing tables to be locked indefinitely. I was able to see the queries by running:
SELECT * from STV_RECENTS where status='Running' order by starttime desc;
then kill them with:
SELECT pg_terminate_backend(<pid>);
I encountered the same error. By CPU, RAM usage everything was ok, solution by #antonagestam didn't work for me.
Basically, the issue was at the step of engine creation. pool_pre_ping=True solved the problem:
engine = sqlalchemy.create_engine(connection_string, pool_pre_ping=True)
What it does, is that each time when the connection is being used, it sends SELECT 1 query to check the connection. If it is failed, then the connection is recycled and checked again. Upon success, the query is then executed.
sqlalchemy docs on pool_pre_ping
In my case, I had the same error in python logs. I checked the log file in /var/log/postgresql/, and there were a lot of error messages could not receive data from client: Connection reset by peer and unexpected EOF on client connection with an open transaction. This can happen due to network issues.
In my case that was OOM killer (query is too heavy)
Check dmesg:
dmesg | grep -A2 Kill
In my case:
Out of memory: Kill process 28715 (postgres) score 150 or sacrifice child
I got this error running a large UPDATE statement on a 3 million row table. In my case it turned out the disk was full. Once I had added more space the UPDATE worked fine.
You may need to express % as %% because % is the placeholder marker. http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries
Related
I have been searching for an answer for this for hours, but unfortunately the closest thing I can find is 1 unanswered question. This is a similar issue, but it unfortunately did not have a resolution.
I had a working connection to a IBM DB2 database, but the web console was erroring out, so I was forced to delete the instance and make a new one. I changed nothing regarding the code to connect other than the values used to connect. When I changed these values the ibm_db.connect function runs continuously. There are no output errors as I have left it running for 10 minutes and nothing happens at all. I do change the values to force an error and it will error out saying the values are not correct. I have no clue what the problem is as I have no information to go off of. My only thought is the SSL could have something to do with it.
dsn_driver = connection_data['dsn_driver']
dsn_database = connection_data['dsn_database']
dsn_hostname = connection_data['dsn_hostname']
dsn_port = connection_data['dsn_port']
dsn_protocol = connection_data['dsn_protocol']
dsn_uid = connection_data['dsn_uid']
dsn_pwd = connection_data['dsn_pwd']
dsn = (
"DRIVER={0};"
"DATABASE={1};"
"HOSTNAME={2};"
"PORT={3};"
"PROTOCOL={4};"
"UID={5};"
"PWD={6};").format(dsn_driver, dsn_database, dsn_hostname,
dsn_port, dsn_protocol, dsn_uid, dsn_pwd)
try:
connection = ibm_db.connect(dsn, "", "")
print("Connected to database: ", dsn_database,
"as user: ", dsn_uid, "on host: ", dsn_hostname)
return connection
except:
print("Unable to connect: ", ibm_db.conn_errormsg())
The breakpoint is at connection = ibm_db.connect(dsn, "", "")
This data is loaded from a local JSON file with the following values (except for sensitive information).
{
"dsn_driver": "{IBM DB2 ODBC DRIVER}",
"dsn_database":"BLUDB",
"dsn_hostname": "hostname",
"dsn_port": "port",
"dsn_protocol": "TCPIP",
"dsn_uid": "uid",
"dsn_pwd": "pwd"
}
I have tried everything I can think of, but since nothing outputs I unfortunately do not know where to start. If someone has experience with this please let me know.
Thank you.
Edit: I did end up getting this error message returned from the ibm_db.connect method
Unable to connect: [IBM][CLI Driver] SQL30081N A communication error has been detected. Communication protocol being used: "TCP/IP". Communication API being used: "SOCKETS". Location where the error was detected: "xxx.xx.xxx.xxx". Communication function detecting the error: "recv". Protocol specific err SQLCODE=-30081054", "*", "0". SQLSTATE=08001
A couple of points for clarification:
When you say "the ibm_db.connect function runs continuously" do you mean you see the CPU spinning or just that the python process doesn't progress past the connect?
What type of database are you connecting to? DB2 LUW or z/OS?
Have you tried to make sure that the connectivity is still working? i.e. did you try the suggestion from the other linked post? This:
To verify that there is network connectivity between you and the database you can try telnet xxxxxx 1234 (or nc xxxxxx 1234, where xxxxxx and 1234 are the service hostname and port, respectively
From a debugging point of view I'd be looking at the logs of the intervening processes:
Db2 Connect log if you are using it
DB2 target logs
TCPIP and z/OS Connect address spaces if z/os. BAQ region ? (not sure if that would just be my site)
Firewall - I know that you had a working connection but always best to check the obvious as well
As you've pointed out, without an error message it's hard to know where to start
I have a simple lambda function which prints an event and then attempts to insert a row into a database. It runs with no error, but does not execute all of the code.
event gets printed, but the row never gets inserted into the table. Anything, even a print statement I put after connection doesn't get executed. I'm guessing something is wrong with the connection, but as far as I know I have no way of telling what is wrong. Are there more logs somewhere? In CloudWatch I see at the end it says Task timed out after 3.00 seconds
import boto3
import psycopg2
s3 = boto3.client('s3')
def insert_data(event=None, context=None):
print(event)
connection = psycopg2.connect(user="xxxx", password="xxxx",
host="xxxx", port="xx",
database="xxxx")
cursor = connection.cursor()
postgres_insert_query = "INSERT INTO dronedata (name,lat,long,other) VALUES ('img2','54','43','from lambda')"
cursor.execute(postgres_insert_query)
connection.commit()
count = cursor.rowcount
print(count, "Record inserted successfully into mobile table")
The typical security setup is:
A security group on the AWS Lambda function (Lambda-SG) that permits all outbound access (no need for inbound rules)
A security group on the database (either an EC2 instance or Amazon RDS) (DB-SG) that permits inbound access on the appropriate port from Lambda-SG
That is, DB-SG should specifically reference Lambda-SG in its inbound rules.
Yes, you have to increase default Timeout from 3 seconds to more:
Timeout – The amount of time that Lambda allows a function to run before stopping it. The default is 3 seconds. The maximum allowed value is 900 seconds.
hence psycopg2 is an external lib, please upload that lib along with your code into your Lambda Function. So Issue is, it is not able to connect, that's why you are facing a timeout issue.
I am trying to resolve a timeout issue regarding my SQL database. The error occurs in the:
SQLUpdate="UPDATE scoutinfo SET patrolID=1 WHERE patrolID=%s"
It seems this command takes too long to execute as I receive this error.
mysql.connector.errors.DatabaseError: 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
Is their some setting I need to change on MySQL to allow python to update/delete rows in the databae. The database is relatively small (Number of rows in each table <25)
SQLPatrolID="SELECT patrolID FROM patrols WHERE patrolname=%s"
mycursor.execute(SQLPatrolID,(DPatrol.get(), ))
myresult=mycursor.fetchall()
PatrolID=myresult[0][0]
print(PatrolID)
SQLUpdate="UPDATE scoutinfo SET patrolID=1 WHERE patrolID=%s"
mycursor.execute(SQLUpdate,(PatrolID, ))
mydb.commit()
print("Success!")
SQLDeletePatrol="DELETE patrolinfo WHERE patrolID=%s"
mycursor.execute(SQLDeletePatrol,(PatrolID, ))
mydb.commit()
Any extra information you require I can happily provide.
No. Your query is not taking too long to execute. It's taking too long to acquire a lock on the tuples you are about to update.
What does this mean? There is another query/transaction, updating the exact same records at the same time. It's probably right there on your code, or it's probably a different thread/application. I would think it is the first case though.
You can see who's holding a lock by inspecting these tables:
INNODB_LOCK_WAITS
INNODB_LOCKS
Or, run the following command:
> show engine innodb status;
This will work if you are using INNODB engine. Which you probably are.
I have a large collection of documents which I'm trying to update using the pymongo.update function. I am finding all documents that fall within a certain polygon and updating all the
points found with "update_value".
for element in geomShapeCollection:
db.collectionName.update({"coordinates":{"$geoWithin":{"$geometry":element["geometry_part"]}}}, {"$set":{"Update_key": update_value}}, multi = True, timeout=False)
For smaller collections this command works as expected. In the largest dataset
the command works for 70-80% of the data and then throws the error:
pymongo.errors.OperationFailure: cursor id '428737620678732339' not
valid at server
The pymongo documentation tells me that this is possibly due to a timeout issue.
Cursors in MongoDB can timeout on the server if they’ve been open for
a long time without any operations being performed on them.
Reading through the pymongo documentation, the find() function has a boolean flag for timeout.
find(spec=None, fields=None, skip=0, limit=0, timeout=True, snapshot=False, tailable=False, _sock=None, _must_use_master=False,_is_command=False)
However the update function appears not to have this:
update(spec, document, upsert=False, manipulate=False, safe=False, multi=False)
Is there any way to set this timeout flag for the update function? Is there any way I can change this so that I do not get this OperationFailure error? Am I correct in assuming this is an timeout error as pymongo states that it throws this error when
Raised when a database operation fails.
After some research and lots of experimentation I found that it was the outer loop cursor that was causing the error.
for element in geomShapeCollection:
geomShapeCollection is a cursor to a mongodb collection. There are several elements in geoShapeCollection where large amounts of elements fall, because these updates take such a considerable amount of time the geomShapeCollection cursor closes.
The problem was not with the update function at all. Adding a (timeout=False) to the outer cursor solves this problem.
for element in db.geomShapeCollectionName.find(timeout=False):
db.collectionName.update({"coordinates":{"$geoWithin":{"$geometry":element["geometry_part"]}}}, {"$set":{"Update_key": update_value}}, multi = True, timeout=False)
I am rewriting a python script to store data from an arduino in a postgresql data base, wanting to run it as a deamon using python-daemon. The original script works fine, but in the deamon, I cannot write to the database. The first attempt ends up with:
<class 'psycopg2.DatabaseError'>, DatabaseError('SSL SYSCALL error: EOF detected\n'
and then:
<class 'psycopg2.InterfaceError'>, InterfaceError('cursor already closed',)
In the working script, I do:
connstring="dbname='"+dbdatabase+"' user='"+dbusername+"' host='"+dbhost+"'password='"+dbpassword+"'"
try:
conn = psycopg2.connect(connstring)
cur=conn.cursor()
except:
my_logger.critical(appname+": Unable to connect to the database")
sys.exit(2)
sql="insert into measure (sensorid,typeid,value) VALUES(%s,%s,%s)"
< more to set up serialport, logging and so on>
while 1:
< fetch a data set and split it to a list >
for (i,val) in enumerate measures:
try:
cur.execute(sql,(sensors[i],typeid[i],val))
conn.commit()
except:
self.logger.error(appname+": error 106 :"+str(sys.exc_info()))
I have a feeling this may be some of the same problem that I initially had with the serial connection, Serial port does not work in rewritten Python code, so I have tried to fiddle with files_preserve, doing:
self.files_preserve=range(daemon.daemon.get_maximum_file_descriptors()+1)
which as far as I can understand should keep open all file handles, but to no avail.
In the daemon, I have tried first to set up the data base connection as attributes in __init__, i. e.:
self.conn = psycopg2.connect(connstring)
self.cur=conn.cursor()
and then do the inserts in the run method. I tried also to create connection at the top of the run method and even setting it up as a global object, but in all cases, something seems to be killing the database connection. Any clues? (or any clues to where to find some documentation (other than the source) for the daemon module?)
Both the daemon and the database are running on debian linux systems with python 2.7and postgresql8.4`.
As far as i can tell from it's source, daemon.runner works by forking and then executing the run method of the daemon app you supplied.
That means that you're creating the database connection in one process, but then try to use it in a forked process, which psycopg2 doesn't like:
libpq connections shouldn’t be used by a forked processes, so [...] make sure to create the connections after the fork.
In this case that means: move your call to psycopg2.connect into the run method.