Integrations · Data and spreadsheets

Validate email addresses in Amazon Redshift

Two SQL functions for Amazon Redshift: f_validate_email(email) returns the status of an address (valid, invalid, catch_all, unknown or disposable), and f_validate_email_details(email) returns the reason, the suggested action and, for a row that could not be checked, an error code instead of a status, as JSON text.

SELECT email, f_validate_email(email) AS status FROM customers;
SELECT email, JSON_EXTRACT_PATH_TEXT(f_validate_email_details(email), 'reason') AS reason FROM customers;

Both are Redshift Lambda UDFs: Redshift sends batches of rows to an AWS Lambda function in your AWS account, which checks each address with our API. Download email-validation-redshift.zip: it unpacks into a folder email-validation-redshift holding the Lambda function (handler/, Node.js, no dependencies), the SQL scripts (sql/) and LICENSE (MIT). It has not run in a real Redshift or AWS account yet: it was built from the AWS documentation and tested against stand-ins.

What it costs

Every non-NULL row a query passes to these functions is checked each time the query runs, and a definite result costs one credit; the same address twice in one batch is checked once. unknown results, malformed addresses and test addresses on sandbox.email-validation.org are free. A check of an address checked before also costs a credit (repeat checks).

  • A query over a million rows can spend a million credits, and running it again spends them again. A view that calls the functions checks again whenever it is read.
  • Redshift does not promise to call a Lambda UDF exactly once per row: the same rows may reach the function again.

So check each address once into a table and join to it (below), and give the key a daily credit cap. The functions script takes EXECUTE back from PUBLIC: grant it only to roles that should spend credits. AWS bills the Lambda time; Redshift adds no charge for Lambda UDFs.

Set up

  1. Key. Create an API key on the API keys page with the permission Validate addresses and a daily credit cap, and store it in AWS Secrets Manager as a secret (the plain key, or JSON {"api_key": "..."}).

  2. Lambda function. Zip the two handler files at the zip's top level, not in a folder:

    cd email-validation-redshift/handler && zip ../ev-redshift-lambda.zip index.mjs core.mjs

    In the Lambda console, create a function named ev-validate-email on Node.js 22.x or later in the same AWS Region as your Redshift cluster or workgroup, upload that zip and set the handler to index.handler and the timeout to 90 seconds. Add the "AWS Parameters and Secrets Lambda Extension" layer, allow the function's role secretsmanager:GetSecretValue on the secret, and set EV_API_KEY_SECRET_ID to the secret's name. Set reserved concurrency to cap how many batches run at once.

  3. IAM role for Redshift. Create a role that Redshift can assume, with lambda:InvokeFunction on that function, and associate it with your cluster, or with your Redshift Serverless namespace.

  4. Functions. In Query Editor v2, open sql/1-functions.sql, put the role's ARN in place of <IAM role ARN> and run it as a superuser (or a user with the CREATE EXTERNAL FUNCTION privilege and USAGE ON LANGUAGE EXFUNC). Its last statement is a free test on test addresses. The script holds no key. Its RETRY_TIMEOUT lets Redshift wait up to 5 minutes when Lambda's reserved concurrency is reached, instead of failing the query after 20 seconds: a call that waits has not run, so it costs nothing.

The two functions call the same Lambda function, which tells them apart by name: a name ending in _details gets the details. Keep that ending if you rename them.

Optional Lambda settings: EV_REGION (eu for our EU host), EV_CONCURRENCY (checks in flight per batch, default 10), EV_CHECK_TIMEOUT_S (default 10) and EV_BATCH_TIME_LIMIT_MS (default 60000: the function answers by then, and always a second before its Lambda timeout).

Our API allows 20 validation requests a second and 100 in flight until your account has made a purchase, and 500 a second after one. Redshift calls the function with several batches at once, so keep the Lambda's reserved concurrency times EV_CONCURRENCY under yours.

Check each address once

sql/2-check-once.sql creates the table ev_email_results and, on each run, checks only the distinct addresses that have no result there yet, at most 10,000 per run (the LIMIT caps what one run spends), and keeps the status, reason, action and error as columns. Replace customers and email with your table and column, run the whole script in one session (it uses a temporary table), and run it again when new addresses arrive. Read the results with a join that costs nothing:

SELECT c.*, r.status AS ev_status, r.reason AS ev_reason, r.action AS ev_action
FROM customers c
LEFT JOIN ev_email_results r ON r.email = c.email;

Start with a small LIMIT and raise it once a run succeeds: a run that fails keeps nothing of what it checked.

Try it free

SELECT f_validate_email('no_mailbox@sandbox.email-validation.org');   -- invalid
SELECT f_validate_email('disposable@sandbox.email-validation.org');   -- disposable

What the functions return

f_validate_email(email) returns one of the five statuses, or NULL for a NULL or empty address and for a row that could not be checked. f_validate_email_details(email) returns a JSON object as text, with the same keys on every row ({status, reason, action, did_you_mean, domain, free_email, role_account, disposable, catch_all_domain, mx_found, processed_at, credits_used, request_id, error, error_detail}): read a key with JSON_EXTRACT_PATH_TEXT, or the whole object with JSON_PARSE. Statuses and reasons explains each value.

When a row is not checked

A row that could not be checked never gets an invented status: its status is NULL and error says why.

errorMeaningCharged
rate_limited, abuse_throttledThe account's request rate limitNo
service_unavailable, upstream_capacityA short outage on our sideNo
insufficient_credits, key_daily_capNo credits left, or the key's daily credit cap reachedNo
invalid_request, internal_errorThe API refused or failed this one addressNo
"no_answer"Sent, but no answer arrived in timeMaybe
unexpected_responseAn answer that is neither a result nor our errorMaybe
"not_sent"Not sent: the batch stopped after one of the codes above, or ran out of timeNo

After a rate limit, an outage, the daily cap or no credits, no further address of that batch is sent. The check-once script checks rows without a status again on its next run, except "no_answer" and unexpected_response rows, which may have been charged (one commented line in the script includes them). If the key is wrong or lacks Validate addresses, the whole query fails with a message that names the error and request id, never the key.

Privacy

The function sends each address only in the body of a POST to https://api.email-validation.org (or https://api-eu.email-validation.org), with the key in the Authorization header, never to another host, and follows no redirect. It never logs an address or the key: it writes one line per batch to CloudWatch with counts, error codes and request ids. What keeps addresses is under your control: Redshift's query history (any address written into the SQL) and ev_email_results.

Licence

The download is under the MIT licence (LICENSE in the zip): you may use, change and share it.

More on Amazon Redshift

Updated · Maintained by: Email Validation LLC

Need help? Contact support, or ask the help chat. Something wrong or missing on this page? Tell us