I have the following function:
CREATE OR REPLACE FUNCTION "Sensor"."PersistTelemetry"(sid character varying, measurement character varying, val numeric, ts character varying)
RETURNS boolean
LANGUAGE 'plpgsql'
VOLATILE
COST 100
AS $BODY$DECLARE SUCCESS BOOLEAN;
BEGIN
BEGIN
SUCCESS = false;
INSERT INTO
"Sensor"."SensorReadings" (
sensorid,
reservoirid,
timestamp,
measurement,
value
)
VALUES
(
sid,
(
SELECT
reservoirid
FROM
"Sensor"."SystemSensors"
WHERE
sensorid = sid
),
to_timestamp(ts, 'YYYY/MM/DD hh24:mi:ss'),
measurement,
val
);
SUCCESS = true;
EXCEPTION WHEN OTHERS THEN
SUCCESS = false;
RAISE NOTICE 'ErError % %', SQLERRM, SQLSTATE;
END;
RETURN SUCCESS;
END; $BODY$;
I am calling it with flask-sqlalchemy with the following execution and payload:
@app.route('/api/telemetry', methods=['POST'])
def persist_telemetry():
if not request.json:
abort(400)
sensorID = request.json['sensorID']
measurement = request.json['measurement']
value = request.json['value']
timestamp = request.json['timestamp']
params = {
'sensorid' : sensorID,
'measurement' : measurement,
'val' : value,
'ts' : timestamp
}
print(params)
result = db.session.execute("""select "Sensor"."PersistTelemetry"(:sensorid, :measurement, :val, :ts)""", params)
for r in result:
print(r)
return "success", 201
{'val': 8.8, 'sensorid': 'phSensorA.haoshiAnalogPh', 'ts': '2019-12-06 18:32:36', 'measurement': 'ph'}
I have enabled logging on my server, and set log_min_messages=notice
But when viewing the logs, All I see is this:
2019-12-07 02:17:00 CST [14757-15] moedepi@SnooSongFarms LOG: statement: BEGIN
2019-12-07 02:17:00 CST [14757-16] moedepi@SnooSongFarms LOG: statement: select "Sensor"."PersistTelemetry"('phSensorA.haoshiAnalogPh', 'ph', 8.8, '2019-12-06 18:32:36')
2019-12-07 02:17:00 CST [14757-17] moedepi@SnooSongFarms LOG: statement: ROLLBACK
The function is returning true, and I don't see the string 'ErError' in the log, so this tells me that an exception is not being raised.
What could be causing this insert to rollback? How do I go about debugging this further?
Any help is much appreciated.
db.session.commit?log_line_prefixto include a timestamp that's more granular, like with milliseconds--to see if there's some gap/wait in the app, before sending the rollback). The transaction sequence numbers (14757-15/16/17) indicate there are no other queries called in the session, so it smells like some bad behavior on the application-side