We are using pg Pool in our Lambda function.
Our connectDb
function looks like this:
const config = {
database,
user: username,
password,
max: max_pool,
idleTimeoutMillis,
connectionTimeoutMillis,
};
if (!pool) {
pool = new Pool(config);
}
client = await pool.connect();
return client;
And this is how the Lambda handler looks like:
import { Client } from "pg";
import { sqs } from "./sqsClient"; // just for example
let dbClient: Client;
exports.handler = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false;
try {
const markQueueStatusAndDeleteMessage = async (
message: any,
QUEUE_URL: any,
dbClient: any,
queue_status_update_query: any,
query_dyanamic_values: any = []
) => {
// Delete processed message from the queue
const deleteParams = {
QueueUrl: QUEUE_URL,
ReceiptHandle: message.receiptHandle,
};
console.log("deleting message from queue...");
await sqs.deleteMessage(deleteParams).promise();
console.log("deleted message from queue...");
console.log(queue_status_update_query);
if (query_dyanamic_values.length > 0) {
await dbClient.query(queue_status_update_query, query_dyanamic_values);
} else {
await dbClient.query(queue_status_update_query);
}
console.log(
"CREATE-ORDER-PROCESSOR-ERROR: ",
queue_status_update_query
);
};
console.log(
"Records Length: ",
event.Records.length,
"Processing messages..."
);
for await (const message of event.Records) {
try {
context.callbackWaitsForEmptyEventLoop = false;
if (!dbClient) {
dbClient = await connectPostgresDb();
}
// then all the processing...
// we aren’t using any release() or end().
// we are using RDS Proxy with 10min timeout.
} catch (err) {
console.error("Error processing message:", err);
}
}
} catch (err) {
console.error("Handler error:", err);
}
};
The Issue
We’re getting the following error:
Error: Connection terminated unexpectedly
at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:132:73)
at Object.onceWrapper (node:events:631:28)
at Connection.emit (node:events:517:28)
at Connection.emit (node:domain:489:12)
at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:63:12)
at Socket.emit (node:events:517:28)
at Socket.emit (node:domain:489:12)
at TCP.<anonymous> (node:net:350:12)
Emitted 'error' event on Client instance at:
at Client._handleErrorEvent (/app/node_modules/pg/lib/client.js:341:10)
at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:150:16)
at Object.onceWrapper (node:events:631:28)
[... lines matching original stack trace ...]
at TCP.<anonymous> (node:net:350:12)
Node.js v18.20.8
Question
- Is this error happening because the connection is not being returned to the pool (since we don’t call
release()
)? - Or is it due to the RDS Proxy timing out / killing the connection?
This is an SQS queue Lambda that processes batches of messages. Ok so i checked an the issue wasnt only in the lambda function but also our nestjs server.. but we are properly releasing the connection there
nestjs config
this.readPool = new Pool({
user: this.configService.get('database.username'),
host: this.configService.get('database.hostRead'), // Reader instance
database: this.configService.get('database.name'),
password: this.configService.get('database.password'),
port: this.configService.get('database.port') || 5432,
max: this.configService.get('database.max_pool') || 100,
idleTimeoutMillis: this.configService.get('database.idleTimeoutMillis') || 30000,
connectionTimeoutMillis: this.configService.get('database.connectionTimeoutMillis') || 2000
});