´ÙÀ½ ÀÌÀü Â÷·Ê

5. Large Object with Transaction

5.1 Large Object¿Í ¿¹Á¦

Æ÷½ºÆ®±×·¹½º¿¡¼­´Â ÇÑ Æ©ÇÃÀÇ »çÀÌÁî°¡ 8192 Byte (8k Bytes) ·Î Á¦ÇѵǾî ÀÖ´Ù. ÇϳªÀÇ ·¹Äڵ忡 µé¾î°¥ ¼ö ÀÖ´Â µ¥ÀÌŸÀÇ ÃÑ Å©±â°¡ Á¦ÇѵǾî ÀÖÀ¸¹Ç·Î À̹ÌÁö³ª »çÀÌÁî°¡ 8K ¸¦ ³Ñ´Â ¹®¼­µéÀº ´Ù¸£°Ô ÀúÀåµÇ¾î¾ß ÇÑ´Ù.

Æ÷½ºÆ®±×·¹½º´Â Large Object ¶ó´Â °³³äÀ¸·Î À̸¦ ±Øº¹ÇÏ·ÁÇÑ´Ù. °ú°Å¿¡ Æ÷½ºÆ®±× ·¹½º´Â ÀÌ·± Å« »çÀÌÁîÀÇ µ¥ÀÌŸ¸¦ À§ÇØ 3°¡ÁöÀÇ Áö¿øÀÌ ÀÖ¾úÀ¸³ª »ç¿ëÀÚµé»çÀÌÀÇ ÀæÀº È¥¶õÀ¸·Î Çϳª¸¸À» Áö¿øÇÏ°Ô µÇ¾ú°í ±×°ÍÀº ´Ü¼øÈ÷ µ¥ÀÌŸº£À̽º¾ÈÀÇ µ¥ÀÌŸ·Î ¼­ÀÇ Large Object ¸¦ Áö¿øÇÑ´Ù. ÀÌ°ÍÀº ¾×¼¼½º¸¦ ÇÒ¶§ ´À¸±¼ö ÀÖÁö¸¸ ¾ö°ÝÇÑ µ¥ÀÌŸ ¹«°á¼ºÀ» Á¦°øÇÑ´Ù.

Æ÷½ºÆ®±×·¹½º´Â Large Object ¸¦ ÂÉ°³¾î À̸¦ µ¥ÀÌŸº£À̽ºÀÇ Æ©Çõ鿡 ÀúÀåÇÑ´Ù. B-tree À妽º´Â ·£´ýÇÑ resd-write ¾×¼¼½º¿¡ ´ëÇÑ ºü¸¥ °Ë»öÀ» º¸ÁõÇÑ´Ù.


´ÙÀ½Àº ¿¹Á¦ÀÌ´Ù.

--------------------------------------------------------------------
   drop  table  image;

   BEGIN  WORK;

   SET  TRANSACTION  ISOLATION  LEVEL  SERIALIZABLE;

   create table image(
     name    text,
     raster  oid
   );

   insert  into    image (name , raster) 
        values ('snapshot' , lo_import('/usr/local/src/snapshot01.gif') );

   select  lo_export (image.raster , '/tmp/snap.gif') 
        from  image  where name='snapshot';

   COMMIT  WORK;
-----------------------------------------------------------------------
Large Object Note

À§ÀÇ ¿¹Á¦¿¡¼­ ¸í½ÃÀûÀ¸·Î Æ®·£Àè¼Ç³»¿¡¼­ Large Object ÀÇ Ã³¸®°¡ ÀÌ·ç¾îÁö°í ÀÖ´Ù. ÀÌ´Â Æ÷½ºÆ®±×·¹½º 6.5 ¹öÁ¯´ë¿¡¼­ºÎÅÍÀÇ Large Object 󸮿¡ ´ëÇÑ ¿ä±¸»çÇ×À¸·Î¼­ 6.5 ÀÌÀü ¹öÁ¯ÀÇ ¾Ï½ÃÀûÀÎ Æ®·£Àè¼Ç ¿ä±¸»çÇ×°ú´Â ´Þ¸® ¸í½ÃÀûÀÎ Æ®·£Àè¼ÇÀ» ¿ä±¸ÇÑ ´Ù. ÀÌ ¿ä±¸»çÇ×ÀÌ ¹«½ÃµÈ´Ù¸é, Áï ¸í½ÃÀûÀÎ Æ®·£Àè¼Ç¹®ÀÌ ÀÛ¼ºµÇÁö ¾Ê´Â´Ù¸é ºñÁ¤ ¼ºÀûÀÎ °á°ú¸¦ ¸¸µç´Ù.

¼³¸í

  BEGIN  WORK;
    »ç¿ëÀÚ Á¤ÀÇ Æ®·£Àè¼Ç ½ÃÀÛ 

  SET  TRANSACTION  ISOLATION  LEVEL  SERIALIZABLE;
    Æ®·£Àè¼Ç ·¹º§ Áß °¡Àå °­·ÂÇÑ SERIZABLE ·¹º§·Î Àç ¼³Á¤À» ÇÔ. 

Æ÷½ºÆ®±×·¹½ºÀÇ Æ®·£Àè¼ÇÀÇ µðÆúÆ® ·¹º§Àº "READ COMMITTED" ·Î¼­ Æ®·£Àè¼Ç ³»ÀÇ 
query ´Â ÀÌ query °¡ ½ÇÇàµÇ±âÀü¿¡ commit µÈ µ¥ÀÌŸ¸¸ ´Ù·ê ¼ö ÀÖ´Ù.

SERIALIZABLE ´Â Æ÷½ºÆ®±×·¹½ºÀÇ °¡Àå °­·ÂÇÑ Æ®·£Àè¼Ç ·¹º§·Î¼­ Æ®·£Àè¼Ç³»ÀÇ 
query´Â query½ÃÀÛÀüÀÌ ¾Æ´Ñ ±× Æ®·£Àè¼ÇÀÌ ½ÃÀ۵DZâÀü¿¡ commitµÈ µ¥ÀÌŸ¸¸À» 
´Ù·ê¼ö ÀÖ´Ù.

OID ´Â °´Ã¼¿¡´ëÇÑ Æ÷½ºÆ®±×·¹½ºÀÇ ½Ã½ºÅÛ °ü·Ã ½Äº°ÀÚÀÌ´Ù. 

lo_import(Àоî¿Ã µ¥ÀÌŸÀÇ PATH); ´Â µ¥ÀÌŸ¸¦ ÀоîµéÀÌ´Â Large Object °ü·Ã 
³»Àå ÇÔ¼öÀÌ´Ù.

lo_export( OID , µ¥ÀÌŸ°¡ ¾²¿©Áú ½Ã½ºÅÛÀÇ PATH); ´Â µ¥ÀÌŸ¸¦ Àо ²¨³»´Â 
Large Object °ü·Ã ³»ÀåÇÔ¼öÀÌ´Ù.

COMMIT WORK; ´Â Æ®·£Àè¼ÇÀÇ ¿Ï·á¸¦ ÀǹÌÇÑ´Ù. ÀÌ·Î ÀÎÇØ ½ÇÁúÀûÀÎ °»½ÅÀ̳ª »èÁ¦µîÀÌ 
ÀÌ·ç¾îÁø´Ù.

5.2 TRANSACTION

Æ®·£Àè¼ÇÀÇ ¼º°Ý(ACID)

¿øÀÚ¼º       : ÇϳªÀÇ Æ®·£Àè¼ÇÀº ´Ù¼öÀÇ query¸¦ ½ÇÇàÇÏÁö¸¸ ÀÌ´Â ´ÜÁö ÇϳªÀÇ
(ATOMIC)       query ÀÎ¾ç ½ÇÇàµÇ¾î¾ß ÇÑ´Ù.  

ÀÏ°ü¼º       : Æ®·£Àè¼ÇÀÇ ¼öÇà¿¡ ´ëÇØ µ¥ÀÌŸº£À̽ºÀÇ µ¥ÀÌŸµéÀÇ ÀÏ°ü¼ºÀº 
(CONSISTENT)   À¯ÁöµÇ¾î¾ß ÇÑ´Ù. 

ºÐ¸®         : °¢ Æ®·£Àè¼ÇÀº ºÐ¸®µÇ¾î ´Ù¸¥ Æ®·£Àè¼ÇÁß¿¡ °£¼·Çؼ­´Â ¾ÈµÈ´Ù.
(ISOLATABLE)   ÀÌ´Â º´·Ä (CONCURRENCY) Á¦¾îÀÇ °³³äÀ¸·Î µ¥ÀÌŸº£À̽º´Â ¸ÖƼ
               À¯Àú ȯ°æÀÏ ¼ö ÀÖÀ¸¹Ç·Î °¢ À¯ÀúÀÇ Æ®·£Àè¼ÇÀº ¾ÈÀüÇÏ°Ô ÀÌ·ç
               ¾îÁ®¾ß ÇÑ´Ù. 

¿µ±¸¼º       : Æ®·£Àè¼ÇÀÇ ¼öÇàÈÄ commit µÈ µ¥ÀÌŸµéÀº ¿µ±¸ÀûÀ¸·Î À¯ÁöµÇ¾î¾ß 
(DURABLE)      ÇÑ´Ù.

Æ®·£Àè¼Ç °ü·Ã SQL ¸í·É¾î Á¤¸®

BEGIN [WORK | TRANSACTION]

   BEGIN : »õ·Î¿î Æ®·£Àè¼ÇÀÌ Chain Mode·Î ½ÃÀÛÇßÀ½À» ¾Ë¸°´Ù.
   WORK , TRANCTION : Optional Keyword. They have no effect.
COMMIT [WORK | TRANSACTION]

   Æ®·£Àè¼ÇÈÄ º¯°æµÈ °á°ú¸¦ ÀúÀå.
END [WORK | TRANCTION]

   ÇöÀç Æ®·£Àè¼ÇÀ» COMMIT.
   END´Â Æ÷½ºÆ®±×·¹½º È®ÀåÀ¸·Î¼­ COMMIT ¿Í °°Àº ÀǹÌÀÌ´Ù.
LOCK [TABLE] name

LOCK [TABLE] name IN [ROW | ACCESS] {SHARE | EXCLUSIVE} MODE

LOCK [TABLE] name IN SHARE ROW EXCLUSIVE MODE

  ¸í½ÃÀûÀ¸·Î Æ®·£Àè¼Ç ³»ÀÇ Å×À̺íÀ» Àá±Ý.
ROLLBACK [WORK | TRANSACTION]

   ÇöÀç Æ®·£Àè¼ÇÀ» ÁßÁöÇÑ´Ù.
 
ABORT [WORK | TRANSACTION]

   ÇöÀç Æ®·£Àè¼ÇÀ» ÁßÁöÇÑ´Ù. ABORT ´Â Æ÷½ºÆ®±×·¹½º È®ÀåÀ¸·Î ROLLBACK¿Í
   °°Àº Àǹ̷μ­ ¾²ÀδÙ.
 
SET TRANSACTION ISOLATION LEVEL {READ COMMITTED | SERIALIZABLE}

   ÇöÀç Æ®·£Àè¼Ç¿¡ ´ëÇÑ ºÐ¸® ·¹º§À» ¼³Á¤ÇÑ´Ù.
   
¼³¸í

   INSERT INTO tab VALUES('qwe','www',123);
   
À§ÀÇ INSERT¹® ÀÌ ¼º°øÀûÀ¸·Î ¼öÇàµÇ¾ú´Ù¸é commit µÉ°ÍÀÌ´Ù.
¾Æ´Ï¸é RollBack µÉ°ÍÀÌ´Ù. ´Ù½Ã ¸»ÇØ,  À§ÀÇ ¹®ÀÌ ¼º°øÇϸé 
µ¥ÀÌŸº£À̽º¿¡ ±×¿¡  µû¸¥  µ¥ÀÌŸ°¡ ÀúÀåµÇ°í ±×·¸Áö ¾Ê°í 
INSERT ÀÇ ½ÇÇà°á°ú°¡ ERROR ÀÌ¸é µ¥ÀÌŸ´Â ÀúÀåµÇÁö ¾Ê´Â´Ù.
   
À̸¦ autocommit ¶ó Çϴµ¥ ¶ÇÇÑ ´Ù¸¥¸»·Î unchained mode
¶ó°íµµ ÇÑ´Ù. 

Æ÷½ºÆ®±×·¹½º¿¡¼­ÀÇ ÀϹÝÀûÀÎ ¸í·ÉµéÀÇ ½ÇÇàÀº unchained mode ÀÌ´Ù.
±×¸®°í À̸¦ Á» ´õ ±×¼úÀûÀ¸·Î ¼­¼úÇÏ¸é ´ÙÀ½°ú °°´Ù.
   
"°¢°¢ÀÇ ¹®Àå(statement)µéÀº ¾Ï½ÃÀûÀÎ Æ®·£Àè¼Ç³»¿¡¼­ ½ÇÇàµÇ¾îÁö°í 
±× ¹®ÀåÀÇ ³¡ºÎºÐ¿¡¼­ commit°¡ ÀÌ·ç¾îÁö´Âµ¥ ½ÇÇàÀÌ ¼º°øÀûÀ̸é commit
°¡ ÇàÇØÁö°í ¹Ý´ë·Î ½ÇÇàÀÌ ¼º°øÀûÀÌÁö ¾ÊÀ¸¸é rollback µÇ¾îÁø´Ù."
   
°á±¹Àº °³º°ÀûÀÎ SQL ¹®µéÀÇ ½ÇÇà¿¡ ÀÖ¾î »ç¿ëÀÚµéÀº Àڽŵµ ¸ð¸£°Ô 
Æ®·£Àè¼Ç³»¿¡¼­ ¼öÇàÇÏ°í ÀÖ°í ¶ÇÇÑ ±× °á°úµµ Àڽŵµ ¸ð¸£°Ô commit
À̰ųª rollbackÀÌ ÀÌ·ç¾îÁø´Ù.


BEGIN Àº ¸í½ÃÀûÀ¸·Î Æ®·£Àè¼ÇÀ» ½ÃÀÛÇÔÀ» ÀǹÌÇϸç autocommit ÀÌ µÇÁö
¾Ê´Â´Ù(chained mode). ¸í½ÃÀûÀÎ commit ¹®ÀÌ ¿Ã¶§±îÁö ÀÛ¾÷µéÀÇ °á°úµéÀÌ
µ¥ÀÌŸº£À̽º¿¡ ÀúÀåµÇÁö ¾Ê´Â´Ù.

BEGIN ¹® ¹Ù·Î µÚ¿¡ SET ¹®À» »ç¿ëÇÏ¿© ±× Æ®·£Àè¼ÇÀÇ Æ®·£Àè¼Ç ºÐ¸® ·¹º§
À» ÁöÁ¤ÇÒ ¼ö ÀÖ´Ù. SET ¹®ÀÇ ¿¹´Â ´ÙÀ½°ú °°´Ù.


   BEGIN WORK;
     SET  TRANSACTION  ISOLATION  LEVEL  SERIALIZABLE 
     INSERT INTO tab VALUES(1,2,3);
     INSERT INTO tab VALUES(3,4,5);
   COMMIT WORK;

Æ®·£Àè¼Ç ºÐ¸® Á¤Ã¥Àº ¿©·¯ À¯ÀúÀÇ µ¿½Ã¼º ¿¡ ´ëÇÑ º¸´Ù °­·ÂÇÑ Á¦ÇÑÀ̶ó ÇÒ 
¼ö ÀÖ°Ú´Ù. Æ÷½ºÆ®±×·¹½º¿¡ÀÇ µðÆúÆ® Æ®·£Àè¼Ç ºÐ¸®·¹º§Àº "READ COMMITTED"
ÀÌ´Ù. READ COMMITTED º¸´Ù ´õ¿í´õ ¾ö°ÝÇÑ ·¹º§ÀÌ SERIALIZABLE ÀÌ´Ù.

´ÙÀ½ ÀÌÀü Â÷·Ê