Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lU9Ij-0004CT-Dt for pgsql-general@arkaria.postgresql.org; Wed, 07 Apr 2021 14:37:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lU9Ii-0001Rn-A6 for pgsql-general@arkaria.postgresql.org; Wed, 07 Apr 2021 14:37:00 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lU9Ii-0001Je-1z for pgsql-general@lists.postgresql.org; Wed, 07 Apr 2021 14:37:00 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lU9IY-0002XE-4B for pgsql-general@lists.postgresql.org; Wed, 07 Apr 2021 14:36:57 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id B304848AB; Wed, 7 Apr 2021 16:36:48 +0200 (CEST) Date: Wed, 7 Apr 2021 16:36:48 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Using indexes through a plpgsql procedure Message-ID: <20210407143648.GA21415@hjp.at> Mail-Followup-To: pgsql-general@lists.postgresql.org References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="PEIAKu/WMn1b1Hv9" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --PEIAKu/WMn1b1Hv9 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2021-04-07 12:15:37 +0100, Ciaran Doherty wrote: > As a quick question. Can Postgres make use of indexes on a table when the= data > from that table is being returned from a procedure? >=20 > Some more details. I have a big table which has a long running query runn= ing on > it. This table gets rebuilt from scratch on a weekly basis. Currently, I = cannot > update/replace the table in a timely manner, I have to wait for the long > running queries to finish before we can update the table (the total time = can be > 5+ hours). >=20 > I am currently trying to build a procedure which will return data from the > latest version of the table (by dynamically choosing the table name, some= thing > like > https://stackoverflow.com/questions/35559093/ > how-to-use-variable-as-table-name-in-plpgsql) So you are creating a new table for each update and drop older tables after some time? And your procedure just returns data from the newest table? > =A0 . To get a consistent return type I am having to use the `create > type`=A0 to build a custom return type. The problem (I think) I am > having is that the indexes on the underlying tables are not usable > after calling this procedure >=20 > (e.g. > ``` > select * > =A0 from example_table as et > =A0=A0=A0 join example_procedure() as ep on et.exapmle_column =3D ep.exam= ple_column > ``` > there will be no index on the column ep.example_column) >=20 > Is there a way to make sure indexes are used even if the data is accessed= threw > a procdure? Any query within example_procedure() will be able to use an index. and the join will in theory be able to use an index on example_table, although = it can't really get useful statistics (it doesn't know what fraction of the example_table will be covered by the output of example_procedure()), so it might default to doing a full scan anyway. I would probably use a view for this: After creating the new table, just recreate the view to use the new table.=20 Or maybe you don't even need the view and can get away by renaming the old and new table: begin; create table ep_new(...); -- populate ep_new here drop table if exists ep_old; alter table ep rename to ep_old; alter table ep_new rename to ep; commit; Partitioning should also work but that feels like a hack. hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --PEIAKu/WMn1b1Hv9 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmBtw3wACgkQ8g5IURL+ KF0sNw//X0cvBqiUTaskPeYA8z3b2KIc6pJ8FWWsHhtoBaAl0K7aUKL0nxQuOGaz nwXARhJdys6IdNICSsbZYtdNLKbZ2Kr2fWTPGJeKP26+1bdp7yudGGKbrPLn/Tbv Z+c/GhPISfdIKTCKdVNCOx2VbBTW/WT9WrAKpzWiyVobl1uARRepiKT83Ie1me0T ywVN5bqUYUvdHY7tjYzVD6wpZX3a7qZFSxyPCxXRs16DfkKVrR5haEV9UrK3hGKw Pspb2h4/hi4yo2PFzqFpHvFArMuOo9Vp3PhFnoy7HzOi4et5i/atwPkuZSaJtFXz XdzHGUBRTOT4DUnpuc4cko4vpnyJ7TNtOz5fEDZEYIx/gKkPzD/2ud2DNAzuxSsV kK5uWyQ79y8MuHXFeBxmDwCSa7tjZKIqfxuYtDXaXNWmfhENJdEXATCOHvVYZbr2 tvZADgBFWgxSpelhKDhS1ianw9rvQ0YXWk00gb5gcFnrWlIrt7CWHpi873Q2oGc9 qK9FUSr2qAnJTwFJy3SILqju90s4Jg2JeIHDgG5qWaqXO+ySKfu/wkbYAsjuLbBE YuAkOFkglVxHdXRfWt/AzRYg4YiYKRW8D3c4FTTugLZ7mJrTMf8+IxH29jMghNGn fA2G0467nw4OucnEH9+xmAvrEHBUkDgFqSxGar4Z0JzNRw0TRXQ= =6Ckh -----END PGP SIGNATURE----- --PEIAKu/WMn1b1Hv9--