通过 GDB 跟踪 PostgreSQL 字符编码

当 psql 或者其他客户端链接到 PostgreSQL 数据库后,可以使用 gdb 跟踪对应的 postgres 进程,使用几个函数打印输出数据库编码方式、客户端编码方式等信息。

第一步,使用 psql 链接到 PostgreSQL 数据库,查看当前数据库的字符编码信息。

	postgres=# \l postgres
	                                List of databases
	   Name   |  Owner   | Encoding  |   Collate   |    Ctype    | Access privileges
	----------+----------+-----------+-------------+-------------+-------------------
	 postgres | postgres | SQL_ASCII | en_US.UTF-8 | en_US.UTF-8 |
(1 row)

第二步,使用 gdb attach 对应的 postgres 进程,并打印输出相关的字符编码。

	(gdb) bt
	#0  0x00007f9375b16463 in __epoll_wait_nocancel () from /lib64/libc.so.6
	#1  0x00000000006e1c5e in WaitEventSetWait ()
	#2  0x0000000000611bd6 in secure_read ()
	#3  0x000000000061a308 in pq_recvbuf ()
	#4  0x000000000061aed5 in pq_getbyte ()
	#5  0x00000000006ff519 in ReadCommand ()
	#6  0x0000000000701272 in PostgresMain ()
	#7  0x0000000000476fcd in ServerLoop ()
	#8  0x000000000069a01b in PostmasterMain ()
	#9  0x00000000004779e1 in main ()
	(gdb) p (char*) GetDatabaseEncodingName()
	$1 = 0x98c6e6 "SQL_ASCII"
	(gdb) p (char*)pg_get_client_encoding_name()
	$2 = 0x98c719 "UTF8"
	(gdb) p pending_client_encoding
	$3 = 6
	(gdb) p (char*)GetConfigOptionByName("client_encoding", NULL, true)
	No symbol "NULL" in current context.
	(gdb) p (char*)GetConfigOptionByName("client_encoding", 0, true)
	No symbol "true" in current context.
	(gdb) p (char*)GetConfigOptionByName("client_encoding", 0, 1)
	$4 = 0x2267068 "UTF8"
	(gdb)

注:数据库采用 SQL_ASCII 编码,psql 采用 UTF8 编码

第三步,改变 psql 客户端编码方式为韩文编码。

	postgres=# set client_encoding to EUC_KR;
	SET

第四步,gdb 再次查看编码方式。

	(gdb)  p (char*) GetDatabaseEncodingName()
	$1 = 0x98c6e6 "SQL_ASCII"
	(gdb) p (char*)pg_get_client_encoding_name()
	$2 = 0x98c6fe "EUC_KR"
	(gdb)  p pending_client_encoding
	$3 = 6
	(gdb)  p (char*)GetConfigOptionByName("client_encoding", 0, 1)
	$4 = 0x2267068 "EUC_KR"

分类:PostgreSQL

Tagged as: ,

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google photo

You are commenting using your Google account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理