use postgres function to get clustername

0

I know this is probably more of a dev type question, but I'm asking anyways. Has anyone figured out a solid way of being able to query the clustername of an aurora postgres cluster inside a function?

I can pass the :'HOST' psql variable to a function and get the results needed, but I'd like to get this embedded into the function itself.

This is how I pass it into the function SELECT func_name(:'HOST','value2');

I've tried something like the following in the function and it doesn't work. It does not like that ":" at the beginning of the variable.

select :'HOST' into v_host;

I've see some custom extensions that do this work, but Aurora PG won't allow for these custom extensions.

Thoughts? Any advice?

thanks in advance!

질문됨 일 년 전593회 조회
1개 답변
0
수락된 답변
  1. In order to fetch the respective instance name (reader or writer) to which you are connected to, you can use below :- (please note you have to connect using cluster endpoint in order to get cluster name)

[userr@machine]$ psql -h database-3.cluster-xxxxx.region.rds.amazonaws.com -U postgres -d postgres

postgres=> SELECT :'HOST';

database-3.cluster-xxxxx.region.rds.amazonaws.com

-- The above output can be further trimmed to just have cluster name

postgres=> SELECT SPLIT_PART(:'HOST','.',1);

database-3

-- To use above as variable in function , following workaround can be used as ":" is considered as variable in the psql.

postgres=>create table host_name_tab (ht varchar); postgres=> insert into host_name_tab select :'HOST';

postgres=> create or replace function get_host() returns varchar language plpgsql as $$ declare host_name varchar; begin SELECT SPLIT_PART(ht,'.',1) into host_name from host_name_tab ; return host_name; end; $$;

postgres=> select get_host(); get_host

database-3 (1 row)

  1. If you which to get the instance name you are connected to you can use below :

postgres=> select aurora_db_instance_identifier() as host_name; host_name

database-3-instance-1-ap-south-1a (1 row)

Please note as this is development related query it can be achieved by different logic.

AWS
답변함 일 년 전
  • I thought of a similar idea (manually inserting into a table like you did) in a shower epiphany. I think I will use this solution for it. It's an extra step, for sure, but it does solve the problem.

    Thank you!

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠