AWS RDS Aurora Table Import from S3 Stub

February 5, 2024

Here is the aws_s3.table_import_from_s3 stub that pairs nicely with the previously blogged aws_s3.query_export_to_s3 stub.

CREATE SCHEMA IF NOT EXISTS aws_s3;
CREATE OR REPLACE
  FUNCTION aws_s3.table_import_from_s3 (
    table_name TEXT, column_list TEXT, opt TEXT,
    bucket TEXT, path TEXT, region TEXT, OUT rows INTEGER)
AS
$$
DECLARE copy_stmt TEXT;
BEGIN
  copy_stmt := 'COPY ' || table_name;
  IF column_list IS NOT NULL AND length(column_list) > 0 THEN
    copy_stmt := copy_stmt || ' (' || column_list || ')';
  END IF;
  copy_stmt := copy_stmt || ' FROM ''' || path || ''' WITH (' || opt || ');';
  EXECUTE copy_stmt;
  GET DIAGNOSTICS rows = ROW_COUNT;
END;
$$
LANGUAGE plpgsql;