Re: seq scan in the case of max() on the primary key column - Mailing list pgsql-performance

From Jim Nasby
Subject Re: seq scan in the case of max() on the primary key column
Date
Msg-id B7D0666C-FE18-4052-A2B1-0442E23CD5CC@nasby.net
Whole thread Raw
In response to Re: seq scan in the case of max() on the primary key column  (Shaun Thomas <sthomas@peak6.com>)
Responses Re: seq scan in the case of max() on the primary key column
List pgsql-performance
On Jun 16, 2011, at 1:36 PM, Shaun Thomas wrote:
> /**
> * Return the Maximum INT Value for a Partitioned Table Column
> *
> * @param string  Name of Schema of the base partition table.
> * @param string  Name of the base partition table.
> * @param string  Name of column to search.
> */
> CREATE OR REPLACE FUNCTION spc_max_part_int(VARCHAR,  VARCHAR, VARCHAR)
> RETURNS INT AS
> $$
> DECLARE
> <snip>
>  SELECT INTO nParent t.oid
>    FROM pg_class t
>    JOIN pg_namespace n ON (t.relnamespace=n.oid)
>   WHERE n.nspname = sSchema
>     AND t.relname = sTable;

FWIW, instead of that, I would do this:

CREATE FUNCTION ...(
  p_parent_schema text
  , p_parent_table text
) ...
DECLARE
  c_parent_oid CONSTANT oid := (p_parent_schema || '.' || p_parent_table )::regclass;

... or ...

CREATE FUNCTION(
  p_parent text
)
DECLARE
  c_parent_oid CONSTANT oid := p_parent::regclass;


Advantages:

- ::regclass is search_path-aware, so you're not forced into providing a schema if you don't want to
- it will throw an error if it doesn't find a regclass entry
- you can cast the oid back to text: EXECUTE 'SELECT max(' ... 'FROM ' || c_parent_oid::regclass
- you can also query directly with the OID: SELECT relkind = 't' AS is_table FROM pg_class WHERE oid = c_parent_oid
--
Jim C. Nasby, Database Architect                   jim@nasby.net
512.569.9461 (cell)                         http://jim.nasby.net



pgsql-performance by date:

Previous
From: Greg Smith
Date:
Subject: Re: seq scan in the case of max() on the primary key column
Next
From: Jose Ildefonso Camargo Tolosa
Date:
Subject: Large rows number, and large objects