Feature request: Comments/Tags at queries

201 views
Skip to first unread message

Rafael Friesen

unread,
Nov 30, 2016, 8:29:54 AM11/30/16
to elixir-ecto
I would like to tag my queries with SQL-comments to later find the code that produced my queries in the DB logs.

I want to use something like:

    from(t in __MODULE__, where: [...], comment: "fileABC.ex:line123")

that results in a query like:

SELECT .... -- fileABC.ex:line123

the comment is then logged but further ignored in the DB.

If you have suggestions to do this in a better way I would like to hear them.

je...@teacherspayteachers.com

unread,
Aug 20, 2018, 11:37:51 AM8/20/18
to elixir-ecto
I'd also like this feature. It's maybe somewhat tricky as i'm not sure how to get the file/line info at runtime. I tried to add a macro to add a tag in this style to queries and ran into a lot of metaprogramming trouble. Has anyone done this successfully?

Blake Kostner

unread,
Apr 24, 2023, 1:40:08 PM4/24/23
to elixir-ecto
Adding to this, there is an sqlcommenter spec that enables some advanced APM features for various services. Here is how Datadog does it.

yevheni...@gmail.com

unread,
Jul 12, 2023, 9:47:36 AM7/12/23
to elixir-ecto
It will be helpful to have an sqlcommenter plugin for Ecto https://google.github.io/sqlcommenter/

Daniel Kukuła

unread,
Jul 14, 2023, 4:01:01 AM7/14/23
to elixir-ecto
I started this:
https://github.com/dkuku/sqlcommenter/
but this would require changes to postgrex to append the comment to the query (I think this is the easiest place to append the query I think)

Alex Sasnouski

unread,
Feb 7, 2026, 6:07:53 AMFeb 7
to elixir-ecto
Hi everyone,

Would such functionality be of interest in Ecto?

For me, it would be convenient to see my own comment (e.g., query name and project) in the Postgres Slow Query Log, for example, or in other places.

If this is something that's wanted, I can implement it. It would require changes in both ecto and ecto_sql.


iex(1)> from(q in Post, limit: 1, comment: "GetPost") |> Repo.one()

12:02:26.743 [debug] QUERY OK source="posts" db=4.2ms decode=0.5ms queue=1.1ms idle=1431.3ms
 /* GetPost */SELECT p0."id", p0."data", FROM "posts" AS p0 LIMIT 1 []


Should I create a ticket & PR? What your thoughts ?
пятница, 14 июля 2023 г. в 10:01:01 UTC+2, daniel...@fresha.com:

José Valim

unread,
Feb 7, 2026, 6:09:56 AMFeb 7
to elixi...@googlegroups.com
I believe it was implemented in some shape. Please double check, I can't recall with 100% confidence. :)


--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/elixir-ecto/5713be8e-9d5a-436a-86ae-f94da9664319n%40googlegroups.com.

Alexander Steppke

unread,
Feb 7, 2026, 11:25:47 AMFeb 7
to elixi...@googlegroups.com
Definitely possible, we’re doing this since quite some time. I can share some code on Monday if nobody else gets to it before. 

On 7. Feb 2026, at 12:09, José Valim <jose....@gmail.com> wrote:


You received this message because you are subscribed to a topic in the Google Groups "elixir-ecto" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-ecto/QjOJp12WgK0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-ecto...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/elixir-ecto/CAGnRm4%2Bd49jXgXCeYNV5rSrm-8kAEqMOam5g38gxxBvPvNGAmA%40mail.gmail.com.

Alex Sasnouski

unread,
Feb 7, 2026, 11:38:03 AMFeb 7
to elixi...@googlegroups.com
I'm not sure if Ecto has something like this, but I'm not that familiar with the codebase yet, there might be some workarounds. 
There's also https://github.com/querysorcery/sqlcommenter, which allows adding a comment to the end of the SQL query. 
However, in most cases, the logged query length is limited by either DB settings or the GCP/AWS logger and gets truncated. 
So, in my opinion, it makes more sense to add it to the beginning of the query.

сб, 7 февр. 2026 г. в 17:25, Alexander Steppke <alexande...@gmail.com>:

Alexander Steppke

unread,
Feb 13, 2026, 3:30:58 AMFeb 13
to elixir-ecto
sorry, i was knocked out the last few days, here you go:

basically in our Repo override

```
def prepare_query(query, opts) do
    meta =
      [
        anything: "youd like as tags"
      ]

    {query, query_opts ++ [prepare: :unnamed, comment: to_str(meta)]}
end

  defp to_str(params) do
    params
    |> to_iodata()
    |> IO.iodata_to_binary()
  end

  defp to_iodata(nil), do: []

  defp to_iodata(params) do
    params
    |> Enum.sort(&(&1 <= &2))
    |> sorted_to_iodata()
  end

  defp sorted_to_iodata(params) do
    for_result =
      for {key, value} <- params, value != nil do
        [stringify(key), ":", stringify(value)]
      end

    Enum.intersperse(for_result, ",")
  end

  defp stringify(value) when is_binary(value), do: value

  defp stringify(value) do
    try do
      to_string(value)
    rescue
      _e ->
        inspect(value)
    end
  end
```


Downside to this is that it disables query caching on ecto level. for us this had no visible performance impact, but that might be different for you

Alex Sasnouski

unread,
Feb 18, 2026, 4:23:09 AMFeb 18
to elixi...@googlegroups.com
Thanks for sharing, Alexander.

It seems to me that everyone is forced to come up with their own approach.
I think it would be convenient to have similar functionality directly in Ecto.
What do you guys think?


Regards 

Reply all
Reply to author
Forward
0 new messages