Loga PostgreSQL á uaslódáil ón scamall AWS

Nó tetriseolaíocht beagán feidhmeach.
Tá gach rud nua dearmad go maith d'aois.
Epigraphs.
Loga PostgreSQL á uaslódáil ón scamall AWS

An fhadhb a fhoirmiú

Is gá an comhad loga PostgreSQL reatha a íoslódáil go tréimhsiúil ón scamall AWS chuig an suíomh áitiúil. Linux óstach. Ní i bhfíor-am, ach, abair, le moill bheag.
Is é 5 nóiméad an tréimhse íoslódála nuashonraithe comhaid loga.
Rothlaíonn an comhad loga in AWS gach uair an chloig.

Uirlisí úsáidte

Chun an comhad logála a íoslódáil chuig an ósta, úsáidtear script bash a ghlaonn an API AWS "aws rds íoslódáil-db-log-comhad-cuid'.

roghanna:

  • —db-instance-identifier: ainm shamplach AWS;
  • --log-file-name: ainm an logchomhaid a ghintear faoi láthair
  • --max-item: Líon iomlán na míreanna a cuireadh ar ais san aschur ordaithe.Méid an chomhaid íoslódáilte.
  • --starting-token: comhartha tosaigh

Sa chás áirithe seo, d'eascair an tasc maidir le logaí a luchtú le linn na hoibre ar monatóireacht a dhéanamh ar fheidhmíocht cheist PostgreSQL.

Agus tá sé simplí - tasc suimiúil le haghaidh oiliúna agus éagsúlacht le linn uaireanta oibre.
Glacfaidh mé leis go bhfuil an fhadhb réitithe cheana féin mar gheall ar an saol laethúil. Ach níor mhol Google tapa aon réitigh, agus ní raibh mórán fonn orm cuardach níos doimhne a dhéanamh. Slí amháin nó slí, is cleachtadh maith é.

An tasc a fhoirmiú

Is éard atá sa logchomhad deiridh ná go leor línte d'fhad athraitheach. Go grafach, is féidir rud éigin mar seo a léiriú don logchomhad:
Loga PostgreSQL á uaslódáil ón scamall AWS

An gcuireann sé rud éigin i gcuimhne duit cheana féin? Cad a chaithfidh Tetris a dhéanamh leis? Agus seo é a bhfuil baint aige leis.
Má shamhlaímid na roghanna a d'fhéadfadh a bheith ann nuair a bhíonn an chéad chomhad eile á luchtú go grafach (ar mhaithe le simplíocht, sa chás seo, lig na línte an fad céanna), faighimid píosaí caighdeánach Tetris:

1) Tá an comhad a íoslódáil ina iomláine agus tá sé deiridh. Tá méid na coda níos mó ná an méid comhaid deiridh:
Loga PostgreSQL á uaslódáil ón scamall AWS

2) Leanann an file. Tá méid an chupa níos lú ná an méid comhaid deiridh:
Loga PostgreSQL á uaslódáil ón scamall AWS

3) Leanann an comhad leis an gcomhad roimhe seo agus tá leanúint leis. Tá an smután níos lú ná an méid atá fágtha den chomhad deiridh:
Loga PostgreSQL á uaslódáil ón scamall AWS

4) Leanann an comhad leis an gcomhad roimhe seo agus is é an ceann deiridh é. Tá an smután níos mó ná méid an chuid eile den chomhad deiridh:
Loga PostgreSQL á uaslódáil ón scamall AWS

Is é an tasc a chur le chéile dronuilleog nó Tetris a imirt ar leibhéal nua.
Loga PostgreSQL á uaslódáil ón scamall AWS

Fadhbanna a thagann chun cinn agus fadhb á réiteach

1) Glue teaghrán 2 phíosa

Loga PostgreSQL á uaslódáil ón scamall AWS
Go ginearálta, níor tháinig aon fhadhbanna speisialta chun cinn. Fadhb chaighdeánach ó chúrsa tosaigh ríomhchláraithe.

An méid freastal is fearr is féidir

Ach tá sé seo beagán níos suimiúla.
Ar an drochuair, níl aon bhealach le fritháireamh a úsáid i ndiaidh lipéad na coda tosaigh:

Mar atá a fhios agat cheana féin, úsáidtear an rogha —token-token chun a shonrú cén áit ar féidir tús a chur le huimhriú. Glacann an rogha seo luachanna Teaghrán a chiallódh má dhéanann tú iarracht luach fhritháireamh a chur os comhair teaghrán Next Token, ní chuirfear an rogha san áireamh mar fhritháireamh.

Agus mar sin, caithfidh tú é a léamh ina smután.
Má léann tú i gcodanna móra, beidh líon na léamha íosta, ach beidh an toirt uasta.
Má léann tú i gcodanna beaga, ansin ar a mhalairt, beidh líon na léamha uasta, ach beidh an toirt íosta.
Dá bhrí sin, chun trácht a laghdú agus chun áilleacht iomlán an réitigh, bhí orm teacht ar réiteach, rud a d'fhéach, ar an drochuair, beagán cosúil le crutch.

Mar léiriú, déanaimis machnamh ar an bpróiseas chun logáil isteach a íoslódáil i 2 leagan atá go mór simplithe. Braitheann líon na léamha sa dá chás ar mhéid na coda.

1) Luchtaigh i gcodanna beaga:
Loga PostgreSQL á uaslódáil ón scamall AWS

2) Luchtaigh i gcodanna móra:
Loga PostgreSQL á uaslódáil ón scamall AWS

Mar is gnách, tá an réiteach is fearr sa lár.
Is beag an méid freastail, ach le linn an phróisis léitheoireachta, is féidir an méid a mhéadú chun líon na léamha a laghdú.

Ba chóir a thabhairt faoi deara nach bhfuil an fhadhb maidir le méid optamach na coda inléite a roghnú réitithe go fóill agus go dteastaíonn staidéar agus anailís níos doimhne. B'fhéidir beagán níos déanaí.

Cur síos ginearálta ar an gcur i bhfeidhm

Táblaí seirbhíse a úsáidtear

CREATE TABLE endpoint
(
id SERIAL ,
host text 
);

TABLE database
(
id SERIAL , 
…
last_aws_log_time text ,
last_aws_nexttoken text ,
aws_max_item_size integer 
);
last_aws_log_time — временная метка последнего загруженного лог-файла в формате YYYY-MM-DD-HH24.
last_aws_nexttoken — текстовая метка последней загруженной порции.
aws_max_item_size- эмпирическим путем, подобранный начальный размер порции.

Téacs iomlán scripte

íoslódáil_aws_piece.sh

#!/bin/bash
#########################################################
# download_aws_piece.sh
# downloan piece of log from AWS
# version HABR
 let min_item_size=1024
 let max_item_size=1048576
 let growth_factor=3
 let growth_counter=1
 let growth_counter_max=3

 echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh:''STARTED'
 
 AWS_LOG_TIME=$1
 echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh:AWS_LOG_TIME='$AWS_LOG_TIME
  
 database_id=$2
 echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh:database_id='$database_id
 RESULT_FILE=$3 
  
 endpoint=`psql -h MONITOR_ENDPOINT.rds.amazonaws.com -U USER -d MONITOR_DATABASE_DATABASE -A -t -c "select e.host from endpoint e join database d on e.id = d.endpoint_id where d.id = $database_id "`
 echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh:endpoint='$endpoint
  
 db_instance=`echo $endpoint | awk -F"." '{print toupper($1)}'`
 
 echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh:db_instance='$db_instance

 LOG_FILE=$RESULT_FILE'.tmp_log'
 TMP_FILE=$LOG_FILE'.tmp'
 TMP_MIDDLE=$LOG_FILE'.tmp_mid'  
 TMP_MIDDLE2=$LOG_FILE'.tmp_mid2'  
  
 current_aws_log_time=`psql -h MONITOR_ENDPOINT.rds.amazonaws.com -U USER -d MONITOR_DATABASE -A -t -c "select last_aws_log_time from database where id = $database_id "`

 echo $(date +%Y%m%d%H%M)':      download_aws_piece.sh:current_aws_log_time='$current_aws_log_time
  
  if [[ $current_aws_log_time != $AWS_LOG_TIME  ]];
  then
    is_new_log='1'
	if ! psql -h MONITOR_ENDPOINT.rds.amazonaws.com -U USER -d MONITOR_DATABASE -v ON_ERROR_STOP=1 -A -t -q -c "update database set last_aws_log_time = '$AWS_LOG_TIME' where id = $database_id "
	then
	  echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh: FATAL_ERROR - update database set last_aws_log_time .'
	  exit 1
	fi
  else
    is_new_log='0'
  fi
  
  echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh:is_new_log='$is_new_log
  
  let last_aws_max_item_size=`psql -h MONITOR_ENDPOINT.rds.amazonaws.com -U USER -d MONITOR_DATABASE -A -t -c "select aws_max_item_size from database where id = $database_id "`
  echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh: last_aws_max_item_size='$last_aws_max_item_size
  
  let count=1
  if [[ $is_new_log == '1' ]];
  then    
	echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh: START DOWNLOADING OF NEW AWS LOG'
	if ! aws rds download-db-log-file-portion 
		--max-items $last_aws_max_item_size 
		--region REGION 
		--db-instance-identifier  $db_instance 
		--log-file-name error/postgresql.log.$AWS_LOG_TIME > $LOG_FILE
	then
		echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh: FATAL_ERROR - Could not get log from AWS .'
		exit 2
	fi  	
  else
    next_token=`psql -h MONITOR_ENDPOINT.rds.amazonaws.com -U USER -d MONITOR_DATABASE -v ON_ERROR_STOP=1 -A -t -c "select last_aws_nexttoken from database where id = $database_id "`
	
	if [[ $next_token == '' ]];
	then
	  next_token='0'	  
	fi
	
	echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh: CONTINUE DOWNLOADING OF AWS LOG'
	if ! aws rds download-db-log-file-portion 
	    --max-items $last_aws_max_item_size 
		--starting-token $next_token 
		--region REGION 
		--db-instance-identifier  $db_instance 
		--log-file-name error/postgresql.log.$AWS_LOG_TIME > $LOG_FILE
	then
		echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh: FATAL_ERROR - Could not get log from AWS .'
		exit 3
	fi       
	
	line_count=`cat  $LOG_FILE | wc -l`
	let lines=$line_count-1
	  
	tail -$lines $LOG_FILE > $TMP_MIDDLE 
	mv -f $TMP_MIDDLE $LOG_FILE
  fi
  
  next_token_str=`cat $LOG_FILE | grep NEXTTOKEN` 
  next_token=`echo $next_token_str | awk -F" " '{ print $2}' `
  
  grep -v NEXTTOKEN $LOG_FILE  > $TMP_FILE 
  
  if [[ $next_token == '' ]];
  then
	  cp $TMP_FILE $RESULT_FILE
	  
	  echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh:  NEXTTOKEN NOT FOUND - FINISH '
	  rm $LOG_FILE 
	  rm $TMP_FILE
	  rm $TMP_MIDDLE
          rm $TMP_MIDDLE2	  
	  exit 0  
  else
	psql -h MONITOR_ENDPOINT.rds.amazonaws.com -U USER -d MONITOR_DATABASE -v ON_ERROR_STOP=1 -A -t -q -c "update database set last_aws_nexttoken = '$next_token' where id = $database_id "
  fi
  
  first_str=`tail -1 $TMP_FILE`
  
  line_count=`cat  $TMP_FILE | wc -l`
  let lines=$line_count-1    
  
  head -$lines $TMP_FILE  > $RESULT_FILE

###############################################
# MAIN CIRCLE
  let count=2
  while [[ $next_token != '' ]];
  do 
    echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh: count='$count
	
	echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh: START DOWNLOADING OF AWS LOG'
	if ! aws rds download-db-log-file-portion 
             --max-items $last_aws_max_item_size 
             --starting-token $next_token 
             --region REGION 
             --db-instance-identifier  $db_instance 
             --log-file-name error/postgresql.log.$AWS_LOG_TIME > $LOG_FILE
	then
		echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh: FATAL_ERROR - Could not get log from AWS .'
		exit 4
	fi

	next_token_str=`cat $LOG_FILE | grep NEXTTOKEN` 
	next_token=`echo $next_token_str | awk -F" " '{ print $2}' `

	TMP_FILE=$LOG_FILE'.tmp'
	grep -v NEXTTOKEN $LOG_FILE  > $TMP_FILE  
	
	last_str=`head -1 $TMP_FILE`
  
    if [[ $next_token == '' ]];
	then
	  concat_str=$first_str$last_str
	  	  
	  echo $concat_str >> $RESULT_FILE
		 
	  line_count=`cat  $TMP_FILE | wc -l`
	  let lines=$line_count-1
	  
	  tail -$lines $TMP_FILE >> $RESULT_FILE
	  
	  echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh:  NEXTTOKEN NOT FOUND - FINISH '
	  rm $LOG_FILE 
	  rm $TMP_FILE
	  rm $TMP_MIDDLE
          rm $TMP_MIDDLE2	  
	  exit 0  
	fi
	
    if [[ $next_token != '' ]];
	then
		let growth_counter=$growth_counter+1
		if [[ $growth_counter -gt $growth_counter_max ]];
		then
			let last_aws_max_item_size=$last_aws_max_item_size*$growth_factor
			let growth_counter=1
		fi
	
		if [[ $last_aws_max_item_size -gt $max_item_size ]]; 
		then
			let last_aws_max_item_size=$max_item_size
		fi 

	  psql -h MONITOR_ENDPOINT.rds.amazonaws.com -U USER -d MONITOR_DATABASE -A -t -q -c "update database set last_aws_nexttoken = '$next_token' where id = $database_id "
	  
	  concat_str=$first_str$last_str
	  	  
	  echo $concat_str >> $RESULT_FILE
		 
	  line_count=`cat  $TMP_FILE | wc -l`
	  let lines=$line_count-1
	  
	  #############################
	  #Get middle of file
	  head -$lines $TMP_FILE > $TMP_MIDDLE
	  
	  line_count=`cat  $TMP_MIDDLE | wc -l`
	  let lines=$line_count-1
	  tail -$lines $TMP_MIDDLE > $TMP_MIDDLE2
	  
	  cat $TMP_MIDDLE2 >> $RESULT_FILE	  
	  
	  first_str=`tail -1 $TMP_FILE`	  
	fi
	  
    let count=$count+1

  done
#
#################################################################

exit 0  

Blúirí scripte le roinnt mínithe:

Paraiméadair ionchur scripte:

  • Stampa ama ainm an logchomhaid san fhormáid YYYY-MM-DD-HH24: AWS_LOG_TIME=$1
  • Aitheantas an Bhunachair Sonraí: database_id=$2
  • Ainm an logchomhaid a bailíodh: RESULT_FILE=$3

Faigh stampa ama an chomhaid logála deiridh a lódaíodh:

current_aws_log_time=`psql -h MONITOR_ENDPOINT.rds.amazonaws.com -U USER -d MONITOR_DATABASE -A -t -c "select last_aws_log_time from database where id = $database_id "`

Mura n-oireann stampa ama an logchomhaid dheireanaigh a lódaíodh leis an bparaiméadar ionchuir, luchtaítear logchomhad nua:

if [[ $current_aws_log_time != $AWS_LOG_TIME  ]];
  then
    is_new_log='1'
	if ! psql -h ENDPOINT.rds.amazonaws.com -U USER -d MONITOR_DATABASE -v ON_ERROR_STOP=1 -A -t -c "update database set last_aws_log_time = '$AWS_LOG_TIME' where id = $database_id "
	then
	  echo '***download_aws_piece.sh -FATAL_ERROR - update database set last_aws_log_time .'
	  exit 1
	fi
  else
    is_new_log='0'
  fi

Faighimid luach an lipéid nexttoken ón gcomhad íoslódáilte:

  next_token_str=`cat $LOG_FILE | grep NEXTTOKEN` 
  next_token=`echo $next_token_str | awk -F" " '{ print $2}' `

Feidhmíonn luach Nexttoken folamh mar chomhartha go bhfuil deireadh leis an íoslódáil.

I lúb, déanaimid codanna den chomhad a chomhaireamh, ag comhchaintí línte feadh na slí agus ag méadú méid na coda:
Príomh Lúb

# MAIN CIRCLE
  let count=2
  while [[ $next_token != '' ]];
  do 
    echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh: count='$count
	
	echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh: START DOWNLOADING OF AWS LOG'
	if ! aws rds download-db-log-file-portion 
     --max-items $last_aws_max_item_size 
	 --starting-token $next_token 
     --region REGION 
     --db-instance-identifier  $db_instance 
     --log-file-name error/postgresql.log.$AWS_LOG_TIME > $LOG_FILE
	then
		echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh: FATAL_ERROR - Could not get log from AWS .'
		exit 4
	fi

	next_token_str=`cat $LOG_FILE | grep NEXTTOKEN` 
	next_token=`echo $next_token_str | awk -F" " '{ print $2}' `

	TMP_FILE=$LOG_FILE'.tmp'
	grep -v NEXTTOKEN $LOG_FILE  > $TMP_FILE  
	
	last_str=`head -1 $TMP_FILE`
  
    if [[ $next_token == '' ]];
	then
	  concat_str=$first_str$last_str
	  	  
	  echo $concat_str >> $RESULT_FILE
		 
	  line_count=`cat  $TMP_FILE | wc -l`
	  let lines=$line_count-1
	  
	  tail -$lines $TMP_FILE >> $RESULT_FILE
	  
	  echo $(date +%Y%m%d%H%M)':    download_aws_piece.sh:  NEXTTOKEN NOT FOUND - FINISH '
	  rm $LOG_FILE 
	  rm $TMP_FILE
	  rm $TMP_MIDDLE
         rm $TMP_MIDDLE2	  
	  exit 0  
	fi
	
    if [[ $next_token != '' ]];
	then
		let growth_counter=$growth_counter+1
		if [[ $growth_counter -gt $growth_counter_max ]];
		then
			let last_aws_max_item_size=$last_aws_max_item_size*$growth_factor
			let growth_counter=1
		fi
	
		if [[ $last_aws_max_item_size -gt $max_item_size ]]; 
		then
			let last_aws_max_item_size=$max_item_size
		fi 

	  psql -h MONITOR_ENDPOINT.rds.amazonaws.com -U USER -d MONITOR_DATABASE -A -t -q -c "update database set last_aws_nexttoken = '$next_token' where id = $database_id "
	  
	  concat_str=$first_str$last_str
	  	  
	  echo $concat_str >> $RESULT_FILE
		 
	  line_count=`cat  $TMP_FILE | wc -l`
	  let lines=$line_count-1
	  
	  #############################
	  #Get middle of file
	  head -$lines $TMP_FILE > $TMP_MIDDLE
	  
	  line_count=`cat  $TMP_MIDDLE | wc -l`
	  let lines=$line_count-1
	  tail -$lines $TMP_MIDDLE > $TMP_MIDDLE2
	  
	  cat $TMP_MIDDLE2 >> $RESULT_FILE	  
	  
	  first_str=`tail -1 $TMP_FILE`	  
	fi
	  
    let count=$count+1

  done

Cad atá romhainn?

Mar sin, tá an chéad tasc idirmheánach - "íoslódáil logchomhad ón scamall" réitithe. Cad atá le déanamh leis an log íoslódáil?
Ar dtús, ní mór duit an comhad logála a pharsáil agus na hiarratais iarbhír a bhaint as.
Níl an tasc an-deacair. Déanann an script bash is simplí an obair go maith.
upload_log_query.sh

#!/bin/bash
#########################################################
# upload_log_query.sh
# Upload table table from dowloaded aws file 
# version HABR
###########################################################  
echo 'TIMESTAMP:'$(date +%c)' Upload log_query table '
source_file=$1
echo 'source_file='$source_file
database_id=$2
echo 'database_id='$database_id

beginer=' '
first_line='1'
let "line_count=0"
sql_line=' '
sql_flag=' '    
space=' '
cat $source_file | while read line
do
  line="$space$line"

  if [[ $first_line == "1" ]]; then
    beginer=`echo $line | awk -F" " '{ print $1}' `
    first_line='0'
  fi

  current_beginer=`echo $line | awk -F" " '{ print $1}' `

  if [[ $current_beginer == $beginer ]]; then
    if [[ $sql_flag == '1' ]]; then
     sql_flag='0' 
     log_date=`echo $sql_line | awk -F" " '{ print $1}' `
     log_time=`echo $sql_line | awk -F" " '{ print $2}' `
     duration=`echo $sql_line | awk -F" " '{ print $5}' `

     #replace ' to ''
     sql_modline=`echo "$sql_line" | sed 's/'''/''''''/g'`
     sql_line=' '

	 ################
	 #PROCESSING OF THE SQL-SELECT IS HERE
     if ! psql -h ENDPOINT.rds.amazonaws.com -U USER -d DATABASE -v ON_ERROR_STOP=1 -A -t -c "select log_query('$ip_port',$database_id , '$log_date' , '$log_time' , '$duration' , '$sql_modline' )" 
     then
        echo 'FATAL_ERROR - log_query '
        exit 1
     fi
	 ################

    fi #if [[ $sql_flag == '1' ]]; then

    let "line_count=line_count+1"

    check=`echo $line | awk -F" " '{ print $8}' `
    check_sql=${check^^}    

    #echo 'check_sql='$check_sql
    
    if [[ $check_sql == 'SELECT' ]]; then
     sql_flag='1'    
     sql_line="$sql_line$line"
	 ip_port=`echo $sql_line | awk -F":" '{ print $4}' `
    fi
  else       

    if [[ $sql_flag == '1' ]]; then
      sql_line="$sql_line$line"
    fi   
    
  fi #if [[ $current_beginer == $beginer ]]; then

done

Anois is féidir leat oibriú leis an iarratas a roghnaíodh ón gcomhad logála.

Agus osclaíonn roinnt deiseanna úsáideacha.

Ní mór fiosruithe parsáilte a stóráil áit éigin. Úsáidtear tábla seirbhíse chuige seo log_cheist

CREATE TABLE log_query
(
   id SERIAL ,
   queryid bigint ,
   query_md5hash text not null ,
   database_id integer not null ,  
   timepoint timestamp without time zone not null,
   duration double precision not null ,
   query text not null ,
   explained_plan text[],
   plan_md5hash text  , 
   explained_plan_wo_costs text[],
   plan_hash_value text  ,
   baseline_id integer ,
   ip text ,
   port text 
);
ALTER TABLE log_query ADD PRIMARY KEY (id);
ALTER TABLE log_query ADD CONSTRAINT queryid_timepoint_unique_key UNIQUE (queryid, timepoint );
ALTER TABLE log_query ADD CONSTRAINT query_md5hash_timepoint_unique_key UNIQUE (query_md5hash, timepoint );

CREATE INDEX log_query_timepoint_idx ON log_query (timepoint);
CREATE INDEX log_query_queryid_idx ON log_query (queryid);
ALTER TABLE log_query ADD CONSTRAINT database_id_fk FOREIGN KEY (database_id) REFERENCES database (id) ON DELETE CASCADE ;

Próiseáiltear an t-iarratas parsáilte i plpgsql feidhmeanna"log_cheist'.
log_query.sql

--log_query.sql
--verison HABR
CREATE OR REPLACE FUNCTION log_query( ip_port text ,log_database_id integer , log_date text , log_time text , duration text , sql_line text   ) RETURNS boolean AS $$
DECLARE
  result boolean ;
  log_timepoint timestamp without time zone ;
  log_duration double precision ; 
  pos integer ;
  log_query text ;
  activity_string text ;
  log_md5hash text ;
  log_explain_plan text[] ;
  
  log_planhash text ;
  log_plan_wo_costs text[] ; 
  
  database_rec record ;
  
  pg_stat_query text ; 
  test_log_query text ;
  log_query_rec record;
  found_flag boolean;
  
  pg_stat_history_rec record ;
  port_start integer ;
  port_end integer ;
  client_ip text ;
  client_port text ;
  log_queryid bigint ;
  log_query_text text ;
  pg_stat_query_text text ; 
BEGIN
  result = TRUE ;

  RAISE NOTICE '***log_query';
  
  port_start = position('(' in ip_port);
  port_end = position(')' in ip_port);
  client_ip = substring( ip_port from 1 for port_start-1 );
  client_port = substring( ip_port from port_start+1 for port_end-port_start-1 );

  SELECT e.host , d.name , d.owner_pwd 
  INTO database_rec
  FROM database d JOIN endpoint e ON e.id = d.endpoint_id
  WHERE d.id = log_database_id ;
  
  log_timepoint = to_timestamp(log_date||' '||log_time,'YYYY-MM-DD HH24-MI-SS');
  log_duration = duration:: double precision; 

  
  pos = position ('SELECT' in UPPER(sql_line) );
  log_query = substring( sql_line from pos for LENGTH(sql_line));
  log_query = regexp_replace(log_query,' +',' ','g');
  log_query = regexp_replace(log_query,';+','','g');
  log_query = trim(trailing ' ' from log_query);
 

  log_md5hash = md5( log_query::text );
  
  --Explain execution plan--
  EXECUTE 'SELECT dblink_connect(''LINK1'',''host='||database_rec.host||' dbname='||database_rec.name||' user=DATABASE password='||database_rec.owner_pwd||' '')'; 
  
  log_explain_plan = ARRAY ( SELECT * FROM dblink('LINK1', 'EXPLAIN '||log_query ) AS t (plan text) );
  log_plan_wo_costs = ARRAY ( SELECT * FROM dblink('LINK1', 'EXPLAIN ( COSTS FALSE ) '||log_query ) AS t (plan text) );
    
  PERFORM dblink_disconnect('LINK1');
  --------------------------
  BEGIN
	INSERT INTO log_query
	(
		query_md5hash ,
		database_id , 
		timepoint ,
		duration ,
		query ,
		explained_plan ,
		plan_md5hash , 
		explained_plan_wo_costs , 
		plan_hash_value , 
		ip , 
		port
	) 
	VALUES 
	(
		log_md5hash ,
		log_database_id , 
		log_timepoint , 
		log_duration , 
		log_query ,
		log_explain_plan , 
		md5(log_explain_plan::text) ,
		log_plan_wo_costs , 
		md5(log_plan_wo_costs::text),
		client_ip , 
		client_port		
	);
	activity_string = 	'New query has logged '||
						' database_id = '|| log_database_id ||
						' query_md5hash='||log_md5hash||
						' , timepoint = '||to_char(log_timepoint,'YYYYMMDD HH24:MI:SS');
					
	RAISE NOTICE '%',activity_string;					
					 
	PERFORM pg_log( log_database_id , 'log_query' , activity_string);  

	EXCEPTION
	  WHEN unique_violation THEN
		RAISE NOTICE '*** unique_violation *** query already has logged';
	END;

	SELECT 	queryid
	INTO   	log_queryid
	FROM 	log_query 
	WHERE 	query_md5hash = log_md5hash AND
			timepoint = log_timepoint;

	IF log_queryid IS NOT NULL 
	THEN 
	  RAISE NOTICE 'log_query with query_md5hash = % and timepoint = % has already has a QUERYID = %',log_md5hash,log_timepoint , log_queryid ;
	  RETURN result;
	END IF;
	
	------------------------------------------------
	RAISE NOTICE 'Update queryid';	
	
	SELECT * 
	INTO log_query_rec
	FROM log_query
	WHERE query_md5hash = log_md5hash AND timepoint = log_timepoint ; 
	
	log_query_rec.query=regexp_replace(log_query_rec.query,';+','','g');
	
	FOR pg_stat_history_rec IN
	 SELECT 
         queryid ,
	  query 
	 FROM 
         pg_stat_db_queries 
     WHERE  
      database_id = log_database_id AND
       queryid is not null 
	LOOP
	  pg_stat_query = pg_stat_history_rec.query ; 
	  pg_stat_query=regexp_replace(pg_stat_query,'n+',' ','g');
	  pg_stat_query=regexp_replace(pg_stat_query,'t+',' ','g');
	  pg_stat_query=regexp_replace(pg_stat_query,' +',' ','g');
	  pg_stat_query=regexp_replace(pg_stat_query,'$.','%','g');
	
	  log_query_text = trim(trailing ' ' from log_query_rec.query);
	  pg_stat_query_text = pg_stat_query; 
	
	  
	  --SELECT log_query_rec.query like pg_stat_query INTO found_flag ; 
	  IF (log_query_text LIKE pg_stat_query_text) THEN
		found_flag = TRUE ;
	  ELSE
		found_flag = FALSE ;
	  END IF;	  
	  
	  
	  IF found_flag THEN
	    
		UPDATE log_query SET queryid = pg_stat_history_rec.queryid WHERE query_md5hash = log_md5hash AND timepoint = log_timepoint ;
		activity_string = 	' updated queryid = '||pg_stat_history_rec.queryid||
		                    ' for log_query with id = '||log_query_rec.id               
		   				    ;						
	    RAISE NOTICE '%',activity_string;	
		EXIT ;
	  END IF ;
	  
	END LOOP ;
	
  RETURN result ;
END
$$ LANGUAGE plpgsql;

Úsáidtear tábla seirbhíse le linn próiseála pg_stat_db_ceisteanna, ina bhfuil léargas ar cheisteanna reatha ón tábla pg_stat_stair (Tá cur síos anseo ar úsáid an tábla − Monatóireacht feidhmíochta ar fhiosrúcháin PostgreSQL. Cuid 1 - tuairisciú)

TABLE pg_stat_db_queries
(
   database_id integer,  
   queryid bigint ,  
   query text , 
   max_time double precision 
);

TABLE pg_stat_history 
(
…
database_id integer ,
…
queryid bigint ,
…
max_time double precision	 , 	
…
);

Ligeann an fheidhm duit roinnt cumais úsáideacha a chur i bhfeidhm chun iarratais a phróiseáil ó chomhad logála. Eadhon:

Deis #1 - Cuir ceist ar stair fhorghníomhaithe

An-úsáideach le tosú ar theagmhas feidhmíochta a réiteach. Ar dtús, cuir eolas ar an stair - cathain a thosaigh an mhoilliú?
Ansin, de réir na clasaiceach, breathnú ar chúiseanna seachtracha. B'fhéidir go bhfuil méadú mór tagtha ar ualach an bhunachair shonraí agus nach bhfuil baint ar bith ag an iarratas sonrach leis.
Cuir iontráil nua leis an tábla log_query

  port_start = position('(' in ip_port);
  port_end = position(')' in ip_port);
  client_ip = substring( ip_port from 1 for port_start-1 );
  client_port = substring( ip_port from port_start+1 for port_end-port_start-1 );

  SELECT e.host , d.name , d.owner_pwd 
  INTO database_rec
  FROM database d JOIN endpoint e ON e.id = d.endpoint_id
  WHERE d.id = log_database_id ;
  
  log_timepoint = to_timestamp(log_date||' '||log_time,'YYYY-MM-DD HH24-MI-SS');
  log_duration = to_number(duration,'99999999999999999999D9999999999'); 

  
  pos = position ('SELECT' in UPPER(sql_line) );
  log_query = substring( sql_line from pos for LENGTH(sql_line));
  log_query = regexp_replace(log_query,' +',' ','g');
  log_query = regexp_replace(log_query,';+','','g');
  log_query = trim(trailing ' ' from log_query);
 
  RAISE NOTICE 'log_query=%',log_query ;   

  log_md5hash = md5( log_query::text );
  
  --Explain execution plan--
  EXECUTE 'SELECT dblink_connect(''LINK1'',''host='||database_rec.host||' dbname='||database_rec.name||' user=DATABASE password='||database_rec.owner_pwd||' '')'; 
  
  log_explain_plan = ARRAY ( SELECT * FROM dblink('LINK1', 'EXPLAIN '||log_query ) AS t (plan text) );
  log_plan_wo_costs = ARRAY ( SELECT * FROM dblink('LINK1', 'EXPLAIN ( COSTS FALSE ) '||log_query ) AS t (plan text) );
    
  PERFORM dblink_disconnect('LINK1');
  --------------------------
  BEGIN
	INSERT INTO log_query
	(
		query_md5hash ,
		database_id , 
		timepoint ,
		duration ,
		query ,
		explained_plan ,
		plan_md5hash , 
		explained_plan_wo_costs , 
		plan_hash_value , 
		ip , 
		port
	) 
	VALUES 
	(
		log_md5hash ,
		log_database_id , 
		log_timepoint , 
		log_duration , 
		log_query ,
		log_explain_plan , 
		md5(log_explain_plan::text) ,
		log_plan_wo_costs , 
		md5(log_plan_wo_costs::text),
		client_ip , 
		client_port		
	);

Féidearthacht #2 - Sábháil pleananna feidhmithe fiosrúcháin

Ag an bpointe seo d’fhéadfadh go n-eascródh agóid-soiléiriú-trácht: “Ach tá autoexplain ann cheana féin" Sea, tá sé ann, ach cad é an pointe má tá an plean forghníomhaithe stóráilte sa chomhad loga céanna agus chun é a shábháil le haghaidh tuilleadh anailíse, caithfidh tú an comhad logála a pharsáil?

An rud a bhí de dhíth orm ná:
an chéad: an plean forghníomhaithe a stóráil sa tábla seirbhíse den bhunachar sonraí monatóireachta;
ar an dara dul síos: a bheith in ann pleananna forghníomhaithe a chur i gcomparáid lena chéile chun a fheiceáil láithreach go bhfuil plean forghníomhaithe an fhiosrúcháin athraithe.

Tá iarratas ann le paraiméadair fhorghníomhaithe ar leith. Is tasc bunúsach é plean forghníomhaithe a fháil agus a shábháil trí úsáid a bhaint as EXPLAIN.
Thairis sin, ag baint úsáide as an abairt MÍNIÚ (COSTAS BRÉAGACH), is féidir leat creatlach an phlean a fháil, a úsáidfear chun luach hash an phlean a fháil, rud a chabhróidh le hanailís ina dhiaidh sin ar stair na n-athruithe sa phlean forghníomhaithe.
Faigh teimpléad plean forghníomhaithe

  --Explain execution plan--
  EXECUTE 'SELECT dblink_connect(''LINK1'',''host='||database_rec.host||' dbname='||database_rec.name||' user=DATABASE password='||database_rec.owner_pwd||' '')'; 
  
  log_explain_plan = ARRAY ( SELECT * FROM dblink('LINK1', 'EXPLAIN '||log_query ) AS t (plan text) );
  log_plan_wo_costs = ARRAY ( SELECT * FROM dblink('LINK1', 'EXPLAIN ( COSTS FALSE ) '||log_query ) AS t (plan text) );
    
  PERFORM dblink_disconnect('LINK1');

Féidearthacht #3 - Loga na gceisteanna a úsáid le haghaidh monatóireachta

Ós rud é go bhfuil méadracht feidhmíochta cumraithe ní ar théacs an iarratais, ach ar a ID, ní mór duit iarratais ón logchomhad a cheangal le hiarratais a bhfuil méadracht feidhmíochta cumraithe dóibh.
Bhuel, ar a laghad ionas go mbeidh an t-am cruinn ar tharla teagmhas feidhmíochta.

Ar an mbealach seo, nuair a tharlaíonn teagmhas feidhmíochta maidir le haitheantas iarratais, beidh nasc chuig iarratas sonrach le luachanna paraiméadair ar leith agus an t-am forghníomhaithe beacht agus fad na hiarrata. Bain úsáid as an radharc amháin an fhaisnéis seo a fháil pg_stat_ráitis - tá sé toirmiscthe.
Faigh queryid an iarratais agus nuashonraigh an iontráil sa tábla log_query

SELECT * 
	INTO log_query_rec
	FROM log_query
	WHERE query_md5hash = log_md5hash AND timepoint = log_timepoint ; 
	
	log_query_rec.query=regexp_replace(log_query_rec.query,';+','','g');
	
	FOR pg_stat_history_rec IN
	 SELECT 
      queryid ,
	  query 
	 FROM 
       pg_stat_db_queries 
     WHERE  
	   database_id = log_database_id AND
       queryid is not null 
	LOOP
	  pg_stat_query = pg_stat_history_rec.query ; 
	  pg_stat_query=regexp_replace(pg_stat_query,'n+',' ','g');
	  pg_stat_query=regexp_replace(pg_stat_query,'t+',' ','g');
	  pg_stat_query=regexp_replace(pg_stat_query,' +',' ','g');
	  pg_stat_query=regexp_replace(pg_stat_query,'$.','%','g');
	
	  log_query_text = trim(trailing ' ' from log_query_rec.query);
	  pg_stat_query_text = pg_stat_query; 
	  
	  --SELECT log_query_rec.query like pg_stat_query INTO found_flag ; 
	  IF (log_query_text LIKE pg_stat_query_text) THEN
		found_flag = TRUE ;
	  ELSE
		found_flag = FALSE ;
	  END IF;	  
	  
	  
	  IF found_flag THEN
	    
		UPDATE log_query SET queryid = pg_stat_history_rec.queryid WHERE query_md5hash = log_md5hash AND timepoint = log_timepoint ;
		activity_string = 	' updated queryid = '||pg_stat_history_rec.queryid||
		                    ' for log_query with id = '||log_query_rec.id		                    
		   				    ;						
					
	    RAISE NOTICE '%',activity_string;	
		EXIT ;
	  END IF ;
	  
	END LOOP ;

Afterword

Fuair ​​​​an teicníc cur síos ar deireadh iarratas i an córas forbartha monatóireachta feidhmíochta fiosrúcháin PostgreSQL, rud a ligeann duit tuilleadh faisnéise a bheith agat le hanailís a dhéanamh air agus tú ag réiteach teagmhais feidhmíochta fiosrúcháin atá ag teacht chun cinn.

Cé go bhfuil, ar ndóigh, i mo thuairim phearsanta, beidh sé riachtanach a bheith ag obair níos mó ar an algartam do roghnú agus a athrú ar an méid den chuid íoslódáil. Níl an fhadhb réitithe fós sa chás ginearálta. Is dócha go mbeidh sé suimiúil.

Ach sin scéal go hiomlán difriúil...

Foinse: will.com

Ceannaigh óstáil iontaofa do shuímh le cosaint DDoS, freastalaithe VPS VDS 🔥 Ceannaigh óstáil gréasáin iontaofa le cosaint DDoS, freastalaithe VPS VDS | ProHoster