Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit967a7b0

Browse files
committed
Avoid reporting "cache lookup failed" for some user-reachable cases.
We have a not-terribly-thoroughly-enforced-yet project policy that internalerrors with SQLSTATE XX000 (ie, plain elog) should not be triggerable fromSQL. record_in, domain_in, and PL validator functions all failed to meetthis standard, because they threw plain elog("cache lookup failed for XXX")errors on bad OIDs, and those are all invokable from SQL.For record_in, the best fix is to upgrade typcache.c (lookup_type_cache)to throw a user-facing error for this case. That seems consistent becauseit was more than halfway there already, having user-facing errors for shelltypes and non-composite types. Having done that, tweak domain_in to relyon the typcache to throw an appropriate error. (This costs little becauseInitDomainConstraintRef would fetch the typcache entry anyway.)For the PL validator functions, we already have a single choke point atCheckFunctionValidatorAccess, so just fix its error to be user-facing.Dilip Kumar, reviewed by Haribabu KommiDiscussion: <87wpxfygg9.fsf@credativ.de>
1 parentec253de commit967a7b0

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

‎src/backend/utils/adt/domains.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,28 @@ static DomainIOData *
7272
domain_state_setup(OiddomainType,boolbinary,MemoryContextmcxt)
7373
{
7474
DomainIOData*my_extra;
75+
TypeCacheEntry*typentry;
7576
OidbaseType;
7677

7778
my_extra= (DomainIOData*)MemoryContextAlloc(mcxt,sizeof(DomainIOData));
7879

79-
/* Find out the base type */
80-
my_extra->typtypmod=-1;
81-
baseType=getBaseTypeAndTypmod(domainType,&my_extra->typtypmod);
82-
if (baseType==domainType)
80+
/*
81+
* Verify that domainType represents a valid domain type. We need to be
82+
* careful here because domain_in and domain_recv can be called from SQL,
83+
* possibly with incorrect arguments. We use lookup_type_cache mainly
84+
* because it will throw a clean user-facing error for a bad OID.
85+
*/
86+
typentry=lookup_type_cache(domainType,0);
87+
if (typentry->typtype!=TYPTYPE_DOMAIN)
8388
ereport(ERROR,
8489
(errcode(ERRCODE_DATATYPE_MISMATCH),
8590
errmsg("type %s is not a domain",
8691
format_type_be(domainType))));
8792

93+
/* Find out the base type */
94+
my_extra->typtypmod=-1;
95+
baseType=getBaseTypeAndTypmod(domainType,&my_extra->typtypmod);
96+
8897
/* Look up underlying I/O function */
8998
if (binary)
9099
getTypeBinaryInputInfo(baseType,

‎src/backend/utils/cache/typcache.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ static intenum_oid_cmp(const void *left, const void *right);
182182
* Fetch the type cache entry for the specified datatype, and make sure that
183183
* all the fields requested by bits in 'flags' are valid.
184184
*
185-
* The result is never NULL --- we willelog() if the passed type OID is
185+
* The result is never NULL --- we willereport() if the passed type OID is
186186
* invalid. Note however that we may fail to find one or more of the
187-
* requestedopclass-dependent fields; the caller needs to check whether
188-
*the fieldsare InvalidOid or not.
187+
*valuesrequestedby 'flags'; the caller needs to check whether the fields
188+
* are InvalidOid or not.
189189
*/
190190
TypeCacheEntry*
191191
lookup_type_cache(Oidtype_id,intflags)
@@ -224,14 +224,18 @@ lookup_type_cache(Oid type_id, int flags)
224224
/*
225225
* If we didn't find one, we want to make one. But first look up the
226226
* pg_type row, just to make sure we don't make a cache entry for an
227-
* invalid type OID.
227+
* invalid type OID. If the type OID is not valid, present a
228+
* user-facing error, since some code paths such as domain_in() allow
229+
* this function to be reached with a user-supplied OID.
228230
*/
229231
HeapTupletp;
230232
Form_pg_typetyptup;
231233

232234
tp=SearchSysCache1(TYPEOID,ObjectIdGetDatum(type_id));
233235
if (!HeapTupleIsValid(tp))
234-
elog(ERROR,"cache lookup failed for type %u",type_id);
236+
ereport(ERROR,
237+
(errcode(ERRCODE_UNDEFINED_OBJECT),
238+
errmsg("type with OID %u does not exist",type_id)));
235239
typtup= (Form_pg_type)GETSTRUCT(tp);
236240
if (!typtup->typisdefined)
237241
ereport(ERROR,
@@ -1230,6 +1234,8 @@ lookup_rowtype_tupdesc_internal(Oid type_id, int32 typmod, bool noError)
12301234
*
12311235
* Given a typeid/typmod that should describe a known composite type,
12321236
* return the tuple descriptor for the type. Will ereport on failure.
1237+
* (Use ereport because this is reachable with user-specified OIDs,
1238+
* for example from record_in().)
12331239
*
12341240
* Note: on success, we increment the refcount of the returned TupleDesc,
12351241
* and log the reference in CurrentResourceOwner. Caller should call

‎src/backend/utils/fmgr/fmgr.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,10 +2455,15 @@ CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
24552455
Form_pg_languagelangStruct;
24562456
AclResultaclresult;
24572457

2458-
/* Get the function's pg_proc entry */
2458+
/*
2459+
* Get the function's pg_proc entry. Throw a user-facing error for bad
2460+
* OID, because validators can be called with user-specified OIDs.
2461+
*/
24592462
procTup=SearchSysCache1(PROCOID,ObjectIdGetDatum(functionOid));
24602463
if (!HeapTupleIsValid(procTup))
2461-
elog(ERROR,"cache lookup failed for function %u",functionOid);
2464+
ereport(ERROR,
2465+
(errcode(ERRCODE_UNDEFINED_FUNCTION),
2466+
errmsg("function with OID %u does not exist",functionOid)));
24622467
procStruct= (Form_pg_proc)GETSTRUCT(procTup);
24632468

24642469
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp