µÚËÄÊ®ÈýÕÂ. À©Õ¹Ë÷Òý½Ó¿Ú

µ½Ä¿Ç°ÎªÖ¹ÎÒÃÇÃèÊöµÄ¹ý³Ì¿ÉÒÔÈÃÄ㶨ÒåÒ»¸öÐÂÀàÐÍ£¬Ðº¯ÊýºÍвÙ×÷·û£®µ«ÊÇ£¬ÎÒÃÇ»¹²»ÄÜÔÚÒ»¸öÐÂÀàÐÍ»òËüµÄ²Ù×÷·ûÉÏÃ涨ÒåÒ»¸ö´ÓÊôË÷Òý£¨ÏóÒ»¸ö B-tree£¬R-tree »ò¹þÏ££¨hash£©·ÃÎÊ·½Ê½£©£®

»Ø¹ýÍ·À´¿´¿´ Ö÷ÒªµÄ Postgres ϵͳ±í¡£ÓҰ벿·ÖÏÔʾÁËÄÇЩÎÒÃÇÈç¹ûÒª¸æËß Postgres ÈçºÎÔÚË÷ÒýÉÏ£¨ÀýÈ磬pg_am£¬pg_amop£¬pg_amproc ºÍ pg_opclass£©È¥Ê¹ÓÃÓû§¶¨ÒåÀàÐͺÍ/»òÓû§¶¨Òå²Ù×÷·ûʱ±ØÐëÒªÐÞ¸ÄµÄ±í£®²»ÐÒµÄÊÇ£¬Ã»Óмòµ¥µÄÃüÁî¿ÉÒÔ×öÕâЩÊÂÇ飮ÎÒÃǽ«Í¨¹ýʵÀýÀ´ÑÝʾÈçºÎͨ¹ýÐÞ¸ÄÕâЩ±íÀ´ÊµÏÖÉÏÃæÐèÇ󣬴ËÀý×ÓÊÇ£ºÒ»¸öеÄÓÃÓÚ B-tree ·ÃÎÊģʽµÄ²Ù×÷·û±í£¬Ëü°Ñ¸´ÊýÒÔ¾ø¶ÔÖµÉýÐòµÄ˳ÐòÅÅÁУ®

pg_am ±íΪÿ¸öÓû§¶¨ÒåµÄ·ÃÎÊģʽ¶¼±£ÁôÒ»Ìõ¼Ç¼£®¶Ô¶ÑµÄ·ÃÎÊģʽµÄÖ§³ÖÄÚ½¨ÓÚ Postgres£¬µ«ÆäËûËùÓзÃÎÊģʽÔÚÕâÀﶼÓÐÃèÊö£®±í½á¹¹ÊÇ
 

±í 43-1. Ë÷Òý±í½á¹¹
 

×Ö¶Î/ÊôÐÔ ÃèÊö
amname ·ÃÎÊģʽÃû³Æ
amowner ËùÓÐÕâÔÚ pg_user ÖеĶÔÏó±êʶ£¨object id £©
amstrategies ´Ë·ÃÎÊģʽµÄ£¨·ÃÎÊ£©²ßÂÔÊý£¨¼ûÏÂÃ棩
amsupport ´Ë·ÃÎÊģʽ֧³ÖµÄ¹ý³ÌÊý£¨¼ûÏÂÃ棩
amorderstrategy Èç¹û¸ÃË÷ÒýûÓÐÌṩÅÅÐò˳Ðò£¬ÎªÁ㣬·ñÔòÊÇÃèÊöÁËÅÅÐò˳ÐòµÄ²ßÂÔ²Ù×÷·ûµÄ²ßÂÔÊý
amgettuple  
aminsert  
... ¸Ã·ÃÎÊģʽµÄ½Ó¿Ú¹ý³ÌµÄ¹ý³Ì±êʶ£®ÀýÈ磬regproc ±íʾ´ò¿ª£¬¹Ø±Õ£¬ºÍ´ÓÕâÀï³öÏֵķÃÎÊģʽÖлñÈ¡¼Ç¼£®
pg_am ÀïµÄ¼Ç¼µÄ ¶ÔÏó±êʶ£¨object ID£© ÓÃÓÚÆäËûºÜ¶à±íµÄÍⲿ¼üÖµ£®Äã²»ÐèÒªÏòÕâ¸ö±íÀïÃæÔö¼ÓÒ»ÌõмǼ£»ÄãÒª¹ØÐĵÄÊÇÄãÏëÒªÀ©Õ¹µÄ·ÃÎÊģʽ¼Ç¼µÄ ¶ÔÏó±êʶ£¨object ID£©£º
SELECT oid FROM pg_am WHERE amname = 'btree';

 oid
-----
 403
(1 row)
ÎÒÃǽ«ÔÚÉÔºóÔÚÒ»¸ö WHERE ×Ó¾äÖÐʹÓÃÕâ¸ö SELECT¡£

amstrategies ×ֶεĴæÔÚÓÃÓÚʹÊý¾ÝÀàÐÍÖ®¼äµÄ±È½Ï±ê×¼»¯£®ÀýÈ磬B-tree ¶Ô¼ü×Ö£¬Ð¡Óںŵ½´óÓÚºÅÊ©¼ÓÁ˺ÜÑϸñµÄ˳Ðò£®ÒòΪ Postgres ÔÊÐíÓû§¶¨Òå²Ù×÷·û£¬ËùÒÔ Postgres²»ÄÜÖ»ÊÇ¿´µ½²Ù×÷·ûµÄÃû³Æ £¨È磬">" »ò "<"£©¾ÍÈÏΪÊÇʲôÑùµÄ±È½Ï£®Êµ¼ÊÉÏ£¬Ò»Ð©·ÃÎÊģʽ²¢²»Ç¿¼ÓÈκÎ˳ÐòÒªÇó£®ÀýÈ磬R-treeµÄ±í´ïʽÊdz¤·½Ðΰüº¬¹Øϵ£¬¶øÒ»¸öÉ¢ÁУ¨hash£©Êý¾Ý½á¹¹±í´ïʽֻÊÇÓëÉ¢ÁУ¨hash£©º¯ÊýµÄ½á¹ûÓÐһЩ루bitwise£©ÏàËÆ£®Postgres ÐèҪijÖÖÁ¬¹áµÄ·½·¨´ÓÄãµÄ²éѯÀïÈ¡À´Ò»¸ö×ʸñ£¨Ìõ¼þ£©£¬²é¿´Ò»Ï²Ù×÷·ûÈ»ºóÂíÉϾö¶¨ÊÇ·ñÓÐÒ»¸ö¿ÉÓõÄË÷Òý´æÔÚ£®ÕâÒâζ×ÅPostgres ÐèÒªÖªµÀ£¬±ÈÈç˵£¬Ïó "<=" ºÍ">" ²Ù×÷·û·Ö¸îÒ»¸ö B-tree£®Postgres ʹÓòßÂÔÀ´±í´ïÕâЩ²Ù×÷·ûÖ®¼äµÄ¹ØϵÒÔ¼°ËüÃÇ¿ÉÒÔÓÃÓÚɨÃèË÷ÒýµÄ·½·¨£®

¶¨ÒåÒ»Ì×еIJßÂÔ³¬³öÁËÕâ¸öÌÖÂ۵ķ¶³ë£¬µ«ÊÇÎÒÃǽ« B-tree ²ßÂÔÈçºÎ¹¤×÷£¬ÒòΪÄ㽫ÐèÒªÖªµÀÁ˽âÕâЩÀ´Ôö¼ÓÒ»¸öеIJÙ×÷·û±í£®ÔÚ pg_am ±íÀamstrategies ×Ö¶ÎÊÇΪÕâ¸ö·ÃÎÊģʽ¶¨ÒåµÄ²ßÂÔÊýÁ¿£®¶ÔÓÚ B-tree£¬Õâ¸öÊýÁ¿ÊÇ 5£®ÕâЩ²ßÂÔ¶ÔÓ¦ÓÚ
 

±í 43-2. B-tree ²ßÂÔ
 

²Ù×÷ Ë÷Òý
less than£¨Ð¡ÓÚ£© 1
less than or equal£¨Ð¡ÓÚ»òµÈÓÚ£© 2
equal£¨µÈÓÚ£© 3
greater than or equal£¨´óÓÚ»òµÈÓÚ£© 4
greater than£¨´óÓÚ£© 5
·½·¨ÊÇÄãÐèÒªÔö¼ÓÓëÉÏÃæ¶ÔÓ¦µÄ±È½Ï¹ý³Ìµ½ pg_amop ¹ØϵÀïÈ¥£¨¼ûÏÂÃæ £©£®·ÃÎÊģʽ´úÂë¿ÉÒÔʹÓÃÕâЩ²ßÂÔÊý£¨²»¹ÜÊý¾ÝÀàÐÍÊÇʲô£©£¬À´È·¶¨ÈçºÎ·Ö¸î B-tree£¬¼ÆËãÑ¡ÔñÐԵȣ®ÏȲ»±Ø¹ØÐÄÔö¼Ó¹ý³ÌµÄϸ½ÚÎÊÌ⣻ֻҪÃ÷°×ÎÒÃDZØÐëÓÐÒ»Ì×ÕâÑùµÄ¹ý³ÌÓÃÓÚ int2£¬int4£¬oid£¬ºÍËùÓÐÆäËû B-tree ¿ÉÒÔ²Ù×÷µÄÊý¾ÝÀàÐÍ£®

ÓÐʱºò£¬²ßÂÔµÄÐÅÏ¢»¹²»×ãÒÔÈÃϵͳ¾ö¶¨ÈçºÎʹÓÃij¸öË÷Òý£® һЩ·ÃÎÊģʽ¾ÍÐèÒªÆäËûµÄһЩ¹ý³ÌÀ´±£Ö¤Äܹ»¹¤×÷£®ÀýÈ磬B-tree ·ÃÎÊģʽ±ØÐëÄܹ»±È½ÏÁ½¸ö¼ü×ÖÒÔ¾ö¶¨ÆäÖÐÒ»¸öÊÇ´óÓÚ£¬µÈÓÚ£¬»¹ÊÇСÓÚÁíÍâÒ»¸ö£®ÀàËƵģ¬R-tree ·ÃÎÊģʽ±ØÐëÄܹ»¼ÆË㳤·½ÐεÄÏཻ£¬ÁªºÏ£¬ºÍ´óСµÈ£®ÕâЩ²Ù×÷²»ÄÜÔÚ SQL ²éѯÀïÓëÓû§µÄ×ʸñ£¨Ìõ¼þ£©¶ÔÓ¦£»ËüÃÇÊDZ»·ÃÎÊģʽµÄ¹ÜÀíÐÔÖʵĹý³ÌÄÚ²¿µ÷ÓõĹý³Ì£®

ΪÁ˹ÜÀíËùÓРPostgres µÄ·ÃÎÊģʽ֧³ÖµÄ¸÷ÖÖ¸÷ÑùµÄ¹ý³Ì£¬pg_am °üº¬Ò»¸ö×ֶνРamsupport£®Õâ¸ö×ֶμǼ±»Ä³¸ö·ÃÎÊģʽ֧³ÖµÄ¹ý³ÌµÄ¸öÊý£®¶ÔÓÚ B-tree£¬Õâ¸öÊý×ÖÊÇÒ» £­Ò»¸ö½ÓÊÜÁ½¸ö¼ü×Ö²¢ÇÒ¸ù¾ÝµÚÒ»¸ö¼ü×ÖÊÇ·ñСÓÚ£¬µÈÓÚ»ò´óÓÚµÚ¶þ¸ö¼ü×Ö¶ø·µ»Ø -1£¬0£¬»ò +1µÄ¹ý³Ì£®

×¢Ò⣺ÑϸñµÄ˵£¬Õâ¸ö¹ý³Ì¿ÉÒÔ·µ»ØÒ»¸ö¸ºÊý £¨< 0£©£¬0£¬»òÒ»¸ö·ÇÁãÕýÊý£¨> 0£©£®
pg_am ÀïµÄ amstrategies ÌõÄ¿Ö»ÊÇÕýÔÚÌÖÂ۵ķÃÎÊģʽ¶¨ÒåµÄ²ßÂÔÊý£®ÓÃÓÚСÓÚ£¬Ð¡ÓÚµÈÓڵȵĹý³Ì²»ÔÚ pg_am Àï³öÏÖ£®ÀàËƵģ¬amsupport Ö»ÊÇ·ÃÎÊģʽÐèÒªµÄÖ§³Ö¹ý³Ì¸öÊý£®Êµ¼ÊµÄ¹ý³ÌÔÚÆäËûµØ·½Áгö£®

˳±ã˵һ¾ä£¬amorderstrategy ×ֶα¨¸æ´Ë·ÃÎÊģʽÊÇ·ñÖ§³ÖÅÅÐòµÄɨÃè¡£ÁãÒâζ×ÅËü²»Ö§³Ö£»Èç¹û¸Ã·ÃÎÊģʽ֧³ÖÅÅÐòµÄɨÃ裬amorderstrategy ¾ÍÊǶÔÓ¦ÅÅÐò²Ù×÷·ûµÄ²ßÂÔ¹ý³ÌµÄÊýÄ¿¡£ÀýÈ磬btree µÄ amorderstrategy = 1£¬¾ÍÊÇËüµÄ"СÓÚ"²ßÂÔÊýÄ¿¡£

ÏÂÒ»¸öÈÃÈ˸ÐÐËȤµÄ±íÊÇ pg_opclass£®Õâ¸ö±íµÄ´æÔÚÖ»ÊÇÓÃÓÚ°ÑÒ»¸öÃû³ÆºÍÒ»¸öȱʡÀàÐÍÓëÒ»¸ö¶ÔÏó±êʶ£¨oid£©Áª½áÆðÀ´£®ÔÚ pg_amop£¬Ã¿¸ö B-tree ²Ù×÷·û±íÓÐÒ»Ì×¹ý³Ì£¬ÏóÉÏÃæÄÇÑù´ÓÒ»µ½Î壮һЩÏÖÓÐµÄ opclasses ÊÇ int2_ops£¬int4_ops£¬ºÍ oid_ops¡£ÄãÐèÒªÓÃÄãµÄ opclass Ãû³Æ£¨ÀýÈ磬complex_abs_ops£©Ôö¼ÓÒ»Ìõ¼Ç¼µ½ pg_opclass ÀïÃ森ÕâÌõ¼Ç¼µÄ oid ÊÇÆäËû±íµÄÍⲿ¼ü×Ö£¬ÓÈÆäÊÇ pg_amop£®

INSERT INTO pg_opclass (opcname, opcdeftype)
    SELECT 'complex_abs_ops', oid FROM pg_type WHERE typname = 'complex';

SELECT oid, opcname, opcdeftype
    FROM pg_opclass
    WHERE opcname = 'complex_abs_ops';

  oid   |     opcname     | opcdeftype
--------+-----------------+------------
 277975 | complex_abs_ops |     277946
(1 row)
×¢ÒâÄãµÄ pg_opclass ¼Ç¼µÄ¶ÔÏó±êʶ£¨oid£©½«»á²»Ò»Ñù£¡ÏȲ»¿¼ÂÇÕâЩ¡£ÎÒÃǽ«ÔÚÉÔºó´Óϵͳ»ñÈ¡ÕâЩÊý×Ö£¬¾ÍÏóÎÒÃÇÔÚÕâÀï»ñÈ¡¸ÃÀàÐ굀 oid Ò»Ñù¡£

ÉÏÃæµÄÀý×Ó¼ÙÉèÄãÏë°ÑÕâ¸öÐ嵀 opclass ×öΪ  complex Êý¾ÝÀàÐ͵ÄȱʡË÷Òý£®Èç¹û²»ÊÇÕâÑù£¬Ö»ÐèÒªÏò opcdeftype ²åÈëÁ㣬¶ø²»ÊDzåÈë¸ÃÊý¾ÝÀàÐ굀 oid£º

INSERT INTO pg_opclass (opcname, opcdeftype) VALUES ('complex_abs_ops', 0);
Òò´Ë£¬ÏÖÔÚÎÒÃÇÓÐÁËÒ»¸ö·ÃÎÊ·½Ê½ºÍÒ»¸ö²Ù×÷·û±í£®ÎÒÃÇ»¹ÐèÒªÒ»ÌײÙ×÷·û£»ÓÃÓÚ¶¨Òå²Ù×÷·ûµÄ¹ý³ÌÒѾ­ÔÚÕâ·ÝÊÖ²áµÄÔçÏȲ¿·ÖÌÖÂÛ¹ýÁË£®¶ÔÕâ¸öÓÃÓÚ Btrees µÄ complex_abs_ops ²Ù×÷·û±í£¬ÎÒÃÇÐèÒªµÄ²Ù×÷·ûÊÇ£º
        absolute value less-than
        absolute value less-than-or-equal
        absolute value equal
        absolute value greater-than-or-equal
        absolute value greater-than
¼ÙÉèʵÏÖº¯Êý²á´úÂë·ÅÔÚÎļþ PGROOT/src/tutorial/complex.c Àï

ÓÐÒ»²¿·Ö´úÂë¿´ÆðÀ´Ï󣺣¨×¢ÒâÎÒÃÇÔÚÓàϵÄÀý×ÓÖн«Ö»Õ¹Ê¾µÈºÅ²Ù×÷·û£®ÆäËûËÄÖÖ²Ù×÷·û¶¼·Ç³£ÏàËÆ£¬Çë²Î¿¼ complex.c »ò complex.sql »ñÈ¡ÏêϸÐÅÏ¢£®£©

#define Mag(c) ((c)->x*(c)->x + (c)->y*(c)->y)

         bool
         complex_abs_eq(Complex *a, Complex *b)
         {
             double amag = Mag(a), bmag = Mag(b);
             return (amag==bmag);
         }
ÎÒÃÇÓÃÏÂÃæÓï¾äÈà Postgres ÕâÑùʶ±ðÕâ¸öº¯Êý£º
CREATE FUNCTION complex_abs_eq(complex, complex)
              RETURNS bool
              AS 'PGROOT/tutorial/obj/complex.so'
              LANGUAGE 'c';
Óм¸¸öºÜÖØÒªµÄÎÊÌâÒªÔÚÕâÀïÖ¸³ö£®

Ê×ÏÈ£¬Çë×¢ÒⶨÒåÁËÓÃÓÚ complex µÄСÓÚ£¬Ð¡ÓÚ»òµÈÓÚ£¬µÈÓÚ£¬´óÓÚ»òµÈÓںʹóÓÚ²Ù×÷·û£®ÎÒÃÇ¿ÉÒÔÖ»ÓÐÒ»¸öÃüÃûÁ˵IJÙ×÷·ûÃû£¬£¨±ÈÈç =£©²¢°ÑÀàÐÍ complex ×öΪÆäÁ½¸ö²Ù×÷Êý£®ÕâÖÖÇé¿öÏÂÎÒÃÇûÓÐÆäËüÓÃÓÚ complex µÄ¡¡=¡¡²Ù×÷·û£¬µ«ÊÇÈç¹ûÎÒÃÇÒªÖÆ×÷Ò»¸öʵÓõÄÊý¾ÝÀàÐÍ£¬ÎÒÃÇ¿ÉÄÜÐèÒª = ×öΪÓÃÓÚ¸´ÊýµÄͨÓõÈÓÚ²Ù×÷µÄ²Ù×÷·û£®ÕâÖÖÇé¿ö£¬ÎÒÃÇ¿ÉÄÜÐèҪʹÓÃһЩÆäËû²Ù×÷·ûÃû³ÆÀ´ÃüÃû complex_abs_eq.

µÚ¶þ£¬¾¡¹Ü Postgres ¿ÉÒÔ´¦ÀíͬÃû²Ù×÷·û£¬Ö»ÒªËüÃǵÄÊäÈëÊý¾ÝÀàÐͲ»Í¬£®C Ö»ÄÜ´¦ÀíÒ»¸ö¾ßÓиø³öÃû³ÆµÄÈ«¾Ö¹ý³Ì£®Òò´ËÎÒÃDz»ÄÜ°Ñ C º¯ÊýÃüÃûΪÏó abs_eq ÕâÑù¼òµ¥µÄÃû×Ö£®Í¨³£ÔÚ C º¯ÊýÃûÀïÃæ°üº¬Êý¾ÝÀàÐÍÃû³ÆÊÇÒ»¸öºÃÏ°¹ß£¬ÕâÑù¾Í²»»áºÍÓÃÓÚÆäËüÊý¾ÝÀàÐ͵ĺ¯Êý³åÍ»£®

µÚÈý£¬ÎÒÃÇ¿ÉÒÔÖÆ×÷º¯Êý abs_eq µÄ Postgres Ãû³Æ£¬ÒÀ¿¿ Postgres ͨ¹ýÊäÈëÊý¾ÝÀàÐÍÀ´ÓëÈκÎÆäËûͬÃû Postgres º¯ÊýÇø·Ö¿ª£®ÎªÁËÁîÀý×Ó¼òµ¥£¬ÎÒÃÇ×öµÄº¯ÊýÔÚ C ²ã´ÎºÍ Postgres ²ã´Î¶¼ÓÐÏàͬµÄÃû³Æ£¬

×îºó£¬Çë×¢ÒâÕâЩ²Ù×÷·ûº¯Êý·µ»Ø²¼¶ûÖµ£®·ÃÎÊģʽÒÀ¿¿Õâ¸öÌØÐÔ£®£¨ÁîÒ»·½Ã棬֧³Öº¯Êý·µ»ØÌض¨µÄ·ÃÎÊģʽµÄÏ£ÍûÖµ £­£­ÔÚÕâÖÖÇé¿öÏÂÊÇÒ»¸ö·ûºÅÕûÊý£®£©ÎļþÀï×îÖյĹý³ÌÊÇÎÒÃÇÔÚÌÖÂÛ±í pg_am Àïamsupport ×Ö¶ÎʱÌáµ½¹ýµÄ"Ö§³Ö¹ý³Ì"£®ÎÒÃÇÉÔºó½«Óõ½Õâ¸ö¶«Î÷£®Ä¿Ç°ÎÒÃÇÔÝʱºöÂÔËü£®

ÏÖÔÚ¶¨ÒåʹÓÃËüÃǵIJÙ×÷·û£º

CREATE OPERATOR = (
     leftarg = complex, rightarg = complex,
     procedure = complex_abs_eq,
     restrict = eqsel, join = eqjoinsel
         )
ÕâÀïµÄÖØÒªÎÊÌâÊǹý³ÌÃû³Æ£¨¾ÍÊÇÉÏÃ涨ÒåµÄ C º¯Êý£©ºÍÕâ¸ö¹ØϵºÍÁªºÏÑ¡ÔñÐÔº¯Êý£®ÄãÓ¦¸ÃֻʹÓÃÀý×ÓÀ²ÎÔÄ complex.source£©µÄÑ¡ÔñÐÔº¯Êý£®Çë×¢Ò⻹ҪÓÐÕâÑùµÄÓÃÓÚСÓÚ£¬µÈÓںʹóÓÚÇé¿öµÄº¯Êý£®±ØÐëÌṩÕâЩ£¨º¯Êý£©£¬·ñÔòÓÅ»¯Æ÷½«ÎÞ·¨ÓÐЧµØʹÓøÃË÷Òý£®

ÏÂÒ»²½ÊÇΪÕâЩ²Ù×÷·ûÏò pg_amop ¹ØϵÀïÃæÔö¼ÓÌõÄ¿£®Òª×öÕâЩ£¬ÎÒÃǽ«ÐèÒªÎÒÃǸն¨ÒåµÄÕâЩ²Ù×÷·ûµÄ oid£®ÎÒÃǽ«´ÓËùÓвÙ×÷·ûÖÐÕÒ³ö½ÓÊÜÁ½¸ö¸´ÊýµÄ²Ù×÷·ûÃû³Æ£¬²¢°ÑËüÃÇÑ¡³öÀ´£º

    SELECT o.oid AS opoid, o.oprname
     INTO TABLE complex_ops_tmp
     FROM pg_operator o, pg_type t
     WHERE o.oprleft = t.oid and o.oprright = t.oid
      and t.typname = 'complex';

 opoid  | oprname
--------+---------
 277963 | +
 277970 | <
 277971 | <=
 277972 | =
 277973 | >=
 277974 | >
(6 rows)
£¨Í¬Ñù£¬Ò»Ð©ÄãµÄ oid £¨¶ÔÏó±êʶ£©½«¿ÉÒԿ϶¨ÊDz»Í¬µÄ£®£©ÎÒÃǸÐÐËȤµÄ²Ù×÷·ûÊÇÄÇЩ oid£¨¶ÔÏó±êʶ£©ÔÚ 277970 µ½ 277974 Ö®¼äµÄ£®ÄãµÃµ½µÄÖµ½«¿ÉÄܲ»Í¬£¬ÄãÓ¦¸ÃÓÃÄãµÃµ½µÄÖµ´úÌæÉÏÃæµÄÊýÖµ£®ÎÒÃÇͨ¹ýÒ»Ìõ select Óï¾äʵÏÖÕâ¸öÄ¿µÄ¡£

ÏÖÔÚÎÒÃÇÒѾ­×¼±¸ºÃÓÃÎÒÃÇеIJÙ×÷·û±í¸üРpg_amop ±í£®ÔÚÕû¸öÌÖÂÛÖÐ×îÖØÒªµÄÊÇÔÚ pg_amop Àï²Ù×÷·ûÊÇ˳ÐòÅÅÁеģ¬´ÓСÓÚµÈÓÚµ½´óÓÚµÈÓÚ£®ÎÒÃÇÓÃÏÂÃæ·½·¨Ôö¼ÓÎÒÃÇÐèÒªµÄʵÀý£º

    INSERT INTO pg_amop (amopid, amopclaid, amopopr, amopstrategy)
        SELECT am.oid, opcl.oid, c.opoid, 1
        FROM pg_am am, pg_opclass opcl, complex_ops_tmp c
        WHERE amname = 'btree' AND
            opcname = 'complex_abs_ops' AND
            c.oprname = '<';
È»ºóÌí¼ÓÆäËû²Ù×÷·û£¬ÏàÓ¦µØÌæ»»ÉÏÃæµÚÈýÐÐµÄ "1" ºÍ×îºóÒ»ÐÐµÄ "<"¡£×¢Òâ˳Ðò£º"СÓÚ" ÊÇ 1£¬"СÓÚ»òµÈÓÚ" ÊÇ 2£¬"µÈÓÚ" ÊÇ 3£¬"´óÓÚ»òµÈÓÚ" ÊÇ 4£¬¶ø "´óÓÚ" ÊÇ 5¡£

ÏÂÒ»²½ÊÇ×¢²áÎÒÃÇÇ°ÃæÔÚÌÖÂÛ pg_am ʱÃèÊö¹ýµÄ"Ö§³Ö¹ý³Ì"£®Ö§³Ö¹ý³ÌµÄ oid£¨¶ÔÏó±êʶ£©´æ·ÅÔÚ±í pg_amproc ÀÓ÷ÃÎÊģʽµÄ oid£¨¶ÔÏó±êʶ£©ºÍ²Ù×÷·û±í oid£¨¶ÔÏó±êʶ£©×ö¹Ø¼ü×Ö£®Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚ Postgres ×¢²áº¯Êý£¨»¹¼ÇµÃÎÒÃÇ°ÑʵÏÖÕâ¸ö¹ý³ÌµÄC ´úÂë·ÅÔÚÁËÎÒÃÇʵÏÖ²Ù×÷·û¹ý³ÌµÄÎļþµÄµ×²¿Â𣿣©£º

    CREATE FUNCTION complex_abs_cmp(complex, complex)
     RETURNS int4
     AS 'PGROOT/tutorial/obj/complex.so'
     LANGUAGE 'c';

    SELECT oid, proname FROM pg_proc
     WHERE proname = 'complex_abs_cmp';

  oid   |     proname
--------+-----------------
 277997 | complex_abs_cmp
(1 row)
£¨Í¬Ñù£¬ÄãµÄ oid £¨¶ÔÏó±êʶ£©µÄÊý×Ö½«¿ÉÄÜ£®£©ÎÒÃÇ¿ÉÒÔÓÃÏÂÃæ·½·¨Ôö¼ÓеļǼ£º
    INSERT INTO pg_amproc (amid, amopclaid, amproc, amprocnum)
        SELECT a.oid, b.oid, c.oid, 1
            FROM pg_am a, pg_opclass b, pg_proc c
            WHERE a.amname = 'btree' AND
                b.opcname = 'complex_abs_ops' AND
                c.proname = 'complex_abs_cmp';
ÕâÑùÎÒÃǾÍÍê³ÉÁË£¡£¨ÎÚÀ­£®£©ÏÖÔÚÎÒÃÇ¿ÉÒÔÔÚÒ»¸ö¸´ÊýÁÐÉÏ´´½¨ºÍʹÓà btree Ë÷ÒýÁË£®
¸