Skip to content

Use postgresql_function #544

@haydenai-rahul-jain

Description

@haydenai-rahul-jain

Hi everyone, I am trying to add this function onto my database:

CREATE OR REPLACE FUNCTION datadog.explain_statement(
   l_query TEXT,
   OUT explain JSON
)
RETURNS SETOF JSON AS
$$
DECLARE
curs REFCURSOR;
plan JSON;

BEGIN
   OPEN curs FOR EXECUTE pg_catalog.concat('EXPLAIN (FORMAT JSON) ', l_query);
   FETCH curs INTO plan;
   CLOSE curs;
   RETURN QUERY SELECT plan;
END;
$$
LANGUAGE 'plpgsql'
RETURNS NULL ON NULL INPUT
SECURITY DEFINER;

I am using the postgresql_function terraform resource.
This is what I am doing:

resource "postgresql_function" "datadog_explain_statement" {
  name     = "explain_statement"
  schema   = postgresql_schema.datadog.name
  database = var.database_name
  
  security_definer = true
  strict          = true

  arg {
    name = "l_query"
    type = "text"
  }

  arg {
    name = "explain"
    type = "json"
    mode = "OUT"
  }

  body = <<-EOT
AS $$
DECLARE
  curs REFCURSOR;
  plan JSON;
BEGIN
  OPEN curs FOR EXECUTE pg_catalog.concat('EXPLAIN (FORMAT JSON) ', l_query);
  FETCH curs INTO plan;
  CLOSE curs;
  RETURN QUERY SELECT plan;
END;
$$
  EOT
}

However, there seems to be something weird that the first one is doing that the other one isn't doing or something like that. When I execute the first SQL command on the server (manually), my downstream works fine. However, when I make the function using the terraform, it doesn't work. I'm trying to see what's wrong with how the terraform module is creating the function vs the manual.

MY GOAL: replicate as if the function were just made manually on the server.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions