0

How to get the definition of a system function? I mean the code of it.

5
  • 1
    You mean these functions? Download the source code for PostGresql and get it from there. Stored procedures? Look here: stackoverflow.com/questions/3524859 Dec 25, 2013 at 6:39
  • 2
    Almost all PostgreSQL system functions are implemented in C - you can find these codes in postgresql source code or in some public postgres git repository - for example lot of string functions are implemented in following file git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/… Dec 25, 2013 at 9:06
  • While it's badly worded this seems like a reasonable question. It's not trivial to go from "here's the function name" to "here's the code that implements it"; you have to look it up via pg_proc.h to find the underlying C function, whose location isn't always obvious so you may want to use ctags to find it. Dec 26, 2013 at 6:12
  • 1
    Answer in comment form as this is closed: Download the PostgreSQL source code, or check it out from git. Get the oid of the system function with SELECT 'funcname'::regproc::oid;. Result like 2176. Look in src/include/catalog/pg_proc.h for the oid. You'll see something like DATA(insert OID = 2176 .... The 4th-last entry is the C procedure name; it's not always the same as the SQL function name (2nd field) due to overloads etc. Find the C procedure definition with 'git grep ^procname' or using ctags eg vim -t procname. Dec 26, 2013 at 6:20
  • 1
    @CraigRinger: You might make an answer out of your comment after this one has been reopened. Apr 15, 2015 at 16:00

1 Answer 1

2

Get the oid of the system function with

SELECT 'funcname'::regproc::oid;

The result is a number like 2176.

Look in src/include/catalog/pg_proc.h for the oid. You'll see something like

DATA(insert OID = 2176 ....

The 4th-last entry is the C function name. It's not always the same as the SQL function name (2nd field) due to overloads etc.

Find the C procedure definition with git grep ^procname or using ctags/cscope eg vim -t procname.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.