Skip to content

Instantly share code, notes, and snippets.

@happysundar
Created June 14, 2014 20:06
Show Gist options
  • Save happysundar/056f3cf848de054b9cda to your computer and use it in GitHub Desktop.
Save happysundar/056f3cf848de054b9cda to your computer and use it in GitHub Desktop.
Creating a plpgsql function
DROP FUNCTION IF EXISTS get_image_records( BIGINT ) CASCADE;
CREATE OR REPLACE FUNCTION
get_image_records(input_program_id BIGINT)
RETURNS SETOF JSON STABLE
AS $$
BEGIN
RETURN QUERY
WITH T2 AS (
SELECT
file_url :: TEXT,
file_height,
file_width,
file_size,
number_of_people,
zoom_level,
caption :: TEXT,
primary_image,
image_type_name :: TEXT
FROM export.programimages_imagefiles
WHERE programimages_imagefiles.program_id = input_program_id)
SELECT
json_agg(to_json(T2.*))
FROM T2;
END
$$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment