简体   繁体   中英

Storing session data better bytea or text type column in database

I would store sessions data in database, but im not sure whitch type of column is better to use (i will store serialized objects and arrays). I think better way is use BYTEA cause when i tried some days ago store object with namespace the pdo of php lose one`s head (slashes from namespace). To slove a problem it turned store object in BYTEA colum and tell php use type PDO::PARAM_LOB when bind values.

  $this->sth[$name]->bindValue($key, $value, PDO::PARAM_LOB);

but this param work only on BYTEA column (Postgresql). So it is good idea choose BYTEA column type instead TEXT ??? Is something interested to know when use this columns type??

I will grateful for solution.

UPDATE:

i would implements my session handler system using session_set_save_handler function and SessionHandlerInterface. It look something like:

class SessionHandler{

   public function open($save_path, $session_name) {// some code}

   public function close() {//some code}

   public function read($id) {// some code}

   public function write($id, $data) {
   // Here i would implement mechanism to store object into database using php pdo. 
   // Here variable $data is already serialized by php session mechanism and ready to put into database. 
   // But i have unpleasantly experience storing serialized object 
   // using pdo client with namespaces thought bindParam function.

   // This experience is:
   // The serialized object (string) with namespace was cut at half, 
   // when i tried used PDO::PARAM_STR as argument in $sth->bindValue() (TEXT column i database). 
   // When to the same operation i used PDO::PARAM_LOB (BYTEA column in database) it was stored all fine.
   }

   public function destroy($id) {//some code}

   public function gc($maxlifetime) {// some code}

}

$hnadler = new SessionHandler()
session_set_save_handler($handler, true);

And after this experience im not sure which coulm use.

UPDATE:

Thank you for cleaver answer Craig Ringer: After your post i decide to go check what exactly is written in php manual about serializing objects:

http://www.php.net/manual/en/function.serialize.php

serialize function

  Returns a string containing a byte-stream representation of value 
  that can be stored anywhere.

  Note that this is a binary string which may include null bytes, 
  and needs to be stored and handled as such. For example, serialize() 
  output should generally be stored in a BLOB field in a database, 
  rather than a CHAR or TEXT field.

So your suggestion to store objects in BYTEA type column instead TEXT is good.

If you are storing serialized application data, bytea is the appropriate choice. It's just bytes, it's meaningless to anything except the application that supplied it, and it isn't text , in the sense that it isn't meangful characters.

So for data modelling reasons, bytea is what you should be using.

Also, if the data may contain null bytes, you must use bytea , because text cannot hold null bytes, and encoding your data as (eg) base64 to work around that is just inefficient and horrible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM