Skip to content

Conversation

simi
Copy link
Owner

@simi simi commented Oct 14, 2019

motivation

Recently I got few times into situation where I was trying to find out what is blocking DELETE queries. Running EXPLAIN (even VERBOSE one) wasn't useful, since the reason was slow trigger (missing index on foreign key column). I had to create testing entry and run EXPLAIN ANALYZE DELETE to get this information.

It will be really valuable for me to show triggers in EXPLAIN query since it will make clear for me there will be any trigger "activated" during execution of DELETE query and that can be the reason for slow DELETE. I have added EXPLAIN option called TRIGGERS enabled by default. There's already autoexplain property for this.

I understand it is not possible to show only triggers which will be really activated unless query is really executed. EXPLAIN ANALYZE remains untouched with this patch.

I have seen initial discussion at https://www.postgresql.org/message-id/flat/20693.1111732761%40sss.pgh.pa.us to show time spent in triggers in EXPLAIN ANALYZE including quick discussion to possibly show triggers during EXPLAIN. Anyway since it doesn't show any additional cost and just inform about the possibilities, I still consider this feature useful. This is probably implementation of idea mentioned at https://www.postgresql.org/message-id/21221.1111736869%40sss.pgh.pa.us by Tom Lane:

We might as well just add one line saying
	<ding> You've got triggers!

💚 All regression tests passed with this change locally.

before:

psql reporting_development -c "explain delete from sales_distribution_channels where id=9;"
                                                        QUERY PLAN                                                        
--------------------------------------------------------------------------------------------------------------------------
 Delete on sales_distribution_channels  (cost=0.14..8.16 rows=1 width=6)
   ->  Index Scan using sales_distribution_channels_pkey on sales_distribution_channels  (cost=0.14..8.16 rows=1 width=6)
         Index Cond: (id = 9)
(3 rows)

psql reporting_development -c "explain verbose delete from sales_distribution_channels where id=9;"
                                                           QUERY PLAN                                                            
---------------------------------------------------------------------------------------------------------------------------------
 Delete on public.sales_distribution_channels  (cost=0.14..8.16 rows=1 width=6)
   ->  Index Scan using sales_distribution_channels_pkey on public.sales_distribution_channels  (cost=0.14..8.16 rows=1 width=6)
         Output: ctid
         Index Cond: (sales_distribution_channels.id = 9)
(4 rows)

after

psql reporting_development -c "explain delete from sales_distribution_channels where id=9;"
                                                        QUERY PLAN                                                        
--------------------------------------------------------------------------------------------------------------------------
 Delete on sales_distribution_channels  (cost=0.14..8.16 rows=1 width=6)
   ->  Index Scan using sales_distribution_channels_pkey on sales_distribution_channels  (cost=0.14..8.16 rows=1 width=6)
         Index Cond: (id = 9)
 Trigger for constraint sales_sales_distribution_channel_id_fk
 Trigger for constraint sales_sales_distribution_channel_id_fk
 Trigger for constraint sales_distribution_channels_account_id_fk
 Trigger for constraint sales_distribution_channels_account_id_fk
(7 rows)

psql reporting_development -c "explain verbose delete from sales_distribution_channels where id=9;"
                                                           QUERY PLAN                                                            
---------------------------------------------------------------------------------------------------------------------------------
 Delete on public.sales_distribution_channels  (cost=0.14..8.16 rows=1 width=6)
   ->  Index Scan using sales_distribution_channels_pkey on public.sales_distribution_channels  (cost=0.14..8.16 rows=1 width=6)
         Output: ctid
         Index Cond: (sales_distribution_channels.id = 9)
 Trigger RI_ConstraintTrigger_a_45806 for constraint sales_sales_distribution_channel_id_fk
 Trigger RI_ConstraintTrigger_a_45807 for constraint sales_sales_distribution_channel_id_fk
 Trigger RI_ConstraintTrigger_c_45698 for constraint sales_distribution_channels_account_id_fk
 Trigger RI_ConstraintTrigger_c_45699 for constraint sales_distribution_channels_account_id_fk
(8 rows)

@simi simi changed the base branch from master to REL2_0B October 14, 2019 07:33
@simi simi changed the base branch from REL2_0B to master October 14, 2019 07:33
@simi simi force-pushed the possible-triggers-3 branch 2 times, most recently from 6c00aad to 1de7010 Compare November 3, 2019 17:03
@simi simi force-pushed the possible-triggers-3 branch from 1de7010 to 18578e3 Compare November 3, 2019 17:06
@simi simi closed this in 55fe609 Jul 24, 2021
simi pushed a commit that referenced this pull request Oct 10, 2021
Currently the pc files use hard coded paths for "includedir" and
"libdir."

Example:

  Cflags: -I/usr/include
  Libs: -L/usr/lib -lpq

This is not very fortunate when cross compiling inside a buildroot,
where the includes and libs are inside a staging directory, because
this introduces host paths into the build:

  checking for pkg-config... /builder/shared-workdir/build/sdk/staging_dir/host/bin/pkg-config
  checking for PostgreSQL libraries via pkg_config... -L/usr/lib <----

This commit addresses this by doing the following two things:

  1. Instead of hard coding the paths in "Cflags" and "Libs"
     "${includedir}" and "${libdir}" are used.  Note: these variables
     can be overriden on the pkg-config command line
     ("--define-variable=libdir=/some/path").

  2. Add the variables "prefix" and "exec_prefix".  If "includedir"
     and/or "libdir" are using these then construct them accordingly.
     This is done because buildroots (for instance OpenWrt) tend to
     rename the real pkg-config and call it indirectly from a script
     that sets "prefix", "exec_prefix" and "bindir", like so:

     pkg-config.real --define-variable=prefix=${STAGING_PREFIX} \
       --define-variable=exec_prefix=${STAGING_PREFIX} \
       --define-variable=bindir=${STAGING_PREFIX}/bin $@

Example #1: user calls ./configure with "--libdir=/some/lib" and
"--includedir=/some/include":

  prefix=/usr/local/pgsql
  exec_prefix=${prefix}
  libdir=/some/lib
  includedir=/some/include

  Name: libpq
  Description: PostgreSQL libpq library
  Url: http://www.postgresql.org/
  Version: 12.1
  Requires:
  Requires.private:
  Cflags: -I${includedir}
  Libs: -L${libdir} -lpq
  Libs.private:  -lcrypt -lm

Example #2: user calls ./configure with no arguments:

  prefix=/usr/local/pgsql
  exec_prefix=${prefix}
  libdir=${exec_prefix}/lib
  includedir=${prefix}/include

  Name: libpq
  Description: PostgreSQL libpq library
  Url: http://www.postgresql.org/
  Version: 12.1
  Requires:
  Requires.private:
  Cflags: -I${includedir}
  Libs: -L${libdir} -lpq
  Libs.private:  -lcrypt -lm

Like this the paths can be forced into the staging directory when
using a buildroot setup:

  checking for pkg-config... /home/sk/tmp/openwrt/staging_dir/host/bin/pkg-config
  checking for PostgreSQL libraries via pkg_config... -L/home/sk/tmp/openwrt/staging_dir/target-mips_24kc_musl/usr/lib

Author: Sebastian Kemper <sebastian_ml@gmx.net>
Co-authored-by: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
Discussion: https://www.postgresql.org/message-id/flat/20200305213827.GA25135%40darth.lan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants