Postgresql 从sql查询中获取列别名

Postgresql 从sql查询中获取列别名,postgresql,Postgresql,根据这样的查询: select aaa.rank as my_rank, aa.*, aa.article_category from articles.article_article_authors as aaa left join article_article as aa on aa.id = aaa.article_id ; 我想获得列别名-在这种情况下,列的别名为aaa.rank。不幸的是,使用EXPLA

根据这样的查询:

select
        aaa.rank as my_rank,
        aa.*,
        aa.article_category
from
        articles.article_article_authors as aaa
        left join article_article as aa on aa.id = aaa.article_id
;
我想获得列别名-在这种情况下,列的别名为
aaa.rank
。不幸的是,使用
EXPLAIN
和任何选项都无法提供我需要的信息:

[
  {
    "Plan": {
      "Node Type": "Nested Loop",
      "Join Type": "Left",
      "Startup Cost": 0.00,
      "Total Cost": 2.66,
      "Plan Rows": 1,
      "Plan Width": 70,
      "Output": ["aaa.article_id", "aaa.user_id", "aaa.rank", "aa.id", "aa.creation_time", "aa.pub_start", "aa.pub_end", "aa.is_visible", "aa.rank", "aa.answer_to_article_id", "aa.title", "aa.hits", "aa.meta_keywords", "aa.content", "aa.archived", "aa.article_category", "aa.article_category"],
      "Join Filter": "(aa.id = aaa.article_id)",
      "Plans": [
        {
          "Node Type": "Seq Scan",
          "Parent Relationship": "Outer",
          "Relation Name": "article_article_authors",
          "Schema": "public",
          "Alias": "aaa",
          "Startup Cost": 0.00,
          "Total Cost": 1.01,
          "Plan Rows": 1,
          "Plan Width": 12,
          "Output": ["aaa.id", "aaa.article_id", "aaa.user_id", "aaa.rank"]
        },
        {
          "Node Type": "Seq Scan",
          "Parent Relationship": "Inner",
          "Relation Name": "article_article",
          "Schema": "public",
          "Alias": "aa",
          "Startup Cost": 0.00,
          "Total Cost": 1.29,
          "Plan Rows": 29,
          "Plan Width": 58,
          "Output": ["aa.id", "aa.creation_time", "aa.pub_start", "aa.pub_end", "aa.is_visible", "aa.rank", "aa.answer_to_article_id", "aa.title", "aa.hits", "aa.meta_keywords", "aa.content", "aa.archived", "aa.article_category"]
        }
      ]
    }
  }
]

是否有一种方法可以使用
explain
获取列别名,或者我应该遵循其他路径?当然,我可以解析查询字符串,然后获取我的列别名,但我更希望postgres为我这样做。

我使用正则表达式匹配SQL字符串的所有列/别名。我想我涵盖了大部分场景,但可能遗漏了一些

(?:              # Start non-capturing group
  select         # Match "select" literally
 |               # OR
  \G             # Match the end of the previous match (our last column)
)                # End non-capturing group
\s+              # Catch extra whitespace
\K               # Remove everything to the left from the match
(?:              # Start non-capturing group
  (?:            # Start non-capturing group
    .*?          # Lazily match column (this can be modified to your needs)
    \s+as\s+     # Match " as " literally
    ([^,]+)      # Capture the alias (anything but a comma)
    ,?           # Optional comma
  )              # End non-capturing group
 |               # OR
  ([^,]+)        # Capture the column (anything but a comma)
  ,?             # Optional comma
)                # End non-capturing group
(?=              # Start lookahead
  .*?            # Lazily match characters (whitespace)
  \bfrom\b       # Up to "from" literally (with word delimiters)
)                # End lookahead
这将捕获第一组中的别名和第二组中的列,整个字符串将被匹配。如果您开始使用疯狂的列别名,但似乎将任何基本内容与中间内容匹配,则可能会出现问题。注意:我使用了修饰语
gsi
来表示全局、点匹配换行符和case instive


您为什么需要这样做?您试图对列名进行哪些后处理?也许你有一个更简单的用例,不需要这种级别的分析…@Sam将其作为一个答案发布,这样我就可以接受。很好,我会的--给我一些时间解释一下..只是想确保这对你的用例有帮助。