Tips & Tricks: PostgreSQL의 메모리 스토리지

-

PostgreSQL은 Red Hat Enterprise Linux와 함께 사용되는 매우 정교하고 강력한 데이터베이스 서버입니다. 하지만, 많은 이들이 MySQL의 메모리 스토리지 엔진이 빠져 있다는 점에 대해 문제를 제기하곤 합니다.

스토리지 엔진은, 드물게 업데이트되지만 빈번히 액세스가 이루어지는 임시 데이터를 처리하는 애플리케이션에 특히 이상적이라 할 수 있습니다. 이들 애플리케이션은 메모리에 저장된 데이터베이스를 사용하여 디스크 오퍼레이션의 수를 줄여 주는데, 이러한 애플리케이션의 예로 웹 기반의 BitTorrent Tracker를 들 수 있습니다.

팁에서는 PostgreSQL 데이터베이스 서버 자체와 Red Hat Enterprise Linux 운영체제가 제공하는 표준 기능을 사용하여 PostgreSQL의 메모리 스토리지를 설정하는 방법에 대해 알아보도록 하겠습니다.

모든 내용을 저장할 수 있는 여유 메모리가 필요합니다.
메모리에 파일시스템을 생성하려면 다음 명령어를 사용합니다.

mount -t ramfs none /mount/point

이 경우 다음과 같은 특성을 지닌 파일시스템이 생성됩니다.

모든 파일이 RAM에 보관됩니다.
액세스 방식은 읽기/쓰기입니다.
고정 용량의 RAM을 사용하지 않습니다.
해당 파일들을 수용하기 위해 파일시스템이 확장/축소됩니다.
파일시스템을 언마운트하면 모든 내용이 손실됩니다.

PostgreSQL 문서에서 발췌:

PostgreSQL의 테이블스페이스는 데이터베이스 관리자가 데이터베이스 오브젝트를 나타내는 파일이 저장될 파일 시스템 내의 위치를 지정할 수 있게 해줍니다. 일단 테이블스페이스가 생성되면 데이터베이스 오브젝트 생성 시 특정 이름을 붙일 수 있습니다. 위치는 반드시 PostgreSQL 시스템 사용자가 소유하고 있는 기존의 빈 디렉토리여야 하는데, 이후로 테이블스페이스 내에 생성되는 모든 오브젝트는 이 디렉토리의 파일에 저장됩니다. 테이블스페이스 자체는 반드시 데이터베이스 수퍼유저로서 생성되어야 하지만, 이후로는 일반 데이터베이스 사용자가 이를 이용하도록 할 수 있고, 그렇게 하려면 이들에게 CREATE 권한을 부여해야 합니다.

저장될 디렉토리를 만듭니다. 이 디렉토리는 반드시 ramfs 파일시스템에 위치해야 합니다.

mkdir /mnt/ramfs/pgdata
chown postgres:postgres /mnt/ramfs/pgdata

chmod go-rwx /mnt/ramfs/pgdata

테이블스페이스를 생성하고 권한을 부여하려면,

CREATE TABLESPACE $TABLESPACE_NAME LOCATION '/mnt/ramfs/pgdata';
GRANT CREATE ON TABLESPACE $TABLESPACE_NAME TO $ROLE_NAME;


PostgreSQL 테이블에 엔트리를 설정하는 것 외에도, 테이블스페이스 디렉토리 아래에 “PG_VERSION”이라는 이름의 파일이 생성됩니다. 나중에 이 구조를 복원해야 하므로 이 파일을 포함하여 생성되는 다른 모든 파일을 기억해두십시오.

테이블스페이스를 'template1' 데이터베이스 내에 정의하며, 일반적으로 다른 모든 데이터베이스는 'template1'로부터 승계를 합니다. 이런 방식으로 우리는 매번 재정의할 필요가 없을 뿐 아니라, 이후로는 데이터베이스 내에서 생성되는 모든 오브젝트에 사용할 수 있게 됩니다.

수퍼유저로서 데이터베이스를 액세스하는 것은 바람직하지 않습니다. 따라서 애플리케이션에서 사용하려면 새로운 역할을 생성토록 하십시오.

구성에 따라서 사용자 'postgres'가 테이블스페이스 디렉토리에 액세스할 수 있도록 SELinux 정책을 조정해야 할 수도 있습니다.

내에 새로운 데이터베이스를 생성하려면 다음 명령어를 사용합니다.

CREATE DATABASE $DATABASE_NAME WITH TABLESPACE = $TABLESPACE_NAME;

이 데이터베이스 내에 생성되는 다른 모든 오브젝트는 동일한 테이블스페이스에 저장되며,
테이블스페이스는 테이블 생성 시 사용할 수 있습니다.

ramfs 파일시스템이 언마운트될 때마다 데이터베이스 구조를 덤프하고 복원해야 합니다.
단, 메모리 데이터베이스를 사용하는 애플리케이션이라면 데이터 손실을 인지하고 재생성 작업을 수행할 수 있어야 합니다.


pg_dump --create --schema-only --file=$FILENAME --host=$HOST -U postgres $DATABASE_NAME



psql --file $FILENAME -U $ROLE_NAME --dbname postgres --host $HOST

재시작할 때마다 이 설정이 적용되도록 하려면 '/etc/init.d/postgresql'을 편집해야 하는데, 항목들이 올바른 순서로 표시되도록 유의하십시오.




pg_dump --create --schema-only --file=/root/memdb.sql --host=localhost -U postgres memdb



umount /mnt/ramfs







if [ ! -d /mnt/ramfs ]; then
mkdir -p /mnt/ramfs

fi
mount -t ramfs none /mnt/ramfs


mkdir /mnt/ramfs/pgdata
echo $PGMAJORVERSION > /mnt/ramfs/pgdata/PG_VERSION
chown -R postgres:postgres /mnt/ramfs/pgdata
chmod -R go-rwx /mnt/ramfs/pgdata





psql --quiet -U postgres --host localhost --command "DROP DATABASE memdb;" 2>/root/psql.log


psql --quiet --file /root/memdb.sql -U $ROLE_NAME -d postgres --host localhost 2>/root/psql.log

PostgreSQL 데이터베이스 서버와 관련하여 메모리 스토리지를 설정하는 방법에 대해 알아보았습니다. PostgreSQL의 모든 기능 이외에도, 메모리 데이터베이스를 사용할 경우 MySQL에서와 같은 제한 사항은 발생하지 않습니다. 완벽한 설정을 위해서 임의대로 init 스크립트를 손보아야 할 수도 있습니다.

  • About Red Hat
  • Red Hat is the world’s leading provider of open source software solutions, using a community-powered approach to reliable and high-performing cloud, Linux, middleware, storage and virtualization technologies. Red Hat also offers award-winning support, training, and consulting services. As the connective hub in a global network of enterprises, partners, and open source communities, Red Hat helps create relevant, innovative technologies that liberate resources for growth and prepare customers for the future of IT. Learn more at http://www.861278361.xyz.



  • Forward-Looking Statements
  • Certain statements contained in this press release may constitute "forward-looking statements" within the meaning of the Private Securities Litigation Reform Act of 1995. Forward-looking statements provide current expectations of future events based on certain assumptions and include any statement that does not directly relate to any historical or current fact. Actual results may differ materially from those indicated by such forward-looking statements as a result of various important factors, including: risks related to delays or reductions in information technology spending; the effects of industry consolidation; the ability of the Company to compete effectively; the integration of acquisitions and the ability to market successfully acquired technologies and products; uncertainty and adverse results in litigation and related settlements; the inability to adequately protect Company intellectual property and the potential for infringement or breach of license claims of or relating to third party intellectual property; the ability to deliver and stimulate demand for new products and technological innovations on a timely basis; risks related to data and information security vulnerabilities; ineffective management of, and control over, the Company’s growth and international operations; fluctuations in exchange rates; and changes in and a dependence on key personnel, as well as other factors contained in our most recent Quarterly Report on Form 10-Q (copies of which may be accessed through the Securities and Exchange Commission’s website at http://www.sec.gov), including those found therein under the captions "Risk Factors" and "Management’s Discussion and Analysis of Financial Condition and Results of Operations". In addition to these factors, actual future performance, outcomes, and results may differ materially because of more general factors including (without limitation) general industry and market conditions and growth rates, economic and political conditions, governmental and public policy changes and the impact of natural disasters such as earthquakes and floods. The forward-looking statements included in this press release represent the Company’s views as of the date of this press release and these views could change. However, while the Company may elect to update these forward-looking statements at some point in the future, the Company specifically disclaims any obligation to do so. These forward-looking statements should not be relied upon as representing the Company’s views as of any date subsequent to the date of this press release.