1

I'm trying to invoke a plpgsql function in an AWS RDS Postgres instance. Currently, I have an AFTER INSERT trigger to call the function I wanted to be executed. However this function takes a time to be executed, and the client keeps the connection hanging while my trigger runs.

What I'd like to do is, find a way to execute the function that is currently invoked by the trigger, in a non-blocking way. I've considered the LISTEN/NOTIFY, however, I don't want an instance running only for this. Besides, keeping it running in a Lambda function would generate extra costs that I don't need only for a background process.

In my actual scenario, I have a Lambda function that executes the "INSERT INTO" query. I don't need to return anything from this Lambda, which means I could finish the Lambda and keep the "AFTER INSERT" trigger in the background.

Here is the Python used to insert from Lambda:

def lambda_handler(event, context):
  
  for record in event['Records']:
    payload = record["body"]
    print(payload)
    payload = json.loads(payload)
    
    credential = getCredentials()
    connection = psycopg2.connect(user=credential['username'], password=credential['password'], host=credential['host'], port=credential['port'], database=credential['db'])
    connection.set_session(autocommit=True)
    cursor = connection.cursor()
    query = "INSERT INTO log.new_data(date, description) values('" + payload['date'] + "', '" + payload['description'] + "');"
    cursor.execute(query)
    cursor.close()

mkdev
  • 11
  • 4

0 Answers0