开发者

How do I look at the source code column only in a function using psql command line utility?

开发者 https://www.devze.com 2023-01-25 05:23 出处:网络
I\'m running the below command to show the source code for a PostgreSQL function, but it is difficult to read because there are other columns being shown. Is there any way to show only the source code

I'm running the below command to show the source code for a PostgreSQL function, but it is difficult to read because there are other columns being shown. Is there any way to show only the source code column? For now, I am copying and paste the output to text editor that does not word wrap. I do not have access to PgAdmin.

haloror=# \df+ latest_vitals_trigger_function
                                                                                                             List of functions
 Schema |              Name              | Result data type | Argument data types | Volatility |  Owner   | Language |                                              Source code                                              | Description 
--------+--------------------------------+------------------+---------------------+------------+----------+----------+-------------------------------------------------------------------------------------------------------+-------------
 public | latest_vitals_trigger_function | trigger          |                     | volatile   | postgres | plpgsql  |                                                                                                       | 
                                                                                                                     :     declare                                                                                             
                                                                                                                     :       row record;                                                                                       
                                                                                                                     :     begin                                                                                               
                                                                                                                     :       for row in (select device_strap_status.id from device_strap_status inner join devices_users       
                                                                                                                     :                     on device_strap_status.id = devices_users.device_id where                           
                                                                                                                     :                     device_strap_status.is_fastened = 1 and devices_users.user_id = new.user_id) loop   
                                                                                                                     :           update latest_vitals set updated_at = now() where id = row.id;                                
                                                                                                                     :       if NOT FOUND then                                                                                 
                                                                                                                     :           insert into latest_vitals (id, updated_at) values (row.id, now());                   开发者_JAVA百科         
                                                                                                                     :       end if;                                                                                           
                                                                                                                     :       end loop;                                                                                         
                                                                                                                     :       return null;                                                                                      
                                                                                                                     :     end;                                                                                                
                                                                                                                     :                                                                                                         
(1 row)


OK, any time you want to see what sql lies behind a psql \ command, just start it like this:

psql -E mydb

Then when you run a \ command, the queries used to make it work will show up above your output.

Just copy and paste that query, and remove the columns you don't want from the select list.


I usually issue "\x" before using "\df+", which will put the values vertically instead of horizontally. Which is fine just as a quick "stop shoving the info over to the right" workaround. I also wrap the psql binary to set my LESS environment variable so that the pager doesn't wrap lines, which helps too.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号