简体   繁体   English

无法通过 bash 脚本向 kubernetes 中的 postgres pod 发出长 sql 查询

[英]Unable to issue long sql query to postgres pod in kubernetes via bash script

I am trying to execute a query on postgres pod in k8s via bash script but cannot get results when i select a large number of columns.我正在尝试通过 bash 脚本对 k8s 中的 postgres pod 执行查询,但是当我选择大量列时无法获得结果。 Here is my query:这是我的查询:

kubectl exec -it postgres-pod-dcd-wvd -- bash -c "psql -U postgres -c \"Select json_build_object('f_name',json_agg(f_name),'l_name',json_agg(l_name),'email',json_agg(email),'date_joined',json_agg(date_joined),'dep_name',json_agg(dep_name),'address',json_agg(address),'zip_code',json_agg(zip_code),'city',json_agg(city), 'country',json_agg(country)) from accounts WHERE last_name='ABC';\""

When i reduce the number of columns to be selected in the query, i get the results but if I use all the column names, the query just hangs indefinitely.当我减少要在查询中选择的列数时,我会得到结果,但是如果我使用所有列名,查询就会无限期地挂起。 What could be wrong here?这里可能有什么问题?

Update:更新:

I tried using the query as :我尝试将查询用作:

kubectl exec -it postgres-pod-dcd-wvd -- bash -c "psql -U postgres -c \"Select last_name,first_name,...(other column names).. row_to_json(accounts) from register_account WHERE last_name='ABC';\""

But this also hangs.但这也悬而未决。

When i try from inside the pod, It works but i need to execute it via bash script当我从 pod 内部尝试时,它可以工作,但我需要通过 bash 脚本执行它

Means it is almost certainly the results pagination;意味着它几乎可以肯定是结果分页; when you run exec -t it sets up a TTY in the Pod, just like you were connected interactively, so it is likely waiting for you to press space or "n" for the next page当您运行exec -t它会在 Pod 中设置一个 TTY,就像您以交互方式连接一样,因此它可能正在等待您按空格或“n”进入下一页

You can disable the pagination with env PAGER=cat psql -c "select ..." or use the --pset pager=off as in psql --pset pager=off -c "Select ..."您可以使用env PAGER=cat psql -c "select ..."或使用--pset pager=off禁用分页,如psql --pset pager=off -c "Select ..."

Also, there's no need to run bash -c unless your .bashrc is setting some variables or otherwise performing work in the Pod.此外,除非您的.bashrc正在设置一些变量或以其他方式在 Pod 中执行工作,否则无需运行bash -c Using exec -- psql should work just fine, all other things being equal.使用exec -- psql应该可以正常工作,所有其他条件都相同。 You will need to use the env command if you want to go with the PAGER=cat approach, because $ ENV=var some_command is shell syntax, and thus cannot be fed directly into exec如果您想使用PAGER=cat方法,您需要使用env命令,因为$ ENV=var some_commandshell 语法,因此不能直接输入exec

As the resulting columns are having a lot of json processing, I think the time taken to execute these two queries are different.由于结果列有很多 json 处理,我认为执行这两个查询所花费的时间是不同的。

Maybe you can login into the pod and execute the query and see.也许您可以登录到 pod 并执行查询并查看。

kubectl exec -it postgres-pod-dcd-wvd -- bash 

Now you are inside the pod.现在您在吊舱内。 Then we can execute the query.然后我们可以执行查询。

# psql -U postgres -c \"Select json_build_object('f_name',json_agg(f_name),'l_name',json_agg(l_name),'email',json_agg(email),'date_joined',json_agg(date_joined),'dep_name',json_agg(dep_name),'address',json_agg(address),'zip_code',json_agg(zip_code),'city',json_agg(city), 'country',json_agg(country)) from accounts WHERE last_name='ABC';\"

# psql -U postgres -c \"Select last_name,first_name,...(other column names).. row_to_json(accounts) from register_account WHERE last_name='ABC';\"

Now you we will be able to see whether one query is taking longer time to execute.现在您将能够看到一个查询是否需要更长的时间来执行。

Also, kubectl exec pod command can be executed with a request timeout value ( --request-timeout=5m ) to see if there is a slowness.此外,可以使用请求超时值 ( --request-timeout=5m ) 执行kubectl exec pod命令以查看是否存在缓慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM