From: Tom Lane Date: Thu, 14 Sep 2006 22:05:06 +0000 (+0000) Subject: Add a couple of information functions to support direct checks on whether X-Git-Tag: REL8_2_BETA1~71 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=65ab9f4f243df8817982191bce2804f1ec7ecfa6;p=postgresql Add a couple of information functions to support direct checks on whether a schema is our own temp schema or another backend's temp schema, and use these in place of some former kluges in information_schema. Per my proposal of yesterday. --- diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 179232ffa0..151e6500af 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -1,4 +1,4 @@ - + Functions and Operators @@ -9232,6 +9232,18 @@ select current_date + s.a as dates from generate_series(0,14,7) as s(a); port of the local connection + + pg_my_temp_schema() + oid + OID of session's temporary schema, or 0 if none + + + + pg_is_other_temp_schema(oid) + boolean + is schema another session's temporary schema? + + pg_postmaster_start_time() timestamp with time zone @@ -9343,6 +9355,24 @@ SET search_path TO schema , schema, .. Unix-domain socket. + + pg_my_temp_schema + + + + pg_is_other_temp_schema + + + + pg_my_temp_schema returns the OID of the current + session's temporary schema, or 0 if it has none (because it has not + created any temporary tables). + pg_is_other_temp_schema returns true if the + given OID is the OID of any other session's temporary schema. + (This can be useful, for example, to exclude other sessions' temporary + tables from a catalog display.) + + pg_postmaster_start_time diff --git a/src/backend/catalog/information_schema.sql b/src/backend/catalog/information_schema.sql index a735fa3081..ea0601f464 100644 --- a/src/backend/catalog/information_schema.sql +++ b/src/backend/catalog/information_schema.sql @@ -4,7 +4,7 @@ * * Copyright (c) 2003-2006, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/backend/catalog/information_schema.sql,v 1.36 2006/09/05 21:08:35 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/catalog/information_schema.sql,v 1.37 2006/09/14 22:05:06 tgl Exp $ */ /* @@ -644,7 +644,8 @@ CREATE VIEW columns AS WHERE a.attrelid = c.oid AND a.atttypid = t.oid AND nc.oid = c.relnamespace - AND (nc.nspname NOT LIKE 'pg!_temp!_%' ESCAPE '!' OR pg_catalog.pg_table_is_visible(c.oid)) + AND (NOT pg_is_other_temp_schema(nc.oid)) + AND a.attnum > 0 AND NOT a.attisdropped AND c.relkind in ('r', 'v') AND (pg_has_role(c.relowner, 'USAGE') @@ -940,7 +941,7 @@ CREATE VIEW key_column_usage AS AND nc.oid = c.connamespace AND c.contype IN ('p', 'u', 'f') AND r.relkind = 'r' - AND (nr.nspname NOT LIKE 'pg!_temp!_%' ESCAPE '!' OR pg_catalog.pg_table_is_visible(r.oid)) + AND (NOT pg_is_other_temp_schema(nr.oid)) AND (pg_has_role(r.relowner, 'USAGE') OR has_table_privilege(c.oid, 'SELECT') OR has_table_privilege(c.oid, 'INSERT') @@ -1467,7 +1468,7 @@ CREATE VIEW sequences AS FROM pg_namespace nc, pg_class c WHERE c.relnamespace = nc.oid AND c.relkind = 'S' - AND (nc.nspname NOT LIKE 'pg!_temp!_%' ESCAPE '!' OR pg_catalog.pg_table_is_visible(c.oid)) + AND (NOT pg_is_other_temp_schema(nc.oid)) AND (pg_has_role(c.relowner, 'USAGE') OR has_table_privilege(c.oid, 'SELECT') OR has_table_privilege(c.oid, 'UPDATE') ); @@ -1698,7 +1699,7 @@ CREATE VIEW table_constraints AS WHERE nc.oid = c.connamespace AND nr.oid = r.relnamespace AND c.conrelid = r.oid AND r.relkind = 'r' - AND (nr.nspname NOT LIKE 'pg!_temp!_%' ESCAPE '!' OR pg_catalog.pg_table_is_visible(r.oid)) + AND (NOT pg_is_other_temp_schema(nr.oid)) AND (pg_has_role(r.relowner, 'USAGE') -- SELECT privilege omitted, per SQL standard OR has_table_privilege(r.oid, 'INSERT') @@ -1731,7 +1732,7 @@ CREATE VIEW table_constraints AS AND a.attnum > 0 AND NOT a.attisdropped AND r.relkind = 'r' - AND (nr.nspname NOT LIKE 'pg!_temp!_%' ESCAPE '!' OR pg_catalog.pg_table_is_visible(r.oid)) + AND (NOT pg_is_other_temp_schema(nr.oid)) AND (pg_has_role(r.relowner, 'USAGE') OR has_table_privilege(r.oid, 'SELECT') OR has_table_privilege(r.oid, 'INSERT') @@ -1806,7 +1807,7 @@ CREATE VIEW tables AS CAST(c.relname AS sql_identifier) AS table_name, CAST( - CASE WHEN nc.nspname LIKE 'pg!_temp!_%' ESCAPE '!' THEN 'LOCAL TEMPORARY' + CASE WHEN nc.oid = pg_my_temp_schema() THEN 'LOCAL TEMPORARY' WHEN c.relkind = 'r' THEN 'BASE TABLE' WHEN c.relkind = 'v' THEN 'VIEW' ELSE null END @@ -1823,7 +1824,7 @@ CREATE VIEW tables AS THEN 'YES' ELSE 'NO' END AS character_data) AS is_insertable_into, CAST('NO' AS character_data) AS is_typed, CAST( - CASE WHEN nc.nspname LIKE 'pg!_temp!_%' ESCAPE '!' THEN 'PRESERVE' + CASE WHEN nc.oid = pg_my_temp_schema() THEN 'PRESERVE' -- FIXME ELSE null END AS character_data) AS commit_action @@ -1831,7 +1832,7 @@ CREATE VIEW tables AS WHERE c.relnamespace = nc.oid AND c.relkind IN ('r', 'v') - AND (nc.nspname NOT LIKE 'pg!_temp!_%' ESCAPE '!' OR pg_catalog.pg_table_is_visible(c.oid)) + AND (NOT pg_is_other_temp_schema(nc.oid)) AND (pg_has_role(c.relowner, 'USAGE') OR has_table_privilege(c.oid, 'SELECT') OR has_table_privilege(c.oid, 'INSERT') @@ -1952,7 +1953,7 @@ CREATE VIEW triggers AS AND c.oid = t.tgrelid AND t.tgtype & em.num <> 0 AND NOT t.tgisconstraint - AND (n.nspname NOT LIKE 'pg!_temp!_%' ESCAPE '!' OR pg_catalog.pg_table_is_visible(c.oid)) + AND (NOT pg_is_other_temp_schema(n.oid)) AND (pg_has_role(c.relowner, 'USAGE') -- SELECT privilege omitted, per SQL standard OR has_table_privilege(c.oid, 'INSERT') @@ -2150,7 +2151,7 @@ CREATE VIEW views AS WHERE c.relnamespace = nc.oid AND c.relkind = 'v' - AND (nc.nspname NOT LIKE 'pg!_temp!_%' ESCAPE '!' OR pg_catalog.pg_table_is_visible(c.oid)) + AND (NOT pg_is_other_temp_schema(nc.oid)) AND (pg_has_role(c.relowner, 'USAGE') OR has_table_privilege(c.oid, 'SELECT') OR has_table_privilege(c.oid, 'INSERT') diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c index 5c10a42f51..abd3547e5e 100644 --- a/src/backend/catalog/namespace.c +++ b/src/backend/catalog/namespace.c @@ -13,7 +13,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/catalog/namespace.c,v 1.86 2006/07/14 14:52:17 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/catalog/namespace.c,v 1.87 2006/09/14 22:05:06 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -143,6 +143,8 @@ Datum pg_function_is_visible(PG_FUNCTION_ARGS); Datum pg_operator_is_visible(PG_FUNCTION_ARGS); Datum pg_opclass_is_visible(PG_FUNCTION_ARGS); Datum pg_conversion_is_visible(PG_FUNCTION_ARGS); +Datum pg_my_temp_schema(PG_FUNCTION_ARGS); +Datum pg_is_other_temp_schema(PG_FUNCTION_ARGS); /* @@ -2035,3 +2037,17 @@ pg_conversion_is_visible(PG_FUNCTION_ARGS) PG_RETURN_BOOL(ConversionIsVisible(oid)); } + +Datum +pg_my_temp_schema(PG_FUNCTION_ARGS) +{ + PG_RETURN_OID(myTempNamespace); +} + +Datum +pg_is_other_temp_schema(PG_FUNCTION_ARGS) +{ + Oid oid = PG_GETARG_OID(0); + + PG_RETURN_BOOL(isOtherTempNamespace(oid)); +} diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index bc8269e398..125cc6cd5e 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -37,7 +37,7 @@ * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.355 2006/09/10 00:29:34 tgl Exp $ + * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.356 2006/09/14 22:05:06 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -53,6 +53,6 @@ */ /* yyyymmddN */ -#define CATALOG_VERSION_NO 200609091 +#define CATALOG_VERSION_NO 200609141 #endif diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index f78371fcad..9e83cb4108 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.423 2006/09/10 00:29:34 tgl Exp $ + * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.424 2006/09/14 22:05:06 tgl Exp $ * * NOTES * The script catalog/genbki.sh reads this file and generates .bki @@ -3096,6 +3096,10 @@ DATA(insert OID = 2083 ( pg_opclass_is_visible PGNSP PGUID 12 f f t f s 1 16 " DESCR("is opclass visible in search path?"); DATA(insert OID = 2093 ( pg_conversion_is_visible PGNSP PGUID 12 f f t f s 1 16 "26" _null_ _null_ _null_ pg_conversion_is_visible - _null_ )); DESCR("is conversion visible in search path?"); +DATA(insert OID = 2854 ( pg_my_temp_schema PGNSP PGUID 12 f f t f s 0 26 "" _null_ _null_ _null_ pg_my_temp_schema - _null_ )); +DESCR("get OID of current session's temp schema, if any"); +DATA(insert OID = 2855 ( pg_is_other_temp_schema PGNSP PGUID 12 f f t f s 1 16 "26" _null_ _null_ _null_ pg_is_other_temp_schema - _null_ )); +DESCR("is schema another session's temp schema?"); DATA(insert OID = 2171 ( pg_cancel_backend PGNSP PGUID 12 f f t f v 1 16 "23" _null_ _null_ _null_ pg_cancel_backend - _null_ )); DESCR("Cancel a server process' current query");