|
16 | 16 | #include "access/amvalidate.h" |
17 | 17 | #include "access/hash.h" |
18 | 18 | #include "access/htup_details.h" |
| 19 | +#include "access/xact.h" |
| 20 | +#include "catalog/pg_am.h" |
19 | 21 | #include "catalog/pg_amop.h" |
20 | 22 | #include "catalog/pg_amproc.h" |
21 | 23 | #include "catalog/pg_opclass.h" |
|
25 | 27 | #include "parser/parse_coerce.h" |
26 | 28 | #include "utils/builtins.h" |
27 | 29 | #include "utils/fmgroids.h" |
| 30 | +#include "utils/lsyscache.h" |
28 | 31 | #include "utils/regproc.h" |
29 | 32 | #include "utils/syscache.h" |
30 | 33 |
|
@@ -341,3 +344,96 @@ check_hash_func_signature(Oid funcid, int16 amprocnum, Oid argtype) |
341 | 344 | ReleaseSysCache(tp); |
342 | 345 | return result; |
343 | 346 | } |
| 347 | + |
| 348 | +/* |
| 349 | + * Prechecking function for adding operators/functions to a hash opfamily. |
| 350 | + */ |
| 351 | +void |
| 352 | +hashadjustmembers(Oid opfamilyoid, |
| 353 | + Oid opclassoid, |
| 354 | + List *operators, |
| 355 | + List *functions) |
| 356 | +{ |
| 357 | + Oid opcintype; |
| 358 | + ListCell *lc; |
| 359 | + |
| 360 | + /* |
| 361 | + * Hash operators and required support functions are always "loose" |
| 362 | + * members of the opfamily if they are cross-type. If they are not |
| 363 | + * cross-type, we prefer to tie them to the appropriate opclass ... but if |
| 364 | + * the user hasn't created one, we can't do that, and must fall back to |
| 365 | + * using the opfamily dependency. (We mustn't force creation of an |
| 366 | + * opclass in such a case, as leaving an incomplete opclass laying about |
| 367 | + * would be bad. Throwing an error is another undesirable alternative.) |
| 368 | + * |
| 369 | + * This behavior results in a bit of a dump/reload hazard, in that the |
| 370 | + * order of restoring objects could affect what dependencies we end up |
| 371 | + * with. pg_dump's existing behavior will preserve the dependency choices |
| 372 | + * in most cases, but not if a cross-type operator has been bound tightly |
| 373 | + * into an opclass. That's a mistake anyway, so silently "fixing" it |
| 374 | + * isn't awful. |
| 375 | + * |
| 376 | + * Optional support functions are always "loose" family members. |
| 377 | + * |
| 378 | + * To avoid repeated lookups, we remember the most recently used opclass's |
| 379 | + * input type. |
| 380 | + */ |
| 381 | + if (OidIsValid(opclassoid)) |
| 382 | + { |
| 383 | + /* During CREATE OPERATOR CLASS, need CCI to see the pg_opclass row */ |
| 384 | + CommandCounterIncrement(); |
| 385 | + opcintype = get_opclass_input_type(opclassoid); |
| 386 | + } |
| 387 | + else |
| 388 | + opcintype = InvalidOid; |
| 389 | + |
| 390 | + /* |
| 391 | + * We handle operators and support functions almost identically, so rather |
| 392 | + * than duplicate this code block, just join the lists. |
| 393 | + */ |
| 394 | + foreach(lc, list_concat_copy(operators, functions)) |
| 395 | + { |
| 396 | + OpFamilyMember *op = (OpFamilyMember *) lfirst(lc); |
| 397 | + |
| 398 | + if (op->is_func && op->number != HASHSTANDARD_PROC) |
| 399 | + { |
| 400 | + /* Optional support proc, so always a soft family dependency */ |
| 401 | + op->ref_is_hard = false; |
| 402 | + op->ref_is_family = true; |
| 403 | + op->refobjid = opfamilyoid; |
| 404 | + } |
| 405 | + else if (op->lefttype != op->righttype) |
| 406 | + { |
| 407 | + /* Cross-type, so always a soft family dependency */ |
| 408 | + op->ref_is_hard = false; |
| 409 | + op->ref_is_family = true; |
| 410 | + op->refobjid = opfamilyoid; |
| 411 | + } |
| 412 | + else |
| 413 | + { |
| 414 | + /* Not cross-type; is there a suitable opclass? */ |
| 415 | + if (op->lefttype != opcintype) |
| 416 | + { |
| 417 | + /* Avoid repeating this expensive lookup, even if it fails */ |
| 418 | + opcintype = op->lefttype; |
| 419 | + opclassoid = opclass_for_family_datatype(HASH_AM_OID, |
| 420 | + opfamilyoid, |
| 421 | + opcintype); |
| 422 | + } |
| 423 | + if (OidIsValid(opclassoid)) |
| 424 | + { |
| 425 | + /* Hard dependency on opclass */ |
| 426 | + op->ref_is_hard = true; |
| 427 | + op->ref_is_family = false; |
| 428 | + op->refobjid = opclassoid; |
| 429 | + } |
| 430 | + else |
| 431 | + { |
| 432 | + /* We're stuck, so make a soft dependency on the opfamily */ |
| 433 | + op->ref_is_hard = false; |
| 434 | + op->ref_is_family = true; |
| 435 | + op->refobjid = opfamilyoid; |
| 436 | + } |
| 437 | + } |
| 438 | + } |
| 439 | +} |
0 commit comments